Check for an up-to-date copyright year in coreutils.texi.
[coreutils/ericb.git] / src / readlink.c
blob0961d235c2f08c53bbf7c4763cac5806db95c3ac
1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Dmitry V. Levin */
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "canonicalize.h"
27 #include "error.h"
28 #include "xreadlink.h"
29 #include "quote.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "readlink"
34 #define AUTHORS "Dmitry V. Levin"
36 /* Name this program was run with. */
37 char *program_name;
39 /* If true, do not output the trailing newline. */
40 static bool no_newline;
42 /* If true, report error messages. */
43 static bool verbose;
45 static struct option const longopts[] =
47 {"canonicalize", no_argument, NULL, 'f'},
48 {"canonicalize-existing", no_argument, NULL, 'e'},
49 {"canonicalize-missing", no_argument, NULL, 'm'},
50 {"no-newline", no_argument, NULL, 'n'},
51 {"quiet", no_argument, NULL, 'q'},
52 {"silent", no_argument, NULL, 's'},
53 {"verbose", no_argument, NULL, 'v'},
54 {GETOPT_HELP_OPTION_DECL},
55 {GETOPT_VERSION_OPTION_DECL},
56 {NULL, 0, NULL, 0}
59 void
60 usage (int status)
62 if (status != EXIT_SUCCESS)
63 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 program_name);
65 else
67 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
68 fputs (_("Display value of a symbolic link on standard output.\n\n"),
69 stdout);
70 fputs (_("\
71 -f, --canonicalize canonicalize by following every symlink in\n\
72 every component of the given name recursively;\n\
73 all but the last component must exist\n\
74 -e, --canonicalize-existing canonicalize by following every symlink in\n\
75 every component of the given name recursively,\n\
76 all components must exist\n\
77 "), stdout);
78 fputs (_("\
79 -m, --canonicalize-missing canonicalize by following every symlink in\n\
80 every component of the given name recursively,\n\
81 without requirements on components existence\n\
82 -n, --no-newline do not output the trailing newline\n\
83 -q, --quiet,\n\
84 -s, --silent suppress most error messages\n\
85 -v, --verbose report error messages\n\
86 "), stdout);
87 fputs (HELP_OPTION_DESCRIPTION, stdout);
88 fputs (VERSION_OPTION_DESCRIPTION, stdout);
89 emit_bug_reporting_address ();
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;
100 /* File name to canonicalize. */
101 const char *fname;
103 /* Result of canonicalize. */
104 char *value;
106 int optc;
108 initialize_main (&argc, &argv);
109 program_name = argv[0];
110 setlocale (LC_ALL, "");
111 bindtextdomain (PACKAGE, LOCALEDIR);
112 textdomain (PACKAGE);
114 atexit (close_stdout);
116 while ((optc = getopt_long (argc, argv, "efmnqsv", longopts, NULL)) != -1)
118 switch (optc)
120 case 'e':
121 can_mode = CAN_EXISTING;
122 break;
123 case 'f':
124 can_mode = CAN_ALL_BUT_LAST;
125 break;
126 case 'm':
127 can_mode = CAN_MISSING;
128 break;
129 case 'n':
130 no_newline = true;
131 break;
132 case 'q':
133 case 's':
134 verbose = false;
135 break;
136 case 'v':
137 verbose = true;
138 break;
139 case_GETOPT_HELP_CHAR;
140 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
141 default:
142 usage (EXIT_FAILURE);
146 if (optind >= argc)
148 error (0, 0, _("missing operand"));
149 usage (EXIT_FAILURE);
152 fname = argv[optind++];
154 if (optind < argc)
156 error (0, 0, _("extra operand %s"), quote (argv[optind]));
157 usage (EXIT_FAILURE);
160 value = (can_mode != -1
161 ? canonicalize_filename_mode (fname, can_mode)
162 : xreadlink (fname));
163 if (value)
165 printf ("%s%s", value, (no_newline ? "" : "\n"));
166 free (value);
167 return EXIT_SUCCESS;
170 if (verbose)
171 error (EXIT_FAILURE, errno, "%s", fname);
173 return EXIT_FAILURE;