#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();
progname = argv[0];
- while ((c = getopt(argc, argv, "cdf:m:")) != EOF)
+ while ((c = getopt(argc, argv, "cdf:Lm:")) != EOF)
switch (c) {
case 'c':
++check;
unwrap(optarg);
++didsomefiles;
break;
+ case 'L':
+ ++followLinks;
+ break;
case 'm':
magicfile = optarg;
break;
}
else
for (; optind < argc; optind++)
- process(argv[optind]);
+ process(argv[optind], 1);
exit(0);
}
else {
while (fgets(buf, FILENAMELEN, f) != NULL) {
buf[strlen(buf)-1] = '\0';
- process(buf);
+ process(buf, 1);
}
(void) fclose(f);
}
/*
* process - process input file
*/
-process(inname)
+process(inname, top)
char *inname;
+int top; /* true if called from top level */
{
int fd;
char buf[HOWMANY];
#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;
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;
}
#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
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