diff options
Diffstat (limited to 'contrib/texinfo/info/filesys.c')
-rw-r--r-- | contrib/texinfo/info/filesys.c | 309 |
1 files changed, 138 insertions, 171 deletions
diff --git a/contrib/texinfo/info/filesys.c b/contrib/texinfo/info/filesys.c index e684bf81f7b1..eb93fdf91276 100644 --- a/contrib/texinfo/info/filesys.c +++ b/contrib/texinfo/info/filesys.c @@ -1,9 +1,7 @@ -/* filesys.c -- File system specific functions for hacking this system. */ +/* filesys.c -- File system specific functions for hacking this system. + $Id: filesys.c,v 1.6 1998/02/21 22:52:46 karl Exp $ -/* This file is part of GNU Info, a program for reading online documentation - stored in Info format. - - Copyright (C) 1993 Free Software Foundation, Inc. + Copyright (C) 1993, 97, 98 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,49 +19,17 @@ Written by Brian Fox (bfox@ai.mit.edu). */ -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#if defined (HAVE_SYS_FILE_H) -#include <sys/file.h> -#endif /* HAVE_SYS_FILE_H */ -#include <sys/errno.h> -#include "general.h" +#include "info.h" + #include "tilde.h" #include "filesys.h" -#if !defined (O_RDONLY) -#if defined (HAVE_SYS_FCNTL_H) -#include <sys/fcntl.h> -#else /* !HAVE_SYS_FCNTL_H */ -#include <fcntl.h> -#endif /* !HAVE_SYS_FCNTL_H */ -#endif /* !O_RDONLY */ - -#if !defined (errno) -extern int errno; -#endif /* !errno */ - -/* Found in info-utils.c. */ -extern char *filename_non_directory (); - -#if !defined (BUILDING_LIBRARY) -/* Found in session.c */ -extern int info_windows_initialized_p; - -/* Found in window.c. */ -extern void message_in_echo_area (), unmessage_in_echo_area (); -#endif /* !BUILDING_LIBRARY */ - /* Local to this file. */ static char *info_file_in_path (), *lookup_info_filename (); static void remember_info_filename (), maybe_initialize_infopath (); -#if !defined (NULL) -# define NULL 0x0 -#endif /* !NULL */ - -typedef struct { +typedef struct +{ char *suffix; char *decompressor; } COMPRESSION_ALIST; @@ -72,6 +38,7 @@ static char *info_suffixes[] = { "", ".info", "-info", + "/index", (char *)NULL }; @@ -114,58 +81,58 @@ info_find_fullpath (partial) expansion = lookup_info_filename (partial); if (expansion) - return (expansion); + return (expansion); /* If we have the full path to this file, we still may have to add - various extensions to it. I guess we have to stat this file - after all. */ + various extensions to it. I guess we have to stat this file + after all. */ if (initial_character == '/') - temp = info_file_in_path (partial + 1, "/"); + temp = info_file_in_path (partial + 1, "/"); else if (initial_character == '~') - { - expansion = tilde_expand_word (partial); - if (*expansion == '/') - { - temp = info_file_in_path (expansion + 1, "/"); - free (expansion); - } - else - temp = expansion; - } + { + expansion = tilde_expand_word (partial); + if (*expansion == '/') + { + temp = info_file_in_path (expansion + 1, "/"); + free (expansion); + } + else + temp = expansion; + } else if (initial_character == '.' && - (partial[1] == '/' || (partial[1] == '.' && partial[2] == '/'))) - { - if (local_temp_filename_size < 1024) - local_temp_filename = (char *)xrealloc - (local_temp_filename, (local_temp_filename_size = 1024)); + (partial[1] == '/' || (partial[1] == '.' && partial[2] == '/'))) + { + if (local_temp_filename_size < 1024) + local_temp_filename = (char *)xrealloc + (local_temp_filename, (local_temp_filename_size = 1024)); #if defined (HAVE_GETCWD) - if (!getcwd (local_temp_filename, local_temp_filename_size)) + if (!getcwd (local_temp_filename, local_temp_filename_size)) #else /* !HAVE_GETCWD */ - if (!getwd (local_temp_filename)) + if (!getwd (local_temp_filename)) #endif /* !HAVE_GETCWD */ - { - filesys_error_number = errno; - return (partial); - } - - strcat (local_temp_filename, "/"); - strcat (local_temp_filename, partial); - return (local_temp_filename); - } + { + filesys_error_number = errno; + return (partial); + } + + strcat (local_temp_filename, "/"); + strcat (local_temp_filename, partial); + return (local_temp_filename); + } else - temp = info_file_in_path (partial, infopath); + temp = info_file_in_path (partial, infopath); if (temp) - { - remember_info_filename (partial, temp); - if (strlen (temp) > local_temp_filename_size) - local_temp_filename = (char *) xrealloc - (local_temp_filename, - (local_temp_filename_size = (50 + strlen (temp)))); - strcpy (local_temp_filename, temp); - free (temp); - return (local_temp_filename); - } + { + remember_info_filename (partial, temp); + if (strlen (temp) > local_temp_filename_size) + local_temp_filename = (char *) xrealloc + (local_temp_filename, + (local_temp_filename_size = (50 + strlen (temp)))); + strcpy (local_temp_filename, temp); + free (temp); + return (local_temp_filename); + } } return (partial); } @@ -183,25 +150,25 @@ info_file_in_path (filename, path) dirname_index = 0; - while (temp_dirname = extract_colon_unit (path, &dirname_index)) + while ((temp_dirname = extract_colon_unit (path, &dirname_index))) { register int i, pre_suffix_length; char *temp; /* Expand a leading tilde if one is present. */ if (*temp_dirname == '~') - { - char *expanded_dirname; + { + char *expanded_dirname; - expanded_dirname = tilde_expand_word (temp_dirname); - free (temp_dirname); - temp_dirname = expanded_dirname; - } + expanded_dirname = tilde_expand_word (temp_dirname); + free (temp_dirname); + temp_dirname = expanded_dirname; + } temp = (char *)xmalloc (30 + strlen (temp_dirname) + strlen (filename)); strcpy (temp, temp_dirname); if (temp[(strlen (temp)) - 1] != '/') - strcat (temp, "/"); + strcat (temp, "/"); strcat (temp, filename); pre_suffix_length = strlen (temp); @@ -209,54 +176,54 @@ info_file_in_path (filename, path) free (temp_dirname); for (i = 0; info_suffixes[i]; i++) - { - strcpy (temp + pre_suffix_length, info_suffixes[i]); - - statable = (stat (temp, &finfo) == 0); - - /* If we have found a regular file, then use that. Else, if we - have found a directory, look in that directory for this file. */ - if (statable) - { - if (S_ISREG (finfo.st_mode)) - { - return (temp); - } - else if (S_ISDIR (finfo.st_mode)) - { - char *newpath, *filename_only, *newtemp; - - newpath = strdup (temp); - filename_only = filename_non_directory (filename); - newtemp = info_file_in_path (filename_only, newpath); - - free (newpath); - if (newtemp) - { - free (temp); - return (newtemp); - } - } - } - else - { - /* Add various compression suffixes to the name to see if - the file is present in compressed format. */ - register int j, pre_compress_suffix_length; - - pre_compress_suffix_length = strlen (temp); - - for (j = 0; compress_suffixes[j].suffix; j++) - { - strcpy (temp + pre_compress_suffix_length, - compress_suffixes[j].suffix); - - statable = (stat (temp, &finfo) == 0); - if (statable && (S_ISREG (finfo.st_mode))) - return (temp); - } - } - } + { + strcpy (temp + pre_suffix_length, info_suffixes[i]); + + statable = (stat (temp, &finfo) == 0); + + /* If we have found a regular file, then use that. Else, if we + have found a directory, look in that directory for this file. */ + if (statable) + { + if (S_ISREG (finfo.st_mode)) + { + return (temp); + } + else if (S_ISDIR (finfo.st_mode)) + { + char *newpath, *filename_only, *newtemp; + + newpath = xstrdup (temp); + filename_only = filename_non_directory (filename); + newtemp = info_file_in_path (filename_only, newpath); + + free (newpath); + if (newtemp) + { + free (temp); + return (newtemp); + } + } + } + else + { + /* Add various compression suffixes to the name to see if + the file is present in compressed format. */ + register int j, pre_compress_suffix_length; + + pre_compress_suffix_length = strlen (temp); + + for (j = 0; compress_suffixes[j].suffix; j++) + { + strcpy (temp + pre_compress_suffix_length, + compress_suffixes[j].suffix); + + statable = (stat (temp, &finfo) == 0); + if (statable && (S_ISREG (finfo.st_mode))) + return (temp); + } + } + } free (temp); } return ((char *)NULL); @@ -290,7 +257,7 @@ extract_colon_unit (string, idx) strncpy (value, &string[start], (i - start)); value[i - start] = '\0'; if (string[i]) - ++i; + ++i; *idx = i; return (value); } @@ -317,10 +284,10 @@ lookup_info_filename (filename) { register int i; for (i = 0; names_and_files[i]; i++) - { - if (strcmp (names_and_files[i]->filename, filename) == 0) - return (names_and_files[i]->expansion); - } + { + if (strcmp (names_and_files[i]->filename, filename) == 0) + return (names_and_files[i]->expansion); + } } return (char *)NULL;; } @@ -340,12 +307,12 @@ remember_info_filename (filename, expansion) alloc_size = names_and_files_slots * sizeof (FILENAME_LIST *); names_and_files = - (FILENAME_LIST **) xrealloc (names_and_files, alloc_size); + (FILENAME_LIST **) xrealloc (names_and_files, alloc_size); } new = (FILENAME_LIST *)xmalloc (sizeof (FILENAME_LIST)); - new->filename = strdup (filename); - new->expansion = expansion ? strdup (expansion) : (char *)NULL; + new->filename = xstrdup (filename); + new->expansion = expansion ? xstrdup (expansion) : (char *)NULL; names_and_files[names_and_files_index++] = new; names_and_files[names_and_files_index] = (FILENAME_LIST *)NULL; @@ -357,7 +324,7 @@ maybe_initialize_infopath () if (!infopath_size) { infopath = (char *) - xmalloc (infopath_size = (1 + strlen (DEFAULT_INFOPATH))); + xmalloc (infopath_size = (1 + strlen (DEFAULT_INFOPATH))); strcpy (infopath, DEFAULT_INFOPATH); } @@ -392,7 +359,7 @@ info_add_path (path, where) } else if (where == INFOPATH_PREPEND) { - char *temp = strdup (infopath); + char *temp = xstrdup (infopath); strcpy (infopath, path); strcat (infopath, ":"); strcat (infopath, temp); @@ -436,21 +403,21 @@ filesys_read_info_file (pathname, filesize, finfo) /* If the file couldn't be opened, give up. */ if (descriptor < 0) - { - filesys_error_number = errno; - return ((char *)NULL); - } + { + filesys_error_number = errno; + return ((char *)NULL); + } /* Try to read the contents of this file. */ st_size = (long) finfo->st_size; contents = (char *)xmalloc (1 + st_size); if ((read (descriptor, contents, st_size)) != st_size) - { - filesys_error_number = errno; - close (descriptor); - free (contents); - return ((char *)NULL); - } + { + filesys_error_number = errno; + close (descriptor); + free (contents); + return ((char *)NULL); + } close (descriptor); @@ -510,20 +477,20 @@ filesys_read_compressed (pathname, filesize, finfo) chunk = (char *)xmalloc (FILESYS_PIPE_BUFFER_SIZE); while (1) - { - int bytes_read; + { + int bytes_read; - bytes_read = fread (chunk, 1, FILESYS_PIPE_BUFFER_SIZE, stream); + bytes_read = fread (chunk, 1, FILESYS_PIPE_BUFFER_SIZE, stream); - if (bytes_read + offset >= size) - contents = (char *)xrealloc - (contents, size += (2 * FILESYS_PIPE_BUFFER_SIZE)); + if (bytes_read + offset >= size) + contents = (char *)xrealloc + (contents, size += (2 * FILESYS_PIPE_BUFFER_SIZE)); - memcpy (contents + offset, chunk, bytes_read); - offset += bytes_read; - if (bytes_read != FILESYS_PIPE_BUFFER_SIZE) - break; - } + memcpy (contents + offset, chunk, bytes_read); + offset += bytes_read; + if (bytes_read != FILESYS_PIPE_BUFFER_SIZE) + break; + } free (chunk); pclose (stream); @@ -572,8 +539,8 @@ filesys_decompressor_for_file (filename) for (i = strlen (filename) - 1; i > 0; i--) if (filename[i] == '.') { - extension = filename + i; - break; + extension = filename + i; + break; } if (!extension) |