home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / text / tex / pastex / source / dvips / amiga / pipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  2.7 KB  |  123 lines

  1. /*
  2.  *   pipe.c
  3.  *
  4.  *   Implements popen and pclose using the Per Bojsen's APIPE: device.
  5.  *   Alternative emulation of popen (read only) is also provided, for people
  6.  *   who haven't the APIPE: device.
  7.  *
  8.  *   Written by Giuseppe Ghibò <ghibo@galileo.polito.it>
  9.  *
  10.  *   last revised: 28 Sep 1994.
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <ios1.h>
  17. #include <ctype.h>
  18. #include <dos/dos.h>
  19. #include <dos/dosextens.h>
  20. #include <proto/dos.h>
  21. #include "pipe.h"
  22.  
  23. static int ExistsDevice(const char *);
  24. static int bstricmp(const char *, BPTR);
  25. static int tempnumber = 0;
  26.  
  27. FILE *popen(command, type)
  28. const char *command, *type;
  29. {
  30.     static char cmd[512];
  31.  
  32.     if (ExistsDevice("APIPE")) {
  33.         sprintf(cmd, "APIPE:%s", command);
  34.         return( fopen(cmd, type) );
  35.     }
  36.     else
  37.     {
  38.         FILE *fh;
  39.         char tempname[18];
  40.         struct UFB *myufb;
  41.  
  42.         sprintf(tempname,"T:dvipstemp.%05d",tempnumber++);
  43.  
  44.         switch (*type) {
  45.             case 'w':
  46.                 return((FILE *) NULL); /* "w" mode is currently not supported with popen as redirection to a file */
  47.  
  48.             case 'r':
  49.                 sprintf(cmd,"%s >%s",command,tempname);
  50.                 system(cmd);
  51.                 if ((fh = fopen(tempname,type)) != NULL) {
  52.                     myufb = chkufb(fileno(fh));
  53.                     myufb->ufbflg |= UFB_TEMP;
  54.                 }
  55.                 return(fh);
  56.  
  57.             default:
  58.                 return((FILE *) NULL);
  59.         }
  60.     }
  61. }
  62.  
  63. int pclose(stream)
  64. FILE *stream;
  65. {
  66.     return( fclose(stream) );
  67. }
  68.  
  69. /*
  70.  * ExistsDevice (const char *devname);
  71.  *
  72.  * devname = name of device to search for, without trailing ':'
  73.  *
  74.  * This function returns TRUE if devname exists as a device, else it
  75.  * returns FALSE.
  76.  *
  77.  */
  78.  
  79. static int ExistsDevice(const char *devname)
  80. {
  81.     extern struct DosLibrary *DOSBase;
  82.     struct DosInfo *dinfo=(struct DosInfo *) BADDR(DOSBase->dl_Root->rn_Info);
  83.     struct DeviceList *dlist=(struct DeviceList *) BADDR(dinfo->di_DevInfo);
  84.  
  85.     while (dlist) {
  86.         if (dlist->dl_Type == DLT_DEVICE) {
  87.             if (!bstricmp(devname,dlist->dl_Name)) return(TRUE);
  88.         }
  89.         dlist = (struct DeviceList *) BADDR (dlist->dl_Next);
  90.     }
  91.     return(FALSE);
  92. }
  93.  
  94. /*
  95.  * bstricmp (cstring, bstring)
  96.  *
  97.  * This function compares a C NULL-terminated string with a BCPL string.
  98.  * It returns 0 if strings are equal, else returns 1.
  99.  *
  100.  * const char *cstring = pointer to string in C format (null-terminated)
  101.  * BSTR bstring        = pointer to string in BCPL format (first byte = length)
  102.  *
  103.  */
  104.  
  105. static int bstricmp(const char *cstring, BSTR bstring)
  106. {
  107.     const char *p = (const char *) BADDR(bstring);
  108.     UBYTE len = (UBYTE) *p++;
  109.  
  110.     while (len--) {
  111.         if (!*cstring)
  112.             return(1); /* strings have different lengths */
  113.         if (toupper(*p) != toupper(*cstring))
  114.             return(1); /* strings have different chars */
  115.         *p++;
  116.         *cstring++;
  117.     }
  118.     if ( *cstring )
  119.         return(1); /* BCPL string is a substring of C string */
  120.  
  121.     return(0);
  122. }
  123.