"Fossies" - the Fresh Open Source Software Archive

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