build: update gnulib submodule to latest (quoting change)
[coreutils/ericb.git] / src / readlink.c
blob5d8cfd9737e1d80f609d4110142bf989c45a29a3
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"
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 {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 newline\n\
81 -q, --quiet,\n\
82 -s, --silent suppress most error messages\n\
83 -v, --verbose report error messages\n\
84 "), stdout);
85 fputs (HELP_OPTION_DESCRIPTION, stdout);
86 fputs (VERSION_OPTION_DESCRIPTION, stdout);
87 emit_ancillary_info ();
89 exit (status);
92 int
93 main (int argc, char **argv)
95 /* If not -1, use this method to canonicalize. */
96 int can_mode = -1;
98 /* File name to canonicalize. */
99 const char *fname;
101 /* Result of canonicalize. */
102 char *value;
104 int optc;
106 initialize_main (&argc, &argv);
107 set_program_name (argv[0]);
108 setlocale (LC_ALL, "");
109 bindtextdomain (PACKAGE, LOCALEDIR);
110 textdomain (PACKAGE);
112 atexit (close_stdout);
114 while ((optc = getopt_long (argc, argv, "efmnqsv", longopts, NULL)) != -1)
116 switch (optc)
118 case 'e':
119 can_mode = CAN_EXISTING;
120 break;
121 case 'f':
122 can_mode = CAN_ALL_BUT_LAST;
123 break;
124 case 'm':
125 can_mode = CAN_MISSING;
126 break;
127 case 'n':
128 no_newline = true;
129 break;
130 case 'q':
131 case 's':
132 verbose = false;
133 break;
134 case 'v':
135 verbose = 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 fname = argv[optind++];
152 if (optind < argc)
154 error (0, 0, _("extra operand %s"), quote (argv[optind]));
155 usage (EXIT_FAILURE);
158 value = (can_mode != -1
159 ? canonicalize_filename_mode (fname, can_mode)
160 : areadlink_with_size (fname, 63));
161 if (value)
163 printf ("%s%s", value, (no_newline ? "" : "\n"));
164 free (value);
165 return EXIT_SUCCESS;
168 if (verbose)
169 error (EXIT_FAILURE, errno, "%s", fname);
171 return EXIT_FAILURE;