Remove declaration of statfs.
[coreutils.git] / src / readlink.c
blob96e5a0aeaefdc52ddacdc0d323a89b61fce27724
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)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Dmitry V. Levin */
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <getopt.h>
27 #include "system.h"
28 #include "canonicalize.h"
29 #include "error.h"
30 #include "xreadlink.h"
31 #include "long-options.h"
32 #include "quote.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. */
40 char *program_name;
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. */
47 static int verbose;
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},
58 {NULL, 0, NULL, 0}
61 void
62 usage (int status)
64 if (status != EXIT_SUCCESS)
65 fprintf (stderr, _("Try `%s --help' for more information.\n"),
66 program_name);
67 else
69 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
70 fputs (_("Display value of a symbolic link on standard output.\n\n"),
71 stdout);
72 fputs (_("\
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\
76 -q, --quiet,\n\
77 -s, --silent suppress most error messages\n\
78 -v, --verbose report error messages\n\
79 "), stdout);
80 fputs (HELP_OPTION_DESCRIPTION, stdout);
81 fputs (VERSION_OPTION_DESCRIPTION, stdout);
82 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
84 exit (status);
87 int
88 main (int argc, char *const argv[])
90 const char *fname;
91 char *value;
92 int optc;
94 setlocale (LC_ALL, "");
95 bindtextdomain (PACKAGE, LOCALEDIR);
96 textdomain (PACKAGE);
98 atexit (close_stdout);
100 if (argc < 1)
101 error (EXIT_FAILURE, 0, _("too few arguments"));
103 program_name = argv[0];
105 while ((optc = getopt_long (argc, argv, "fnqsv", longopts, NULL)) != -1)
107 switch (optc)
109 case 0:
110 break;
111 case 'f':
112 canonicalize = 1;
113 break;
114 case 'n':
115 no_newline = 1;
116 break;
117 case 'q':
118 case 's':
119 verbose = 0;
120 break;
121 case 'v':
122 verbose = 1;
123 break;
124 case_GETOPT_HELP_CHAR;
125 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
126 default:
127 usage (EXIT_FAILURE);
131 if (optind >= argc)
133 error (EXIT_SUCCESS, 0, _("too few arguments"));
134 usage (EXIT_FAILURE);
137 fname = argv[optind++];
139 if (optind < argc)
141 error (EXIT_SUCCESS, 0, _("too many arguments"));
142 usage (EXIT_FAILURE);
145 value = (canonicalize ? canonicalize_file_name : xreadlink) (fname);
146 if (value)
148 printf ("%s%s", value, (no_newline ? "" : "\n"));
149 free (value);
150 return EXIT_SUCCESS;
153 if (verbose)
154 error (EXIT_SUCCESS, errno, "%s", fname);
156 return EXIT_FAILURE;