Back to index

tetex-bin  3.0
access.c
Go to the documentation of this file.
00001 /* access -- test for access permissions of a file.
00002    Copyright (C) 1997, 2000 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-unistd.h>
00021 #include <kpathsea/c-memstr.h>
00022 
00023 /*
00024  *      access mode x
00025  *      Returns 0 is x exists and can be accessed in accordance with mode.
00026  *      We use this rather than test because test looks at the permissions
00027  *      only, which doesn't take read-only file systems into account.
00028  */
00029 
00030 int
00031 main P2C(int, argc, char **, argv)
00032 {
00033     int mode;
00034     int status;
00035     char * i;
00036 
00037     if (argc > 1 && strcmp (argv[1], "--help") == 0) {
00038         printf("Usage: %s -MODE FILE\n\
00039   MODE is one or more of rwx.  Exit successfully if FILE exists and is\n\
00040   readable (r), writable (w), or executable (x).\n\
00041 \n\
00042 --help      display this help and exit\n\
00043 --version   output version information and exit\n\n", argv[0]);
00044         fputs ("Email bug reports to tex-k@mail.tug.org.\n", stdout);
00045         exit(0);
00046     } else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
00047         printf ("%s (%s)\n\
00048 Copyright (C) 1997 Olaf Weber.\n\
00049 There is NO warranty.  You may redistribute this software\n\
00050 under the terms of the GNU General Public License\n\
00051 For more information about these matters, see the file named COPYING.\n\
00052 Primary author of %s: Olaf Weber.\n",
00053 argv[0], KPSEVERSION, argv[0]);
00054         exit (0);
00055     }
00056 
00057     /* insist on exactly two args */
00058     if (argc != 3) {
00059         fprintf(stderr, "%s: Need exactly two arguments.\n\
00060 Try `%s --help' for more information.\n", argv[0], argv[0]);
00061         exit(1);
00062     }
00063 
00064     /* The option parsing is somewhat primitive: '-' need not be the first
00065      * character of the mode.  The mode must be specified in a single
00066      * option.  Both of these may change.
00067      */
00068     mode = 0;
00069     i = argv[1];
00070     for (i = argv[1]; *i; ++i)
00071         switch (*i) {
00072         case 'r': mode |= R_OK; break;
00073         case 'w': mode |= W_OK; break;
00074         case 'x': mode |= X_OK; break;
00075         case '-': if (i == argv[1]) break;
00076         default:
00077             fprintf(stderr, "%s: Invalid MODE.\n", argv[0]);
00078             exit(1);
00079         }
00080     
00081     status = access(argv[2], mode);
00082         
00083     /* fail if the access call failed */
00084     if (status != 0) {
00085         return 1;
00086     }
00087     
00088     /* otherwise, succeed */
00089     return 0;
00090 }