filename.c revision 1.1.1.1 1 1.1 tron /* $NetBSD */
2 1.1 tron
3 1.1 tron /*
4 1.1 tron * Copyright (C) 1984-2011 Mark Nudelman
5 1.1 tron *
6 1.1 tron * You may distribute under the terms of either the GNU General Public
7 1.1 tron * License or the Less License, as specified in the README file.
8 1.1 tron *
9 1.1 tron * For more information about less, or for information on how to
10 1.1 tron * contact the author, see the README file.
11 1.1 tron */
12 1.1 tron
13 1.1 tron
14 1.1 tron /*
15 1.1 tron * Routines to mess around with filenames (and files).
16 1.1 tron * Much of this is very OS dependent.
17 1.1 tron */
18 1.1 tron
19 1.1 tron #include "less.h"
20 1.1 tron #include "lglob.h"
21 1.1 tron #if MSDOS_COMPILER
22 1.1 tron #include <dos.h>
23 1.1 tron #if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER)
24 1.1 tron #include <dir.h>
25 1.1 tron #endif
26 1.1 tron #if MSDOS_COMPILER==DJGPPC
27 1.1 tron #include <glob.h>
28 1.1 tron #include <dir.h>
29 1.1 tron #define _MAX_PATH PATH_MAX
30 1.1 tron #endif
31 1.1 tron #endif
32 1.1 tron #ifdef _OSK
33 1.1 tron #include <rbf.h>
34 1.1 tron #ifndef _OSK_MWC32
35 1.1 tron #include <modes.h>
36 1.1 tron #endif
37 1.1 tron #endif
38 1.1 tron #if OS2
39 1.1 tron #include <signal.h>
40 1.1 tron #endif
41 1.1 tron
42 1.1 tron #if HAVE_STAT
43 1.1 tron #include <sys/stat.h>
44 1.1 tron #ifndef S_ISDIR
45 1.1 tron #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
46 1.1 tron #endif
47 1.1 tron #ifndef S_ISREG
48 1.1 tron #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
49 1.1 tron #endif
50 1.1 tron #endif
51 1.1 tron
52 1.1 tron
53 1.1 tron extern int force_open;
54 1.1 tron extern int secure;
55 1.1 tron extern int use_lessopen;
56 1.1 tron extern int ctldisp;
57 1.1 tron extern int utf_mode;
58 1.1 tron extern IFILE curr_ifile;
59 1.1 tron extern IFILE old_ifile;
60 1.1 tron #if SPACES_IN_FILENAMES
61 1.1 tron extern char openquote;
62 1.1 tron extern char closequote;
63 1.1 tron #endif
64 1.1 tron
65 1.1 tron /*
66 1.1 tron * Remove quotes around a filename.
67 1.1 tron */
68 1.1 tron public char *
69 1.1 tron shell_unquote(str)
70 1.1 tron char *str;
71 1.1 tron {
72 1.1 tron char *name;
73 1.1 tron char *p;
74 1.1 tron
75 1.1 tron name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
76 1.1 tron if (*str == openquote)
77 1.1 tron {
78 1.1 tron str++;
79 1.1 tron while (*str != '\0')
80 1.1 tron {
81 1.1 tron if (*str == closequote)
82 1.1 tron {
83 1.1 tron if (str[1] != closequote)
84 1.1 tron break;
85 1.1 tron str++;
86 1.1 tron }
87 1.1 tron *p++ = *str++;
88 1.1 tron }
89 1.1 tron } else
90 1.1 tron {
91 1.1 tron char *esc = get_meta_escape();
92 1.1 tron int esclen = strlen(esc);
93 1.1 tron while (*str != '\0')
94 1.1 tron {
95 1.1 tron if (esclen > 0 && strncmp(str, esc, esclen) == 0)
96 1.1 tron str += esclen;
97 1.1 tron *p++ = *str++;
98 1.1 tron }
99 1.1 tron }
100 1.1 tron *p = '\0';
101 1.1 tron return (name);
102 1.1 tron }
103 1.1 tron
104 1.1 tron /*
105 1.1 tron * Get the shell's escape character.
106 1.1 tron */
107 1.1 tron public char *
108 1.1 tron get_meta_escape()
109 1.1 tron {
110 1.1 tron char *s;
111 1.1 tron
112 1.1 tron s = lgetenv("LESSMETAESCAPE");
113 1.1 tron if (s == NULL)
114 1.1 tron s = DEF_METAESCAPE;
115 1.1 tron return (s);
116 1.1 tron }
117 1.1 tron
118 1.1 tron /*
119 1.1 tron * Get the characters which the shell considers to be "metacharacters".
120 1.1 tron */
121 1.1 tron static char *
122 1.1 tron metachars()
123 1.1 tron {
124 1.1 tron static char *mchars = NULL;
125 1.1 tron
126 1.1 tron if (mchars == NULL)
127 1.1 tron {
128 1.1 tron mchars = lgetenv("LESSMETACHARS");
129 1.1 tron if (mchars == NULL)
130 1.1 tron mchars = DEF_METACHARS;
131 1.1 tron }
132 1.1 tron return (mchars);
133 1.1 tron }
134 1.1 tron
135 1.1 tron /*
136 1.1 tron * Is this a shell metacharacter?
137 1.1 tron */
138 1.1 tron static int
139 1.1 tron metachar(c)
140 1.1 tron char c;
141 1.1 tron {
142 1.1 tron return (strchr(metachars(), c) != NULL);
143 1.1 tron }
144 1.1 tron
145 1.1 tron /*
146 1.1 tron * Insert a backslash before each metacharacter in a string.
147 1.1 tron */
148 1.1 tron public char *
149 1.1 tron shell_quote(s)
150 1.1 tron char *s;
151 1.1 tron {
152 1.1 tron char *p;
153 1.1 tron char *newstr;
154 1.1 tron int len;
155 1.1 tron char *esc = get_meta_escape();
156 1.1 tron int esclen = strlen(esc);
157 1.1 tron int use_quotes = 0;
158 1.1 tron int have_quotes = 0;
159 1.1 tron
160 1.1 tron /*
161 1.1 tron * Determine how big a string we need to allocate.
162 1.1 tron */
163 1.1 tron len = 1; /* Trailing null byte */
164 1.1 tron for (p = s; *p != '\0'; p++)
165 1.1 tron {
166 1.1 tron len++;
167 1.1 tron if (*p == openquote || *p == closequote)
168 1.1 tron have_quotes = 1;
169 1.1 tron if (metachar(*p))
170 1.1 tron {
171 1.1 tron if (esclen == 0)
172 1.1 tron {
173 1.1 tron /*
174 1.1 tron * We've got a metachar, but this shell
175 1.1 tron * doesn't support escape chars. Use quotes.
176 1.1 tron */
177 1.1 tron use_quotes = 1;
178 1.1 tron } else
179 1.1 tron {
180 1.1 tron /*
181 1.1 tron * Allow space for the escape char.
182 1.1 tron */
183 1.1 tron len += esclen;
184 1.1 tron }
185 1.1 tron }
186 1.1 tron }
187 1.1 tron if (use_quotes)
188 1.1 tron {
189 1.1 tron if (have_quotes)
190 1.1 tron /*
191 1.1 tron * We can't quote a string that contains quotes.
192 1.1 tron */
193 1.1 tron return (NULL);
194 1.1 tron len = strlen(s) + 3;
195 1.1 tron }
196 1.1 tron /*
197 1.1 tron * Allocate and construct the new string.
198 1.1 tron */
199 1.1 tron newstr = p = (char *) ecalloc(len, sizeof(char));
200 1.1 tron if (use_quotes)
201 1.1 tron {
202 1.1 tron SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
203 1.1 tron } else
204 1.1 tron {
205 1.1 tron while (*s != '\0')
206 1.1 tron {
207 1.1 tron if (metachar(*s))
208 1.1 tron {
209 1.1 tron /*
210 1.1 tron * Add the escape char.
211 1.1 tron */
212 1.1 tron strcpy(p, esc);
213 1.1 tron p += esclen;
214 1.1 tron }
215 1.1 tron *p++ = *s++;
216 1.1 tron }
217 1.1 tron *p = '\0';
218 1.1 tron }
219 1.1 tron return (newstr);
220 1.1 tron }
221 1.1 tron
222 1.1 tron /*
223 1.1 tron * Return a pathname that points to a specified file in a specified directory.
224 1.1 tron * Return NULL if the file does not exist in the directory.
225 1.1 tron */
226 1.1 tron static char *
227 1.1 tron dirfile(dirname, filename)
228 1.1 tron char *dirname;
229 1.1 tron char *filename;
230 1.1 tron {
231 1.1 tron char *pathname;
232 1.1 tron char *qpathname;
233 1.1 tron int len;
234 1.1 tron int f;
235 1.1 tron
236 1.1 tron if (dirname == NULL || *dirname == '\0')
237 1.1 tron return (NULL);
238 1.1 tron /*
239 1.1 tron * Construct the full pathname.
240 1.1 tron */
241 1.1 tron len= strlen(dirname) + strlen(filename) + 2;
242 1.1 tron pathname = (char *) calloc(len, sizeof(char));
243 1.1 tron if (pathname == NULL)
244 1.1 tron return (NULL);
245 1.1 tron SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
246 1.1 tron /*
247 1.1 tron * Make sure the file exists.
248 1.1 tron */
249 1.1 tron qpathname = shell_unquote(pathname);
250 1.1 tron f = open(qpathname, OPEN_READ);
251 1.1 tron if (f < 0)
252 1.1 tron {
253 1.1 tron free(pathname);
254 1.1 tron pathname = NULL;
255 1.1 tron } else
256 1.1 tron {
257 1.1 tron close(f);
258 1.1 tron }
259 1.1 tron free(qpathname);
260 1.1 tron return (pathname);
261 1.1 tron }
262 1.1 tron
263 1.1 tron /*
264 1.1 tron * Return the full pathname of the given file in the "home directory".
265 1.1 tron */
266 1.1 tron public char *
267 1.1 tron homefile(filename)
268 1.1 tron char *filename;
269 1.1 tron {
270 1.1 tron register char *pathname;
271 1.1 tron
272 1.1 tron /*
273 1.1 tron * Try $HOME/filename.
274 1.1 tron */
275 1.1 tron pathname = dirfile(lgetenv("HOME"), filename);
276 1.1 tron if (pathname != NULL)
277 1.1 tron return (pathname);
278 1.1 tron #if OS2
279 1.1 tron /*
280 1.1 tron * Try $INIT/filename.
281 1.1 tron */
282 1.1 tron pathname = dirfile(lgetenv("INIT"), filename);
283 1.1 tron if (pathname != NULL)
284 1.1 tron return (pathname);
285 1.1 tron #endif
286 1.1 tron #if MSDOS_COMPILER || OS2
287 1.1 tron /*
288 1.1 tron * Look for the file anywhere on search path.
289 1.1 tron */
290 1.1 tron pathname = (char *) calloc(_MAX_PATH, sizeof(char));
291 1.1 tron #if MSDOS_COMPILER==DJGPPC
292 1.1 tron {
293 1.1 tron char *res = searchpath(filename);
294 1.1 tron if (res == 0)
295 1.1 tron *pathname = '\0';
296 1.1 tron else
297 1.1 tron strcpy(pathname, res);
298 1.1 tron }
299 1.1 tron #else
300 1.1 tron _searchenv(filename, "PATH", pathname);
301 1.1 tron #endif
302 1.1 tron if (*pathname != '\0')
303 1.1 tron return (pathname);
304 1.1 tron free(pathname);
305 1.1 tron #endif
306 1.1 tron return (NULL);
307 1.1 tron }
308 1.1 tron
309 1.1 tron /*
310 1.1 tron * Expand a string, substituting any "%" with the current filename,
311 1.1 tron * and any "#" with the previous filename.
312 1.1 tron * But a string of N "%"s is just replaced with N-1 "%"s.
313 1.1 tron * Likewise for a string of N "#"s.
314 1.1 tron * {{ This is a lot of work just to support % and #. }}
315 1.1 tron */
316 1.1 tron public char *
317 1.1 tron fexpand(s)
318 1.1 tron char *s;
319 1.1 tron {
320 1.1 tron register char *fr, *to;
321 1.1 tron register int n;
322 1.1 tron register char *e;
323 1.1 tron IFILE ifile;
324 1.1 tron
325 1.1 tron #define fchar_ifile(c) \
326 1.1 tron ((c) == '%' ? curr_ifile : \
327 1.1 tron (c) == '#' ? old_ifile : NULL_IFILE)
328 1.1 tron
329 1.1 tron /*
330 1.1 tron * Make one pass to see how big a buffer we
331 1.1 tron * need to allocate for the expanded string.
332 1.1 tron */
333 1.1 tron n = 0;
334 1.1 tron for (fr = s; *fr != '\0'; fr++)
335 1.1 tron {
336 1.1 tron switch (*fr)
337 1.1 tron {
338 1.1 tron case '%':
339 1.1 tron case '#':
340 1.1 tron if (fr > s && fr[-1] == *fr)
341 1.1 tron {
342 1.1 tron /*
343 1.1 tron * Second (or later) char in a string
344 1.1 tron * of identical chars. Treat as normal.
345 1.1 tron */
346 1.1 tron n++;
347 1.1 tron } else if (fr[1] != *fr)
348 1.1 tron {
349 1.1 tron /*
350 1.1 tron * Single char (not repeated). Treat specially.
351 1.1 tron */
352 1.1 tron ifile = fchar_ifile(*fr);
353 1.1 tron if (ifile == NULL_IFILE)
354 1.1 tron n++;
355 1.1 tron else
356 1.1 tron n += strlen(get_filename(ifile));
357 1.1 tron }
358 1.1 tron /*
359 1.1 tron * Else it is the first char in a string of
360 1.1 tron * identical chars. Just discard it.
361 1.1 tron */
362 1.1 tron break;
363 1.1 tron default:
364 1.1 tron n++;
365 1.1 tron break;
366 1.1 tron }
367 1.1 tron }
368 1.1 tron
369 1.1 tron e = (char *) ecalloc(n+1, sizeof(char));
370 1.1 tron
371 1.1 tron /*
372 1.1 tron * Now copy the string, expanding any "%" or "#".
373 1.1 tron */
374 1.1 tron to = e;
375 1.1 tron for (fr = s; *fr != '\0'; fr++)
376 1.1 tron {
377 1.1 tron switch (*fr)
378 1.1 tron {
379 1.1 tron case '%':
380 1.1 tron case '#':
381 1.1 tron if (fr > s && fr[-1] == *fr)
382 1.1 tron {
383 1.1 tron *to++ = *fr;
384 1.1 tron } else if (fr[1] != *fr)
385 1.1 tron {
386 1.1 tron ifile = fchar_ifile(*fr);
387 1.1 tron if (ifile == NULL_IFILE)
388 1.1 tron *to++ = *fr;
389 1.1 tron else
390 1.1 tron {
391 1.1 tron strcpy(to, get_filename(ifile));
392 1.1 tron to += strlen(to);
393 1.1 tron }
394 1.1 tron }
395 1.1 tron break;
396 1.1 tron default:
397 1.1 tron *to++ = *fr;
398 1.1 tron break;
399 1.1 tron }
400 1.1 tron }
401 1.1 tron *to = '\0';
402 1.1 tron return (e);
403 1.1 tron }
404 1.1 tron
405 1.1 tron
406 1.1 tron #if TAB_COMPLETE_FILENAME
407 1.1 tron
408 1.1 tron /*
409 1.1 tron * Return a blank-separated list of filenames which "complete"
410 1.1 tron * the given string.
411 1.1 tron */
412 1.1 tron public char *
413 1.1 tron fcomplete(s)
414 1.1 tron char *s;
415 1.1 tron {
416 1.1 tron char *fpat;
417 1.1 tron char *qs;
418 1.1 tron
419 1.1 tron if (secure)
420 1.1 tron return (NULL);
421 1.1 tron /*
422 1.1 tron * Complete the filename "s" by globbing "s*".
423 1.1 tron */
424 1.1 tron #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
425 1.1 tron /*
426 1.1 tron * But in DOS, we have to glob "s*.*".
427 1.1 tron * But if the final component of the filename already has
428 1.1 tron * a dot in it, just do "s*".
429 1.1 tron * (Thus, "FILE" is globbed as "FILE*.*",
430 1.1 tron * but "FILE.A" is globbed as "FILE.A*").
431 1.1 tron */
432 1.1 tron {
433 1.1 tron char *slash;
434 1.1 tron int len;
435 1.1 tron for (slash = s+strlen(s)-1; slash > s; slash--)
436 1.1 tron if (*slash == *PATHNAME_SEP || *slash == '/')
437 1.1 tron break;
438 1.1 tron len = strlen(s) + 4;
439 1.1 tron fpat = (char *) ecalloc(len, sizeof(char));
440 1.1 tron if (strchr(slash, '.') == NULL)
441 1.1 tron SNPRINTF1(fpat, len, "%s*.*", s);
442 1.1 tron else
443 1.1 tron SNPRINTF1(fpat, len, "%s*", s);
444 1.1 tron }
445 1.1 tron #else
446 1.1 tron {
447 1.1 tron int len = strlen(s) + 2;
448 1.1 tron fpat = (char *) ecalloc(len, sizeof(char));
449 1.1 tron SNPRINTF1(fpat, len, "%s*", s);
450 1.1 tron }
451 1.1 tron #endif
452 1.1 tron qs = lglob(fpat);
453 1.1 tron s = shell_unquote(qs);
454 1.1 tron if (strcmp(s,fpat) == 0)
455 1.1 tron {
456 1.1 tron /*
457 1.1 tron * The filename didn't expand.
458 1.1 tron */
459 1.1 tron free(qs);
460 1.1 tron qs = NULL;
461 1.1 tron }
462 1.1 tron free(s);
463 1.1 tron free(fpat);
464 1.1 tron return (qs);
465 1.1 tron }
466 1.1 tron #endif
467 1.1 tron
468 1.1 tron /*
469 1.1 tron * Try to determine if a file is "binary".
470 1.1 tron * This is just a guess, and we need not try too hard to make it accurate.
471 1.1 tron */
472 1.1 tron public int
473 1.1 tron bin_file(f)
474 1.1 tron int f;
475 1.1 tron {
476 1.1 tron int n;
477 1.1 tron int bin_count = 0;
478 1.1 tron char data[256];
479 1.1 tron char* p;
480 1.1 tron char* pend;
481 1.1 tron
482 1.1 tron if (!seekable(f))
483 1.1 tron return (0);
484 1.1 tron if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
485 1.1 tron return (0);
486 1.1 tron n = read(f, data, sizeof(data));
487 1.1 tron pend = &data[n];
488 1.1 tron for (p = data; p < pend; )
489 1.1 tron {
490 1.1 tron LWCHAR c = step_char(&p, +1, pend);
491 1.1 tron if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
492 1.1 tron {
493 1.1 tron do {
494 1.1 tron c = step_char(&p, +1, pend);
495 1.1 tron } while (p < pend && is_ansi_middle(c));
496 1.1 tron } else if (binary_char(c))
497 1.1 tron bin_count++;
498 1.1 tron }
499 1.1 tron /*
500 1.1 tron * Call it a binary file if there are more than 5 binary characters
501 1.1 tron * in the first 256 bytes of the file.
502 1.1 tron */
503 1.1 tron return (bin_count > 5);
504 1.1 tron }
505 1.1 tron
506 1.1 tron /*
507 1.1 tron * Try to determine the size of a file by seeking to the end.
508 1.1 tron */
509 1.1 tron static POSITION
510 1.1 tron seek_filesize(f)
511 1.1 tron int f;
512 1.1 tron {
513 1.1 tron off_t spos;
514 1.1 tron
515 1.1 tron spos = lseek(f, (off_t)0, SEEK_END);
516 1.1 tron if (spos == BAD_LSEEK)
517 1.1 tron return (NULL_POSITION);
518 1.1 tron return ((POSITION) spos);
519 1.1 tron }
520 1.1 tron
521 1.1 tron /*
522 1.1 tron * Read a string from a file.
523 1.1 tron * Return a pointer to the string in memory.
524 1.1 tron */
525 1.1 tron static char *
526 1.1 tron readfd(fd)
527 1.1 tron FILE *fd;
528 1.1 tron {
529 1.1 tron int len;
530 1.1 tron int ch;
531 1.1 tron char *buf;
532 1.1 tron char *p;
533 1.1 tron
534 1.1 tron /*
535 1.1 tron * Make a guess about how many chars in the string
536 1.1 tron * and allocate a buffer to hold it.
537 1.1 tron */
538 1.1 tron len = 100;
539 1.1 tron buf = (char *) ecalloc(len, sizeof(char));
540 1.1 tron for (p = buf; ; p++)
541 1.1 tron {
542 1.1 tron if ((ch = getc(fd)) == '\n' || ch == EOF)
543 1.1 tron break;
544 1.1 tron if (p - buf >= len-1)
545 1.1 tron {
546 1.1 tron /*
547 1.1 tron * The string is too big to fit in the buffer we have.
548 1.1 tron * Allocate a new buffer, twice as big.
549 1.1 tron */
550 1.1 tron len *= 2;
551 1.1 tron *p = '\0';
552 1.1 tron p = (char *) ecalloc(len, sizeof(char));
553 1.1 tron strcpy(p, buf);
554 1.1 tron free(buf);
555 1.1 tron buf = p;
556 1.1 tron p = buf + strlen(buf);
557 1.1 tron }
558 1.1 tron *p = ch;
559 1.1 tron }
560 1.1 tron *p = '\0';
561 1.1 tron return (buf);
562 1.1 tron }
563 1.1 tron
564 1.1 tron
565 1.1 tron
566 1.1 tron #if HAVE_POPEN
567 1.1 tron
568 1.1 tron FILE *popen();
569 1.1 tron
570 1.1 tron /*
571 1.1 tron * Execute a shell command.
572 1.1 tron * Return a pointer to a pipe connected to the shell command's standard output.
573 1.1 tron */
574 1.1 tron static FILE *
575 1.1 tron shellcmd(cmd)
576 1.1 tron char *cmd;
577 1.1 tron {
578 1.1 tron FILE *fd;
579 1.1 tron
580 1.1 tron #if HAVE_SHELL
581 1.1 tron char *shell;
582 1.1 tron
583 1.1 tron shell = lgetenv("SHELL");
584 1.1 tron if (shell != NULL && *shell != '\0')
585 1.1 tron {
586 1.1 tron char *scmd;
587 1.1 tron char *esccmd;
588 1.1 tron
589 1.1 tron /*
590 1.1 tron * Read the output of <$SHELL -c cmd>.
591 1.1 tron * Escape any metacharacters in the command.
592 1.1 tron */
593 1.1 tron esccmd = shell_quote(cmd);
594 1.1 tron if (esccmd == NULL)
595 1.1 tron {
596 1.1 tron fd = popen(cmd, "r");
597 1.1 tron } else
598 1.1 tron {
599 1.1 tron int len = strlen(shell) + strlen(esccmd) + 5;
600 1.1 tron scmd = (char *) ecalloc(len, sizeof(char));
601 1.1 tron SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
602 1.1 tron free(esccmd);
603 1.1 tron fd = popen(scmd, "r");
604 1.1 tron free(scmd);
605 1.1 tron }
606 1.1 tron } else
607 1.1 tron #endif
608 1.1 tron {
609 1.1 tron fd = popen(cmd, "r");
610 1.1 tron }
611 1.1 tron /*
612 1.1 tron * Redirection in `popen' might have messed with the
613 1.1 tron * standard devices. Restore binary input mode.
614 1.1 tron */
615 1.1 tron SET_BINARY(0);
616 1.1 tron return (fd);
617 1.1 tron }
618 1.1 tron
619 1.1 tron #endif /* HAVE_POPEN */
620 1.1 tron
621 1.1 tron
622 1.1 tron /*
623 1.1 tron * Expand a filename, doing any system-specific metacharacter substitutions.
624 1.1 tron */
625 1.1 tron public char *
626 1.1 tron lglob(filename)
627 1.1 tron char *filename;
628 1.1 tron {
629 1.1 tron char *gfilename;
630 1.1 tron char *ofilename;
631 1.1 tron
632 1.1 tron ofilename = fexpand(filename);
633 1.1 tron if (secure)
634 1.1 tron return (ofilename);
635 1.1 tron filename = shell_unquote(ofilename);
636 1.1 tron
637 1.1 tron #ifdef DECL_GLOB_LIST
638 1.1 tron {
639 1.1 tron /*
640 1.1 tron * The globbing function returns a list of names.
641 1.1 tron */
642 1.1 tron int length;
643 1.1 tron char *p;
644 1.1 tron char *qfilename;
645 1.1 tron DECL_GLOB_LIST(list)
646 1.1 tron
647 1.1 tron GLOB_LIST(filename, list);
648 1.1 tron if (GLOB_LIST_FAILED(list))
649 1.1 tron {
650 1.1 tron free(filename);
651 1.1 tron return (ofilename);
652 1.1 tron }
653 1.1 tron length = 1; /* Room for trailing null byte */
654 1.1 tron for (SCAN_GLOB_LIST(list, p))
655 1.1 tron {
656 1.1 tron INIT_GLOB_LIST(list, p);
657 1.1 tron qfilename = shell_quote(p);
658 1.1 tron if (qfilename != NULL)
659 1.1 tron {
660 1.1 tron length += strlen(qfilename) + 1;
661 1.1 tron free(qfilename);
662 1.1 tron }
663 1.1 tron }
664 1.1 tron gfilename = (char *) ecalloc(length, sizeof(char));
665 1.1 tron for (SCAN_GLOB_LIST(list, p))
666 1.1 tron {
667 1.1 tron INIT_GLOB_LIST(list, p);
668 1.1 tron qfilename = shell_quote(p);
669 1.1 tron if (qfilename != NULL)
670 1.1 tron {
671 1.1 tron sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
672 1.1 tron free(qfilename);
673 1.1 tron }
674 1.1 tron }
675 1.1 tron /*
676 1.1 tron * Overwrite the final trailing space with a null terminator.
677 1.1 tron */
678 1.1 tron *--p = '\0';
679 1.1 tron GLOB_LIST_DONE(list);
680 1.1 tron }
681 1.1 tron #else
682 1.1 tron #ifdef DECL_GLOB_NAME
683 1.1 tron {
684 1.1 tron /*
685 1.1 tron * The globbing function returns a single name, and
686 1.1 tron * is called multiple times to walk thru all names.
687 1.1 tron */
688 1.1 tron register char *p;
689 1.1 tron register int len;
690 1.1 tron register int n;
691 1.1 tron char *pathname;
692 1.1 tron char *qpathname;
693 1.1 tron DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
694 1.1 tron
695 1.1 tron GLOB_FIRST_NAME(filename, &fnd, handle);
696 1.1 tron if (GLOB_FIRST_FAILED(handle))
697 1.1 tron {
698 1.1 tron free(filename);
699 1.1 tron return (ofilename);
700 1.1 tron }
701 1.1 tron
702 1.1 tron _splitpath(filename, drive, dir, fname, ext);
703 1.1 tron len = 100;
704 1.1 tron gfilename = (char *) ecalloc(len, sizeof(char));
705 1.1 tron p = gfilename;
706 1.1 tron do {
707 1.1 tron n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
708 1.1 tron pathname = (char *) ecalloc(n, sizeof(char));
709 1.1 tron SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
710 1.1 tron qpathname = shell_quote(pathname);
711 1.1 tron free(pathname);
712 1.1 tron if (qpathname != NULL)
713 1.1 tron {
714 1.1 tron n = strlen(qpathname);
715 1.1 tron while (p - gfilename + n + 2 >= len)
716 1.1 tron {
717 1.1 tron /*
718 1.1 tron * No room in current buffer.
719 1.1 tron * Allocate a bigger one.
720 1.1 tron */
721 1.1 tron len *= 2;
722 1.1 tron *p = '\0';
723 1.1 tron p = (char *) ecalloc(len, sizeof(char));
724 1.1 tron strcpy(p, gfilename);
725 1.1 tron free(gfilename);
726 1.1 tron gfilename = p;
727 1.1 tron p = gfilename + strlen(gfilename);
728 1.1 tron }
729 1.1 tron strcpy(p, qpathname);
730 1.1 tron free(qpathname);
731 1.1 tron p += n;
732 1.1 tron *p++ = ' ';
733 1.1 tron }
734 1.1 tron } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
735 1.1 tron
736 1.1 tron /*
737 1.1 tron * Overwrite the final trailing space with a null terminator.
738 1.1 tron */
739 1.1 tron *--p = '\0';
740 1.1 tron GLOB_NAME_DONE(handle);
741 1.1 tron }
742 1.1 tron #else
743 1.1 tron #if HAVE_POPEN
744 1.1 tron {
745 1.1 tron /*
746 1.1 tron * We get the shell to glob the filename for us by passing
747 1.1 tron * an "echo" command to the shell and reading its output.
748 1.1 tron */
749 1.1 tron FILE *fd;
750 1.1 tron char *s;
751 1.1 tron char *lessecho;
752 1.1 tron char *cmd;
753 1.1 tron char *esc;
754 1.1 tron int len;
755 1.1 tron
756 1.1 tron esc = get_meta_escape();
757 1.1 tron if (strlen(esc) == 0)
758 1.1 tron esc = "-";
759 1.1 tron esc = shell_quote(esc);
760 1.1 tron if (esc == NULL)
761 1.1 tron {
762 1.1 tron free(filename);
763 1.1 tron return (ofilename);
764 1.1 tron }
765 1.1 tron lessecho = lgetenv("LESSECHO");
766 1.1 tron if (lessecho == NULL || *lessecho == '\0')
767 1.1 tron lessecho = "lessecho";
768 1.1 tron /*
769 1.1 tron * Invoke lessecho, and read its output (a globbed list of filenames).
770 1.1 tron */
771 1.1 tron len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24;
772 1.1 tron cmd = (char *) ecalloc(len, sizeof(char));
773 1.1 tron SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
774 1.1 tron free(esc);
775 1.1 tron for (s = metachars(); *s != '\0'; s++)
776 1.1 tron sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
777 1.1 tron sprintf(cmd + strlen(cmd), "-- %s", ofilename);
778 1.1 tron fd = shellcmd(cmd);
779 1.1 tron free(cmd);
780 1.1 tron if (fd == NULL)
781 1.1 tron {
782 1.1 tron /*
783 1.1 tron * Cannot create the pipe.
784 1.1 tron * Just return the original (fexpanded) filename.
785 1.1 tron */
786 1.1 tron free(filename);
787 1.1 tron return (ofilename);
788 1.1 tron }
789 1.1 tron gfilename = readfd(fd);
790 1.1 tron pclose(fd);
791 1.1 tron if (*gfilename == '\0')
792 1.1 tron {
793 1.1 tron free(gfilename);
794 1.1 tron free(filename);
795 1.1 tron return (ofilename);
796 1.1 tron }
797 1.1 tron }
798 1.1 tron #else
799 1.1 tron /*
800 1.1 tron * No globbing functions at all. Just use the fexpanded filename.
801 1.1 tron */
802 1.1 tron gfilename = save(filename);
803 1.1 tron #endif
804 1.1 tron #endif
805 1.1 tron #endif
806 1.1 tron free(filename);
807 1.1 tron free(ofilename);
808 1.1 tron return (gfilename);
809 1.1 tron }
810 1.1 tron
811 1.1 tron /*
812 1.1 tron * See if we should open a "replacement file"
813 1.1 tron * instead of the file we're about to open.
814 1.1 tron */
815 1.1 tron public char *
816 1.1 tron open_altfile(filename, pf, pfd)
817 1.1 tron char *filename;
818 1.1 tron int *pf;
819 1.1 tron void **pfd;
820 1.1 tron {
821 1.1 tron #if !HAVE_POPEN
822 1.1 tron return (NULL);
823 1.1 tron #else
824 1.1 tron char *lessopen;
825 1.1 tron char *cmd;
826 1.1 tron int len;
827 1.1 tron FILE *fd;
828 1.1 tron #if HAVE_FILENO
829 1.1 tron int returnfd = 0;
830 1.1 tron #endif
831 1.1 tron
832 1.1 tron if (!use_lessopen || secure)
833 1.1 tron return (NULL);
834 1.1 tron ch_ungetchar(-1);
835 1.1 tron if ((lessopen = lgetenv("LESSOPEN")) == NULL)
836 1.1 tron return (NULL);
837 1.1 tron if (*lessopen == '|')
838 1.1 tron {
839 1.1 tron /*
840 1.1 tron * If LESSOPEN starts with a |, it indicates
841 1.1 tron * a "pipe preprocessor".
842 1.1 tron */
843 1.1 tron #if !HAVE_FILENO
844 1.1 tron error("LESSOPEN pipe is not supported", NULL_PARG);
845 1.1 tron return (NULL);
846 1.1 tron #else
847 1.1 tron lessopen++;
848 1.1 tron returnfd = 1;
849 1.1 tron #endif
850 1.1 tron }
851 1.1 tron if (*lessopen == '-') {
852 1.1 tron /*
853 1.1 tron * Lessopen preprocessor will accept "-" as a filename.
854 1.1 tron */
855 1.1 tron lessopen++;
856 1.1 tron } else {
857 1.1 tron if (strcmp(filename, "-") == 0)
858 1.1 tron return (NULL);
859 1.1 tron }
860 1.1 tron
861 1.1 tron len = strlen(lessopen) + strlen(filename) + 2;
862 1.1 tron cmd = (char *) ecalloc(len, sizeof(char));
863 1.1 tron SNPRINTF1(cmd, len, lessopen, filename);
864 1.1 tron fd = shellcmd(cmd);
865 1.1 tron free(cmd);
866 1.1 tron if (fd == NULL)
867 1.1 tron {
868 1.1 tron /*
869 1.1 tron * Cannot create the pipe.
870 1.1 tron */
871 1.1 tron return (NULL);
872 1.1 tron }
873 1.1 tron #if HAVE_FILENO
874 1.1 tron if (returnfd)
875 1.1 tron {
876 1.1 tron int f;
877 1.1 tron char c;
878 1.1 tron
879 1.1 tron /*
880 1.1 tron * Read one char to see if the pipe will produce any data.
881 1.1 tron * If it does, push the char back on the pipe.
882 1.1 tron */
883 1.1 tron f = fileno(fd);
884 1.1 tron SET_BINARY(f);
885 1.1 tron if (read(f, &c, 1) != 1)
886 1.1 tron {
887 1.1 tron /*
888 1.1 tron * Pipe is empty. This means there is no alt file.
889 1.1 tron */
890 1.1 tron pclose(fd);
891 1.1 tron return (NULL);
892 1.1 tron }
893 1.1 tron ch_ungetchar(c);
894 1.1 tron *pfd = (void *) fd;
895 1.1 tron *pf = f;
896 1.1 tron return (save("-"));
897 1.1 tron }
898 1.1 tron #endif
899 1.1 tron cmd = readfd(fd);
900 1.1 tron pclose(fd);
901 1.1 tron if (*cmd == '\0')
902 1.1 tron /*
903 1.1 tron * Pipe is empty. This means there is no alt file.
904 1.1 tron */
905 1.1 tron return (NULL);
906 1.1 tron return (cmd);
907 1.1 tron #endif /* HAVE_POPEN */
908 1.1 tron }
909 1.1 tron
910 1.1 tron /*
911 1.1 tron * Close a replacement file.
912 1.1 tron */
913 1.1 tron public void
914 1.1 tron close_altfile(altfilename, filename, pipefd)
915 1.1 tron char *altfilename;
916 1.1 tron char *filename;
917 1.1 tron void *pipefd;
918 1.1 tron {
919 1.1 tron #if HAVE_POPEN
920 1.1 tron char *lessclose;
921 1.1 tron FILE *fd;
922 1.1 tron char *cmd;
923 1.1 tron int len;
924 1.1 tron
925 1.1 tron if (secure)
926 1.1 tron return;
927 1.1 tron if (pipefd != NULL)
928 1.1 tron {
929 1.1 tron #if OS2
930 1.1 tron /*
931 1.1 tron * The pclose function of OS/2 emx sometimes fails.
932 1.1 tron * Send SIGINT to the piped process before closing it.
933 1.1 tron */
934 1.1 tron kill(((FILE*)pipefd)->_pid, SIGINT);
935 1.1 tron #endif
936 1.1 tron pclose((FILE*) pipefd);
937 1.1 tron }
938 1.1 tron if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
939 1.1 tron return;
940 1.1 tron len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
941 1.1 tron cmd = (char *) ecalloc(len, sizeof(char));
942 1.1 tron SNPRINTF2(cmd, len, lessclose, filename, altfilename);
943 1.1 tron fd = shellcmd(cmd);
944 1.1 tron free(cmd);
945 1.1 tron if (fd != NULL)
946 1.1 tron pclose(fd);
947 1.1 tron #endif
948 1.1 tron }
949 1.1 tron
950 1.1 tron /*
951 1.1 tron * Is the specified file a directory?
952 1.1 tron */
953 1.1 tron public int
954 1.1 tron is_dir(filename)
955 1.1 tron char *filename;
956 1.1 tron {
957 1.1 tron int isdir = 0;
958 1.1 tron
959 1.1 tron filename = shell_unquote(filename);
960 1.1 tron #if HAVE_STAT
961 1.1 tron {
962 1.1 tron int r;
963 1.1 tron struct stat statbuf;
964 1.1 tron
965 1.1 tron r = stat(filename, &statbuf);
966 1.1 tron isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
967 1.1 tron }
968 1.1 tron #else
969 1.1 tron #ifdef _OSK
970 1.1 tron {
971 1.1 tron register int f;
972 1.1 tron
973 1.1 tron f = open(filename, S_IREAD | S_IFDIR);
974 1.1 tron if (f >= 0)
975 1.1 tron close(f);
976 1.1 tron isdir = (f >= 0);
977 1.1 tron }
978 1.1 tron #endif
979 1.1 tron #endif
980 1.1 tron free(filename);
981 1.1 tron return (isdir);
982 1.1 tron }
983 1.1 tron
984 1.1 tron /*
985 1.1 tron * Returns NULL if the file can be opened and
986 1.1 tron * is an ordinary file, otherwise an error message
987 1.1 tron * (if it cannot be opened or is a directory, etc.)
988 1.1 tron */
989 1.1 tron public char *
990 1.1 tron bad_file(filename)
991 1.1 tron char *filename;
992 1.1 tron {
993 1.1 tron register char *m = NULL;
994 1.1 tron
995 1.1 tron filename = shell_unquote(filename);
996 1.1 tron if (!force_open && is_dir(filename))
997 1.1 tron {
998 1.1 tron static char is_a_dir[] = " is a directory";
999 1.1 tron
1000 1.1 tron m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
1001 1.1 tron sizeof(char));
1002 1.1 tron strcpy(m, filename);
1003 1.1 tron strcat(m, is_a_dir);
1004 1.1 tron } else
1005 1.1 tron {
1006 1.1 tron #if HAVE_STAT
1007 1.1 tron int r;
1008 1.1 tron struct stat statbuf;
1009 1.1 tron
1010 1.1 tron r = stat(filename, &statbuf);
1011 1.1 tron if (r < 0)
1012 1.1 tron {
1013 1.1 tron m = errno_message(filename);
1014 1.1 tron } else if (force_open)
1015 1.1 tron {
1016 1.1 tron m = NULL;
1017 1.1 tron } else if (!S_ISREG(statbuf.st_mode))
1018 1.1 tron {
1019 1.1 tron static char not_reg[] = " is not a regular file (use -f to see it)";
1020 1.1 tron m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1021 1.1 tron sizeof(char));
1022 1.1 tron strcpy(m, filename);
1023 1.1 tron strcat(m, not_reg);
1024 1.1 tron }
1025 1.1 tron #endif
1026 1.1 tron }
1027 1.1 tron free(filename);
1028 1.1 tron return (m);
1029 1.1 tron }
1030 1.1 tron
1031 1.1 tron /*
1032 1.1 tron * Return the size of a file, as cheaply as possible.
1033 1.1 tron * In Unix, we can stat the file.
1034 1.1 tron */
1035 1.1 tron public POSITION
1036 1.1 tron filesize(f)
1037 1.1 tron int f;
1038 1.1 tron {
1039 1.1 tron #if HAVE_STAT
1040 1.1 tron struct stat statbuf;
1041 1.1 tron
1042 1.1 tron if (fstat(f, &statbuf) >= 0)
1043 1.1 tron return ((POSITION) statbuf.st_size);
1044 1.1 tron #else
1045 1.1 tron #ifdef _OSK
1046 1.1 tron long size;
1047 1.1 tron
1048 1.1 tron if ((size = (long) _gs_size(f)) >= 0)
1049 1.1 tron return ((POSITION) size);
1050 1.1 tron #endif
1051 1.1 tron #endif
1052 1.1 tron return (seek_filesize(f));
1053 1.1 tron }
1054 1.1 tron
1055 1.1 tron /*
1056 1.1 tron *
1057 1.1 tron */
1058 1.1 tron public char *
1059 1.1 tron shell_coption()
1060 1.1 tron {
1061 1.1 tron return ("-c");
1062 1.1 tron }
1063 1.1 tron
1064 1.1 tron /*
1065 1.1 tron * Return last component of a pathname.
1066 1.1 tron */
1067 1.1 tron public char *
1068 1.1 tron last_component(name)
1069 1.1 tron char *name;
1070 1.1 tron {
1071 1.1 tron char *slash;
1072 1.1 tron
1073 1.1 tron for (slash = name + strlen(name); slash > name; )
1074 1.1 tron {
1075 1.1 tron --slash;
1076 1.1 tron if (*slash == *PATHNAME_SEP || *slash == '/')
1077 1.1 tron return (slash + 1);
1078 1.1 tron }
1079 1.1 tron return (name);
1080 1.1 tron }
1081 1.1 tron
1082