maint: avoid new syntax-check failure
[coreutils/ericb.git] / src / readlink.c
blob5beebb604b395c0d9eae289e2297c05bf9b4e364
1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002-2011 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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 program_name);
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 newline\n\
82 -q, --quiet,\n\
83 -s, --silent suppress most error messages\n\
84 -v, --verbose report error messages\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;
99 /* File name to canonicalize. */
100 const char *fname;
102 /* Result of canonicalize. */
103 char *value;
105 int optc;
107 initialize_main (&argc, &argv);
108 set_program_name (argv[0]);
109 setlocale (LC_ALL, "");
110 bindtextdomain (PACKAGE, LOCALEDIR);
111 textdomain (PACKAGE);
113 atexit (close_stdout);
115 while ((optc = getopt_long (argc, argv, "efmnqsv", longopts, NULL)) != -1)
117 switch (optc)
119 case 'e':
120 can_mode = CAN_EXISTING;
121 break;
122 case 'f':
123 can_mode = CAN_ALL_BUT_LAST;
124 break;
125 case 'm':
126 can_mode = CAN_MISSING;
127 break;
128 case 'n':
129 no_newline = true;
130 break;
131 case 'q':
132 case 's':
133 verbose = false;
134 break;
135 case 'v':
136 verbose = true;
137 break;
138 case_GETOPT_HELP_CHAR;
139 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
140 default:
141 usage (EXIT_FAILURE);
145 if (optind >= argc)
147 error (0, 0, _("missing operand"));
148 usage (EXIT_FAILURE);
151 fname = argv[optind++];
153 if (optind < argc)
155 error (0, 0, _("extra operand %s"), quote (argv[optind]));
156 usage (EXIT_FAILURE);
159 value = (can_mode != -1
160 ? canonicalize_filename_mode (fname, can_mode)
161 : areadlink_with_size (fname, 63));
162 if (value)
164 printf ("%s%s", value, (no_newline ? "" : "\n"));
165 free (value);
166 return EXIT_SUCCESS;
169 if (verbose)
170 error (EXIT_FAILURE, errno, "%s", fname);
172 return EXIT_FAILURE;