all: replace most uses of quotearg_colon() with quote()
[coreutils.git] / src / readlink.c
blob2b61fda92fa998de435991767b70e082376a953e
1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002-2015 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"
28 #include "quote.h"
30 /* The official name of this program (e.g., no 'g' prefix). */
31 #define PROGRAM_NAME "readlink"
33 #define AUTHORS proper_name ("Dmitry V. Levin")
35 /* If true, do not output the trailing newline. */
36 static bool no_newline;
38 /* If true, report error messages. */
39 static bool verbose;
41 static struct option const longopts[] =
43 {"canonicalize", no_argument, NULL, 'f'},
44 {"canonicalize-existing", no_argument, NULL, 'e'},
45 {"canonicalize-missing", no_argument, NULL, 'm'},
46 {"no-newline", no_argument, NULL, 'n'},
47 {"quiet", no_argument, NULL, 'q'},
48 {"silent", no_argument, NULL, 's'},
49 {"verbose", no_argument, NULL, 'v'},
50 {"zero", no_argument, NULL, 'z'},
51 {GETOPT_HELP_OPTION_DECL},
52 {GETOPT_VERSION_OPTION_DECL},
53 {NULL, 0, NULL, 0}
56 void
57 usage (int status)
59 if (status != EXIT_SUCCESS)
60 emit_try_help ();
61 else
63 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
64 fputs (_("Print value of a symbolic link or canonical file name\n\n"),
65 stdout);
66 fputs (_("\
67 -f, --canonicalize canonicalize by following every symlink in\n\
68 every component of the given name recursively;\
69 \n\
70 all but the last component must exist\n\
71 -e, --canonicalize-existing canonicalize by following every symlink in\n\
72 every component of the given name recursively,\
73 \n\
74 all components must exist\n\
75 "), stdout);
76 fputs (_("\
77 -m, --canonicalize-missing canonicalize by following every symlink in\n\
78 every component of the given name recursively,\
79 \n\
80 without requirements on components existence\n\
81 -n, --no-newline do not output the trailing delimiter\n\
82 -q, --quiet,\n\
83 -s, --silent suppress most error messages\n\
84 -v, --verbose report error messages\n\
85 -z, --zero end each output line with NUL, not newline\n\
86 "), stdout);
87 fputs (HELP_OPTION_DESCRIPTION, stdout);
88 fputs (VERSION_OPTION_DESCRIPTION, stdout);
89 emit_ancillary_info (PROGRAM_NAME);
91 exit (status);
94 int
95 main (int argc, char **argv)
97 /* If not -1, use this method to canonicalize. */
98 int can_mode = -1;
99 int status = EXIT_SUCCESS;
100 int optc;
101 bool use_nuls = false;
103 initialize_main (&argc, &argv);
104 set_program_name (argv[0]);
105 setlocale (LC_ALL, "");
106 bindtextdomain (PACKAGE, LOCALEDIR);
107 textdomain (PACKAGE);
109 atexit (close_stdout);
111 while ((optc = getopt_long (argc, argv, "efmnqsvz", longopts, NULL)) != -1)
113 switch (optc)
115 case 'e':
116 can_mode = CAN_EXISTING;
117 break;
118 case 'f':
119 can_mode = CAN_ALL_BUT_LAST;
120 break;
121 case 'm':
122 can_mode = CAN_MISSING;
123 break;
124 case 'n':
125 no_newline = true;
126 break;
127 case 'q':
128 case 's':
129 verbose = false;
130 break;
131 case 'v':
132 verbose = true;
133 break;
134 case 'z':
135 use_nuls = true;
136 break;
137 case_GETOPT_HELP_CHAR;
138 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
139 default:
140 usage (EXIT_FAILURE);
144 if (optind >= argc)
146 error (0, 0, _("missing operand"));
147 usage (EXIT_FAILURE);
150 if (argc - optind > 1)
152 if (no_newline)
153 error (0, 0, _("ignoring --no-newline with multiple arguments"));
154 no_newline = false;
157 for (; optind < argc; ++optind)
159 const char *fname = argv[optind];
160 char *value = (can_mode != -1
161 ? canonicalize_filename_mode (fname, can_mode)
162 : areadlink_with_size (fname, 63));
163 if (value)
165 fputs (value, stdout);
166 if (! no_newline)
167 putchar (use_nuls ? '\0' : '\n');
168 free (value);
170 else
172 status = EXIT_FAILURE;
173 if (verbose)
174 error (0, errno, "%s", quote (fname));
178 return status;