home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / mm1_tracker.lzh / TRACKER4.6 / Amiga / popen.c < prev    next >
Text File  |  1994-11-24  |  2KB  |  101 lines

  1. /* amiga/popen.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: popen.c,v 1.7 1994/06/22 21:54:12 Espie Exp Espie $
  6.  * $Log: popen.c,v $
  7.  * Revision 1.5  1994/01/07  15:08:54  Espie
  8.  * Maybe correct code now...
  9.  */
  10.  
  11.  
  12. #include <proto/dos.h>
  13. #include <proto/exec.h>
  14. #include <exec/tasks.h>
  15. #include <dos/dostags.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "defs.h"
  19. ID("$Id: popen.c,v 1.7 1994/06/22 21:54:12 Espie Exp Espie $")
  20.  
  21. /*
  22. ###   CSupport/popen
  23. ###
  24. ###   NAME
  25. ###      popen/pclose -- Unix-like pipes
  26. ###
  27. ###   STATUS
  28. ###      Experimental
  29. ###      does not work with csh !
  30. ###
  31.  */
  32. FILE *popen(char *command, char *mode)
  33.    {
  34.    static char pname[25];
  35.    struct Task *me = FindTask(0);
  36.    static count = 0;
  37.    
  38.    count++;
  39.    
  40.       /* guarantees a unique pipe name ! */
  41.    sprintf(pname, "pipe:tr_%lx_%d", me, count);
  42.    
  43.    if (strcmp(mode, "r") == 0)
  44.       /* open pipe for reading */
  45.       {
  46.       FILE *reader;
  47.       BPTR writer, null;
  48.  
  49.       writer = Open(pname, MODE_NEWFILE);
  50.       reader = fopen(pname, "r");
  51.       null = Open("NIL:", MODE_NEWFILE);
  52.       if (SystemTags(command, SYS_Input, null, 
  53.          SYS_Output, writer, SYS_Asynch, TRUE, 
  54.          NP_StackSize, me->tc_SPUpper - me->tc_SPLower,
  55.          TAG_END) == -1)
  56.          {
  57.          Close(null);
  58.          Close(writer);
  59.          fclose(reader);
  60.          return NULL;
  61.          }
  62.       else
  63.          return reader;
  64.       }
  65.    else if (strcmp(mode, "w") == 0)
  66.       /* open pipe for writing */
  67.       {
  68.       FILE *writer;
  69.       BPTR reader, null;
  70.       
  71.       writer = fopen(pname, "w");
  72.       reader = Open(pname, MODE_OLDFILE);
  73.       null = Open("NIL:", MODE_NEWFILE);
  74.       if (SystemTags(command, SYS_Input, reader, 
  75.          SYS_Output, null, SYS_Asynch, TRUE, 
  76.          NP_StackSize, me->tc_SPUpper - me->tc_SPLower, 
  77.          TAG_END) == -1)
  78.          {
  79.          Close(null);
  80.          Close(reader);
  81.          fclose(writer);
  82.          return NULL;
  83.          }
  84.       else
  85.          return writer;
  86.       }
  87.    else
  88.       return NULL;
  89.    }
  90.  
  91. /* for us, pclose is just fclose.
  92.  * But we have to insure the file is empty first
  93.  */
  94. void pclose(FILE *f)
  95.    {
  96.    while (fgetc(f) != EOF)
  97.       ;
  98.    fclose(f);
  99.    }
  100.  
  101.