home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / SMAILSRC.ZIP / SMAIL.ZIP / POPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  1.8 KB  |  68 lines

  1. /*
  2.  *      popen.c: popen/pclose routines for MS-DOS smail
  3.  *
  4.  *      Stephen Trier
  5.  *      3/27/90
  6.  *
  7.  *      These routines are for the MS-DOS version of smail only.  They
  8.  *      implement only write pipes, and only one pipe can be used at a
  9.  *      time.  Although this implementation is not very general, it is
  10.  *      sufficient for smail.
  11.  *
  12.  *      This file is in the public domain.
  13.  *
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. #include <process.h>
  21. #include <dir.h>
  22. #include <io.h>
  23.  
  24. static FILE *pipefp;
  25. static char cmd[128], pipefile[80];
  26. extern char *ms_tmpdir;
  27.  
  28. FILE *popen(char *command, char *type)
  29. {
  30.     if ((*type == 'w') && (strcpy(cmd, command) != NULL))  {
  31.     sprintf(pipefile, "%s/pipeXXXXXX", ms_tmpdir);
  32.     mktemp(pipefile);
  33.     pipefp = fopen(pipefile, "w");
  34.     return pipefp;  /* Return NULL if error */
  35.     }
  36.     else
  37.     return NULL;
  38. }
  39.  
  40. int pclose(FILE *fp)
  41. {
  42.     int pipefd, stdinfd, returnval = 0;
  43.     char *args;
  44.  
  45.     if (fp == pipefp)  {
  46.     args = strchr(cmd, ' ');
  47.     if (args != NULL)  {
  48.         *args = '\0';  /* Mark end of command and beginning of args */
  49.         args++;
  50.         }
  51.     fclose(pipefp);
  52.     pipefp = fopen(pipefile, "rb");
  53.     pipefd = fileno(pipefp);
  54.     stdinfd = dup(0);         /* Save standard input for afterwards */
  55.     dup2(pipefd, 0);          /* New standard input is from pipe    */
  56.     returnval = spawnlp(P_WAIT, cmd, cmd, args, NULL);  /* Fork the command */
  57.     dup2(stdinfd, 0);         /* Restore the standard input         */
  58.     close(stdinfd);           /* Get rid of the duplicate stdin     */
  59.     fclose(pipefp);           /* Prepare to remove the pipe         */
  60.     unlink(pipefile);         /* Delete the pipe                    */
  61.  
  62.     return returnval << 8;
  63.     }
  64.     else
  65.     return -1;
  66. }
  67.  
  68.