*** empty log message ***
[platform/upstream/coreutils.git] / src / readlink.c
1 /* readlink -- display value of a symbolic link.
2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Written by Dmitry V. Levin */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24
25 #include "system.h"
26 #include "canonicalize.h"
27 #include "error.h"
28 #include "xreadlink.h"
29 #include "quote.h"
30
31 /* The official name of this program (e.g., no `g' prefix).  */
32 #define PROGRAM_NAME "readlink"
33
34 #define AUTHORS "Dmitry V. Levin"
35
36 /* Name this program was run with.  */
37 char *program_name;
38
39 /* If true, do not output the trailing newline.  */
40 static bool no_newline;
41
42 /* If true, report error messages.  */
43 static bool verbose;
44
45 static struct option const longopts[] =
46 {
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}
57 };
58
59 void
60 usage (int status)
61 {
62   if (status != EXIT_SUCCESS)
63     fprintf (stderr, _("Try `%s --help' for more information.\n"),
64              program_name);
65   else
66     {
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   -m, --canonicalize-missing    canonicalize by following every symlink in\n\
78                                 every component of the given name recursively,\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       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
88     }
89   exit (status);
90 }
91
92 int
93 main (int argc, char **argv)
94 {
95   /* If not -1, use this method to canonicalize.  */
96   int can_mode = -1;
97
98   /* File name to canonicalize.  */
99   const char *fname;
100
101   /* Result of canonicalize.  */
102   char *value;
103
104   int optc;
105
106   initialize_main (&argc, &argv);
107   program_name = argv[0];
108   setlocale (LC_ALL, "");
109   bindtextdomain (PACKAGE, LOCALEDIR);
110   textdomain (PACKAGE);
111
112   atexit (close_stdout);
113
114   while ((optc = getopt_long (argc, argv, "efmnqsv", longopts, NULL)) != -1)
115     {
116       switch (optc)
117         {
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);
141         }
142     }
143
144   if (optind >= argc)
145     {
146       error (0, 0, _("missing operand"));
147       usage (EXIT_FAILURE);
148     }
149
150   fname = argv[optind++];
151
152   if (optind < argc)
153     {
154       error (0, 0, _("extra operand %s"), quote (argv[optind]));
155       usage (EXIT_FAILURE);
156     }
157
158   value = (can_mode != -1
159            ? canonicalize_filename_mode (fname, can_mode)
160            : xreadlink (fname, 1024));
161   if (value)
162     {
163       printf ("%s%s", value, (no_newline ? "" : "\n"));
164       free (value);
165       return EXIT_SUCCESS;
166     }
167
168   if (verbose)
169     error (EXIT_FAILURE, errno, "%s", fname);
170
171   return EXIT_FAILURE;
172 }