doc: clarify when dd iflag=fullblock is useful
[coreutils.git] / src / readlink.c
blobff7d67fa7a132b58201f14d6ec699d4d7353a5c7
1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002-2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Dmitry V. Levin */
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "canonicalize.h"
26 #include "error.h"
27 #include "areadlink.h"
29 /* The official name of this program (e.g., no 'g' prefix). */
30 #define PROGRAM_NAME "readlink"
32 #define AUTHORS proper_name ("Dmitry V. Levin")
34 /* If true, do not output the trailing newline. */
35 static bool no_newline;
37 /* If true, report error messages. */
38 static bool verbose;
40 static struct option const longopts[] =
42 {"canonicalize", no_argument, NULL, 'f'},
43 {"canonicalize-existing", no_argument, NULL, 'e'},
44 {"canonicalize-missing", no_argument, NULL, 'm'},
45 {"no-newline", no_argument, NULL, 'n'},
46 {"quiet", no_argument, NULL, 'q'},
47 {"silent", no_argument, NULL, 's'},
48 {"verbose", no_argument, NULL, 'v'},
49 {"zero", no_argument, NULL, 'z'},
50 {GETOPT_HELP_OPTION_DECL},
51 {GETOPT_VERSION_OPTION_DECL},
52 {NULL, 0, NULL, 0}
55 void
56 usage (int status)
58 if (status != EXIT_SUCCESS)
59 emit_try_help ();
60 else
62 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
63 fputs (_("Print value of a symbolic link or canonical file name\n\n"),
64 stdout);
65 fputs (_("\
66 -f, --canonicalize canonicalize by following every symlink in\n\
67 every component of the given name recursively;\
68 \n\
69 all but the last component must exist\n\
70 -e, --canonicalize-existing canonicalize by following every symlink in\n\
71 every component of the given name recursively,\
72 \n\
73 all components must exist\n\
74 "), stdout);
75 fputs (_("\
76 -m, --canonicalize-missing canonicalize by following every symlink in\n\
77 every component of the given name recursively,\
78 \n\
79 without requirements on components existence\n\
80 -n, --no-newline do not output the trailing delimiter\n\
81 -q, --quiet,\n\
82 -s, --silent suppress most error messages\n\
83 -v, --verbose report error messages\n\
84 -z, --zero separate output with NUL rather than newline\n\
85 "), stdout);
86 fputs (HELP_OPTION_DESCRIPTION, stdout);
87 fputs (VERSION_OPTION_DESCRIPTION, stdout);
88 emit_ancillary_info ();
90 exit (status);
93 int
94 main (int argc, char **argv)
96 /* If not -1, use this method to canonicalize. */
97 int can_mode = -1;
98 int status = EXIT_SUCCESS;
99 int optc;
100 bool use_nuls = false;
102 initialize_main (&argc, &argv);
103 set_program_name (argv[0]);
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 atexit (close_stdout);
110 while ((optc = getopt_long (argc, argv, "efmnqsvz", longopts, NULL)) != -1)
112 switch (optc)
114 case 'e':
115 can_mode = CAN_EXISTING;
116 break;
117 case 'f':
118 can_mode = CAN_ALL_BUT_LAST;
119 break;
120 case 'm':
121 can_mode = CAN_MISSING;
122 break;
123 case 'n':
124 no_newline = true;
125 break;
126 case 'q':
127 case 's':
128 verbose = false;
129 break;
130 case 'v':
131 verbose = true;
132 break;
133 case 'z':
134 use_nuls = true;
135 break;
136 case_GETOPT_HELP_CHAR;
137 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
138 default:
139 usage (EXIT_FAILURE);
143 if (optind >= argc)
145 error (0, 0, _("missing operand"));
146 usage (EXIT_FAILURE);
149 if (argc - optind > 1)
151 if (no_newline)
152 error (0, 0, _("ignoring --no-newline with multiple arguments"));
153 no_newline = false;
156 for (; optind < argc; ++optind)
158 const char *fname = argv[optind];
159 char *value = (can_mode != -1
160 ? canonicalize_filename_mode (fname, can_mode)
161 : areadlink_with_size (fname, 63));
162 if (value)
164 fputs (value, stdout);
165 if (! no_newline)
166 putchar (use_nuls ? '\0' : '\n');
167 free (value);
169 else
171 status = EXIT_FAILURE;
172 if (verbose)
173 error (0, errno, "%s", fname);
177 return status;