1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002-2022 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Dmitry V. Levin */
22 #include <sys/types.h>
25 #include "canonicalize.h"
27 #include "areadlink.h"
29 /* The official name of this program (e.g., no 'g' prefix). */
30 #define PROGRAM_NAME "readlink"
32 #define AUTHORS proper_name ("Dmitry V. Levin")
34 /* If true, do not output the trailing newline. */
35 static bool no_newline
;
37 /* If true, report error messages. */
40 static struct option
const longopts
[] =
42 {"canonicalize", no_argument
, NULL
, 'f'},
43 {"canonicalize-existing", no_argument
, NULL
, 'e'},
44 {"canonicalize-missing", no_argument
, NULL
, 'm'},
45 {"no-newline", no_argument
, NULL
, 'n'},
46 {"quiet", no_argument
, NULL
, 'q'},
47 {"silent", no_argument
, NULL
, 's'},
48 {"verbose", no_argument
, NULL
, 'v'},
49 {"zero", no_argument
, NULL
, 'z'},
50 {GETOPT_HELP_OPTION_DECL
},
51 {GETOPT_VERSION_OPTION_DECL
},
58 if (status
!= EXIT_SUCCESS
)
62 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
63 fputs (_("Print value of a symbolic link or canonical file name\n\n"),
66 -f, --canonicalize canonicalize by following every symlink in\n\
67 every component of the given name recursively;\
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,\
73 all components must exist\n\
76 -m, --canonicalize-missing canonicalize by following every symlink in\n\
77 every component of the given name recursively,\
79 without requirements on components existence\n\
80 -n, --no-newline do not output the trailing delimiter\n\
82 -s, --silent suppress most error messages (on by default)\n\
83 -v, --verbose report error messages\n\
84 -z, --zero end each output line with NUL, not newline\n\
86 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
87 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
88 emit_ancillary_info (PROGRAM_NAME
);
94 main (int argc
, char **argv
)
96 /* If not -1, use this method to canonicalize. */
98 int status
= EXIT_SUCCESS
;
100 bool use_nuls
= false;
102 initialize_main (&argc
, &argv
);
103 set_program_name (argv
[0]);
104 setlocale (LC_ALL
, "");
105 bindtextdomain (PACKAGE
, LOCALEDIR
);
106 textdomain (PACKAGE
);
108 atexit (close_stdout
);
110 while ((optc
= getopt_long (argc
, argv
, "efmnqsvz", longopts
, NULL
)) != -1)
115 can_mode
= CAN_EXISTING
;
118 can_mode
= CAN_ALL_BUT_LAST
;
121 can_mode
= CAN_MISSING
;
136 case_GETOPT_HELP_CHAR
;
137 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
139 usage (EXIT_FAILURE
);
145 error (0, 0, _("missing operand"));
146 usage (EXIT_FAILURE
);
149 if (argc
- optind
> 1)
152 error (0, 0, _("ignoring --no-newline with multiple arguments"));
156 for (; optind
< argc
; ++optind
)
158 char const *fname
= argv
[optind
];
159 char *value
= (can_mode
!= -1
160 ? canonicalize_filename_mode (fname
, can_mode
)
161 : areadlink_with_size (fname
, 63));
164 fputs (value
, stdout
);
166 putchar (use_nuls
? '\0' : '\n');
171 status
= EXIT_FAILURE
;
173 error (0, errno
, "%s", quotef (fname
));