"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/littleutils/filemode.c" (29 Oct 2021, 4562 Bytes) of package /linux/privat/littleutils-1.2.5.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "filemode.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.4_vs_1.2.5.

    1 /* filemode:  Prints out a standard octal formatted file mode for the specified
    2    files.
    3 
    4    Copyright (C) 2004-2021 by Brian Lindholm.
    5    This file is part of the littleutils utility set.
    6 
    7    The filemode utility is free software; you can redistribute it and/or modify
    8    it under the terms of the GNU General Public License as published by the
    9    Free Software Foundation; either version 3, or (at your option) any later
   10    version.
   11 
   12    The filemode utility is distributed in the hope that it will be useful, but
   13    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   15    more details.
   16 
   17    You should have received a copy of the GNU General Public License along with
   18    the littleutils.  If not, see <https://www.gnu.org/licenses/>. */
   19 
   20 
   21 #include <config.h>
   22 
   23 #include <limits.h>
   24 #ifdef HAVE_STDIO_H
   25 # include <stdio.h>
   26 #endif
   27 #ifdef HAVE_STRING_H
   28 # include <string.h>
   29 #endif
   30 #ifdef HAVE_SYS_STAT_H
   31 # include <sys/stat.h>
   32 #endif
   33 
   34 #ifdef HAVE_UNISTD_H
   35 # include <unistd.h>
   36 # define OPTEND -1
   37 #else
   38 # define OPTEND EOF
   39 #endif
   40 #ifdef HAVE_GETOPT_H
   41 # include <getopt.h>
   42 #endif
   43 
   44 #ifdef __MINGW32__
   45 extern int getopt (int argc, char * const *argv, const char *optstring);
   46 extern char *optarg;
   47 extern int optind;
   48 #endif
   49 
   50 #ifdef DJGPP
   51 /* speed up stat command for DJGPP, but allow for executable check */
   52 unsigned short _djstat_flags = 63 - _STAT_EXEC_EXT;
   53 #endif
   54 
   55 #ifndef PATH_MAX
   56 # define PATH_MAX 256
   57 #endif
   58 
   59 #ifndef FOUR_SEVENS
   60 # define FOUR_SEVENS 4095
   61 #endif
   62 
   63 
   64 /* help function */
   65 
   66 static void
   67 help (FILE *where)
   68 {
   69   fprintf (where,
   70     "filemode " PACKAGE_VERSION "\n"
   71     "usage: filemode [-f file_list] [-h(elp)] [-p(ipe)] [-q(uiet)]\n"
   72     "         [-v(erbose)] file...\n");
   73 }
   74 
   75 
   76 /* print filemode function */
   77 
   78 static void
   79 printmode (char *filename, int verbose)
   80 {
   81   struct stat file_stats;
   82 
   83   if (stat (filename, &file_stats))
   84     fprintf (stderr, "filemode error: can't determine mode of %s\n", filename);
   85   else
   86     {
   87       if (verbose == 1)
   88         fprintf (stdout, "%s\t%04o\n", filename, (unsigned int) file_stats.st_mode &
   89           FOUR_SEVENS);
   90       else
   91         fprintf (stdout, "%04o\n", (unsigned int) file_stats.st_mode & FOUR_SEVENS);
   92     }
   93 }
   94 
   95 
   96 /* main program */
   97 
   98 int
   99 main (int argc, char *argv[])
  100 {
  101   FILE *infile;
  102   char filename[PATH_MAX], *listname, *newline, *rc;
  103   int argn, opt, use_file, use_pipe, verbose;
  104 
  105   /* parse options */
  106 
  107   listname = "";
  108   verbose = 0;
  109   use_file = 0;
  110   use_pipe = 0;
  111   while ((opt = getopt (argc, argv, "f:hpqv")) != OPTEND)
  112     switch (opt)
  113       {
  114       case 'f':
  115         use_file = 1;
  116         listname = optarg;
  117         break;
  118       case 'h':
  119         help (stdout);
  120         return (0);
  121       case 'p':
  122         use_pipe = 1;
  123         break;
  124       case 'q':
  125         verbose = -1;
  126         break;
  127       case 'v':
  128         verbose = 1;
  129         break;
  130       case '?':
  131         help (stderr);
  132         return (1);
  133       }
  134 
  135   /* finalize options */
  136 
  137   if ((optind == argc) && (use_file == 0) && (use_pipe == 0))
  138     {
  139       help (stdout);
  140       return (0);
  141     }
  142   if (verbose == 0)
  143     {
  144       if (((argc - optind) != 1) || use_file || use_pipe)
  145         verbose = 1;
  146       else
  147         verbose = -1;
  148     }
  149 
  150   /* process files in listed in file specified by -f option */
  151 
  152   if (use_file)
  153     {
  154       infile = fopen (listname, "r");
  155       if (infile == NULL)
  156         fprintf (stderr, "filemode error: can't open %s!\n", listname);
  157       else
  158         {
  159           while (!feof (infile))
  160             {
  161               rc = fgets (filename, PATH_MAX - 1, infile);
  162               if (rc != NULL)
  163                 {
  164                   newline = strchr (filename, '\n');
  165                   if (newline != NULL)
  166                     *newline = '\0';
  167                   if (strlen (filename) != 0)
  168                     printmode (filename, verbose);
  169                 }
  170             }
  171           (void) fclose (infile);
  172         }
  173     }
  174 
  175   /* process files listed on stdin (i.e., the -p option) */
  176 
  177   if (use_pipe)
  178     while (!feof (stdin))
  179       {
  180         rc = fgets (filename, PATH_MAX - 1, stdin);
  181         if (rc != NULL)
  182           {
  183             newline = strchr (filename, '\n');
  184             if (newline != NULL)
  185               *newline = '\0';
  186             if (strlen (filename) != 0)
  187               printmode (filename, verbose);
  188           }
  189       }
  190 
  191   /* process files given in the argument list */
  192 
  193   for (argn = optind; argn < argc; argn++)
  194     printmode (argv[argn], verbose);
  195 
  196   /* indicate successful finish */
  197 
  198   return (0);
  199 }