TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Mick Howe
from: Thomas Seeling
date: 1996-07-22 10:40:24
subject: EMX\GNU C

Hallo, Mick!

*** Am Donnerstag 11. Juli 1996 um 23:21 schrieb Mick Howe an Thomas Seeling:

 TS>> a utility program called redir for this purpose.


=== Cut ===
/* redir.c (emx+gcc) -- Copyright (c) 1994-1995 by Eberhard Mattes */

#include 
#include 
#include 
#include "redirlib.h"


static void usage (void)
{
  puts ("Usage: redir \"\"
\"\"\n\n"
        "Redirections (seperate them with spaces):\n"
        "  file      Redirect standard output to FILE\n"
        "  >>file     Redirect standard output to FILE, appending\n"
        "  nfile     Redirect file descriptor N to FILE\n"
        "  nfile    Redirect file descriptor N from and to FILE\n"
        "  <&m        Redirect standard input from file
descriptor M\n"
        "  >&m        Redirect standard output to file
descriptor M\n"
        "  n<&m       Redirect file descriptor N from file
descriptor M\n"
        "  n>&m       Redirect file descriptor N to file
descriptor M\n"
        "  <&-        Close standard input\n\n"
        "Example:\n\n"
        "  redir \">output 2>&1\" \"make\"");
  exit (1);
}


int main (int argc, char *argv[])
{
  struct redir_save save;
  int r, e;

  if (argc != 3)
    usage ();
  r = redir_redirect (&save, argv[1]);
  if (r > 0)
    usage ();
  if (r < 0)
    exit (2);
  r = system (argv[2]);
  e = errno;
  redir_restore (&save);
  errno = e;
  if (r == -1)
    {
      perror ("system()");
      exit (2);
    }
  return r;
}
=== Cut ===
/* redirlib.c (emx+gcc) -- Copyright (c) 1994-1995 by Eberhard Mattes */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "redirlib.h"

#define FALSE 0
#define TRUE  1


int redir_restore (struct redir_save *save)
{
  int i, r, e;

  r = 0; e = errno;
  for (i = 0; i < 10; ++i)
    if (save->fds[i] != -1)
      {
        if (dup2 (save->fds[i], i) != 0)
          {
            r = -1; e = errno;
          }
        close (save->fds[i]);
        save->fds[i] = -1;
      }
  errno = e;
  return r;
}



static int failure (struct redir_save *save, const char *msg, int fd_close)
{
  int e;

  e = errno;
  redir_restore (save);
  if (fd_close != -1)
    close (fd_close);
  errno = e;
  perror (msg);
  return -1;
}


static int syntax (struct redir_save *save, int fd_close)
{
  int e;

  e = errno;
  redir_restore (save);
  if (fd_close != -1)
    close (fd_close);
  errno = e;
  return 1;
}


/* Return values:

   -1   Error; an error message has been printed to stderr
   0    OK
   1    Syntax error */

int redir_redirect (struct redir_save *save, const char *s)
{
  int fd_explicit, fd_default, fd_source, fd_target, fd_open, mode;
  char fname[270];
  char f_amp;
  int i;

  for (i = 0; i < 10; ++i)
    save->fds[i] = -1;

  for (;;)
    {
      while (isspace ((unsigned char)*s))
        ++s;
      if (*s == 0)
        break;
      fd_explicit = -1; fd_open = -1;
      if (isdigit ((unsigned char)*s))
        fd_explicit = *s++ - '0';
      switch (*s)
        {
        case '<':
          ++s;
          if (*s == '>' && fd_explicit != -1)
            {
              ++s;
              mode = O_RDWR; f_amp = FALSE;
            }
          else
            {
              mode = O_RDONLY; f_amp = TRUE;
            }
          fd_default = 0;
          break;

        case '>':
          ++s;
          if (*s == '>')
            {
              ++s;
              mode = O_WRONLY|O_CREAT|O_APPEND; f_amp = FALSE;
            }
          else
            {
              mode = O_WRONLY|O_CREAT|O_TRUNC; f_amp = TRUE;
            }
          fd_default = 1;
          break;

        default:
          return syntax (save, -1);
        }
      if (fd_explicit != -1)
        fd_target = fd_explicit;
      else
        fd_target = fd_default;
      fd_source = -1;
      if (*s == '&' && f_amp)
        {
          ++s;
          if (*s == '-' && fd_explicit == -1)
            ++s;                /* fd_source = -1 */
          else if (!isdigit ((unsigned char)*s))
            return syntax (save, -1);
          else
            fd_source = *s++ - '0';
        }
      else
        {
          i = 0;
          while (*s != 0 && !isspace ((unsigned char)*s) &&
i < sizeof (fname))
            fname[i++] = *s++;
          if (i >= sizeof (fname))
            return syntax (save, -1);
          fname[i] = 0;
          fd_source = fd_open = open (fname, mode, S_IREAD | S_IWRITE);
          if (fd_source == -1)
            return failure (save, fname, -1);
        }

      if (*s != 0 && !isspace ((unsigned char)*s))
        return syntax (save, fd_open);

      if (save->fds[fd_target] == -1)
        {
          save->fds[fd_target] = dup (fd_target);
          if (save->fds[fd_target] == -1)
            return failure (save, "dup()", fd_open);
        }

      if (fd_source == -1)
        close (fd_target);
      else if (fd_source == fd_target)
        return syntax (save, fd_open);
      else if (dup2 (fd_source, fd_target) == -1)
        return failure (save, "dup2()", fd_open);
      if (fd_open != -1)
        close (fd_open);
    }
  return 0;
}
=== Cut ===
/* redirlib.h (emx+gcc) -- Copyright (c) 1995 by Eberhard Mattes */

struct redir_save
{
  int fds[10];
};

int redir_restore (struct redir_save *save);
int redir_redirect (struct redir_save *save, const char *s);
=== Cut ===


Tschau...Thomas

--- E3-32/1.11-32/2.50+
* Origin: Die TeX-Box +49-6034-930021 V.34 -930022 ISDN 24h (2:244/1130.42)
SEEN-BY: 50/99 270/101 620/243 625/100 711/401 409 410 413 430 808 809 934
SEEN-BY: 711/955 712/407 515 624 628 713/888 800/1
@PATH: 244/1130 24/999 888 396/1 270/101 712/515 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.