home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / gopherd / special.c < prev    next >
C/C++ Source or Header  |  1992-05-25  |  2KB  |  117 lines

  1. /*
  2.  * This file contains routines to deal with special types of files.
  3.  * including compressed text and shell scripts.
  4.  */
  5.  
  6.  
  7. #include "gopherd.h"
  8.  
  9.  
  10. /* Check to see if this file needs special treatment before heading
  11.  * back to the client... We will check for:
  12.  *    Compressed files    if so, zcat first
  13.  *    Shellscript        if so, "do it"
  14.  * Note: it would be somewhat non-portable to check of a binary
  15.  *  (we'd need to check for so many different magic numbers; the
  16.  *  shell script designation should be sufficient, since the script
  17.  *  can call an executable anyway
  18.  * Recognized elsewhere:
  19.  *    .snd            needs special processing on client
  20.  *    uuencoded        needs special processing on client
  21.  * Other filetypes we could check for:
  22.  *    GIF        ->    Bring up GIF previewer
  23.  *    Postscript    ->    Bring up Postscript previewer
  24.  */
  25.  
  26. static int ispipe;
  27.  
  28. FILE *
  29. specialfile(fp, pathname)
  30.   FILE *fp;
  31.   char *pathname;
  32. {
  33.      FILE *pp;
  34.      char buf[256], s[256];
  35.      long i;
  36.      
  37.      ispipe = 0;
  38.      
  39.      /* Keep track of where we are */
  40.      i = ftell(fp);
  41.      rewind(fp);
  42.      
  43.      /* Grab the first three bytes, and rewind */
  44.      if (fgets(s, 255, fp) == NULL)
  45.       return (FILE *)0;
  46.  
  47.      fseek(fp, i, 0);
  48.      
  49.      /* Compressed? */
  50.      if (iscompressed(s)) {
  51.       if (dochroot)
  52.            sprintf(buf, "/bin/zcat \"%s\"\n", pathname);
  53.       else
  54.            sprintf(buf, "/usr/ucb/zcat \"%s/%s\"\n", Data_Dir, pathname);
  55.  
  56.       if (! (pp = popen(buf, "r")))
  57.            return (FILE *)0;
  58.       ispipe = 1;
  59.       return pp;
  60.      }
  61.  
  62.      /* Script? */
  63.      if (isshellscript(s)) {
  64.       s[strlen(s)-1] = '\0';
  65.       if (dochroot)
  66.            sprintf(buf, "\"%s\" %s\n", pathname, (EXECargs == NULL) ? "" : EXECargs);
  67.       else
  68.            sprintf(buf, "\"%s/%s\" %s\n", Data_Dir, pathname, (EXECargs == NULL) ? "" : EXECargs);
  69.  
  70.       if (DEBUG)
  71.            fprintf(stderr, "Executing %s", buf);
  72.  
  73.       if (! (pp = popen(buf, "r")))
  74.            return (FILE *)0;
  75.       ispipe = 1;
  76.       
  77.       if (DEBUG)
  78.            fprintf(stderr, "Zcat popen is okay\n");
  79.       
  80.       return pp;
  81.      }
  82.  
  83.      return (FILE *)0;
  84. }
  85.  
  86.  
  87. int
  88. iscompressed(s)
  89.   char *s;
  90. {
  91.      if (s[0] == '\037' && s[1] == '\235')
  92.       return 1;
  93.      else
  94.       return 0;
  95. }
  96.  
  97. int
  98. isshellscript(s)
  99.   char *s;
  100. {
  101.      if (! strncmp(s, "#!/", 3))
  102.       return 1;
  103.      else
  104.       return 0;
  105. }
  106.  
  107.  
  108. int
  109. Specialclose(fp)
  110.   FILE *fp;
  111. {
  112.      if (ispipe)
  113.       return(pclose(fp));
  114.      else
  115.       return(fclose(fp));
  116. }
  117.