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