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