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