]> granicus.if.org Git - file/commitdiff
John Gilmore mod: allow "-" as a filename argument!
authorIan Darwin <ian@darwinsys.com>
Fri, 28 Aug 1987 21:04:38 +0000 (21:04 +0000)
committerIan Darwin <ian@darwinsys.com>
Fri, 28 Aug 1987 21:04:38 +0000 (21:04 +0000)
Clever, but involves a goto.

src/file.c

index 66cb4cb5f54bc0e31880e126d29be056fe98abd9..65af4d00e8d5fe0ab743d68b32c1864fc704bd07 100644 (file)
@@ -15,7 +15,11 @@ int check = 0,               /* check format of magic file */
        nbytes = 0,     /* number of bytes read from a datafile */
        nmagic = 0;     /* number of valid magic[]s */
 FILE *efopen();
+#ifdef MAGIC
+char *magicfile = MAGIC;       /* where magic be found */
+#else
 char *magicfile = "/etc/magic";        /* where magic be found */
+#endif
 char *progname;
 struct stat statbuf;
 struct utimbuf {       /* for utime(2), belongs in a .h file */
@@ -101,9 +105,17 @@ process(inname)
 char   *inname;
 {
        int     fd;
-       char    buf[HOWMANY];
+       char    buf[HOWMANY+1];
        struct utimbuf utbuf;
 
+       if (strcmp("-", inname) == 0) {
+               /* Standard input */
+               if (fstat(0, &statbuf)<0)
+                       warning("cannot fstat; ");
+               fd = 0;
+               goto readit;
+       }
+               
        (void) printf("%s:\t", inname);
 
        /*
@@ -113,15 +125,20 @@ char      *inname;
        if (fsmagic(inname) != 0) {
                /*NULLBODY*/;
        } else if ((fd = open(inname, 0)) < 0) {
-               warning("can't open for reading");
+               /* We can't open it, but we were able to stat it. */
+               if (statbuf.st_mode & 0002) ckfputs("writeable, ", stdout);
+               if (statbuf.st_mode & 0111) ckfputs("executable, ", stdout);
+               warning("can't read");
        } else {
+readit:
                /*
                 * try looking at the first HOWMANY bytes
                 */
                if ((nbytes = read(fd, buf, HOWMANY)) == -1)
                        warning("read failed");
+               buf[nbytes] = '\0';     /* Terminate string */
                /*
-                * try tests in /etc/magic (or surrogate magic file
+                * try tests in /etc/magic (or surrogate magic file)
                 */
                if (softmagic(buf) == 1)
                        /*NULLBODY*/;
@@ -136,13 +153,16 @@ char      *inname;
                         */
                        ckfputs("data", stdout);
                }
-               /*
-                * Restore access, modification times if we read the file.
-                */
-               utbuf.actime = statbuf.st_atime;
-               utbuf.modtime = statbuf.st_mtime;
-               (void) utime(inname, &utbuf); /* don't care if we lack perms */
-               (void) close(fd);
+               if (0 != strcmp("-", inname)) {
+                       /*
+                        * Restore access, modification times if we read it.
+                        */
+                       utbuf.actime = statbuf.st_atime;
+                       utbuf.modtime = statbuf.st_mtime;
+                       (void) utime(inname, &utbuf);
+                       /* we don't care if we lack perms */
+                       (void) close(fd);
+               }
        }
 
        (void) putchar('\n');