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