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