Back to index

tetex-bin  3.0
kpsestat.c
Go to the documentation of this file.
00001 /* kpsestat -- show file permissions of a file in octal form.
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 <kpathsea/config.h>
00019 #include <kpathsea/c-stat.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 
00023 /*
00024  *      kpsestat mode x
00025  *      Print stat bits of file x on stdout, as modified by mode.
00026  */
00027 
00028 int
00029 main P2C(int, argc, char **, argv)
00030 {
00031     char * mode_string;
00032     int to_set, to_keep, to_clear;
00033     int result;
00034     struct stat f;
00035 
00036     if (argc > 1 && strcmp (argv[1], "--help") == 0) {
00037         printf ("Usage: %s MODE FILE\n\
00038   Print octal permissions of FILE as modified by MODE on standard output.\n\
00039   MODE is a subset of the symbolic permissions accepted by chmod.\n\
00040   Use MODE = to obtain the unchanged permissions.\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     mode_string = argv[1];
00065     to_set = to_keep = to_clear = 0;
00066     for (;;++mode_string) {
00067         int affected = 0;
00068         int action = 0;
00069         int value = 0;
00070 
00071         for (;;++mode_string)
00072             switch (*mode_string) {
00073             case 'u': affected |= 04700; break;
00074             case 'g': affected |= 02070; break;
00075             case 'o': affected |= 01007; break;
00076             case 'a': affected |= 07777; break;
00077             default: goto no_more_affected;
00078             }
00079     no_more_affected:
00080         if (affected == 0)
00081             affected = 07777;
00082         action = *mode_string;
00083         ++mode_string;
00084         for (;;++mode_string)
00085             switch (*mode_string) {
00086             case 'r': value |= 00444 & affected; break;
00087             case 'w': value |= 00222 & affected; break;
00088             case 'x': value |= 00111 & affected; break;
00089             case 's': value |= 06000 & affected; break;
00090             case 't': value |= 01000 & affected; break;
00091             default: goto no_more_values;
00092             }
00093     no_more_values:
00094         switch (action) {
00095         case '-': to_clear |= value; break;
00096         case '=': to_keep  |= value; break;
00097         case '+': to_set   |= value; break;
00098         default:
00099             fprintf(stderr, "%s: Invalid mode\n", argv[0]);
00100             exit(1);
00101         }
00102         if (*mode_string != ',')
00103             break;
00104     }
00105     if (*mode_string != 0) {
00106         fprintf(stderr, "%s: Invalid mode.\n", argv[0]);
00107         exit(1);
00108     }
00109 
00110     /* does the file exist? */
00111     if (stat (argv[2], &f) < 0) {
00112         fprintf(stderr, "%s: ", argv[0]);
00113         perror(argv[2]);
00114         return 1;
00115     }
00116    
00117     result = f.st_mode & 07777;
00118     result |= to_set;
00119     result |= to_keep & result;
00120     result &= ~to_clear;
00121 
00122     printf("%o\n", result);
00123 
00124     return 0;
00125 }