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