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 */
28 #include "canonicalize.h"
30 #include "xreadlink.h"
31 #include "long-options.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "readlink"
37 #define AUTHORS "Dmitry V. Levin"
39 /* Name this program was run with. */
42 /* If nonzero, canonicalize file name. */
43 static int canonicalize
;
44 /* If nonzero, do not output the trailing newline. */
45 static int no_newline
;
46 /* If nonzero, report error messages. */
49 static struct option
const longopts
[] =
51 {"canonicalize", no_argument
, 0, 'f'},
52 {"no-newline", no_argument
, 0, 'n'},
53 {"quiet", no_argument
, 0, 'q'},
54 {"silent", no_argument
, 0, 's'},
55 {"verbose", no_argument
, 0, 'v'},
56 {GETOPT_HELP_OPTION_DECL
},
57 {GETOPT_VERSION_OPTION_DECL
},
64 if (status
!= EXIT_SUCCESS
)
65 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
69 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
70 fputs (_("Display value of a symbolic link on standard output.\n\n"),
73 -f, --canonicalize canonicalize by following every symlink in every\n\
74 component of the given path recursively\n\
75 -n, --no-newline do not output the trailing newline\n\
77 -s, --silent suppress most error messages\n\
78 -v, --verbose report error messages\n\
80 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
81 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
82 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
88 main (int argc
, char *const argv
[])
94 setlocale (LC_ALL
, "");
95 bindtextdomain (PACKAGE
, LOCALEDIR
);
98 atexit (close_stdout
);
101 error (EXIT_FAILURE
, 0, _("too few arguments"));
103 program_name
= argv
[0];
105 while ((optc
= getopt_long (argc
, argv
, "fnqsv", longopts
, NULL
)) != -1)
124 case_GETOPT_HELP_CHAR
;
125 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
127 usage (EXIT_FAILURE
);
133 error (EXIT_SUCCESS
, 0, _("too few arguments"));
134 usage (EXIT_FAILURE
);
137 fname
= argv
[optind
++];
141 error (EXIT_SUCCESS
, 0, _("too many arguments"));
142 usage (EXIT_FAILURE
);
145 value
= (canonicalize
? canonicalize_file_name
: xreadlink
) (fname
);
148 printf ("%s%s", value
, (no_newline
? "" : "\n"));
154 error (EXIT_SUCCESS
, errno
, "%s", fname
);