Back to index

courier  0.68.2
Defines | Functions
preline.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include "waitlib/waitlib.h"

Go to the source code of this file.

Defines

#define EX_TEMPFAIL   75

Functions

static void usage ()
int main (int argc, char **argv)

Define Documentation

#define EX_TEMPFAIL   75

Definition at line 19 of file preline.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file preline.c.

{
int    pipefd[2];
pid_t  p;
char   *env;
int    waitstat;
char   **newargv;
int    c;
char *cmd;

       if (argc <= 1)
       {
              usage();
       }

       if (pipe(pipefd) < 0)
       {
              perror("pipe");
              exit(EX_TEMPFAIL);
       }

       signal(SIGCHLD, SIG_DFL);
       p=fork();
       if (p == -1)
       {
              perror("fork");
              exit(EX_TEMPFAIL);
       }

       if (p == 0)
       {
              p=fork();
              if (p == -1)
              {
                     perror("fork");
                     exit(EX_TEMPFAIL);
              }
              if (p)
                     exit(0);
              close(pipefd[0]);
              dup2(pipefd[1], 1);
              close(pipefd[1]);
              if ((env=getenv("UFLINE")) != 0)
                     printf("%s\n", env);
              if ((env=getenv("RPLINE")) != 0)
                     printf("%s\n", env);
              if ((env=getenv("DTLINE")) != 0)
                     printf("%s\n", env);

              while ((c=getchar()) != EOF)
                     putchar(c);
              fflush(stdout);
              exit(0);
       }

       close(pipefd[1]);
       dup2(pipefd[0], 0);
       close(pipefd[0]);

       while (wait(&waitstat) != p)
              ;
       if (!WIFEXITED(waitstat) || WEXITSTATUS(waitstat))
              exit(EX_TEMPFAIL);

       if ((newargv=(char **)malloc(sizeof(char *)*argc)) == 0)
       {
              perror("malloc");
              exit(EX_TEMPFAIL);
       }
       for (c=1; c<argc; c++)
              newargv[c-1]=argv[c];
       newargv[c-1]=0;

       cmd=newargv[0];

       if ((newargv[0]=strrchr(newargv[0], '/')) != 0)
              ++newargv[0];
       else
              newargv[0]=cmd;

       execvp(cmd, newargv);
       perror("exec");
       exit(EX_TEMPFAIL);
}

Here is the call graph for this function:

static void usage ( ) [static]

Definition at line 24 of file preline.c.

{
       fprintf(stderr, "Usage: preline [program] [arguments]\n");
       exit(1);
}

Here is the caller graph for this function: