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).
30 #include <sys/types.h>
32 #include <sys/param.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
= calloc(1, sizeof(*nc
));
115 wchar_t wc
[LOGIN_NAME_MAX
+ 1];
117 if (mbstowcs(wc
, name
, LOGIN_NAME_MAX
) > 0) {
118 wc
[LOGIN_NAME_MAX
] = '\0';
119 w
= wcswidth(wc
, LOGIN_NAME_MAX
);
125 /* note, we ignore names with non-printable widechars */
127 nc
->name
= strdup(name
);
128 else if (asprintf(&nc
->name
, "%lu", id
) == -1)
133 for (x
= *ic
; x
&& x
->next
; x
= x
->next
);
135 /* add 'nc' at end of the 'ic' list */
141 w
= strlen(nc
->name
);
142 *width
= *width
< w
? w
: *width
;
146 err(EXIT_FAILURE
, _("out of memory?"));
150 add_uid(unsigned long int id
)
152 struct idcache
*ic
= get_id(ucache
, id
);
155 struct passwd
*pw
= getpwuid((uid_t
) id
);
156 add_id(&ucache
, pw
? pw
->pw_name
: NULL
, id
, &uwidth
);
161 add_gid(unsigned long int id
)
163 struct idcache
*ic
= get_id(gcache
, id
);
166 struct group
*gr
= getgrgid((gid_t
) id
);
167 add_id(&gcache
, gr
? gr
->gr_name
: NULL
, id
, &gwidth
);
172 free_namei(struct namei
*nm
)
175 struct namei
*next
= nm
->next
;
184 readlink_to_namei(struct namei
*nm
, const char *path
)
189 sz
= readlink(path
, sym
, sizeof(sym
));
191 err(EXIT_FAILURE
, _("failed to read symlink: %s"), path
);
193 char *p
= strrchr(path
, '/');
195 nm
->relstart
= p
? p
- path
: 0;
197 sz
+= nm
->relstart
+ 1;
199 nm
->abslink
= xmalloc(sz
+ 1);
201 if (*sym
!= '/' && nm
->relstart
) {
202 /* create the absolute path from the relative symlink */
203 memcpy(nm
->abslink
, path
, nm
->relstart
);
204 *(nm
->abslink
+ nm
->relstart
) = '/';
206 memcpy(nm
->abslink
+ nm
->relstart
, sym
, sz
- nm
->relstart
);
208 memcpy(nm
->abslink
, sym
, sz
);
209 nm
->abslink
[sz
] = '\0';
213 dotdot_stat(const char *dirname
, struct stat
*st
)
218 #define DOTDOTDIR "/.."
223 len
= strlen(dirname
);
224 path
= malloc(len
+ sizeof(DOTDOTDIR
));
226 err(EXIT_FAILURE
, _("out of memory?"));
228 memcpy(path
, dirname
, len
);
229 memcpy(path
+ len
, DOTDOTDIR
, sizeof(DOTDOTDIR
));
232 err(EXIT_FAILURE
, _("could not stat '%s'"), path
);
237 static struct namei
*
238 new_namei(struct namei
*parent
, const char *path
, const char *fname
, int lev
)
244 nm
= calloc(1, sizeof(*nm
));
246 err(EXIT_FAILURE
, _("out of memory?"));
251 nm
->name
= strdup(fname
);
253 err(EXIT_FAILURE
, _("out of memory?"));
255 nm
->noent
= (lstat(path
, &nm
->st
) == -1);
259 if (S_ISLNK(nm
->st
.st_mode
))
260 readlink_to_namei(nm
, path
);
261 if (flags
& NAMEI_OWNERS
) {
262 add_uid(nm
->st
.st_uid
);
263 add_gid(nm
->st
.st_gid
);
266 if ((flags
& NAMEI_MNTS
) && S_ISDIR(nm
->st
.st_mode
)) {
267 struct stat stbuf
, *sb
= NULL
;
269 if (parent
&& S_ISDIR(parent
->st
.st_mode
))
271 else if (!parent
|| S_ISLNK(parent
->st
.st_mode
))
272 sb
= dotdot_stat(path
, &stbuf
);
274 if (sb
&& (sb
->st_dev
!= nm
->st
.st_dev
|| /* different device */
275 sb
->st_ino
== nm
->st
.st_ino
)) /* root directory */
282 static struct namei
*
283 add_namei(struct namei
*parent
, const char *orgpath
, int start
, struct namei
**last
)
285 struct namei
*nm
= NULL
, *first
= NULL
;
286 char *fname
, *end
, *path
;
293 level
= parent
->level
+ 1;
295 path
= strdup(orgpath
);
297 err(EXIT_FAILURE
, _("out of memory?"));
298 fname
= path
+ start
;
302 while (*fname
== '/')
303 fname
++; /* eat extra '/' */
304 first
= nm
= new_namei(nm
, "/", "/", level
);
307 for (end
= fname
; fname
&& end
; ) {
308 /* set end of filename */
310 end
= strchr(fname
, '/');
314 /* create a new entry */
315 nm
= new_namei(nm
, path
, fname
, level
);
320 /* set begin of the next filename */
324 end
++; /* eat extra '/' */
338 follow_symlinks(struct namei
*nm
)
342 for (; nm
; nm
= nm
->next
) {
343 struct namei
*next
, *last
;
347 if (!S_ISLNK(nm
->st
.st_mode
))
349 if (++symcount
> MAXSYMLINKS
) {
350 /* drop the rest of the list */
351 free_namei(nm
->next
);
356 nm
->next
= add_namei(nm
, nm
->abslink
, nm
->relstart
, &last
);
366 print_namei(struct namei
*nm
, char *path
)
371 printf("f: %s\n", path
);
373 for (; nm
; nm
= nm
->next
) {
377 printf(_("%s - No such file or directory\n"), nm
->name
);
381 strmode(nm
->st
.st_mode
, md
);
386 if (!(flags
& NAMEI_VERTICAL
)) {
387 for (i
= 0; i
< nm
->level
; i
++)
392 if (flags
& NAMEI_MODES
)
397 if (flags
& NAMEI_OWNERS
) {
398 printf(" %-*s", uwidth
,
399 get_id(ucache
, nm
->st
.st_uid
)->name
);
400 printf(" %-*s", gwidth
,
401 get_id(gcache
, nm
->st
.st_gid
)->name
);
404 if (flags
& NAMEI_VERTICAL
)
405 for (i
= 0; i
< nm
->level
; i
++)
408 if (S_ISLNK(nm
->st
.st_mode
))
409 printf(" %s -> %s\n", nm
->name
,
410 nm
->abslink
+ nm
->relstart
);
412 printf(" %s\n", nm
->name
);
420 const char *p
= program_invocation_short_name
;
425 printf(_("\nUsage: %s [options] pathname [pathname ...]\n"), p
);
426 printf(_("\nOptions:\n"));
429 " -h, --help displays this help text\n"
430 " -x, --mountpoints show mount point directories with a 'D'\n"
431 " -m, --modes show the mode bits of each file\n"
432 " -o, --owners show owner and group name of each file\n"
433 " -l, --long use a long listing format (-m -o -v) \n"
434 " -n, --nosymlinks don't follow symlinks\n"
435 " -v, --vertical vertical align of modes and owners\n"));
437 printf(_("\nFor more information see namei(1).\n"));
441 static const struct option longopts
[] =
443 { "help", 0, 0, 'h' },
444 { "mountpoints",0, 0, 'x' },
445 { "modes", 0, 0, 'm' },
446 { "owners", 0, 0, 'o' },
447 { "long", 0, 0, 'l' },
448 { "nolinks", 0, 0, 'n' },
449 { "vertical", 0, 0, 'v' },
454 main(int argc
, char **argv
)
458 int rc
= EXIT_SUCCESS
;
460 setlocale(LC_ALL
, "");
461 bindtextdomain(PACKAGE
, LOCALEDIR
);
467 while ((c
= getopt_long(argc
, argv
, "+h?lmnovx", longopts
, NULL
)) != -1) {
474 flags
|= (NAMEI_OWNERS
| NAMEI_MODES
| NAMEI_VERTICAL
);
477 flags
|= NAMEI_MODES
;
480 flags
|= NAMEI_NOLINKS
;
483 flags
|= NAMEI_OWNERS
;
489 flags
|= NAMEI_VERTICAL
;
493 for(; optind
< argc
; optind
++) {
494 char *path
= argv
[optind
];
495 struct namei
*nm
= NULL
;
498 if (stat(path
, &st
) != 0)
501 nm
= add_namei(NULL
, path
, 0, NULL
);
504 if (!(flags
& NAMEI_NOLINKS
))
505 sml
= follow_symlinks(nm
);
506 if (print_namei(nm
, path
)) {
513 warnx(_("%s: exceeded limit of symlinks"), path
);
519 free_idcache(ucache
);
520 free_idcache(gcache
);