]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/namei.c
misc: never use usage(ERROR)
[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 written by:
17 * Roger S. Southwick (May 2, 1990)
18 * Steve Tell (March 28, 1991)
19 * Arkadiusz Miƛkiewicz (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 #include "idcache.h"
42
43 #ifndef MAXSYMLINKS
44 #define MAXSYMLINKS 256
45 #endif
46
47 #define NAMEI_NOLINKS (1 << 1)
48 #define NAMEI_MODES (1 << 2)
49 #define NAMEI_MNTS (1 << 3)
50 #define NAMEI_OWNERS (1 << 4)
51 #define NAMEI_VERTICAL (1 << 5)
52
53
54 struct namei {
55 struct stat st; /* item lstat() */
56 char *name; /* item name */
57 char *abslink; /* absolute symlink path */
58 int relstart; /* offset of relative path in 'abslink' */
59 struct namei *next; /* next item */
60 int level;
61 int mountpoint; /* is mount point */
62 int noent; /* is this item not existing */
63 };
64
65 static int flags;
66 static struct idcache *gcache; /* groupnames */
67 static struct idcache *ucache; /* usernames */
68
69 static void
70 free_namei(struct namei *nm)
71 {
72 while (nm) {
73 struct namei *next = nm->next;
74 free(nm->name);
75 free(nm->abslink);
76 free(nm);
77 nm = next;
78 }
79 }
80
81 static void
82 readlink_to_namei(struct namei *nm, const char *path)
83 {
84 char sym[PATH_MAX];
85 ssize_t sz;
86 int isrel = 0;
87
88 sz = readlink(path, sym, sizeof(sym));
89 if (sz < 1)
90 err(EXIT_FAILURE, _("failed to read symlink: %s"), path);
91 if (*sym != '/') {
92 char *p = strrchr(path, '/');
93
94 if (p) {
95 isrel = 1;
96 nm->relstart = p - path;
97 sz += nm->relstart + 1;
98 }
99 }
100 nm->abslink = xmalloc(sz + 1);
101
102 if (*sym != '/' && isrel) {
103 /* create the absolute path from the relative symlink */
104 memcpy(nm->abslink, path, nm->relstart);
105 *(nm->abslink + nm->relstart) = '/';
106 nm->relstart++;
107 memcpy(nm->abslink + nm->relstart, sym, sz - nm->relstart);
108 } else
109 /* - absolute link (foo -> /path/bar)
110 * - or link without any subdir (foo -> bar)
111 */
112 memcpy(nm->abslink, sym, sz);
113
114 nm->abslink[sz] = '\0';
115 }
116
117 static struct stat *
118 dotdot_stat(const char *dirname, struct stat *st)
119 {
120 char *path;
121 size_t len;
122
123 #define DOTDOTDIR "/.."
124
125 if (!dirname)
126 return NULL;
127
128 len = strlen(dirname);
129 path = xmalloc(len + sizeof(DOTDOTDIR));
130
131 memcpy(path, dirname, len);
132 memcpy(path + len, DOTDOTDIR, sizeof(DOTDOTDIR));
133
134 if (stat(path, st))
135 err(EXIT_FAILURE, _("stat of %s failed"), path);
136 free(path);
137 return st;
138 }
139
140 static struct namei *
141 new_namei(struct namei *parent, const char *path, const char *fname, int lev)
142 {
143 struct namei *nm;
144
145 if (!fname)
146 return NULL;
147 nm = xcalloc(1, sizeof(*nm));
148 if (parent)
149 parent->next = nm;
150
151 nm->level = lev;
152 nm->name = xstrdup(fname);
153
154 nm->noent = (lstat(path, &nm->st) == -1);
155 if (nm->noent)
156 return nm;
157
158 if (S_ISLNK(nm->st.st_mode))
159 readlink_to_namei(nm, path);
160 if (flags & NAMEI_OWNERS) {
161 add_uid(ucache, nm->st.st_uid);
162 add_gid(gcache, nm->st.st_gid);
163 }
164
165 if ((flags & NAMEI_MNTS) && S_ISDIR(nm->st.st_mode)) {
166 struct stat stbuf, *sb = NULL;
167
168 if (parent && S_ISDIR(parent->st.st_mode))
169 sb = &parent->st;
170 else if (!parent || S_ISLNK(parent->st.st_mode))
171 sb = dotdot_stat(path, &stbuf);
172
173 if (sb && (sb->st_dev != nm->st.st_dev || /* different device */
174 sb->st_ino == nm->st.st_ino)) /* root directory */
175 nm->mountpoint = 1;
176 }
177
178 return nm;
179 }
180
181 static struct namei *
182 add_namei(struct namei *parent, const char *orgpath, int start, struct namei **last)
183 {
184 struct namei *nm = NULL, *first = NULL;
185 char *fname, *end, *path;
186 int level = 0;
187
188 if (!orgpath)
189 return NULL;
190 if (parent) {
191 nm = parent;
192 level = parent->level + 1;
193 }
194 path = xstrdup(orgpath);
195 fname = path + start;
196
197 /* root directory */
198 if (*fname == '/') {
199 while (*fname == '/')
200 fname++; /* eat extra '/' */
201 first = nm = new_namei(nm, "/", "/", level);
202 }
203
204 for (end = fname; fname && end; ) {
205 /* set end of filename */
206 if (*fname) {
207 end = strchr(fname, '/');
208 if (end)
209 *end = '\0';
210
211 /* create a new entry */
212 nm = new_namei(nm, path, fname, level);
213 } else
214 end = NULL;
215 if (!first)
216 first = nm;
217 /* set begin of the next filename */
218 if (end) {
219 *end++ = '/';
220 while (*end == '/')
221 end++; /* eat extra '/' */
222 }
223 fname = end;
224 }
225
226 if (last)
227 *last = nm;
228
229 free(path);
230
231 return first;
232 }
233
234 static int
235 follow_symlinks(struct namei *nm)
236 {
237 int symcount = 0;
238
239 for (; nm; nm = nm->next) {
240 struct namei *next, *last;
241
242 if (nm->noent)
243 continue;
244 if (!S_ISLNK(nm->st.st_mode))
245 continue;
246 if (++symcount > MAXSYMLINKS) {
247 /* drop the rest of the list */
248 free_namei(nm->next);
249 nm->next = NULL;
250 return -1;
251 }
252 next = nm->next;
253 nm->next = add_namei(nm, nm->abslink, nm->relstart, &last);
254 if (last)
255 last->next = next;
256 else
257 nm->next = next;
258 }
259 return 0;
260 }
261
262 static int
263 print_namei(struct namei *nm, char *path)
264 {
265 int i;
266
267 if (path)
268 printf("f: %s\n", path);
269
270 for (; nm; nm = nm->next) {
271 char md[11];
272
273 if (nm->noent) {
274 int blanks = 1;
275 if (flags & NAMEI_MODES)
276 blanks += 9;
277 if (flags & NAMEI_OWNERS)
278 blanks += ucache->width + gcache->width + 2;
279 if (!(flags & NAMEI_VERTICAL))
280 blanks += 1;
281 blanks += nm->level * 2;
282 printf("%*s ", blanks, "");
283 printf(_("%s - No such file or directory\n"), nm->name);
284 return -1;
285 }
286
287 xstrmode(nm->st.st_mode, md);
288
289 if (nm->mountpoint)
290 md[0] = 'D';
291
292 if (!(flags & NAMEI_VERTICAL)) {
293 for (i = 0; i < nm->level; i++)
294 fputs(" ", stdout);
295 fputc(' ', stdout);
296 }
297
298 if (flags & NAMEI_MODES)
299 printf("%s", md);
300 else
301 printf("%c", md[0]);
302
303 if (flags & NAMEI_OWNERS) {
304 printf(" %-*s", ucache->width,
305 get_id(ucache, nm->st.st_uid)->name);
306 printf(" %-*s", gcache->width,
307 get_id(gcache, nm->st.st_gid)->name);
308 }
309
310 if (flags & NAMEI_VERTICAL)
311 for (i = 0; i < nm->level; i++)
312 fputs(" ", stdout);
313
314 if (S_ISLNK(nm->st.st_mode))
315 printf(" %s -> %s\n", nm->name,
316 nm->abslink + nm->relstart);
317 else
318 printf(" %s\n", nm->name);
319 }
320 return 0;
321 }
322
323 static void __attribute__((__noreturn__)) usage(void)
324 {
325 const char *p = program_invocation_short_name;
326 FILE *out = stdout;
327
328 if (!*p)
329 p = "namei";
330
331 fputs(USAGE_HEADER, out);
332 fprintf(out,
333 _(" %s [options] <pathname>...\n"), p);
334
335 fputs(USAGE_SEPARATOR, out);
336 fputs(_("Follow a pathname until a terminal point is found.\n"), out);
337
338 fputs(USAGE_OPTIONS, out);
339 fputs(_(" -h, --help displays this help text\n"
340 " -V, --version output version information and exit\n"
341 " -x, --mountpoints show mount point directories with a 'D'\n"
342 " -m, --modes show the mode bits of each file\n"
343 " -o, --owners show owner and group name of each file\n"
344 " -l, --long use a long listing format (-m -o -v) \n"
345 " -n, --nosymlinks don't follow symlinks\n"
346 " -v, --vertical vertical align of modes and owners\n"), out);
347
348 fprintf(out, USAGE_MAN_TAIL("namei(1)"));
349 exit(EXIT_SUCCESS);
350 }
351
352 static const struct option longopts[] =
353 {
354 { "help", no_argument, NULL, 'h' },
355 { "version", no_argument, NULL, 'V' },
356 { "mountpoints", no_argument, NULL, 'x' },
357 { "modes", no_argument, NULL, 'm' },
358 { "owners", no_argument, NULL, 'o' },
359 { "long", no_argument, NULL, 'l' },
360 { "nolinks", no_argument, NULL, 'n' },
361 { "vertical", no_argument, NULL, 'v' },
362 { NULL, 0, NULL, 0 },
363 };
364
365 int
366 main(int argc, char **argv)
367 {
368 int c;
369 int rc = EXIT_SUCCESS;
370
371 setlocale(LC_ALL, "");
372 bindtextdomain(PACKAGE, LOCALEDIR);
373 textdomain(PACKAGE);
374 atexit(close_stdout);
375
376 while ((c = getopt_long(argc, argv, "hVlmnovx", longopts, NULL)) != -1) {
377 switch(c) {
378 case 'h':
379 usage();
380 break;
381 case 'V':
382 printf(UTIL_LINUX_VERSION);
383 return EXIT_SUCCESS;
384 case 'l':
385 flags |= (NAMEI_OWNERS | NAMEI_MODES | NAMEI_VERTICAL);
386 break;
387 case 'm':
388 flags |= NAMEI_MODES;
389 break;
390 case 'n':
391 flags |= NAMEI_NOLINKS;
392 break;
393 case 'o':
394 flags |= NAMEI_OWNERS;
395 break;
396 case 'x':
397 flags |= NAMEI_MNTS;
398 break;
399 case 'v':
400 flags |= NAMEI_VERTICAL;
401 break;
402 default:
403 errtryhelp(EXIT_FAILURE);
404 }
405 }
406
407 if (optind == argc) {
408 warnx(_("pathname argument is missing"));
409 errtryhelp(EXIT_FAILURE);
410 }
411
412 ucache = new_idcache();
413 if (!ucache)
414 err(EXIT_FAILURE, _("failed to allocate UID cache"));
415 gcache = new_idcache();
416 if (!gcache)
417 err(EXIT_FAILURE, _("failed to allocate GID cache"));
418
419 for(; optind < argc; optind++) {
420 char *path = argv[optind];
421 struct namei *nm = NULL;
422 struct stat st;
423
424 if (stat(path, &st) != 0)
425 rc = EXIT_FAILURE;
426
427 nm = add_namei(NULL, path, 0, NULL);
428 if (nm) {
429 int sml = 0;
430 if (!(flags & NAMEI_NOLINKS))
431 sml = follow_symlinks(nm);
432 if (print_namei(nm, path)) {
433 rc = EXIT_FAILURE;
434 continue;
435 }
436 free_namei(nm);
437 if (sml == -1) {
438 rc = EXIT_FAILURE;
439 warnx(_("%s: exceeded limit of symlinks"), path);
440 continue;
441 }
442 }
443 }
444
445 free_idcache(ucache);
446 free_idcache(gcache);
447
448 return rc;
449 }
450