~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to misc-utils/namei.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <sys/types.h>
31
31
#include <sys/stat.h>
32
32
#include <sys/param.h>
33
 
#include <err.h>
34
33
#include <pwd.h>
35
34
#include <grp.h>
36
35
 
106
105
        struct idcache *nc, *x;
107
106
        int w = 0;
108
107
 
109
 
        nc = calloc(1, sizeof(*nc));
110
 
        if (!nc)
111
 
                goto alloc_err;
 
108
        nc = xcalloc(1, sizeof(*nc));
112
109
        nc->id = id;
113
110
 
114
111
        if (name) {
125
122
        }
126
123
        /* note, we ignore names with non-printable widechars */
127
124
        if (w > 0)
128
 
                nc->name = strdup(name);
 
125
                nc->name = xstrdup(name);
129
126
        else if (asprintf(&nc->name, "%lu", id) == -1)
130
127
                nc->name = NULL;
131
 
        if (!nc->name)
132
 
                goto alloc_err;
133
128
 
134
129
        for (x = *ic; x && x->next; x = x->next);
135
130
 
143
138
        *width = *width < w ? w : *width;
144
139
 
145
140
        return;
146
 
alloc_err:
147
 
        err(EXIT_FAILURE, _("out of memory?"));
148
141
}
149
142
 
150
143
static void
222
215
                return NULL;
223
216
 
224
217
        len = strlen(dirname);
225
 
        path = malloc(len + sizeof(DOTDOTDIR));
226
 
        if (!path)
227
 
                err(EXIT_FAILURE, _("out of memory?"));
 
218
        path = xmalloc(len + sizeof(DOTDOTDIR));
228
219
 
229
220
        memcpy(path, dirname, len);
230
221
        memcpy(path + len, DOTDOTDIR, sizeof(DOTDOTDIR));
242
233
 
243
234
        if (!fname)
244
235
                return NULL;
245
 
        nm = calloc(1, sizeof(*nm));
246
 
        if (!nm)
247
 
                err(EXIT_FAILURE, _("out of memory?"));
 
236
        nm = xcalloc(1, sizeof(*nm));
248
237
        if (parent)
249
238
                parent->next = nm;
250
239
 
251
240
        nm->level = lev;
252
 
        nm->name = strdup(fname);
253
 
        if (!nm->name)
254
 
                err(EXIT_FAILURE, _("out of memory?"));
 
241
        nm->name = xstrdup(fname);
255
242
 
256
243
        nm->noent = (lstat(path, &nm->st) == -1);
257
244
        if (nm->noent)
293
280
                nm = parent;
294
281
                level = parent->level + 1;
295
282
        }
296
 
        path = strdup(orgpath);
297
 
        if (!path)
298
 
                err(EXIT_FAILURE, _("out of memory?"));
 
283
        path = xstrdup(orgpath);
299
284
        fname = path + start;
300
285
 
301
286
        /* root directory */
366
351
static int
367
352
print_namei(struct namei *nm, char *path)
368
353
{
369
 
        struct namei *prev = NULL;
370
354
        int i;
371
355
 
372
356
        if (path)
373
357
                printf("f: %s\n", path);
374
358
 
375
 
        for (; nm; prev = nm, nm = nm->next) {
 
359
        for (; nm; nm = nm->next) {
376
360
                char md[11];
377
361
 
378
362
                if (nm->noent) {
416
400
        return 0;
417
401
}
418
402
 
419
 
static void
420
 
usage(int rc)
 
403
static void usage(int rc)
421
404
{
422
405
        const char *p = program_invocation_short_name;
 
406
        FILE *out = rc == EXIT_FAILURE ? stderr : stdout;
423
407
 
424
408
        if (!*p)
425
409
                p = "namei";
426
410
 
427
 
        printf(_("\nUsage: %s [options] pathname [pathname ...]\n"), p);
428
 
        printf(_("\nOptions:\n"));
429
 
 
430
 
        printf(_(
431
 
        " -h, --help          displays this help text\n"
432
 
        " -x, --mountpoints   show mount point directories with a 'D'\n"
433
 
        " -m, --modes         show the mode bits of each file\n"
434
 
        " -o, --owners        show owner and group name of each file\n"
435
 
        " -l, --long          use a long listing format (-m -o -v) \n"
436
 
        " -n, --nosymlinks    don't follow symlinks\n"
437
 
        " -v, --vertical      vertical align of modes and owners\n"));
438
 
 
439
 
        printf(_("\nFor more information see namei(1).\n"));
 
411
        fputs(_("\nUsage:\n"), out);
 
412
        fprintf(out,
 
413
              _(" %s [options] pathname [pathname ...]\n"), p);
 
414
 
 
415
        fputs(_("\nOptions:\n"), out);
 
416
        fputs(_(" -h, --help          displays this help text\n"
 
417
                " -V, --version       output version information and exit\n"
 
418
                " -x, --mountpoints   show mount point directories with a 'D'\n"
 
419
                " -m, --modes         show the mode bits of each file\n"
 
420
                " -o, --owners        show owner and group name of each file\n"
 
421
                " -l, --long          use a long listing format (-m -o -v) \n"
 
422
                " -n, --nosymlinks    don't follow symlinks\n"
 
423
                " -v, --vertical      vertical align of modes and owners\n"), out);
 
424
 
 
425
        fputs(_("\nFor more information see namei(1).\n"), out);
440
426
        exit(rc);
441
427
}
442
428
 
443
 
struct option longopts[] =
 
429
static const struct option longopts[] =
444
430
{
445
431
        { "help",       0, 0, 'h' },
 
432
        { "version",    0, 0, 'V' },
446
433
        { "mountpoints",0, 0, 'x' },
447
434
        { "modes",      0, 0, 'm' },
448
435
        { "owners",     0, 0, 'o' },
455
442
int
456
443
main(int argc, char **argv)
457
444
{
458
 
        extern int optind;
459
445
        int c;
460
446
        int rc = EXIT_SUCCESS;
461
447
 
463
449
        bindtextdomain(PACKAGE, LOCALEDIR);
464
450
        textdomain(PACKAGE);
465
451
 
466
 
        if (argc < 2)
467
 
                usage(EXIT_FAILURE);
468
 
 
469
 
        while ((c = getopt_long(argc, argv, "+h?lmnovx", longopts, NULL)) != -1) {
 
452
        while ((c = getopt_long(argc, argv, "hVlmnovx", longopts, NULL)) != -1) {
470
453
                switch(c) {
471
454
                case 'h':
472
 
                case '?':
473
455
                        usage(EXIT_SUCCESS);
474
456
                        break;
 
457
                case 'V':
 
458
                        printf(_("%s from %s\n"), program_invocation_short_name,
 
459
                                                  PACKAGE_STRING);
 
460
                        return EXIT_SUCCESS;
475
461
                case 'l':
476
462
                        flags |= (NAMEI_OWNERS | NAMEI_MODES | NAMEI_VERTICAL);
477
463
                        break;
489
475
                        break;
490
476
                case 'v':
491
477
                        flags |= NAMEI_VERTICAL;
 
478
                        break;
 
479
                default:
 
480
                        usage(EXIT_FAILURE);
492
481
                }
493
482
        }
494
483
 
 
484
        if (optind == argc) {
 
485
                warnx(_("pathname argument is missing"));
 
486
                usage(EXIT_FAILURE);
 
487
        }
 
488
 
495
489
        for(; optind < argc; optind++) {
496
490
                char *path = argv[optind];
497
491
                struct namei *nm = NULL;