home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / info / filesys.c < prev    next >
C/C++ Source or Header  |  1997-11-15  |  17KB  |  623 lines

  1. /* filesys.c -- File system specific functions for hacking this system.
  2.    $Id: filesys.c,v 1.4 1997/07/24 21:23:07 karl Exp $
  3.  
  4.    Copyright (C) 1993, 97 Free Software Foundation, Inc.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20.    Written by Brian Fox (bfox@ai.mit.edu). */
  21.  
  22. #include "info.h"
  23.  
  24. #include "tilde.h"
  25. #include "filesys.h"
  26.  
  27. #ifdef __EMX__
  28. #define COLONCHR ';'
  29. #define COLONSTR ";"
  30. #include <ctype.h>
  31. #else
  32. #define COLONCHR ':'
  33. #define COLONSTR ":"
  34. #endif
  35.  
  36. /* Local to this file. */
  37. static char *info_file_in_path (), *lookup_info_filename ();
  38. static char *info_check_file ();
  39. static void remember_info_filename (), maybe_initialize_infopath ();
  40.  
  41. typedef struct
  42. {
  43.   char *suffix;
  44.   char *decompressor;
  45. } COMPRESSION_ALIST;
  46.  
  47. static char *info_suffixes[] = {
  48.   "",
  49.   ".info",
  50.   "-info",
  51.   ".inf",
  52.   (char *)NULL
  53. };
  54.  
  55. static COMPRESSION_ALIST compress_suffixes[] = {
  56.   { ".Z", "compress -d" },
  57.   { ".Y", "unyabba" },
  58.   { ".z", "gzip -d" },
  59.   { ".gz", "gzip -d" },
  60.   { (char *)NULL, (char *)NULL }
  61. };
  62.  
  63. /* The path on which we look for info files.  You can initialize this
  64.    from the environment variable INFOPATH if there is one, or you can
  65.    call info_add_path () to add paths to the beginning or end of it.
  66.    You can call zap_infopath () to make the path go away. */
  67. char *infopath = (char *)NULL;
  68. static int infopath_size = 0;
  69.  
  70. /* Expand the filename in PARTIAL to make a real name for this operating
  71.    system.  This looks in INFO_PATHS in order to find the correct file.
  72.    If it can't find the file, it returns NULL. */
  73. static char *local_temp_filename = (char *)NULL;
  74. static int local_temp_filename_size = 0;
  75.  
  76. char *
  77. info_find_fullpath (partial)
  78.      char *partial;
  79. {
  80.   int initial_character;
  81.   char *temp;
  82.  
  83.   filesys_error_number = 0;
  84.  
  85.   maybe_initialize_infopath ();
  86.  
  87.   if (partial && (initial_character = *partial))
  88.     {
  89.       char *expansion;
  90.  
  91.       expansion = lookup_info_filename (partial);
  92.  
  93.       if (expansion)
  94.         return (expansion);
  95.  
  96.       /* If we have the full path to this file, we still may have to add
  97.          various extensions to it.  I guess we have to stat this file
  98.          after all. */
  99.       if (initial_character == '/')
  100.     temp = info_check_file (partial);
  101. #ifdef __EMX__
  102.       else if (isalpha(initial_character) && partial[1] == ':')
  103.     temp = info_check_file (partial);
  104. #endif
  105.       else if (initial_character == '~')
  106.         {
  107.           expansion = tilde_expand_word (partial);
  108.           if (*expansion == '/')
  109.             {
  110.           temp = info_check_file (expansion);
  111.               free (expansion);
  112.             }
  113.           else
  114.             temp = expansion;
  115.         }
  116.       else if (initial_character == '.' &&
  117.                (partial[1] == '/' || (partial[1] == '.' && partial[2] == '/')))
  118.         {
  119.           if (local_temp_filename_size < 1024)
  120.             local_temp_filename = (char *)xrealloc
  121.               (local_temp_filename, (local_temp_filename_size = 1024));
  122. #if defined (HAVE_GETCWD)
  123.           if (!getcwd (local_temp_filename, local_temp_filename_size))
  124. #else /*  !HAVE_GETCWD */
  125.           if (!getwd (local_temp_filename))
  126. #endif /* !HAVE_GETCWD */
  127.             {
  128.               filesys_error_number = errno;
  129.               return (partial);
  130.             }
  131.  
  132.           strcat (local_temp_filename, "/");
  133.           strcat (local_temp_filename, partial);
  134.  
  135.       temp = info_check_file (local_temp_filename);
  136.       free (local_temp_filename);
  137.  
  138.       return (temp);
  139.         }
  140.       else
  141.         temp = info_file_in_path (partial, infopath);
  142.  
  143.       if (temp)
  144.         {
  145.           remember_info_filename (partial, temp);
  146.           if (strlen (temp) > local_temp_filename_size)
  147.             local_temp_filename = (char *) xrealloc
  148.               (local_temp_filename,
  149.                (local_temp_filename_size = (50 + strlen (temp))));
  150.           strcpy (local_temp_filename, temp);
  151.           free (temp);
  152.           return (local_temp_filename);
  153.         }
  154.     }
  155.   return (partial);
  156. }
  157.  
  158. /* Scan the list of directories in PATH looking for FILENAME.  If we find
  159.    one that is a regular file, return it as a new string.  Otherwise, return
  160.    a NULL pointer. */
  161. static char *
  162. info_file_in_path (filename, path)
  163.      char *filename, *path;
  164. {
  165.   char *temp_dirname, *final_name;
  166.   int dirname_index;
  167.  
  168.   dirname_index = 0;
  169.  
  170.   while ((temp_dirname = extract_colon_unit (path, &dirname_index)))
  171.     {
  172.       register int i, pre_suffix_length;
  173.       char *temp;
  174.  
  175.       /* Expand a leading tilde if one is present. */
  176.       if (*temp_dirname == '~')
  177.         {
  178.           char *expanded_dirname;
  179.  
  180.           expanded_dirname = tilde_expand_word (temp_dirname);
  181.           free (temp_dirname);
  182.           temp_dirname = expanded_dirname;
  183.         }
  184.  
  185.       temp = (char *)xmalloc (30 + strlen (temp_dirname) + strlen (filename));
  186.       strcpy (temp, temp_dirname);
  187.       if (temp[(strlen (temp)) - 1] != '/')
  188.         strcat (temp, "/");
  189.       strcat (temp, filename);
  190.  
  191.       free (temp_dirname);
  192.  
  193.       final_name = info_check_file (temp);
  194.  
  195.       free (temp);
  196.  
  197.       if (final_name)
  198.     return (final_name);
  199.     }
  200.   return ((char *)NULL);
  201. }
  202.  
  203. /* check if the file exists, possibly trying the compressed file suffixes */
  204. static char *
  205. info_check_file (filename)
  206.      char *filename;
  207. {
  208.   register int i, statable, pre_suffix_length;
  209.   struct stat finfo;
  210.   char *temp;
  211.  
  212.   temp = (char *)xmalloc (30 + strlen (filename));
  213.   strcpy (temp, filename);
  214.  
  215.   pre_suffix_length = strlen (temp);
  216.  
  217.       for (i = 0; info_suffixes[i]; i++)
  218.         {
  219.           strcpy (temp + pre_suffix_length, info_suffixes[i]);
  220.  
  221.           statable = (stat (temp, &finfo) == 0);
  222.  
  223.           /* If we have found a regular file, then use that.  Else, if we
  224.              have found a directory, look in that directory for this file. */
  225.           if (statable)
  226.             {
  227.               if (S_ISREG (finfo.st_mode))
  228.                 {
  229.                   return (temp);
  230.                 }
  231.               else if (S_ISDIR (finfo.st_mode))
  232.                 {
  233.                   char *newpath, *filename_only, *newtemp;
  234.  
  235.                   newpath = xstrdup (temp);
  236.                   filename_only = filename_non_directory (filename);
  237.                   newtemp = info_file_in_path (filename_only, newpath);
  238.  
  239.                   free (newpath);
  240.                   if (newtemp)
  241.                     {
  242.                       free (temp);
  243.                       return (newtemp);
  244.                     }
  245.                 }
  246.             }
  247.           else
  248.             {
  249.               /* Add various compression suffixes to the name to see if
  250.                  the file is present in compressed format. */
  251.               register int j, pre_compress_suffix_length;
  252.  
  253.               pre_compress_suffix_length = strlen (temp);
  254.  
  255.               for (j = 0; compress_suffixes[j].suffix; j++)
  256.                 {
  257.                   strcpy (temp + pre_compress_suffix_length,
  258.                           compress_suffixes[j].suffix);
  259.  
  260.                   statable = (stat (temp, &finfo) == 0);
  261.                   if (statable && (S_ISREG (finfo.st_mode)))
  262.                     return (temp);
  263.                 }
  264.             }
  265.         }
  266.   return ((char *) NULL);
  267. }
  268.  
  269. /* Given a string containing units of information separated by colons,
  270.    return the next one pointed to by IDX, or NULL if there are no more.
  271.    Advance IDX to the character after the colon. */
  272. char *
  273. extract_colon_unit (string, idx)
  274.      char *string;
  275.      int *idx;
  276. {
  277.   register int i, start;
  278.  
  279.   i = start = *idx;
  280.   if ((i >= strlen (string)) || !string)
  281.     return ((char *) NULL);
  282.  
  283.   while (string[i] && string[i] != COLONCHR)
  284.     i++;
  285.   if (i == start)
  286.     {
  287.       return ((char *) NULL);
  288.     }
  289.   else
  290.     {
  291.       char *value;
  292.  
  293.       value = (char *) xmalloc (1 + (i - start));
  294.       strncpy (value, &string[start], (i - start));
  295.       value[i - start] = '\0';
  296.       if (string[i])
  297.         ++i;
  298.       *idx = i;
  299.       return (value);
  300.     }
  301. }
  302.  
  303. /* A structure which associates a filename with its expansion. */
  304. typedef struct {
  305.   char *filename;
  306.   char *expansion;
  307. } FILENAME_LIST;
  308.  
  309. /* An array of remembered arguments and results. */
  310. static FILENAME_LIST **names_and_files = (FILENAME_LIST **)NULL;
  311. static int names_and_files_index = 0;
  312. static int names_and_files_slots = 0;
  313.  
  314. /* Find the result for having already called info_find_fullpath () with
  315.    FILENAME. */
  316. static char *
  317. lookup_info_filename (filename)
  318.      char *filename;
  319. {
  320.   if (filename && names_and_files)
  321.     {
  322.       register int i;
  323.       for (i = 0; names_and_files[i]; i++)
  324.         {
  325.           if (strcmp (names_and_files[i]->filename, filename) == 0)
  326.             return (names_and_files[i]->expansion);
  327.         }
  328.     }
  329.   return (char *)NULL;;
  330. }
  331.  
  332. /* Add a filename and its expansion to our list. */
  333. static void
  334. remember_info_filename (filename, expansion)
  335.      char *filename, *expansion;
  336. {
  337.   FILENAME_LIST *new;
  338.  
  339.   if (names_and_files_index + 2 > names_and_files_slots)
  340.     {
  341.       int alloc_size;
  342.       names_and_files_slots += 10;
  343.  
  344.       alloc_size = names_and_files_slots * sizeof (FILENAME_LIST *);
  345.  
  346.       names_and_files =
  347.         (FILENAME_LIST **) xrealloc (names_and_files, alloc_size);
  348.     }
  349.  
  350.   new = (FILENAME_LIST *)xmalloc (sizeof (FILENAME_LIST));
  351.   new->filename = xstrdup (filename);
  352.   new->expansion = expansion ? xstrdup (expansion) : (char *)NULL;
  353.  
  354.   names_and_files[names_and_files_index++] = new;
  355.   names_and_files[names_and_files_index] = (FILENAME_LIST *)NULL;
  356. }
  357.  
  358. static void
  359. maybe_initialize_infopath ()
  360. {
  361.   if (!infopath_size)
  362.     {
  363.       infopath = (char *)
  364.         xmalloc (infopath_size = (1 + strlen (DEFAULT_INFOPATH)));
  365.  
  366.       strcpy (infopath, DEFAULT_INFOPATH);
  367.     }
  368. }
  369.  
  370. /* Add PATH to the list of paths found in INFOPATH.  2nd argument says
  371.    whether to put PATH at the front or end of INFOPATH. */
  372. void
  373. info_add_path (path, where)
  374.      char *path;
  375.      int where;
  376. {
  377.   int len;
  378.  
  379.   if (!infopath)
  380.     {
  381.       infopath = (char *)xmalloc (infopath_size = 200 + strlen (path));
  382.       infopath[0] = '\0';
  383.     }
  384.  
  385.   len = strlen (path) + strlen (infopath);
  386.  
  387.   if (len + 2 >= infopath_size)
  388.     infopath = (char *)xrealloc (infopath, (infopath_size += (2 * len) + 2));
  389.  
  390.   if (!*infopath)
  391.     strcpy (infopath, path);
  392.   else if (where == INFOPATH_APPEND)
  393.     {
  394.       strcat (infopath, COLONSTR);
  395.       strcat (infopath, path);
  396.     }
  397.   else if (where == INFOPATH_PREPEND)
  398.     {
  399.       char *temp = xstrdup (infopath);
  400.       strcpy (infopath, path);
  401.       strcat (infopath, COLONSTR);
  402.       strcat (infopath, temp);
  403.       free (temp);
  404.     }
  405. }
  406.  
  407. /* Make INFOPATH have absolutely nothing in it. */
  408. void
  409. zap_infopath ()
  410. {
  411.   if (infopath)
  412.     free (infopath);
  413.  
  414.   infopath = (char *)NULL;
  415.   infopath_size = 0;
  416. }
  417.  
  418. /* Read the contents of PATHNAME, returning a buffer with the contents of
  419.    that file in it, and returning the size of that buffer in FILESIZE.
  420.    FINFO is a stat struct which has already been filled in by the caller.
  421.    If the file cannot be read, return a NULL pointer. */
  422. char *
  423. filesys_read_info_file (pathname, filesize, finfo)
  424.      char *pathname;
  425.      long *filesize;
  426.      struct stat *finfo;
  427. {
  428.   long st_size, result;
  429.  
  430.   *filesize = filesys_error_number = 0;
  431.  
  432.   if (compressed_filename_p (pathname))
  433.     return (filesys_read_compressed (pathname, filesize, finfo));
  434.   else
  435.     {
  436.       int descriptor;
  437.       char *contents;
  438.  
  439.       descriptor = open (pathname, O_RDONLY, 0666);
  440.  
  441.       /* If the file couldn't be opened, give up. */
  442.       if (descriptor < 0)
  443.         {
  444.           filesys_error_number = errno;
  445.           return ((char *)NULL);
  446.         }
  447.  
  448.       /* Try to read the contents of this file. */
  449.       st_size = (long) finfo->st_size;
  450.       contents = (char *)xmalloc (1 + st_size);
  451.       result = read (descriptor, contents, st_size);
  452.       if (result == -1 || result > st_size)
  453.         {
  454.           filesys_error_number = errno;
  455.           close (descriptor);
  456.           free (contents);
  457.           return ((char *)NULL);
  458.         }
  459.  
  460.       close (descriptor);
  461.  
  462.       *filesize = st_size;
  463.       return (contents);
  464.     }
  465. }
  466.  
  467. /* Typically, pipe buffers are 4k. */
  468. #define BASIC_PIPE_BUFFER (4 * 1024)
  469.  
  470. /* We use some large multiple of that. */
  471. #define FILESYS_PIPE_BUFFER_SIZE (16 * BASIC_PIPE_BUFFER)
  472.  
  473. char *
  474. filesys_read_compressed (pathname, filesize, finfo)
  475.      char *pathname;
  476.      long *filesize;
  477.      struct stat *finfo;
  478. {
  479.   FILE *stream;
  480.   char *command, *decompressor;
  481.   char *contents = (char *)NULL;
  482.  
  483.   *filesize = filesys_error_number = 0;
  484.  
  485.   decompressor = filesys_decompressor_for_file (pathname);
  486.  
  487.   if (!decompressor)
  488.     return ((char *)NULL);
  489.  
  490.   command = (char *)xmalloc (10 + strlen (pathname) + strlen (decompressor));
  491.   sprintf (command, "%s < %s", decompressor, pathname);
  492.  
  493. #if !defined (BUILDING_LIBRARY)
  494.   if (info_windows_initialized_p)
  495.     {
  496.       char *temp;
  497.  
  498.       temp = (char *)xmalloc (5 + strlen (command));
  499.       sprintf (temp, "%s...", command);
  500.       message_in_echo_area ("%s", temp);
  501.       free (temp);
  502.     }
  503. #endif /* !BUILDING_LIBRARY */
  504.  
  505.   stream = popen (command, "r");
  506.   free (command);
  507.  
  508.   /* Read chunks from this file until there are none left to read. */
  509.   if (stream)
  510.     {
  511.       int offset, size;
  512.       char *chunk;
  513.     
  514.       offset = size = 0;
  515.       chunk = (char *)xmalloc (FILESYS_PIPE_BUFFER_SIZE);
  516.  
  517.       while (1)
  518.         {
  519.           int bytes_read;
  520.  
  521.           bytes_read = fread (chunk, 1, FILESYS_PIPE_BUFFER_SIZE, stream);
  522.  
  523.           if (bytes_read + offset >= size)
  524.             contents = (char *)xrealloc
  525.               (contents, size += (2 * FILESYS_PIPE_BUFFER_SIZE));
  526.  
  527.           memcpy (contents + offset, chunk, bytes_read);
  528.           offset += bytes_read;
  529.           if (bytes_read != FILESYS_PIPE_BUFFER_SIZE)
  530.             break;
  531.         }
  532.  
  533.       free (chunk);
  534.       pclose (stream);
  535.       contents = (char *)xrealloc (contents, offset + 1);
  536.       *filesize = offset;
  537.     }
  538.   else
  539.     {
  540.       filesys_error_number = errno;
  541.     }
  542.  
  543. #if !defined (BUILDING_LIBARARY)
  544.   if (info_windows_initialized_p)
  545.     unmessage_in_echo_area ();
  546. #endif /* !BUILDING_LIBRARY */
  547.   return (contents);
  548. }
  549.  
  550. /* Return non-zero if FILENAME belongs to a compressed file. */
  551. int
  552. compressed_filename_p (filename)
  553.      char *filename;
  554. {
  555.   char *decompressor;
  556.  
  557.   /* Find the final extension of this filename, and see if it matches one
  558.      of our known ones. */
  559.   decompressor = filesys_decompressor_for_file (filename);
  560.  
  561.   if (decompressor)
  562.     return (1);
  563.   else
  564.     return (0);
  565. }
  566.  
  567. /* Return the command string that would be used to decompress FILENAME. */
  568. char *
  569. filesys_decompressor_for_file (filename)
  570.      char *filename;
  571. {
  572.   register int i;
  573.   char *extension = (char *)NULL;
  574.  
  575.   /* Find the final extension of FILENAME, and see if it appears in our
  576.      list of known compression extensions. */
  577.   for (i = strlen (filename) - 1; i > 0; i--)
  578.     if (filename[i] == '.')
  579.       {
  580.         extension = filename + i;
  581.         break;
  582.       }
  583.  
  584.   if (!extension)
  585.     return ((char *)NULL);
  586.  
  587.   for (i = 0; compress_suffixes[i].suffix; i++)
  588.     if (strcmp (extension, compress_suffixes[i].suffix) == 0)
  589.       return (compress_suffixes[i].decompressor);
  590.  
  591.   return ((char *)NULL);
  592. }
  593.  
  594. /* The number of the most recent file system error. */
  595. int filesys_error_number = 0;
  596.  
  597. /* A function which returns a pointer to a static buffer containing
  598.    an error message for FILENAME and ERROR_NUM. */
  599. static char *errmsg_buf = (char *)NULL;
  600. static int errmsg_buf_size = 0;
  601.  
  602. char *
  603. filesys_error_string (filename, error_num)
  604.      char *filename;
  605.      int error_num;
  606. {
  607.   int len;
  608.   char *result;
  609.  
  610.   if (error_num == 0)
  611.     return ((char *)NULL);
  612.  
  613.   result = strerror (error_num);
  614.  
  615.   len = 4 + strlen (filename) + strlen (result);
  616.   if (len >= errmsg_buf_size)
  617.     errmsg_buf = (char *)xrealloc (errmsg_buf, (errmsg_buf_size = 2 + len));
  618.  
  619.   sprintf (errmsg_buf, "%s: %s", filename, result);
  620.   return (errmsg_buf);
  621. }
  622.  
  623.