diff options
Diffstat (limited to 'contrib/file/file.c')
-rw-r--r-- | contrib/file/file.c | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/contrib/file/file.c b/contrib/file/file.c index 80f5c41..a482f63 100644 --- a/contrib/file/file.c +++ b/contrib/file/file.c @@ -39,7 +39,6 @@ #include <sys/types.h> #include <sys/param.h> /* for MAXPATHLEN */ #include <sys/stat.h> -#include <fcntl.h> /* for open() */ #ifdef RESTORE_TIME # if (__COHERENT__ >= 0x420) # include <sys/utime.h> @@ -72,12 +71,12 @@ #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$Id: file.c,v 1.95 2004/09/27 15:28:37 christos Exp $") +FILE_RCSID("@(#)$Id: file.c,v 1.100 2005/10/17 18:41:44 christos Exp $") #endif /* lint */ #ifdef S_IFLNK -#define SYMLINKFLAG "L" +#define SYMLINKFLAG "Lh" #else #define SYMLINKFLAG "" #endif @@ -95,7 +94,7 @@ private int /* Global command-line options */ private const char *magicfile = 0; /* where the magic is */ private const char *default_magicfile = MAGIC; -private char *separator = ":"; /* Default field separator */ +private const char *separator = ":"; /* Default field separator */ private char *progname; /* used throughout */ @@ -127,7 +126,7 @@ main(int argc, char *argv[]) int flags = 0; char *home, *usermagic; struct stat sb; -#define OPTSTRING "bcCdf:F:ikLm:nNprsvz" +#define OPTSTRING "bcCdf:F:hikLm:nNprsvz" #ifdef HAVE_GETOPT_LONG int longindex; private struct option long_options[] = @@ -143,6 +142,7 @@ main(int argc, char *argv[]) {"keep-going", 0, 0, 'k'}, #ifdef S_IFLNK {"dereference", 0, 0, 'L'}, + {"no-dereference", 0, 0, 'h'}, #endif {"magic-file", 1, 0, 'm'}, #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) @@ -159,7 +159,8 @@ main(int argc, char *argv[]) #endif #ifdef LC_CTYPE - setlocale(LC_CTYPE, ""); /* makes islower etc work for other langs */ + /* makes islower etc work for other langs */ + (void)setlocale(LC_CTYPE, ""); #endif #ifdef __EMX__ @@ -187,6 +188,9 @@ main(int argc, char *argv[]) } } +#ifdef S_IFLNK + flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0; +#endif #ifndef HAVE_GETOPT_LONG while ((c = getopt(argc, argv, OPTSTRING)) != -1) #else @@ -249,9 +253,9 @@ main(int argc, char *argv[]) flags |= MAGIC_DEVICES; break; case 'v': - (void) fprintf(stdout, "%s-%d.%.2d\n", progname, + (void)fprintf(stdout, "%s-%d.%.2d\n", progname, FILE_VERSION_MAJOR, patchlevel); - (void) fprintf(stdout, "magic file from %s\n", + (void)fprintf(stdout, "magic file from %s\n", magicfile); return 1; case 'z': @@ -261,6 +265,9 @@ main(int argc, char *argv[]) case 'L': flags |= MAGIC_SYMLINK; break; + case 'h': + flags &= ~MAGIC_SYMLINK; + break; #endif case '?': default: @@ -316,6 +323,7 @@ main(int argc, char *argv[]) private void +/*ARGSUSED*/ load(const char *m, int flags) { if (magic) @@ -341,6 +349,7 @@ unwrap(char *fn) char buf[MAXPATHLEN]; FILE *f; int wid = 0, cwid; + size_t len; if (strcmp("-", fn) == 0) { f = stdin; @@ -353,7 +362,10 @@ unwrap(char *fn) } while (fgets(buf, MAXPATHLEN, f) != NULL) { - cwid = file_mbswidth(buf) - 1; + len = strlen(buf); + if (len > 0 && buf[len - 1] == '\n') + buf[len - 1] = '\0'; + cwid = file_mbswidth(buf); if (cwid > wid) wid = cwid; } @@ -362,13 +374,15 @@ unwrap(char *fn) } while (fgets(buf, MAXPATHLEN, f) != NULL) { - buf[file_mbswidth(buf)-1] = '\0'; + len = strlen(buf); + if (len > 0 && buf[len - 1] == '\n') + buf[len - 1] = '\0'; process(buf, wid); if(nobuffer) - (void) fflush(stdout); + (void)fflush(stdout); } - (void) fclose(f); + (void)fclose(f); } private void @@ -378,14 +392,14 @@ process(const char *inname, int wid) int std_in = strcmp(inname, "-") == 0; if (wid > 0 && !bflag) - (void) printf("%s%s%*s ", std_in ? "/dev/stdin" : inname, + (void)printf("%s%s%*s ", std_in ? "/dev/stdin" : inname, separator, (int) (nopad ? 0 : (wid - file_mbswidth(inname))), ""); type = magic_file(magic, std_in ? NULL : inname); if (type == NULL) - printf("ERROR: %s\n", magic_error(magic)); + (void)printf("ERROR: %s\n", magic_error(magic)); else - printf("%s\n", type); + (void)printf("%s\n", type); } @@ -494,7 +508,7 @@ usage(void) private void help(void) { - puts( + (void)puts( "Usage: file [OPTION]... [FILE]...\n" "Determine file type of FILEs.\n" "\n" |