Deleted Added
full compact
filename.c (89019) filename.c (128345)
1/*
1/*
2 * Copyright (C) 1984-2000 Mark Nudelman
2 * Copyright (C) 1984-2002 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10

--- 34 unchanged lines hidden (view full) ---

45#ifndef S_ISREG
46#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
47#endif
48#endif
49
50
51extern int force_open;
52extern int secure;
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10

--- 34 unchanged lines hidden (view full) ---

45#ifndef S_ISREG
46#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
47#endif
48#endif
49
50
51extern int force_open;
52extern int secure;
53extern int use_lessopen;
53extern IFILE curr_ifile;
54extern IFILE old_ifile;
55#if SPACES_IN_FILENAMES
56extern char openquote;
57extern char closequote;
58#endif
59
60/*
61 * Remove quotes around a filename.
62 */
63 public char *
54extern IFILE curr_ifile;
55extern IFILE old_ifile;
56#if SPACES_IN_FILENAMES
57extern char openquote;
58extern char closequote;
59#endif
60
61/*
62 * Remove quotes around a filename.
63 */
64 public char *
64unquote_file(str)
65shell_unquote(str)
65 char *str;
66{
66 char *str;
67{
67#if SPACES_IN_FILENAMES
68 char *name;
69 char *p;
70
68 char *name;
69 char *p;
70
71 if (*str != openquote)
72 return (save(str));
73 name = (char *) ecalloc(strlen(str), sizeof(char));
74 strcpy(name, str+1);
75 p = name + strlen(name) - 1;
76 if (*p == closequote)
77 *p = '\0';
71 name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
72 if (*str == openquote)
73 {
74 str++;
75 while (*str != '\0')
76 {
77 if (*str == closequote)
78 {
79 if (str[1] != closequote)
80 break;
81 str++;
82 }
83 *p++ = *str++;
84 }
85 } else
86 {
87 char *esc = get_meta_escape();
88 int esclen = strlen(esc);
89 while (*str != '\0')
90 {
91 if (esclen > 0 && strncmp(str, esc, esclen) == 0)
92 str += esclen;
93 *p++ = *str++;
94 }
95 }
96 *p = '\0';
78 return (name);
97 return (name);
79#else
80 return (save(str));
81#endif
82}
83
84/*
98}
99
100/*
101 * Get the shell's escape character.
102 */
103 public char *
104get_meta_escape()
105{
106 char *s;
107
108 s = lgetenv("LESSMETAESCAPE");
109 if (s == NULL)
110 s = DEF_METAESCAPE;
111 return (s);
112}
113
114/*
115 * Get the characters which the shell considers to be "metacharacters".
116 */
117 static char *
118metachars()
119{
120 static char *mchars = NULL;
121
122 if (mchars == NULL)
123 {
124 mchars = lgetenv("LESSMETACHARS");
125 if (mchars == NULL)
126 mchars = DEF_METACHARS;
127 }
128 return (mchars);
129}
130
131/*
132 * Is this a shell metacharacter?
133 */
134 static int
135metachar(c)
136 char c;
137{
138 return (strchr(metachars(), c) != NULL);
139}
140
141/*
142 * Insert a backslash before each metacharacter in a string.
143 */
144 public char *
145shell_quote(s)
146 char *s;
147{
148 char *p;
149 char *newstr;
150 int len;
151 char *esc = get_meta_escape();
152 int esclen = strlen(esc);
153 int use_quotes = 0;
154 int have_quotes = 0;
155
156 /*
157 * Determine how big a string we need to allocate.
158 */
159 len = 1; /* Trailing null byte */
160 for (p = s; *p != '\0'; p++)
161 {
162 len++;
163 if (*p == openquote || *p == closequote)
164 have_quotes = 1;
165 if (metachar(*p))
166 {
167 if (esclen == 0)
168 {
169 /*
170 * We've got a metachar, but this shell
171 * doesn't support escape chars. Use quotes.
172 */
173 use_quotes = 1;
174 } else
175 {
176 /*
177 * Allow space for the escape char.
178 */
179 len += esclen;
180 }
181 }
182 }
183 if (use_quotes)
184 {
185 if (have_quotes)
186 /*
187 * We can't quote a string that contains quotes.
188 */
189 return (NULL);
190 len = strlen(s) + 3;
191 }
192 /*
193 * Allocate and construct the new string.
194 */
195 newstr = p = (char *) ecalloc(len, sizeof(char));
196 if (use_quotes)
197 {
198 sprintf(newstr, "%c%s%c", openquote, s, closequote);
199 } else
200 {
201 while (*s != '\0')
202 {
203 if (metachar(*s))
204 {
205 /*
206 * Add the escape char.
207 */
208 strcpy(p, esc);
209 p += esclen;
210 }
211 *p++ = *s++;
212 }
213 *p = '\0';
214 }
215 return (newstr);
216}
217
218/*
85 * Return a pathname that points to a specified file in a specified directory.
86 * Return NULL if the file does not exist in the directory.
87 */
88 static char *
89dirfile(dirname, filename)
90 char *dirname;
91 char *filename;
92{

--- 9 unchanged lines hidden (view full) ---

102 pathname = (char *) calloc(strlen(dirname) + strlen(filename) + 2,
103 sizeof(char));
104 if (pathname == NULL)
105 return (NULL);
106 sprintf(pathname, "%s%s%s", dirname, PATHNAME_SEP, filename);
107 /*
108 * Make sure the file exists.
109 */
219 * Return a pathname that points to a specified file in a specified directory.
220 * Return NULL if the file does not exist in the directory.
221 */
222 static char *
223dirfile(dirname, filename)
224 char *dirname;
225 char *filename;
226{

--- 9 unchanged lines hidden (view full) ---

236 pathname = (char *) calloc(strlen(dirname) + strlen(filename) + 2,
237 sizeof(char));
238 if (pathname == NULL)
239 return (NULL);
240 sprintf(pathname, "%s%s%s", dirname, PATHNAME_SEP, filename);
241 /*
242 * Make sure the file exists.
243 */
110 qpathname = unquote_file(pathname);
244 qpathname = shell_unquote(pathname);
111 f = open(qpathname, OPEN_READ);
112 if (f < 0)
113 {
114 free(pathname);
115 pathname = NULL;
116 } else
117 {
118 close(f);

--- 150 unchanged lines hidden (view full) ---

269 * Return a blank-separated list of filenames which "complete"
270 * the given string.
271 */
272 public char *
273fcomplete(s)
274 char *s;
275{
276 char *fpat;
245 f = open(qpathname, OPEN_READ);
246 if (f < 0)
247 {
248 free(pathname);
249 pathname = NULL;
250 } else
251 {
252 close(f);

--- 150 unchanged lines hidden (view full) ---

403 * Return a blank-separated list of filenames which "complete"
404 * the given string.
405 */
406 public char *
407fcomplete(s)
408 char *s;
409{
410 char *fpat;
411 char *qs;
277
278 if (secure)
279 return (NULL);
280 /*
281 * Complete the filename "s" by globbing "s*".
282 */
283#if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
284 /*

--- 13 unchanged lines hidden (view full) ---

298 sprintf(fpat, "%s*.*", s);
299 else
300 sprintf(fpat, "%s*", s);
301 }
302#else
303 fpat = (char *) ecalloc(strlen(s)+2, sizeof(char));
304 sprintf(fpat, "%s*", s);
305#endif
412
413 if (secure)
414 return (NULL);
415 /*
416 * Complete the filename "s" by globbing "s*".
417 */
418#if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
419 /*

--- 13 unchanged lines hidden (view full) ---

433 sprintf(fpat, "%s*.*", s);
434 else
435 sprintf(fpat, "%s*", s);
436 }
437#else
438 fpat = (char *) ecalloc(strlen(s)+2, sizeof(char));
439 sprintf(fpat, "%s*", s);
440#endif
306 s = lglob(fpat);
441 qs = lglob(fpat);
442 s = shell_unquote(qs);
307 if (strcmp(s,fpat) == 0)
308 {
309 /*
310 * The filename didn't expand.
311 */
443 if (strcmp(s,fpat) == 0)
444 {
445 /*
446 * The filename didn't expand.
447 */
312 free(s);
313 s = NULL;
448 free(qs);
449 qs = NULL;
314 }
450 }
451 free(s);
315 free(fpat);
452 free(fpat);
316 return (s);
453 return (qs);
317}
318#endif
319
320/*
321 * Try to determine if a file is "binary".
322 * This is just a guess, and we need not try too hard to make it accurate.
323 */
324 public int

--- 68 unchanged lines hidden (view full) ---

393 p = buf + strlen(buf);
394 }
395 *p = ch;
396 }
397 *p = '\0';
398 return (buf);
399}
400
454}
455#endif
456
457/*
458 * Try to determine if a file is "binary".
459 * This is just a guess, and we need not try too hard to make it accurate.
460 */
461 public int

--- 68 unchanged lines hidden (view full) ---

530 p = buf + strlen(buf);
531 }
532 *p = ch;
533 }
534 *p = '\0';
535 return (buf);
536}
537
401#if HAVE_SHELL
402
538
403/*
404 * Get the shell's escape character.
405 */
406 static char *
407get_meta_escape()
408{
409 char *s;
410
539
411 s = lgetenv("LESSMETAESCAPE");
412 if (s == NULL)
413 s = DEF_METAESCAPE;
414 return (s);
415}
416
417/*
418 * Is this a shell metacharacter?
419 */
420 static int
421metachar(c)
422 char c;
423{
424 static char *metachars = NULL;
425
426 if (metachars == NULL)
427 {
428 metachars = lgetenv("LESSMETACHARS");
429 if (metachars == NULL)
430 metachars = DEF_METACHARS;
431 }
432 return (strchr(metachars, c) != NULL);
433}
434
435/*
436 * Insert a backslash before each metacharacter in a string.
437 */
438 public char *
439esc_metachars(s)
440 char *s;
441{
442 char *p;
443 char *newstr;
444 int len;
445 char *esc;
446 int esclen;
447
448 /*
449 * Determine how big a string we need to allocate.
450 */
451 esc = get_meta_escape();
452 esclen = strlen(esc);
453 len = 1; /* Trailing null byte */
454 for (p = s; *p != '\0'; p++)
455 {
456 len++;
457 if (metachar(*p))
458 {
459 if (*esc == '\0')
460 {
461 /*
462 * We've got a metachar, but this shell
463 * doesn't support escape chars. Give up.
464 */
465 return (NULL);
466 }
467 /*
468 * Allow space for the escape char.
469 */
470 len += esclen;
471 }
472 }
473 /*
474 * Allocate and construct the new string.
475 */
476 newstr = p = (char *) ecalloc(len, sizeof(char));
477 while (*s != '\0')
478 {
479 if (metachar(*s))
480 {
481 /*
482 * Add the escape char.
483 */
484 strcpy(p, esc);
485 p += esclen;
486 }
487 *p++ = *s++;
488 }
489 *p = '\0';
490 return (newstr);
491}
492
493#else /* HAVE_SHELL */
494
495 public char *
496esc_metachars(s)
497 char *s;
498{
499 return (save(s));
500}
501
502#endif /* HAVE_SHELL */
503
504
505#if HAVE_POPEN
506
507FILE *popen();
508
509/*
510 * Execute a shell command.
511 * Return a pointer to a pipe connected to the shell command's standard output.
512 */

--- 8 unchanged lines hidden (view full) ---

521
522 shell = lgetenv("SHELL");
523 if (shell != NULL && *shell != '\0')
524 {
525 char *scmd;
526 char *esccmd;
527
528 /*
540#if HAVE_POPEN
541
542FILE *popen();
543
544/*
545 * Execute a shell command.
546 * Return a pointer to a pipe connected to the shell command's standard output.
547 */

--- 8 unchanged lines hidden (view full) ---

556
557 shell = lgetenv("SHELL");
558 if (shell != NULL && *shell != '\0')
559 {
560 char *scmd;
561 char *esccmd;
562
563 /*
529 * Try to escape any metacharacters in the command.
530 * If we can't do that, just put the command in quotes.
531 * (But that doesn't work well if the command itself
532 * contains quotes.)
564 * Read the output of <$SHELL -c cmd>.
565 * Escape any metacharacters in the command.
533 */
566 */
534 if ((esccmd = esc_metachars(cmd)) == NULL)
567 esccmd = shell_quote(cmd);
568 if (esccmd == NULL)
535 {
569 {
536 /*
537 * Cannot escape the metacharacters, so use quotes.
538 * Read the output of <$SHELL -c "cmd">.
539 */
540 scmd = (char *) ecalloc(strlen(shell) + strlen(cmd) + 7,
541 sizeof(char));
542 sprintf(scmd, "%s -c \"%s\"", shell, cmd);
570 fd = popen(cmd, "r");
543 } else
544 {
571 } else
572 {
545 /*
546 * Read the output of <$SHELL -c cmd>.
547 * No quotes; use the escaped cmd.
548 */
549 scmd = (char *) ecalloc(strlen(shell) + strlen(esccmd) + 5,
550 sizeof(char));
573 scmd = (char *) ecalloc(strlen(shell) + strlen(esccmd) + 5,
574 sizeof(char));
551 sprintf(scmd, "%s -c %s", shell, esccmd);
575 sprintf(scmd, "%s %s %s", shell, shell_coption(), esccmd);
552 free(esccmd);
576 free(esccmd);
577 fd = popen(scmd, "r");
578 free(scmd);
553 }
579 }
554 fd = popen(scmd, "r");
555 free(scmd);
556 } else
557#endif
558 {
559 fd = popen(cmd, "r");
580 } else
581#endif
582 {
583 fd = popen(cmd, "r");
560 /*
561 * Redirection in `popen' might have messed with the
562 * standard devices. Restore binary input mode.
563 */
564 SET_BINARY(0);
565 }
584 }
585 /*
586 * Redirection in `popen' might have messed with the
587 * standard devices. Restore binary input mode.
588 */
589 SET_BINARY(0);
566 return (fd);
567}
568
569#endif /* HAVE_POPEN */
570
571
572/*
573 * Expand a filename, doing any system-specific metacharacter substitutions.
574 */
575 public char *
576lglob(filename)
577 char *filename;
578{
579 char *gfilename;
580 char *ofilename;
581
582 ofilename = fexpand(filename);
583 if (secure)
584 return (ofilename);
590 return (fd);
591}
592
593#endif /* HAVE_POPEN */
594
595
596/*
597 * Expand a filename, doing any system-specific metacharacter substitutions.
598 */
599 public char *
600lglob(filename)
601 char *filename;
602{
603 char *gfilename;
604 char *ofilename;
605
606 ofilename = fexpand(filename);
607 if (secure)
608 return (ofilename);
585 filename = unquote_file(ofilename);
609 filename = shell_unquote(ofilename);
586
587#ifdef DECL_GLOB_LIST
588{
589 /*
590 * The globbing function returns a list of names.
591 */
592 int length;
593 char *p;
610
611#ifdef DECL_GLOB_LIST
612{
613 /*
614 * The globbing function returns a list of names.
615 */
616 int length;
617 char *p;
618 char *qfilename;
594 DECL_GLOB_LIST(list)
595
596 GLOB_LIST(filename, list);
597 if (GLOB_LIST_FAILED(list))
598 {
599 free(filename);
600 return (ofilename);
601 }
602 length = 1; /* Room for trailing null byte */
603 for (SCAN_GLOB_LIST(list, p))
604 {
605 INIT_GLOB_LIST(list, p);
619 DECL_GLOB_LIST(list)
620
621 GLOB_LIST(filename, list);
622 if (GLOB_LIST_FAILED(list))
623 {
624 free(filename);
625 return (ofilename);
626 }
627 length = 1; /* Room for trailing null byte */
628 for (SCAN_GLOB_LIST(list, p))
629 {
630 INIT_GLOB_LIST(list, p);
606 length += strlen(p) + 1;
607#if SPACES_IN_FILENAMES
608 if (strchr(p, ' ') != NULL)
609 length += 2; /* Allow for quotes */
610#endif
631 qfilename = shell_quote(p);
632 if (qfilename != NULL)
633 {
634 length += strlen(qfilename) + 1;
635 free(qfilename);
636 }
611 }
612 gfilename = (char *) ecalloc(length, sizeof(char));
613 for (SCAN_GLOB_LIST(list, p))
614 {
615 INIT_GLOB_LIST(list, p);
637 }
638 gfilename = (char *) ecalloc(length, sizeof(char));
639 for (SCAN_GLOB_LIST(list, p))
640 {
641 INIT_GLOB_LIST(list, p);
616#if SPACES_IN_FILENAMES
617 if (strchr(p, ' ') != NULL)
618 sprintf(gfilename + strlen(gfilename), "%c%s%c ",
619 openquote, p, closequote);
620 else
621#endif
622 sprintf(gfilename + strlen(gfilename), "%s ", p);
642 qfilename = shell_quote(p);
643 if (qfilename != NULL)
644 {
645 sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
646 free(qfilename);
647 }
623 }
624 /*
625 * Overwrite the final trailing space with a null terminator.
626 */
627 *--p = '\0';
628 GLOB_LIST_DONE(list);
629}
630#else
631#ifdef DECL_GLOB_NAME
632{
633 /*
634 * The globbing function returns a single name, and
635 * is called multiple times to walk thru all names.
636 */
637 register char *p;
638 register int len;
639 register int n;
648 }
649 /*
650 * Overwrite the final trailing space with a null terminator.
651 */
652 *--p = '\0';
653 GLOB_LIST_DONE(list);
654}
655#else
656#ifdef DECL_GLOB_NAME
657{
658 /*
659 * The globbing function returns a single name, and
660 * is called multiple times to walk thru all names.
661 */
662 register char *p;
663 register int len;
664 register int n;
640#if SPACES_IN_FILENAMES
641 register int spaces_in_file;
642#endif
665 char *pathname;
666 char *qpathname;
643 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
644
645 GLOB_FIRST_NAME(filename, &fnd, handle);
646 if (GLOB_FIRST_FAILED(handle))
647 {
648 free(filename);
649 return (ofilename);
650 }
651
652 _splitpath(filename, drive, dir, fname, ext);
653 len = 100;
654 gfilename = (char *) ecalloc(len, sizeof(char));
655 p = gfilename;
656 do {
657 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
667 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
668
669 GLOB_FIRST_NAME(filename, &fnd, handle);
670 if (GLOB_FIRST_FAILED(handle))
671 {
672 free(filename);
673 return (ofilename);
674 }
675
676 _splitpath(filename, drive, dir, fname, ext);
677 len = 100;
678 gfilename = (char *) ecalloc(len, sizeof(char));
679 p = gfilename;
680 do {
681 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
658#if SPACES_IN_FILENAMES
659 spaces_in_file = 0;
660 if (strchr(fnd.GLOB_NAME, ' ') != NULL ||
661 strchr(filename, ' ') != NULL)
682 pathname = (char *) ecalloc(n, sizeof(char));
683 sprintf(pathname, "%s%s%s", drive, dir, fnd.GLOB_NAME);
684 qpathname = shell_quote(pathname);
685 free(pathname);
686 if (qpathname != NULL)
662 {
687 {
663 spaces_in_file = 1;
664 n += 2;
688 n = strlen(qpathname);
689 while (p - gfilename + n + 2 >= len)
690 {
691 /*
692 * No room in current buffer.
693 * Allocate a bigger one.
694 */
695 len *= 2;
696 *p = '\0';
697 p = (char *) ecalloc(len, sizeof(char));
698 strcpy(p, gfilename);
699 free(gfilename);
700 gfilename = p;
701 p = gfilename + strlen(gfilename);
702 }
703 strcpy(p, qpathname);
704 free(qpathname);
705 p += n;
706 *p++ = ' ';
665 }
707 }
666#endif
667 while (p - gfilename + n+2 >= len)
668 {
669 /*
670 * No room in current buffer. Allocate a bigger one.
671 */
672 len *= 2;
673 *p = '\0';
674 p = (char *) ecalloc(len, sizeof(char));
675 strcpy(p, gfilename);
676 free(gfilename);
677 gfilename = p;
678 p = gfilename + strlen(gfilename);
679 }
680#if SPACES_IN_FILENAMES
681 if (spaces_in_file)
682 sprintf(p, "%c%s%s%s%c ", openquote,
683 drive, dir, fnd.GLOB_NAME, closequote);
684 else
685#endif
686 sprintf(p, "%s%s%s ", drive, dir, fnd.GLOB_NAME);
687 p += n;
688 } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
689
690 /*
691 * Overwrite the final trailing space with a null terminator.
692 */
693 *--p = '\0';
694 GLOB_NAME_DONE(handle);
695}
696#else
697#if HAVE_POPEN
698{
699 /*
700 * We get the shell to glob the filename for us by passing
701 * an "echo" command to the shell and reading its output.
702 */
703 FILE *fd;
704 char *s;
705 char *lessecho;
706 char *cmd;
708 } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
709
710 /*
711 * Overwrite the final trailing space with a null terminator.
712 */
713 *--p = '\0';
714 GLOB_NAME_DONE(handle);
715}
716#else
717#if HAVE_POPEN
718{
719 /*
720 * We get the shell to glob the filename for us by passing
721 * an "echo" command to the shell and reading its output.
722 */
723 FILE *fd;
724 char *s;
725 char *lessecho;
726 char *cmd;
727 char *esc;
707
728
708 lessecho = lgetenv("LESSECHO");
709 if (lessecho == NULL || *lessecho == '\0')
710 lessecho = "lessecho";
711 s = esc_metachars(filename);
712 if (s == NULL)
729 esc = get_meta_escape();
730 if (strlen(esc) == 0)
731 esc = "-";
732 esc = shell_quote(esc);
733 if (esc == NULL)
713 {
734 {
714 /*
715 * There may be dangerous metachars in this name.
716 * We can't risk passing it to the shell.
717 * {{ For example, do "!;TAB" when the first file
718 * in the dir is named "rm". }}
719 */
720 free(filename);
721 return (ofilename);
722 }
735 free(filename);
736 return (ofilename);
737 }
738 lessecho = lgetenv("LESSECHO");
739 if (lessecho == NULL || *lessecho == '\0')
740 lessecho = "lessecho";
723 /*
724 * Invoke lessecho, and read its output (a globbed list of filenames).
725 */
741 /*
742 * Invoke lessecho, and read its output (a globbed list of filenames).
743 */
726 cmd = (char *) ecalloc(strlen(lessecho) + strlen(s) + 24, sizeof(char));
727 sprintf(cmd, "%s -p0x%x -d0x%x -- %s",
728 lessecho, openquote, closequote, s);
744 cmd = (char *) ecalloc(strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24, sizeof(char));
745 sprintf(cmd, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
746 free(esc);
747 for (s = metachars(); *s != '\0'; s++)
748 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
749 sprintf(cmd + strlen(cmd), "-- %s", ofilename);
729 fd = shellcmd(cmd);
750 fd = shellcmd(cmd);
730 free(s);
731 free(cmd);
732 if (fd == NULL)
733 {
734 /*
735 * Cannot create the pipe.
736 * Just return the original (fexpanded) filename.
737 */
738 free(filename);

--- 30 unchanged lines hidden (view full) ---

769 char *filename;
770 int *pf;
771 void **pfd;
772{
773#if !HAVE_POPEN
774 return (NULL);
775#else
776 char *lessopen;
751 free(cmd);
752 if (fd == NULL)
753 {
754 /*
755 * Cannot create the pipe.
756 * Just return the original (fexpanded) filename.
757 */
758 free(filename);

--- 30 unchanged lines hidden (view full) ---

789 char *filename;
790 int *pf;
791 void **pfd;
792{
793#if !HAVE_POPEN
794 return (NULL);
795#else
796 char *lessopen;
777 char *gfilename;
778 char *cmd;
779 FILE *fd;
780#if HAVE_FILENO
781 int returnfd = 0;
782#endif
783
797 char *cmd;
798 FILE *fd;
799#if HAVE_FILENO
800 int returnfd = 0;
801#endif
802
784 if (secure)
803 if (!use_lessopen || secure)
785 return (NULL);
786 ch_ungetchar(-1);
787 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
788 return (NULL);
789 if (strcmp(filename, "-") == 0)
790 return (NULL);
791 if (*lessopen == '|')
792 {

--- 5 unchanged lines hidden (view full) ---

798 lessopen++;
799 returnfd = 1;
800#else
801 error("LESSOPEN pipe is not supported", NULL_PARG);
802 return (NULL);
803#endif
804 }
805
804 return (NULL);
805 ch_ungetchar(-1);
806 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
807 return (NULL);
808 if (strcmp(filename, "-") == 0)
809 return (NULL);
810 if (*lessopen == '|')
811 {

--- 5 unchanged lines hidden (view full) ---

817 lessopen++;
818 returnfd = 1;
819#else
820 error("LESSOPEN pipe is not supported", NULL_PARG);
821 return (NULL);
822#endif
823 }
824
806 gfilename = esc_metachars(filename);
807 if (gfilename == NULL)
808 {
809 /*
810 * Cannot escape metacharacters.
811 */
812 return (NULL);
813 }
814 cmd = (char *) ecalloc(strlen(lessopen) + strlen(gfilename) + 2,
825 cmd = (char *) ecalloc(strlen(lessopen) + strlen(filename) + 2,
815 sizeof(char));
826 sizeof(char));
816 sprintf(cmd, lessopen, gfilename);
827 sprintf(cmd, lessopen, filename);
817 fd = shellcmd(cmd);
828 fd = shellcmd(cmd);
818 free(gfilename);
819 free(cmd);
820 if (fd == NULL)
821 {
822 /*
823 * Cannot create the pipe.
824 */
825 return (NULL);
826 }

--- 18 unchanged lines hidden (view full) ---

845 return (NULL);
846 }
847 ch_ungetchar(c);
848 *pfd = (void *) fd;
849 *pf = f;
850 return (save("-"));
851 }
852#endif
829 free(cmd);
830 if (fd == NULL)
831 {
832 /*
833 * Cannot create the pipe.
834 */
835 return (NULL);
836 }

--- 18 unchanged lines hidden (view full) ---

855 return (NULL);
856 }
857 ch_ungetchar(c);
858 *pfd = (void *) fd;
859 *pf = f;
860 return (save("-"));
861 }
862#endif
853 gfilename = readfd(fd);
863 cmd = readfd(fd);
854 pclose(fd);
864 pclose(fd);
855 if (*gfilename == '\0')
865 if (*cmd == '\0')
856 /*
857 * Pipe is empty. This means there is no alt file.
858 */
859 return (NULL);
866 /*
867 * Pipe is empty. This means there is no alt file.
868 */
869 return (NULL);
860 return (gfilename);
870 return (cmd);
861#endif /* HAVE_POPEN */
862}
863
864/*
865 * Close a replacement file.
866 */
867 public void
868close_altfile(altfilename, filename, pipefd)
869 char *altfilename;
870 char *filename;
871 void *pipefd;
872{
873#if HAVE_POPEN
874 char *lessclose;
871#endif /* HAVE_POPEN */
872}
873
874/*
875 * Close a replacement file.
876 */
877 public void
878close_altfile(altfilename, filename, pipefd)
879 char *altfilename;
880 char *filename;
881 void *pipefd;
882{
883#if HAVE_POPEN
884 char *lessclose;
875 char *gfilename;
876 char *galtfilename;
877 FILE *fd;
878 char *cmd;
879
880 if (secure)
881 return;
882 if (pipefd != NULL)
883 {
884#if OS2
885 /*
886 * The pclose function of OS/2 emx sometimes fails.
887 * Send SIGINT to the piped process before closing it.
888 */
889 kill(((FILE*)pipefd)->_pid, SIGINT);
890#endif
891 pclose((FILE*) pipefd);
892 }
893 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
894 return;
885 FILE *fd;
886 char *cmd;
887
888 if (secure)
889 return;
890 if (pipefd != NULL)
891 {
892#if OS2
893 /*
894 * The pclose function of OS/2 emx sometimes fails.
895 * Send SIGINT to the piped process before closing it.
896 */
897 kill(((FILE*)pipefd)->_pid, SIGINT);
898#endif
899 pclose((FILE*) pipefd);
900 }
901 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
902 return;
895 gfilename = esc_metachars(filename);
896 if (gfilename == NULL)
897 {
898 return;
899 }
900 galtfilename = esc_metachars(altfilename);
901 if (galtfilename == NULL)
902 {
903 free(gfilename);
904 return;
905 }
906 cmd = (char *) ecalloc(strlen(lessclose) + strlen(gfilename) +
907 strlen(galtfilename) + 2, sizeof(char));
908 sprintf(cmd, lessclose, gfilename, galtfilename);
903 cmd = (char *) ecalloc(strlen(lessclose) + strlen(filename) +
904 strlen(altfilename) + 2, sizeof(char));
905 sprintf(cmd, lessclose, filename, altfilename);
909 fd = shellcmd(cmd);
906 fd = shellcmd(cmd);
910 free(galtfilename);
911 free(gfilename);
912 free(cmd);
913 if (fd != NULL)
914 pclose(fd);
915#endif
916}
917
918/*
919 * Is the specified file a directory?
920 */
921 public int
922is_dir(filename)
923 char *filename;
924{
925 int isdir = 0;
926
907 free(cmd);
908 if (fd != NULL)
909 pclose(fd);
910#endif
911}
912
913/*
914 * Is the specified file a directory?
915 */
916 public int
917is_dir(filename)
918 char *filename;
919{
920 int isdir = 0;
921
927 filename = unquote_file(filename);
922 filename = shell_unquote(filename);
928#if HAVE_STAT
929{
930 int r;
931 struct stat statbuf;
932
933 r = stat(filename, &statbuf);
934 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
935}

--- 19 unchanged lines hidden (view full) ---

955 * (if it cannot be opened or is a directory, etc.)
956 */
957 public char *
958bad_file(filename)
959 char *filename;
960{
961 register char *m = NULL;
962
923#if HAVE_STAT
924{
925 int r;
926 struct stat statbuf;
927
928 r = stat(filename, &statbuf);
929 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
930}

--- 19 unchanged lines hidden (view full) ---

950 * (if it cannot be opened or is a directory, etc.)
951 */
952 public char *
953bad_file(filename)
954 char *filename;
955{
956 register char *m = NULL;
957
963 filename = unquote_file(filename);
958 filename = shell_unquote(filename);
964 if (is_dir(filename))
965 {
959 if (is_dir(filename))
960 {
966 static char is_dir[] = " is a directory";
961 static char is_a_dir[] = " is a directory";
967
962
968 m = (char *) ecalloc(strlen(filename) + sizeof(is_dir),
963 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
969 sizeof(char));
970 strcpy(m, filename);
964 sizeof(char));
965 strcpy(m, filename);
971 strcat(m, is_dir);
966 strcat(m, is_a_dir);
972 } else
973 {
974#if HAVE_STAT
975 int r;
976 struct stat statbuf;
977
978 r = stat(filename, &statbuf);
979 if (r < 0)

--- 35 unchanged lines hidden (view full) ---

1015
1016 if ((size = (long) _gs_size(f)) >= 0)
1017 return ((POSITION) size);
1018#endif
1019#endif
1020 return (seek_filesize(f));
1021}
1022
967 } else
968 {
969#if HAVE_STAT
970 int r;
971 struct stat statbuf;
972
973 r = stat(filename, &statbuf);
974 if (r < 0)

--- 35 unchanged lines hidden (view full) ---

1010
1011 if ((size = (long) _gs_size(f)) >= 0)
1012 return ((POSITION) size);
1013#endif
1014#endif
1015 return (seek_filesize(f));
1016}
1017
1018/*
1019 *
1020 */
1021 public char *
1022shell_coption()
1023{
1024 return ("-c");
1025}