1
1
/* dirname -- strip suffix from file name
3
Copyright (C) 1990-1997, 1999-2002, 2004-2011 Free Software Foundation, Inc.
3
Copyright (C) 1990-2012 Free Software Foundation, Inc.
5
5
This program is free software: you can redistribute it and/or modify
6
6
it under the terms of the GNU General Public License as published by
23
23
#include <sys/types.h>
25
25
#include "system.h"
26
#include "long-options.h"
30
/* The official name of this program (e.g., no `g' prefix). */
28
/* The official name of this program (e.g., no 'g' prefix). */
31
29
#define PROGRAM_NAME "dirname"
34
32
proper_name ("David MacKenzie"), \
35
33
proper_name ("Jim Meyering")
35
static struct option const longopts[] =
37
{"zero", no_argument, NULL, 'z'},
38
{GETOPT_HELP_OPTION_DECL},
39
{GETOPT_VERSION_OPTION_DECL},
40
46
if (status != EXIT_SUCCESS)
41
fprintf (stderr, _("Try `%s --help' for more information.\n"),
51
Usage: %s [OPTION] NAME...\n\
49
program_name, program_name);
51
Output NAME with its last non-slash component and trailing slashes removed;\n\
52
if NAME contains no /'s, output `.' (meaning the current directory).\n\
55
Output each NAME with its last non-slash component and trailing slashes\n\
56
removed; if NAME contains no /'s, output '.' (meaning the current directory).\n\
60
-z, --zero separate output with NUL rather than newline\n\
55
62
fputs (HELP_OPTION_DESCRIPTION, stdout);
56
63
fputs (VERSION_OPTION_DESCRIPTION, stdout);
60
%s /usr/bin/ Output \"/usr\".\n\
61
%s stdio.h Output \".\".\n\
67
%s /usr/bin/ -> \"/usr\"\n\
68
%s dir1/str dir2/str -> \"dir1\" followed by \"dir2\"\n\
69
%s stdio.h -> \".\"\n\
63
program_name, program_name);
71
program_name, program_name, program_name);
64
72
emit_ancillary_info ();
82
91
atexit (close_stdout);
84
parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
85
usage, AUTHORS, (char const *) NULL);
86
if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
95
int c = getopt_long (argc, argv, "z", longopts, NULL);
106
case_GETOPT_HELP_CHAR;
107
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
110
usage (EXIT_FAILURE);
89
114
if (argc < optind + 1)
92
117
usage (EXIT_FAILURE);
95
if (optind + 1 < argc)
97
error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
101
result = argv[optind];
102
len = dir_len (result);
110
fwrite (result, 1, len, stdout);
120
for (; optind < argc; optind++)
122
result = argv[optind];
123
len = dir_len (result);
131
fwrite (result, 1, len, stdout);
132
putchar (use_nuls ? '\0' :'\n');
113
135
exit (EXIT_SUCCESS);