]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/namei.c
docs: AUTHORS: remove four duplicate entries
[thirdparty/util-linux.git] / misc-utils / namei.c
1 /*
2 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3 *
4 * This file is part of util-linux.
5 *
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.
10 *
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.
15 *
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).
21 */
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/param.h>
32 #include <pwd.h>
33 #include <grp.h>
34
35 #include "c.h"
36 #include "xalloc.h"
37 #include "nls.h"
38 #include "widechar.h"
39 #include "strutils.h"
40 #include "closestream.h"
41
42 #ifndef MAXSYMLINKS
43 #define MAXSYMLINKS 256
44 #endif
45
46 #ifndef LOGIN_NAME_MAX
47 #define LOGIN_NAME_MAX 256
48 #endif
49
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)
55
56
57 struct namei {
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 */
63 int level;
64 int mountpoint; /* is mount point */
65 int noent; /* is this item not existing */
66 };
67
68 struct idcache {
69 unsigned long int id;
70 char *name;
71 struct idcache *next;
72 };
73
74 static int flags;
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 */
79
80 static struct idcache *
81 get_id(struct idcache *ic, unsigned long int id)
82 {
83 while(ic) {
84 if (ic->id == id)
85 return ic;
86 ic = ic->next;
87 }
88 return NULL;
89 }
90
91 static void
92 free_idcache(struct idcache *ic)
93 {
94 while(ic) {
95 struct idcache *next = ic->next;
96 free(ic->name);
97 free(ic);
98 ic = next;
99 }
100 }
101
102 static void
103 add_id(struct idcache **ic, char *name, unsigned long int id, int *width)
104 {
105 struct idcache *nc, *x;
106 int w = 0;
107
108 nc = xcalloc(1, sizeof(*nc));
109 nc->id = id;
110
111 if (name) {
112 #ifdef HAVE_WIDECHAR
113 wchar_t wc[LOGIN_NAME_MAX + 1];
114
115 if (mbstowcs(wc, name, LOGIN_NAME_MAX) > 0) {
116 wc[LOGIN_NAME_MAX] = '\0';
117 w = wcswidth(wc, LOGIN_NAME_MAX);
118 }
119 else
120 #endif
121 w = strlen(name);
122 }
123 /* note, we ignore names with non-printable widechars */
124 if (w > 0)
125 nc->name = xstrdup(name);
126 else
127 xasprintf(&nc->name, "%lu", id);
128
129 for (x = *ic; x && x->next; x = x->next);
130
131 /* add 'nc' at end of the 'ic' list */
132 if (x)
133 x->next = nc;
134 else
135 *ic = nc;
136 if (w <= 0)
137 w = nc->name ? strlen(nc->name) : 0;
138
139 *width = *width < w ? w : *width;
140 return;
141 }
142
143 static void
144 add_uid(unsigned long int id)
145 {
146 struct idcache *ic = get_id(ucache, id);
147
148 if (!ic) {
149 struct passwd *pw = getpwuid((uid_t) id);
150 add_id(&ucache, pw ? pw->pw_name : NULL, id, &uwidth);
151 }
152 }
153
154 static void
155 add_gid(unsigned long int id)
156 {
157 struct idcache *ic = get_id(gcache, id);
158
159 if (!ic) {
160 struct group *gr = getgrgid((gid_t) id);
161 add_id(&gcache, gr ? gr->gr_name : NULL, id, &gwidth);
162 }
163 }
164
165 static void
166 free_namei(struct namei *nm)
167 {
168 while (nm) {
169 struct namei *next = nm->next;
170 free(nm->name);
171 free(nm->abslink);
172 free(nm);
173 nm = next;
174 }
175 }
176
177 static void
178 readlink_to_namei(struct namei *nm, const char *path)
179 {
180 char sym[PATH_MAX];
181 ssize_t sz;
182 int isrel = 0;
183
184 sz = readlink(path, sym, sizeof(sym));
185 if (sz < 1)
186 err(EXIT_FAILURE, _("failed to read symlink: %s"), path);
187 if (*sym != '/') {
188 char *p = strrchr(path, '/');
189
190 if (p) {
191 isrel = 1;
192 nm->relstart = p - path;
193 sz += nm->relstart + 1;
194 }
195 }
196 nm->abslink = xmalloc(sz + 1);
197
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) = '/';
202 nm->relstart++;
203 memcpy(nm->abslink + nm->relstart, sym, sz - nm->relstart);
204 } else
205 /* - absolute link (foo -> /path/bar)
206 * - or link without any subdir (foo -> bar)
207 */
208 memcpy(nm->abslink, sym, sz);
209
210 nm->abslink[sz] = '\0';
211 }
212
213 static struct stat *
214 dotdot_stat(const char *dirname, struct stat *st)
215 {
216 char *path;
217 size_t len;
218
219 #define DOTDOTDIR "/.."
220
221 if (!dirname)
222 return NULL;
223
224 len = strlen(dirname);
225 path = xmalloc(len + sizeof(DOTDOTDIR));
226
227 memcpy(path, dirname, len);
228 memcpy(path + len, DOTDOTDIR, sizeof(DOTDOTDIR));
229
230 if (stat(path, st))
231 err(EXIT_FAILURE, _("stat failed %s"), path);
232 free(path);
233 return st;
234 }
235
236 static struct namei *
237 new_namei(struct namei *parent, const char *path, const char *fname, int lev)
238 {
239 struct namei *nm;
240
241 if (!fname)
242 return NULL;
243 nm = xcalloc(1, sizeof(*nm));
244 if (parent)
245 parent->next = nm;
246
247 nm->level = lev;
248 nm->name = xstrdup(fname);
249
250 nm->noent = (lstat(path, &nm->st) == -1);
251 if (nm->noent)
252 return nm;
253
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);
259 }
260
261 if ((flags & NAMEI_MNTS) && S_ISDIR(nm->st.st_mode)) {
262 struct stat stbuf, *sb = NULL;
263
264 if (parent && S_ISDIR(parent->st.st_mode))
265 sb = &parent->st;
266 else if (!parent || S_ISLNK(parent->st.st_mode))
267 sb = dotdot_stat(path, &stbuf);
268
269 if (sb && (sb->st_dev != nm->st.st_dev || /* different device */
270 sb->st_ino == nm->st.st_ino)) /* root directory */
271 nm->mountpoint = 1;
272 }
273
274 return nm;
275 }
276
277 static struct namei *
278 add_namei(struct namei *parent, const char *orgpath, int start, struct namei **last)
279 {
280 struct namei *nm = NULL, *first = NULL;
281 char *fname, *end, *path;
282 int level = 0;
283
284 if (!orgpath)
285 return NULL;
286 if (parent) {
287 nm = parent;
288 level = parent->level + 1;
289 }
290 path = xstrdup(orgpath);
291 fname = path + start;
292
293 /* root directory */
294 if (*fname == '/') {
295 while (*fname == '/')
296 fname++; /* eat extra '/' */
297 first = nm = new_namei(nm, "/", "/", level);
298 }
299
300 for (end = fname; fname && end; ) {
301 /* set end of filename */
302 if (*fname) {
303 end = strchr(fname, '/');
304 if (end)
305 *end = '\0';
306
307 /* create a new entry */
308 nm = new_namei(nm, path, fname, level);
309 } else
310 end = NULL;
311 if (!first)
312 first = nm;
313 /* set begin of the next filename */
314 if (end) {
315 *end++ = '/';
316 while (*end == '/')
317 end++; /* eat extra '/' */
318 }
319 fname = end;
320 }
321
322 if (last)
323 *last = nm;
324
325 free(path);
326
327 return first;
328 }
329
330 static int
331 follow_symlinks(struct namei *nm)
332 {
333 int symcount = 0;
334
335 for (; nm; nm = nm->next) {
336 struct namei *next, *last;
337
338 if (nm->noent)
339 continue;
340 if (!S_ISLNK(nm->st.st_mode))
341 continue;
342 if (++symcount > MAXSYMLINKS) {
343 /* drop the rest of the list */
344 free_namei(nm->next);
345 nm->next = NULL;
346 return -1;
347 }
348 next = nm->next;
349 nm->next = add_namei(nm, nm->abslink, nm->relstart, &last);
350 if (last)
351 last->next = next;
352 else
353 nm->next = next;
354 }
355 return 0;
356 }
357
358 static int
359 print_namei(struct namei *nm, char *path)
360 {
361 int i;
362
363 if (path)
364 printf("f: %s\n", path);
365
366 for (; nm; nm = nm->next) {
367 char md[11];
368
369 if (nm->noent) {
370 printf(_("%s - No such file or directory\n"), nm->name);
371 return -1;
372 }
373
374 strmode(nm->st.st_mode, md);
375
376 if (nm->mountpoint)
377 md[0] = 'D';
378
379 if (!(flags & NAMEI_VERTICAL)) {
380 for (i = 0; i < nm->level; i++)
381 fputs(" ", stdout);
382 fputc(' ', stdout);
383 }
384
385 if (flags & NAMEI_MODES)
386 printf("%s", md);
387 else
388 printf("%c", md[0]);
389
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);
395 }
396
397 if (flags & NAMEI_VERTICAL)
398 for (i = 0; i < nm->level; i++)
399 fputs(" ", stdout);
400
401 if (S_ISLNK(nm->st.st_mode))
402 printf(" %s -> %s\n", nm->name,
403 nm->abslink + nm->relstart);
404 else
405 printf(" %s\n", nm->name);
406 }
407 return 0;
408 }
409
410 static void usage(int rc)
411 {
412 const char *p = program_invocation_short_name;
413 FILE *out = rc == EXIT_FAILURE ? stderr : stdout;
414
415 if (!*p)
416 p = "namei";
417
418 fputs(_("\nUsage:\n"), out);
419 fprintf(out,
420 _(" %s [options] pathname [pathname ...]\n"), p);
421
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);
431
432 fputs(_("\nFor more information see namei(1).\n"), out);
433 exit(rc);
434 }
435
436 static const struct option longopts[] =
437 {
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' },
446 { NULL, 0, 0, 0 },
447 };
448
449 int
450 main(int argc, char **argv)
451 {
452 int c;
453 int rc = EXIT_SUCCESS;
454
455 setlocale(LC_ALL, "");
456 bindtextdomain(PACKAGE, LOCALEDIR);
457 textdomain(PACKAGE);
458 atexit(close_stdout);
459
460 while ((c = getopt_long(argc, argv, "hVlmnovx", longopts, NULL)) != -1) {
461 switch(c) {
462 case 'h':
463 usage(EXIT_SUCCESS);
464 break;
465 case 'V':
466 printf(UTIL_LINUX_VERSION);
467 return EXIT_SUCCESS;
468 case 'l':
469 flags |= (NAMEI_OWNERS | NAMEI_MODES | NAMEI_VERTICAL);
470 break;
471 case 'm':
472 flags |= NAMEI_MODES;
473 break;
474 case 'n':
475 flags |= NAMEI_NOLINKS;
476 break;
477 case 'o':
478 flags |= NAMEI_OWNERS;
479 break;
480 case 'x':
481 flags |= NAMEI_MNTS;
482 break;
483 case 'v':
484 flags |= NAMEI_VERTICAL;
485 break;
486 default:
487 usage(EXIT_FAILURE);
488 }
489 }
490
491 if (optind == argc) {
492 warnx(_("pathname argument is missing"));
493 usage(EXIT_FAILURE);
494 }
495
496 for(; optind < argc; optind++) {
497 char *path = argv[optind];
498 struct namei *nm = NULL;
499 struct stat st;
500
501 if (stat(path, &st) != 0)
502 rc = EXIT_FAILURE;
503
504 nm = add_namei(NULL, path, 0, NULL);
505 if (nm) {
506 int sml = 0;
507 if (!(flags & NAMEI_NOLINKS))
508 sml = follow_symlinks(nm);
509 if (print_namei(nm, path)) {
510 rc = EXIT_FAILURE;
511 continue;
512 }
513 free_namei(nm);
514 if (sml == -1) {
515 rc = EXIT_FAILURE;
516 warnx(_("%s: exceeded limit of symlinks"), path);
517 continue;
518 }
519 }
520 }
521
522 free_idcache(ucache);
523 free_idcache(gcache);
524
525 return rc;
526 }
527