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