beta-0.89.2
[luatex.git] / source / texk / kpathsea / readlink.c
blob1a0fe72c26a57e6aa655b0819e4e8dfc35d9a3e7
1 /* readlink -- obtain contents of symlink.
3 Copyright 2008, 2009 Karl Berry.
4 Copyright 1998, 1999, 2001, 2005 Olaf Weber.
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.
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.
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/>. */
19 #include <kpathsea/config.h>
20 #include <kpathsea/c-pathmx.h>
21 #include <kpathsea/c-unistd.h>
22 #include <kpathsea/c-stat.h>
24 #ifdef WIN32
25 #include <string.h>
26 #endif
29 * readlink name
30 * Returns 0 if name exists and is a symlink, 1 otherwise. The contents
31 * of the link are printed on standard output.
34 int
35 main (int argc, char **argv)
37 #ifdef S_ISLNK
38 int status;
39 char path[PATH_MAX];
40 #endif
42 if (argc > 1 && strcmp (argv[1], "--help") == 0) {
43 printf("Usage: %s FILE\n\
44 If FILE exists and is a symlink, print the contents of the link and\n\
45 exit successfully. Otherwise print nothing and fail.\n\
46 \n\
47 --help display this help and exit\n\
48 --version output version information and exit\n\n", argv[0]);
49 fputs ("Email bug reports to tex-k@tug.org.\n", stdout);
50 exit(0);
51 } else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
52 printf ("%s (%s)\n\
53 Copyright (C) 2009 Olaf Weber & Karl Berry.\n\
54 There is NO warranty. You may redistribute this software\n\
55 under the terms of the GNU General Public License\n\
56 For more information about these matters, see the file named GPL.\n\
57 Primary author of %s: Olaf Weber.\n",
58 argv[0], KPSEVERSION, argv[0]);
59 exit (0);
62 /* insist on exactly one arg */
63 if (argc != 2) {
64 fprintf(stderr, "%s: Need exactly one argument.\n\
65 Try `%s --help' for more information.\n", argv[0], argv[0]);
66 exit(1);
69 #ifdef S_ISLNK
70 status = readlink(argv[1], path, PATH_MAX);
71 if (status != -1) {
72 printf("%.*s\n", status, path);
73 return 0;
75 #endif
76 return 1;