1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002-2010 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 */
22 #include <sys/types.h>
25 #include "canonicalize.h"
27 #include "areadlink.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. */
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
},
58 if (status
!= EXIT_SUCCESS
)
59 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
63 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
64 fputs (_("Print value of a symbolic link or canonical file name\n\n"),
67 -f, --canonicalize canonicalize by following every symlink in\n\
68 every component of the given name recursively;\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,\n\
72 all components must exist\n\
75 -m, --canonicalize-missing canonicalize by following every symlink in\n\
76 every component of the given name recursively,\n\
77 without requirements on components existence\n\
78 -n, --no-newline do not output the trailing newline\n\
80 -s, --silent suppress most error messages\n\
81 -v, --verbose report error messages\n\
83 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
84 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
85 emit_ancillary_info ();
91 main (int argc
, char **argv
)
93 /* If not -1, use this method to canonicalize. */
96 /* File name to canonicalize. */
99 /* Result of canonicalize. */
104 initialize_main (&argc
, &argv
);
105 set_program_name (argv
[0]);
106 setlocale (LC_ALL
, "");
107 bindtextdomain (PACKAGE
, LOCALEDIR
);
108 textdomain (PACKAGE
);
110 atexit (close_stdout
);
112 while ((optc
= getopt_long (argc
, argv
, "efmnqsv", longopts
, NULL
)) != -1)
117 can_mode
= CAN_EXISTING
;
120 can_mode
= CAN_ALL_BUT_LAST
;
123 can_mode
= CAN_MISSING
;
135 case_GETOPT_HELP_CHAR
;
136 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
138 usage (EXIT_FAILURE
);
144 error (0, 0, _("missing operand"));
145 usage (EXIT_FAILURE
);
148 fname
= argv
[optind
++];
152 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
153 usage (EXIT_FAILURE
);
156 value
= (can_mode
!= -1
157 ? canonicalize_filename_mode (fname
, can_mode
)
158 : areadlink_with_size (fname
, 63));
161 printf ("%s%s", value
, (no_newline
? "" : "\n"));
167 error (EXIT_FAILURE
, errno
, "%s", fname
);