Back to index

tetex-bin  3.0
readlink.c
Go to the documentation of this file.
00001 /* readlink -- obtain contents of symlink.
00002    Copyright (C) 1998 Olaf Weber.
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software Foundation,
00016    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00017 
00018 #include <stdio.h>
00019 #include <kpathsea/config.h>
00020 #include <kpathsea/c-pathmx.h>
00021 #include <kpathsea/c-unistd.h>
00022 #include <kpathsea/c-stat.h>
00023 
00024 #ifdef WIN32
00025 #include <string.h>
00026 #endif
00027 
00028 /*
00029  *      readlink name
00030  *      Returns 0 if name exists and is a symlink, 1 otherwise.  The contents
00031  *      of the link are printed on standard output.
00032  */
00033 
00034 int
00035 main P2C(int, argc, char **, argv)
00036 {
00037 #ifdef S_ISLNK
00038     int status;
00039     char path[PATH_MAX];
00040 #endif
00041     
00042     if (argc > 1 && strcmp (argv[1], "--help") == 0) {
00043         printf("Usage: %s FILE\n\
00044   If FILE exists and is a symlink, print the contents of the link and\n\
00045   exit successfully.  Otherwise print nothing and fail.\n\
00046 \n\
00047 --help      display this help and exit\n\
00048 --version   output version information and exit\n\n", argv[0]);
00049         fputs ("Email bug reports to tex-k@mail.tug.org.\n", stdout);
00050         exit(0);
00051     } else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
00052         printf ("%s (%s)\n\
00053 Copyright (C) 1998 Olaf Weber.\n\
00054 There is NO warranty.  You may redistribute this software\n\
00055 under the terms of the GNU General Public License\n\
00056 For more information about these matters, see the file named COPYING.\n\
00057 Primary author of %s: Olaf Weber.\n",
00058 argv[0], KPSEVERSION, argv[0]);
00059         exit (0);
00060     }
00061 
00062     /* insist on exactly one arg */
00063     if (argc != 2) {
00064         fprintf(stderr, "%s: Need exactly one argument.\n\
00065 Try `%s --help' for more information.\n", argv[0], argv[0]);
00066         exit(1);
00067     }
00068 
00069 #ifdef S_ISLNK
00070     status = readlink(argv[1], path, PATH_MAX);
00071     if (status != -1) {
00072         printf("%.*s\n", status, path);
00073         return 0;
00074     }
00075 #endif
00076     return 1;
00077 }