From: Ian Darwin Date: Wed, 3 Oct 1990 17:48:40 +0000 (+0000) Subject: Add a -L option that lets you follow symlinks, after BSD ls(1)'s -L option. X-Git-Tag: FILE3_27~318 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7b446f70e780aec7a2169a4822525e0f43088a5d;p=file Add a -L option that lets you follow symlinks, after BSD ls(1)'s -L option. --- diff --git a/src/file.c b/src/file.c index 2403867b..db76131c 100644 --- a/src/file.c +++ b/src/file.c @@ -34,10 +34,11 @@ #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]; diff --git a/src/fsmagic.c b/src/fsmagic.c index 88afc4a6..84cd8541 100644 --- a/src/fsmagic.c +++ b/src/fsmagic.c @@ -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