]> granicus.if.org Git - file/commitdiff
Add a -L option that lets you follow symlinks, after BSD ls(1)'s -L option.
authorIan Darwin <ian@darwinsys.com>
Wed, 3 Oct 1990 17:48:40 +0000 (17:48 +0000)
committerIan Darwin <ian@darwinsys.com>
Wed, 3 Oct 1990 17:48:40 +0000 (17:48 +0000)
src/file.c
src/fsmagic.c

index 2403867b5543ee8b999c7fc436c68832f4755005..db76131cfcf892233532db8edd0689d9d33f40db 100644 (file)
 
 #ifndef        lint
 static char *moduleid = 
-       "@(#)$Header: /home/glen/git/file/cvs/file/src/file.c,v 1.15 1988/03/23 11:42:16 ian Exp $";
+       "@(#)$Header: /home/glen/git/file/cvs/file/src/file.c,v 1.16 1990/10/03 17:49:09 ian Exp $";
 #endif /* lint */
 extern char *ckfmsg;
 int    debug = 0,      /* huh? */
+       followLinks = 0, /* follow Symlinks (BSD only) */
        nbytes = 0,     /* number of bytes read from a datafile */
        nmagic = 0;     /* number of valid magic[]s */
 FILE *efopen();
@@ -67,7 +68,7 @@ char *argv[];
 
        progname = argv[0];
 
-       while ((c = getopt(argc, argv, "cdf:m:")) != EOF)
+       while ((c = getopt(argc, argv, "cdf:Lm:")) != EOF)
                switch (c) {
                case 'c':
                        ++check;
@@ -79,6 +80,9 @@ char *argv[];
                        unwrap(optarg);
                        ++didsomefiles;
                        break;
+               case 'L':
+                       ++followLinks;
+                       break;
                case 'm':
                        magicfile = optarg;
                        break;
@@ -104,7 +108,7 @@ char *argv[];
        }
        else
                for (; optind < argc; optind++)
-                       process(argv[optind]);
+                       process(argv[optind], 1);
 
        exit(0);
 }
@@ -125,7 +129,7 @@ char *fn;
        else {
                while (fgets(buf, FILENAMELEN, f) != NULL) {
                        buf[strlen(buf)-1] = '\0';
-                       process(buf);
+                       process(buf, 1);
                }
                (void) fclose(f);
        }
@@ -134,8 +138,9 @@ char *fn;
 /*
  * process - process input file
  */
-process(inname)
+process(inname, top)
 char   *inname;
+int top;               /* true if called from top level */
 {
        int     fd;
        char    buf[HOWMANY];
index 88afc4a66db14e58880bce269ff1327def9bfcb7..84cd8541792ab27cd9f5ef898aa3b7d0c30141d1 100644 (file)
@@ -42,7 +42,7 @@
 
 #ifndef        lint
 static char *moduleid = 
-       "@(#)$Header: /home/glen/git/file/cvs/file/src/fsmagic.c,v 1.8 1988/02/28 10:50:50 ian Exp $";
+       "@(#)$Header: /home/glen/git/file/cvs/file/src/fsmagic.c,v 1.9 1990/10/03 17:48:40 ian Exp $";
 #endif /* lint */
 
 extern char *progname;
@@ -50,21 +50,26 @@ extern char *ckfmsg, *magicfile;
 extern int debug;
 extern FILE *efopen();
 
+
 fsmagic(fn)
 char *fn;
 {
        extern struct stat statbuf;
+       extern followLinks;
+       int ret = 0;
 
        /*
         * Fstat is cheaper but fails for files you don't have read perms on.
         * On 4.2BSD and similar systems, use lstat() so identify symlinks.
         */
 #ifdef S_IFLNK
-       if (lstat(fn, &statbuf) <0)
-#else
-       if (stat(fn, &statbuf) <0)
+       if (!followLinks)
+               ret = lstat(fn, &statbuf);
+       else
 #endif
-               {
+       ret = stat(fn, &statbuf);
+
+       if (ret) {
                        warning("can't stat");
                        return -1;
                }
@@ -93,8 +98,23 @@ char *fn;
 #endif
 #ifdef S_IFLNK
        case S_IFLNK:
-               ckfputs("symbolic link", stdout);
-               readsymbolic(fn);
+               {
+                       char buf[BUFSIZ+4];
+                       register int nch;
+
+                       if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
+                               error("readlink failed");
+                               return 0;
+                       }
+                       buf[nch] = '\0';        /* readlink(2) forgets this */
+                       if (followLinks) {
+                               process(buf, 0);
+                               return 1;
+                       } else { /* just print what it points to */
+                               ckfputs("symbolic link to", stdout);
+                               ckfputs(buf, stdout);
+                       }
+               }
                return 1;
 #endif
 #ifdef S_IFSOCK
@@ -118,17 +138,3 @@ char *fn;
        return 0;
 }
 
-#ifdef S_IFLNK
-readsymbolic(fn)
-char *fn;
-{      char buf[BUFSIZ+4];
-       register int cc;
-
-       strcpy(buf, " to ");
-       cc = readlink(fn, &buf[4], BUFSIZ-1);
-       if (cc <= 0)
-               return;
-       buf[cc+4] = '\0';
-       ckfputs(buf, stdout);
-}
-#endif