📄 filesys.c
字号:
/* filesys.c -- File system specific functions for hacking this system. $Id: filesys.c,v 1.1.1.3 1998/03/24 18:20:10 law Exp $ 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 the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Written by Brian Fox (bfox@ai.mit.edu). */#include "info.h"#include "tilde.h"#include "filesys.h"/* Local to this file. */static char *info_file_in_path (), *lookup_info_filename ();static void remember_info_filename (), maybe_initialize_infopath ();typedef struct{ char *suffix; char *decompressor;} COMPRESSION_ALIST;static char *info_suffixes[] = { "", ".info", "-info", "/index", (char *)NULL};static COMPRESSION_ALIST compress_suffixes[] = { { ".Z", "uncompress" }, { ".Y", "unyabba" }, { ".z", "gunzip" }, { ".gz", "gunzip" }, { (char *)NULL, (char *)NULL }};/* The path on which we look for info files. You can initialize this from the environment variable INFOPATH if there is one, or you can call info_add_path () to add paths to the beginning or end of it. You can call zap_infopath () to make the path go away. */char *infopath = (char *)NULL;static int infopath_size = 0;/* Expand the filename in PARTIAL to make a real name for this operating system. This looks in INFO_PATHS in order to find the correct file. If it can't find the file, it returns NULL. */static char *local_temp_filename = (char *)NULL;static int local_temp_filename_size = 0;char *info_find_fullpath (partial) char *partial;{ int initial_character; char *temp; filesys_error_number = 0; maybe_initialize_infopath (); if (partial && (initial_character = *partial)) { char *expansion; expansion = lookup_info_filename (partial); if (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. */ if (initial_character == '/') 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; } 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));#if defined (HAVE_GETCWD) if (!getcwd (local_temp_filename, local_temp_filename_size))#else /* !HAVE_GETCWD */ 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); } else 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); } } return (partial);}/* Scan the list of directories in PATH looking for FILENAME. If we find one that is a regular file, return it as a new string. Otherwise, return a NULL pointer. */static char *info_file_in_path (filename, path) char *filename, *path;{ struct stat finfo; char *temp_dirname; int statable, dirname_index; dirname_index = 0; 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; 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, filename); pre_suffix_length = strlen (temp); 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 = 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);}/* Given a string containing units of information separated by colons, return the next one pointed to by IDX, or NULL if there are no more. Advance IDX to the character after the colon. */char *extract_colon_unit (string, idx) char *string; int *idx;{ register int i, start; i = start = *idx; if ((i >= strlen (string)) || !string) return ((char *) NULL); while (string[i] && string[i] != ':') i++; if (i == start) { return ((char *) NULL); } else { char *value; value = (char *) xmalloc (1 + (i - start)); strncpy (value, &string[start], (i - start)); value[i - start] = '\0'; if (string[i]) ++i; *idx = i; return (value); }}/* A structure which associates a filename with its expansion. */typedef struct { char *filename; char *expansion;} FILENAME_LIST;/* An array of remembered arguments and results. */static FILENAME_LIST **names_and_files = (FILENAME_LIST **)NULL;static int names_and_files_index = 0;static int names_and_files_slots = 0;/* Find the result for having already called info_find_fullpath () with FILENAME. */static char *lookup_info_filename (filename) char *filename;{ if (filename && names_and_files) { 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); } } return (char *)NULL;;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -