shred: enable direct I/O when possible
[coreutils.git] / src / dirname.c
blob94eec9de1c2d051545fafa6d7fa00677f00d0a35
1 /* dirname -- strip suffix from file name
3 Copyright (C) 1990-2013 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by David MacKenzie and Jim Meyering. */
20 #include <config.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "error.h"
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "dirname"
31 #define AUTHORS \
32 proper_name ("David MacKenzie"), \
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 {NULL, 0, NULL, 0}
43 void
44 usage (int status)
46 if (status != EXIT_SUCCESS)
47 emit_try_help ();
48 else
50 printf (_("\
51 Usage: %s [OPTION] NAME...\n\
52 "),
53 program_name);
54 fputs (_("\
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\
57 \n\
58 "), stdout);
59 fputs (_("\
60 -z, --zero separate output with NUL rather than newline\n\
61 "), stdout);
62 fputs (HELP_OPTION_DESCRIPTION, stdout);
63 fputs (VERSION_OPTION_DESCRIPTION, stdout);
64 printf (_("\
65 \n\
66 Examples:\n\
67 %s /usr/bin/ -> \"/usr\"\n\
68 %s dir1/str dir2/str -> \"dir1\" followed by \"dir2\"\n\
69 %s stdio.h -> \".\"\n\
70 "),
71 program_name, program_name, program_name);
72 emit_ancillary_info ();
74 exit (status);
77 int
78 main (int argc, char **argv)
80 static char const dot = '.';
81 bool use_nuls = false;
82 char const *result;
83 size_t len;
85 initialize_main (&argc, &argv);
86 set_program_name (argv[0]);
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEDIR);
89 textdomain (PACKAGE);
91 atexit (close_stdout);
93 while (true)
95 int c = getopt_long (argc, argv, "z", longopts, NULL);
97 if (c == -1)
98 break;
100 switch (c)
102 case 'z':
103 use_nuls = true;
104 break;
106 case_GETOPT_HELP_CHAR;
107 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
109 default:
110 usage (EXIT_FAILURE);
114 if (argc < optind + 1)
116 error (0, 0, _("missing operand"));
117 usage (EXIT_FAILURE);
120 for (; optind < argc; optind++)
122 result = argv[optind];
123 len = dir_len (result);
125 if (! len)
127 result = &dot;
128 len = 1;
131 fwrite (result, 1, len, stdout);
132 putchar (use_nuls ? '\0' :'\n');
135 exit (EXIT_SUCCESS);