home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************
- * lindner
- * 3.3
- * 1993/04/09 15:00:19
- * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/special.c,v
- * Exp
- *
- * Paul Lindner, University of Minnesota CIS.
- *
- * Copyright 1991, 1992 by the Regents of the University of Minnesota
- * see the file "Copyright" in the distribution for conditions of use.
- *********************************************************************
- * MODULE: special.c
- * routines to deal with special types of files, compressed, scripts, etc.
- *********************************************************************
- * Revision History:
- * special.c,v
- * Revision 3.3 1993/04/09 15:00:19 lindner
- * Fixes for ask shell scripts, ensure that they're run in the current
- * directory of the script.
- *
- * Revision 3.2 1993/03/24 20:24:23 lindner
- * Addition for compressed file support rfopenz()
- *
- * Revision 3.1.1.1 1993/02/11 18:02:53 lindner
- * Gopher+1.2beta release
- *
- * Revision 1.3 1993/02/09 22:15:36 lindner
- * additions for askfile
- *
- * Revision 1.2 1993/01/30 23:57:44 lindner
- * Additions for ASK block support.
- *
- * Revision 1.1 1992/12/10 23:13:27 lindner
- * gopher 1.1 release
- *
- *
- *********************************************************************/
-
- #include "gopherd.h"
-
-
- /* Check to see if this file needs special treatment before heading
- * back to the client... We will check for:
- * Compressed files if so, zcat first
- * Shellscript if so, "do it"
- * (add ask block params if exists..)
- * Note: it would be somewhat non-portable to check of a binary
- * (we'd need to check for so many different magic numbers; the
- * shell script designation should be sufficient, since the script
- * can call an executable anyway
- * Recognized elsewhere:
- * .snd needs special processing on client
- * uuencoded needs special processing on client
- * Other filetypes we could check for:
- * GIF -> Bring up GIF previewer
- * Postscript -> Bring up Postscript previewer
- */
-
- static int ispipe;
-
- FILE *
- specialfile(fp, pathname)
- FILE *fp;
- char *pathname;
- {
- FILE *pp;
- char buf[256], s[256], *cp;
- long i;
-
- ispipe = 0;
-
- /* Keep track of where we are */
- i = ftell(fp);
- rewind(fp);
-
- /* Grab the first line or 254 bytes, and rewind */
- if (fgets(s, 255, fp) == NULL)
- return (FILE *)0;
-
- fseek(fp, i, 0);
-
- /* Compressed? */
- if (iscompressed(s)) {
- if (dochroot)
- sprintf(buf, "%s \"%s\"\n", ZCATCMD, pathname);
- else
- sprintf(buf, "%s \"%s/%s\"\n", ZCATCMD, Data_Dir, pathname);
-
- if (! (pp = popen(buf, "r")))
- return (FILE *)0;
- ispipe = 1;
- return pp;
- }
-
- /* Script? */
- if (isshellscript(s)) {
- s[strlen(s)-1] = '\0';
- if (dochroot)
- sprintf(buf, "\"%s\" %s", pathname, (EXECargs == NULL) ? "" : EXECargs);
- else
- sprintf(buf, "\"%s/%s\" %s", Data_Dir, pathname, (EXECargs == NULL) ? "" : EXECargs);
-
- if (ASKfile != NULL) {
- strcat(buf, " < ");
- strcat(buf, ASKfile);
- }
-
- /*
- * Okay, let's change our working directory for the benefit of
- * shell script writers everywhere :-)
- */
-
- strcpy(s, pathname);
- cp = strrchr(s, '/');
- if (cp != NULL) {
- *cp = '\0';
- ;
- } else
- strcpy(s, "/");
-
- rchdir(s);
-
- if (DEBUG)
- fprintf(stderr, "Executing %s\n", buf);
-
- if (! (pp = popen(buf, "r")))
- return (FILE *)0;
- ispipe = 1;
-
- if (DEBUG)
- fprintf(stderr, "Zcat/popen is okay\n");
-
-
- return pp;
- }
-
- return (FILE *)0;
- }
-
-
- int
- iscompressed(s)
- char *s;
- {
- /* Magic number stuff for compressed file */
- if ( (s[0] & 0xff ) == 0x1f && ( s[1] & 0xff ) == 0x9d)
- return 1;
- else
- return 0;
- }
-
- int
- isshellscript(s)
- char *s;
- {
- if (! strncmp(s, "#!/", 3))
- return 1;
- else
- return 0;
- }
-
-
- int
- Specialclose(fp)
- FILE *fp;
- {
- if (ASKfile != NULL)
- unlink(ASKfile);
- if (ispipe)
- return(pclose(fp));
- else
- return(fclose(fp));
- }
-
- /* Pandora extension: Try opening a .Z file if regular file not found
- or vica versa */
- FILE *
- rfopenz( filename, type )
- char *filename, *type;
- {
- FILE *TheFile;
- if (( TheFile = rfopen( filename,type)) == NULL ) {
- /* Couldnt open the file as is , try the other*/
- if (strcmp(filename + strlen(filename) -2, ".Z") == 0)
- /* Tried compressed, so try regular next */
- filename[strlen(filename) - 2] = '\0';
- else /* Must have been a regular file , try compressed */
- strcat(filename,".Z");
- if (DEBUG)
- fprintf(stderr, "Trying alternative %s", filename);
- return (rfopen( filename,type));
- }
- else return(TheFile);
- }
-
-