1 /* readlink -- display value of a symbolic link.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Dmitry V. Levin */
23 #include <sys/types.h>
26 #include "canonicalize.h"
28 #include "xreadlink.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "readlink"
34 #define AUTHORS "Dmitry V. Levin"
36 /* Name this program was run with. */
39 /* If true, do not output the trailing newline. */
40 static bool no_newline
;
42 /* If true, report error messages. */
45 static struct option
const longopts
[] =
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
},
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\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\
79 -m, --canonicalize-missing canonicalize by following every symlink in\n\
80 every component of the given name recursively,\n\
81 without requirements on components existence\n\
82 -n, --no-newline do not output the trailing newline\n\
84 -s, --silent suppress most error messages\n\
85 -v, --verbose report error messages\n\
87 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
88 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
89 emit_bug_reporting_address ();
95 main (int argc
, char **argv
)
97 /* If not -1, use this method to canonicalize. */
100 /* File name to canonicalize. */
103 /* Result of canonicalize. */
108 initialize_main (&argc
, &argv
);
109 program_name
= argv
[0];
110 setlocale (LC_ALL
, "");
111 bindtextdomain (PACKAGE
, LOCALEDIR
);
112 textdomain (PACKAGE
);
114 atexit (close_stdout
);
116 while ((optc
= getopt_long (argc
, argv
, "efmnqsv", longopts
, NULL
)) != -1)
121 can_mode
= CAN_EXISTING
;
124 can_mode
= CAN_ALL_BUT_LAST
;
127 can_mode
= CAN_MISSING
;
139 case_GETOPT_HELP_CHAR
;
140 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
142 usage (EXIT_FAILURE
);
148 error (0, 0, _("missing operand"));
149 usage (EXIT_FAILURE
);
152 fname
= argv
[optind
++];
156 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
157 usage (EXIT_FAILURE
);
160 value
= (can_mode
!= -1
161 ? canonicalize_filename_mode (fname
, can_mode
)
162 : xreadlink (fname
));
165 printf ("%s%s", value
, (no_newline
? "" : "\n"));
171 error (EXIT_FAILURE
, errno
, "%s", fname
);