2 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
4 * This file is part of util-linux.
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * The original namei(1) was writtent by:
17 * Roger S. Southwick (May 2, 1990)
18 * Steve Tell (March 28, 1991)
19 * Arkadiusz Mikiewicz (1999-02-22)
20 * Li Zefan (2007-09-10).
29 #include <sys/types.h>
31 #include <sys/param.h>
40 #include "closestream.h"
43 #define MAXSYMLINKS 256
46 #ifndef LOGIN_NAME_MAX
47 #define LOGIN_NAME_MAX 256
50 #define NAMEI_NOLINKS (1 << 1)
51 #define NAMEI_MODES (1 << 2)
52 #define NAMEI_MNTS (1 << 3)
53 #define NAMEI_OWNERS (1 << 4)
54 #define NAMEI_VERTICAL (1 << 5)
58 struct stat st
; /* item lstat() */
59 char *name
; /* item name */
60 char *abslink
; /* absolute symlink path */
61 int relstart
; /* offset of relative path in 'abslink' */
62 struct namei
*next
; /* next item */
64 int mountpoint
; /* is mount point */
65 int noent
; /* is this item not existing */
75 static int uwidth
; /* maximal width of username */
76 static int gwidth
; /* maximal width of groupname */
77 static struct idcache
*gcache
; /* groupnames */
78 static struct idcache
*ucache
; /* usernames */
80 static struct idcache
*
81 get_id(struct idcache
*ic
, unsigned long int id
)
92 free_idcache(struct idcache
*ic
)
95 struct idcache
*next
= ic
->next
;
103 add_id(struct idcache
**ic
, char *name
, unsigned long int id
, int *width
)
105 struct idcache
*nc
, *x
;
108 nc
= xcalloc(1, sizeof(*nc
));
113 wchar_t wc
[LOGIN_NAME_MAX
+ 1];
115 if (mbstowcs(wc
, name
, LOGIN_NAME_MAX
) > 0) {
116 wc
[LOGIN_NAME_MAX
] = '\0';
117 w
= wcswidth(wc
, LOGIN_NAME_MAX
);
123 /* note, we ignore names with non-printable widechars */
125 nc
->name
= xstrdup(name
);
127 xasprintf(&nc
->name
, "%lu", id
);
129 for (x
= *ic
; x
&& x
->next
; x
= x
->next
);
131 /* add 'nc' at end of the 'ic' list */
137 w
= nc
->name
? strlen(nc
->name
) : 0;
139 *width
= *width
< w
? w
: *width
;
144 add_uid(unsigned long int id
)
146 struct idcache
*ic
= get_id(ucache
, id
);
149 struct passwd
*pw
= getpwuid((uid_t
) id
);
150 add_id(&ucache
, pw
? pw
->pw_name
: NULL
, id
, &uwidth
);
155 add_gid(unsigned long int id
)
157 struct idcache
*ic
= get_id(gcache
, id
);
160 struct group
*gr
= getgrgid((gid_t
) id
);
161 add_id(&gcache
, gr
? gr
->gr_name
: NULL
, id
, &gwidth
);
166 free_namei(struct namei
*nm
)
169 struct namei
*next
= nm
->next
;
178 readlink_to_namei(struct namei
*nm
, const char *path
)
184 sz
= readlink(path
, sym
, sizeof(sym
));
186 err(EXIT_FAILURE
, _("failed to read symlink: %s"), path
);
188 char *p
= strrchr(path
, '/');
192 nm
->relstart
= p
- path
;
193 sz
+= nm
->relstart
+ 1;
196 nm
->abslink
= xmalloc(sz
+ 1);
198 if (*sym
!= '/' && isrel
) {
199 /* create the absolute path from the relative symlink */
200 memcpy(nm
->abslink
, path
, nm
->relstart
);
201 *(nm
->abslink
+ nm
->relstart
) = '/';
203 memcpy(nm
->abslink
+ nm
->relstart
, sym
, sz
- nm
->relstart
);
205 /* - absolute link (foo -> /path/bar)
206 * - or link without any subdir (foo -> bar)
208 memcpy(nm
->abslink
, sym
, sz
);
210 nm
->abslink
[sz
] = '\0';
214 dotdot_stat(const char *dirname
, struct stat
*st
)
219 #define DOTDOTDIR "/.."
224 len
= strlen(dirname
);
225 path
= xmalloc(len
+ sizeof(DOTDOTDIR
));
227 memcpy(path
, dirname
, len
);
228 memcpy(path
+ len
, DOTDOTDIR
, sizeof(DOTDOTDIR
));
231 err(EXIT_FAILURE
, _("stat failed %s"), path
);
236 static struct namei
*
237 new_namei(struct namei
*parent
, const char *path
, const char *fname
, int lev
)
243 nm
= xcalloc(1, sizeof(*nm
));
248 nm
->name
= xstrdup(fname
);
250 nm
->noent
= (lstat(path
, &nm
->st
) == -1);
254 if (S_ISLNK(nm
->st
.st_mode
))
255 readlink_to_namei(nm
, path
);
256 if (flags
& NAMEI_OWNERS
) {
257 add_uid(nm
->st
.st_uid
);
258 add_gid(nm
->st
.st_gid
);
261 if ((flags
& NAMEI_MNTS
) && S_ISDIR(nm
->st
.st_mode
)) {
262 struct stat stbuf
, *sb
= NULL
;
264 if (parent
&& S_ISDIR(parent
->st
.st_mode
))
266 else if (!parent
|| S_ISLNK(parent
->st
.st_mode
))
267 sb
= dotdot_stat(path
, &stbuf
);
269 if (sb
&& (sb
->st_dev
!= nm
->st
.st_dev
|| /* different device */
270 sb
->st_ino
== nm
->st
.st_ino
)) /* root directory */
277 static struct namei
*
278 add_namei(struct namei
*parent
, const char *orgpath
, int start
, struct namei
**last
)
280 struct namei
*nm
= NULL
, *first
= NULL
;
281 char *fname
, *end
, *path
;
288 level
= parent
->level
+ 1;
290 path
= xstrdup(orgpath
);
291 fname
= path
+ start
;
295 while (*fname
== '/')
296 fname
++; /* eat extra '/' */
297 first
= nm
= new_namei(nm
, "/", "/", level
);
300 for (end
= fname
; fname
&& end
; ) {
301 /* set end of filename */
303 end
= strchr(fname
, '/');
307 /* create a new entry */
308 nm
= new_namei(nm
, path
, fname
, level
);
313 /* set begin of the next filename */
317 end
++; /* eat extra '/' */
331 follow_symlinks(struct namei
*nm
)
335 for (; nm
; nm
= nm
->next
) {
336 struct namei
*next
, *last
;
340 if (!S_ISLNK(nm
->st
.st_mode
))
342 if (++symcount
> MAXSYMLINKS
) {
343 /* drop the rest of the list */
344 free_namei(nm
->next
);
349 nm
->next
= add_namei(nm
, nm
->abslink
, nm
->relstart
, &last
);
359 print_namei(struct namei
*nm
, char *path
)
364 printf("f: %s\n", path
);
366 for (; nm
; nm
= nm
->next
) {
370 printf(_("%s - No such file or directory\n"), nm
->name
);
374 strmode(nm
->st
.st_mode
, md
);
379 if (!(flags
& NAMEI_VERTICAL
)) {
380 for (i
= 0; i
< nm
->level
; i
++)
385 if (flags
& NAMEI_MODES
)
390 if (flags
& NAMEI_OWNERS
) {
391 printf(" %-*s", uwidth
,
392 get_id(ucache
, nm
->st
.st_uid
)->name
);
393 printf(" %-*s", gwidth
,
394 get_id(gcache
, nm
->st
.st_gid
)->name
);
397 if (flags
& NAMEI_VERTICAL
)
398 for (i
= 0; i
< nm
->level
; i
++)
401 if (S_ISLNK(nm
->st
.st_mode
))
402 printf(" %s -> %s\n", nm
->name
,
403 nm
->abslink
+ nm
->relstart
);
405 printf(" %s\n", nm
->name
);
410 static void usage(int rc
)
412 const char *p
= program_invocation_short_name
;
413 FILE *out
= rc
== EXIT_FAILURE
? stderr
: stdout
;
418 fputs(_("\nUsage:\n"), out
);
420 _(" %s [options] pathname [pathname ...]\n"), p
);
422 fputs(_("\nOptions:\n"), out
);
423 fputs(_(" -h, --help displays this help text\n"
424 " -V, --version output version information and exit\n"
425 " -x, --mountpoints show mount point directories with a 'D'\n"
426 " -m, --modes show the mode bits of each file\n"
427 " -o, --owners show owner and group name of each file\n"
428 " -l, --long use a long listing format (-m -o -v) \n"
429 " -n, --nosymlinks don't follow symlinks\n"
430 " -v, --vertical vertical align of modes and owners\n"), out
);
432 fputs(_("\nFor more information see namei(1).\n"), out
);
436 static const struct option longopts
[] =
438 { "help", 0, 0, 'h' },
439 { "version", 0, 0, 'V' },
440 { "mountpoints",0, 0, 'x' },
441 { "modes", 0, 0, 'm' },
442 { "owners", 0, 0, 'o' },
443 { "long", 0, 0, 'l' },
444 { "nolinks", 0, 0, 'n' },
445 { "vertical", 0, 0, 'v' },
450 main(int argc
, char **argv
)
453 int rc
= EXIT_SUCCESS
;
455 setlocale(LC_ALL
, "");
456 bindtextdomain(PACKAGE
, LOCALEDIR
);
458 atexit(close_stdout
);
460 while ((c
= getopt_long(argc
, argv
, "hVlmnovx", longopts
, NULL
)) != -1) {
466 printf(UTIL_LINUX_VERSION
);
469 flags
|= (NAMEI_OWNERS
| NAMEI_MODES
| NAMEI_VERTICAL
);
472 flags
|= NAMEI_MODES
;
475 flags
|= NAMEI_NOLINKS
;
478 flags
|= NAMEI_OWNERS
;
484 flags
|= NAMEI_VERTICAL
;
491 if (optind
== argc
) {
492 warnx(_("pathname argument is missing"));
496 for(; optind
< argc
; optind
++) {
497 char *path
= argv
[optind
];
498 struct namei
*nm
= NULL
;
501 if (stat(path
, &st
) != 0)
504 nm
= add_namei(NULL
, path
, 0, NULL
);
507 if (!(flags
& NAMEI_NOLINKS
))
508 sml
= follow_symlinks(nm
);
509 if (print_namei(nm
, path
)) {
516 warnx(_("%s: exceeded limit of symlinks"), path
);
522 free_idcache(ucache
);
523 free_idcache(gcache
);