1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002, 2003 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)
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Dmitry V. Levin */
23 #include <sys/types.h>
26 #include "canonicalize.h"
28 #include "xreadlink.h"
29 #include "long-options.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "readlink"
35 #define WRITTEN_BY _("Written by Dmitry V. Levin.")
37 /* Name this program was run with. */
40 /* If nonzero, canonicalize file name. */
41 static int canonicalize;
42 /* If nonzero, do not output the trailing newline. */
43 static int no_newline;
44 /* If nonzero, report error messages. */
47 static struct option const longopts[] =
49 {"canonicalize", no_argument, 0, 'f'},
50 {"no-newline", no_argument, 0, 'n'},
51 {"quiet", no_argument, 0, 'q'},
52 {"silent", no_argument, 0, 's'},
53 {"verbose", no_argument, 0, 'v'},
54 {GETOPT_HELP_OPTION_DECL},
55 {GETOPT_VERSION_OPTION_DECL},
62 if (status != EXIT_SUCCESS)
63 fprintf (stderr, _("Try `%s --help' for more information.\n"),
67 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
68 fputs (_("Display value of a symbolic link on standard output.\n\n"),
71 -f, --canonicalize canonicalize by following every symlink in every\n\
72 component of the given path recursively\n\
73 -n, --no-newline do not output the trailing newline\n\
75 -s, --silent suppress most error messages\n\
76 -v, --verbose report error messages\n\
78 fputs (HELP_OPTION_DESCRIPTION, stdout);
79 fputs (VERSION_OPTION_DESCRIPTION, stdout);
80 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
86 main (int argc, char *const argv[])
92 initialize_main (&argc, &argv);
93 program_name = argv[0];
94 setlocale (LC_ALL, "");
95 bindtextdomain (PACKAGE, LOCALEDIR);
98 atexit (close_stdout);
100 while ((optc = getopt_long (argc, argv, "fnqsv", longopts, NULL)) != -1)
119 case_GETOPT_HELP_CHAR;
120 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
122 usage (EXIT_FAILURE);
128 error (0, 0, _("too few arguments"));
129 usage (EXIT_FAILURE);
132 fname = argv[optind++];
136 error (0, 0, _("too many arguments"));
137 usage (EXIT_FAILURE);
140 value = (canonicalize ? canonicalize_file_name : xreadlink) (fname);
143 printf ("%s%s", value, (no_newline ? "" : "\n"));
149 error (EXIT_FAILURE, errno, "%s", fname);