diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2015-11-13 08:30:19 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2015-11-13 08:30:19 +0000 |
commit | 53950e2c447fd6bb7444c362beb77abe68a9cd4b (patch) | |
tree | 391f35a6e26185f38f00fa072e650baf27487c34 | |
parent | 1807e69c6fc53984f200744f88f41cb74d0afb66 (diff) |
Call stat not lstat with -L, makes links actually be followed. Reported
by and ok semarie@.
-rw-r--r-- | usr.bin/file/file.c | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/usr.bin/file/file.c b/usr.bin/file/file.c index 4462cbddf88..c0f8d7d9dab 100644 --- a/usr.bin/file/file.c +++ b/usr.bin/file/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.53 2015/10/17 04:41:37 deraadt Exp $ */ +/* $OpenBSD: file.c,v 1.54 2015/11/13 08:30:18 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org> @@ -116,7 +116,7 @@ usage(void) int main(int argc, char **argv) { - int opt, pair[2], fd, idx, mode; + int opt, pair[2], fd, idx, mode, error; char *home; struct passwd *pw; struct imsgbuf ibuf; @@ -218,26 +218,33 @@ main(int argc, char **argv) msg.error = errno; } else fd = STDIN_FILENO; - } else if (lstat(argv[idx], &msg.sb) == -1) { - fd = -1; - msg.error = errno; } else { - /* - * pledge(2) doesn't let us pass directory file - * descriptors around - but in fact we don't need them, - * so just don't open directories or symlinks (which - * could be to directories). - */ - mode = msg.sb.st_mode; - if (!S_ISDIR(mode) && !S_ISLNK(mode)) { - fd = open(argv[idx], O_RDONLY|O_NONBLOCK); - if (fd == -1 && - (errno == ENFILE || errno == EMFILE)) - err(1, "open"); - } else + if (Lflag) + error = stat(argv[idx], &msg.sb); + else + error = lstat(argv[idx], &msg.sb); + if (error == -1) { fd = -1; - if (S_ISLNK(mode)) - read_link(&msg, argv[idx]); + msg.error = errno; + } else { + /* + * pledge(2) doesn't let us pass directory file + * descriptors around - but in fact we don't + * need them, so just don't open directories or + * symlinks (which could be to directories). + */ + mode = msg.sb.st_mode; + if (!S_ISDIR(mode) && !S_ISLNK(mode)) { + fd = open(argv[idx], + O_RDONLY|O_NONBLOCK); + if (fd == -1 && (errno == ENFILE || + errno == EMFILE)) + err(1, "open"); + } else + fd = -1; + if (S_ISLNK(mode)) + read_link(&msg, argv[idx]); + } } send_message(&ibuf, &msg, sizeof msg, fd); @@ -329,13 +336,8 @@ read_link(struct input_msg *msg, const char *path) free(copy); } - if (Lflag) { - if (stat(path, &msg->sb) == -1) - msg->error = errno; - } else { - if (stat(path, &sb) == -1) - msg->link_target = errno; - } + if (!Lflag && stat(path, &sb) == -1) + msg->link_target = errno; } static __dead void |