diff options
-rw-r--r-- | tests/file.test | 4 | ||||
-rw-r--r-- | toys/posix/file.c | 9 |
2 files changed, 10 insertions, 3 deletions
diff --git a/tests/file.test b/tests/file.test index 234282f9..4c7f001c 100644 --- a/tests/file.test +++ b/tests/file.test @@ -10,6 +10,7 @@ echo "#! /bin/bash" > bash.script2 echo "#! /usr/bin/env python" > env.python.script echo "Hello, world!" > ascii echo "cafebabe000000310000" | xxd -r -p > java.class +ln -s java.class symlink testing "empty" "file empty" "empty: empty\n" "" "" testing "bash.script" "file bash.script" "bash.script: /bin/bash script\n" "" "" @@ -17,5 +18,8 @@ testing "bash.script with spaces" "file bash.script2" "bash.script2: /bin/bash s testing "env python script" "file env.python.script" "env.python.script: python script\n" "" "" testing "ascii" "file ascii" "ascii: ASCII text\n" "" "" testing "java class" "file java.class" "java.class: Java class file, version 49.0\n" "" "" +testing "symlink" "file symlink" "symlink: symbolic link\n" "" "" +testing "symlink -h" "file -h symlink" "symlink: symbolic link\n" "" "" +testing "symlink -L" "file -L symlink" "symlink: Java class file, version 49.0\n" "" "" rm empty bash.script bash.script2 env.python.script ascii java.class diff --git a/toys/posix/file.c b/toys/posix/file.c index 27b4c0c2..335c67ce 100644 --- a/toys/posix/file.c +++ b/toys/posix/file.c @@ -6,15 +6,18 @@ * * TODO: ar -USE_FILE(NEWTOY(file, "<1", TOYFLAG_USR|TOYFLAG_BIN)) +USE_FILE(NEWTOY(file, "<1hL[!hL]", TOYFLAG_USR|TOYFLAG_BIN)) config FILE bool "file" default y help - usage: file [file...] + usage: file [-hL] [file...] Examine the given files and describe their content types. + + -h don't follow symlinks (default) + -L follow symlinks */ #define FOR_file @@ -281,7 +284,7 @@ void file_main(void) xprintf("%s: %*s", name, (int)(TT.max_name_len - strlen(name)), ""); - if (fd || !lstat(name, &sb)) { + if (fd || !((toys.optflags & FLAG_L) ? stat : lstat)(name, &sb)) { if (fd || S_ISREG(sb.st_mode)) { if (!sb.st_size) what = "empty"; else if ((fd = openro(name, O_RDONLY)) != -1) { |