]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/namei.c
lsblk: Fall back to ID_SERIAL
[thirdparty/util-linux.git] / misc-utils / namei.c
index dcaf5d2ebb49bf2ce3f4336d0b0b63bd3adc83c0..9c5f5fa4ac3952423c1c5f9e4b4d3ae4b157c148 100644 (file)
@@ -59,7 +59,7 @@ struct namei {
        struct namei    *next;          /* next item */
        int             level;
        int             mountpoint;     /* is mount point */
-       int             noent;          /* is this item not existing */
+       int             noent;          /* this item not existing (stores errno from stat()) */
 };
 
 static int flags;
@@ -151,9 +151,10 @@ new_namei(struct namei *parent, const char *path, const char *fname, int lev)
        nm->level = lev;
        nm->name = xstrdup(fname);
 
-       nm->noent = (lstat(path, &nm->st) == -1);
-       if (nm->noent)
+       if (lstat(path, &nm->st) != 0) {
+               nm->noent = errno;
                return nm;
+       }
 
        if (S_ISLNK(nm->st.st_mode))
                readlink_to_namei(nm, path);
@@ -280,7 +281,7 @@ print_namei(struct namei *nm, char *path)
                                blanks += 1;
                        blanks += nm->level * 2;
                        printf("%*s ", blanks, "");
-                       printf(_("%s - No such file or directory\n"), nm->name);
+                       printf("%s - %s\n", nm->name, strerror(nm->noent));
                        return -1;
                }
 
@@ -336,16 +337,16 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_("Follow a pathname until a terminal point is found.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
-       fputs(_(" -h, --help          displays this help text\n"
-               " -V, --version       output version information and exit\n"
+       fputs(_(
                " -x, --mountpoints   show mount point directories with a 'D'\n"
                " -m, --modes         show the mode bits of each file\n"
                " -o, --owners        show owner and group name of each file\n"
                " -l, --long          use a long listing format (-m -o -v) \n"
                " -n, --nosymlinks    don't follow symlinks\n"
                " -v, --vertical      vertical align of modes and owners\n"), out);
+       printf(USAGE_HELP_OPTIONS(21));
 
-       fprintf(out, USAGE_MAN_TAIL("namei(1)"));
+       printf(USAGE_MAN_TAIL("namei(1)"));
        exit(EXIT_SUCCESS);
 }
 
@@ -371,16 +372,10 @@ main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((c = getopt_long(argc, argv, "hVlmnovx", longopts, NULL)) != -1) {
                switch(c) {
-               case 'h':
-                       usage();
-                       break;
-               case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
                case 'l':
                        flags |= (NAMEI_OWNERS | NAMEI_MODES | NAMEI_VERTICAL);
                        break;
@@ -399,6 +394,11 @@ main(int argc, char **argv)
                case 'v':
                        flags |= NAMEI_VERTICAL;
                        break;
+
+               case 'h':
+                       usage();
+               case 'V':
+                       print_version(EXIT_SUCCESS);
                default:
                        errtryhelp(EXIT_FAILURE);
                }