tentative fix for issue 3 (ex 53)
[mplib] / src / texk / kpathsea / readlink.c
1 /* readlink -- obtain contents of symlink.
2
3    Copyright 2008 Karl Berry.
4    Copyright 1998, 1999, 2001, 2005 Olaf Weber.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <stdio.h>
20 #include <kpathsea/config.h>
21 #include <kpathsea/c-pathmx.h>
22 #include <kpathsea/c-unistd.h>
23 #include <kpathsea/c-stat.h>
24
25 #ifdef WIN32
26 #include <string.h>
27 #endif
28
29 /*
30  *      readlink name
31  *      Returns 0 if name exists and is a symlink, 1 otherwise.  The contents
32  *      of the link are printed on standard output.
33  */
34
35 int
36 main P2C(int, argc, char **, argv)
37 {
38 #ifdef S_ISLNK
39     int status;
40     char path[PATH_MAX];
41 #endif
42     
43     if (argc > 1 && strcmp (argv[1], "--help") == 0) {
44         printf("Usage: %s FILE\n\
45   If FILE exists and is a symlink, print the contents of the link and\n\
46   exit successfully.  Otherwise print nothing and fail.\n\
47 \n\
48 --help      display this help and exit\n\
49 --version   output version information and exit\n\n", argv[0]);
50         fputs ("Email bug reports to tex-k@mail.tug.org.\n", stdout);
51         exit(0);
52     } else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
53         printf ("%s (%s)\n\
54 Copyright (C) 1998 Olaf Weber.\n\
55 There is NO warranty.  You may redistribute this software\n\
56 under the terms of the GNU General Public License\n\
57 For more information about these matters, see the file named GPL.\n\
58 Primary author of %s: Olaf Weber.\n",
59 argv[0], KPSEVERSION, argv[0]);
60         exit (0);
61     }
62
63     /* insist on exactly one arg */
64     if (argc != 2) {
65         fprintf(stderr, "%s: Need exactly one argument.\n\
66 Try `%s --help' for more information.\n", argv[0], argv[0]);
67         exit(1);
68     }
69
70 #ifdef S_ISLNK
71     status = readlink(argv[1], path, PATH_MAX);
72     if (status != -1) {
73         printf("%.*s\n", status, path);
74         return 0;
75     }
76 #endif
77     return 1;
78 }