1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002-2024 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"
26 #include "areadlink.h"
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "readlink"
31 #define AUTHORS proper_name ("Dmitry V. Levin")
33 /* If true, do not output the trailing newline. */
34 static bool no_newline
;
36 /* If true, report error messages. */
39 static struct option
const longopts
[] =
41 {"canonicalize", no_argument
, nullptr, 'f'},
42 {"canonicalize-existing", no_argument
, nullptr, 'e'},
43 {"canonicalize-missing", no_argument
, nullptr, 'm'},
44 {"no-newline", no_argument
, nullptr, 'n'},
45 {"quiet", no_argument
, nullptr, 'q'},
46 {"silent", no_argument
, nullptr, 's'},
47 {"verbose", no_argument
, nullptr, 'v'},
48 {"zero", no_argument
, nullptr, 'z'},
49 {GETOPT_HELP_OPTION_DECL
},
50 {GETOPT_VERSION_OPTION_DECL
},
51 {nullptr, 0, nullptr, 0}
57 if (status
!= EXIT_SUCCESS
)
61 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
62 fputs (_("Print value of a symbolic link or canonical file name\n\n"),
65 -f, --canonicalize canonicalize by following every symlink in\n\
66 every component of the given name recursively;\
68 all but the last component must exist\n\
69 -e, --canonicalize-existing canonicalize by following every symlink in\n\
70 every component of the given name recursively,\
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,\
78 without requirements on components existence\n\
79 -n, --no-newline do not output the trailing delimiter\n\
81 -s, --silent suppress most error messages (on by default)\n\
82 -v, --verbose report error messages\n\
83 -z, --zero end each output line with NUL, not newline\n\
85 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
86 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
87 emit_ancillary_info (PROGRAM_NAME
);
93 main (int argc
, char **argv
)
95 /* If not -1, use this method to canonicalize. */
97 int status
= EXIT_SUCCESS
;
99 bool use_nuls
= false;
101 initialize_main (&argc
, &argv
);
102 set_program_name (argv
[0]);
103 setlocale (LC_ALL
, "");
104 bindtextdomain (PACKAGE
, LOCALEDIR
);
105 textdomain (PACKAGE
);
107 atexit (close_stdout
);
109 while ((optc
= getopt_long (argc
, argv
, "efmnqsvz", longopts
, nullptr)) != -1)
114 can_mode
= CAN_EXISTING
;
117 can_mode
= CAN_ALL_BUT_LAST
;
120 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 if (argc
- optind
> 1)
151 error (0, 0, _("ignoring --no-newline with multiple arguments"));
155 for (; optind
< argc
; ++optind
)
157 char const *fname
= argv
[optind
];
158 char *value
= (can_mode
!= -1
159 ? canonicalize_filename_mode (fname
, can_mode
)
160 : areadlink_with_size (fname
, 63));
163 fputs (value
, stdout
);
165 putchar (use_nuls
? '\0' : '\n');
170 status
= EXIT_FAILURE
;
172 error (0, errno
, "%s", quotef (fname
));