277f0f68ddb860d058fc24ef8cf24b67cc340ff8
[util-vserver.git] / src / readlink.c
1 // $Id: readlink.c,v 1.4 2005/03/24 12:44:17 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or (at your option)
8 // any later version.
9 //  
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //  
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24
25 #include <string.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <unistd.h>
29 #include <stdio.h>
30
31 static void
32 showHelp(char const *cmd)
33 {
34   WRITE_MSG(1, "Usage:  ");
35   WRITE_STR(1, cmd);
36   WRITE_MSG(1,
37             " [--] <filename>\n"
38             "\n"
39             "Display value of a symbolic link on standard output.\n"
40             "\n"
41             "Please report bugs to " PACKAGE_BUGREPORT "\n");
42   exit(0);
43 }
44
45 static void
46 showVersion()
47 {
48   WRITE_MSG(1,
49             "readlink " VERSION " -- display value of symlink\n"
50             "This program is part of " PACKAGE_STRING "\n\n"
51             "Copyright (C) 2004 Enrico Scholz\n"
52             VERSION_COPYRIGHT_DISCLAIMER);
53   exit(0);
54 }
55
56
57 int main (int argc, char *argv[])
58 {
59   char                  buf[PATH_MAX + 2];
60   int                   idx = 1;
61   int                   len;
62   
63   if (argc>=2) {
64     if (strcmp(argv[1], "--help")   ==0) showHelp(argv[0]);
65     if (strcmp(argv[1], "--version")==0) showVersion();
66     if (strcmp(argv[1], "--")       ==0) ++idx;
67   }
68   if (argc<idx+1)
69     WRITE_MSG(2, "No filename specified; use '--help' for more information\n");
70   else if ((len=readlink(argv[idx], buf, sizeof(buf)-2))==-1)
71     PERROR_Q("readlink: readlink", argv[idx]);
72   else {
73     buf[len++] = '\n';
74     Vwrite(1, buf, len);
75     return EXIT_SUCCESS;
76   }
77
78   return EXIT_FAILURE;
79 }
80