2 * Routines to mess around with filenames (and files).
3 * Much of this is very OS dependent.
11 extern int force_open;
12 extern IFILE curr_ifile;
13 extern IFILE old_ifile;
16 * Return the full pathname of the given file in the "home directory".
22 register char *pathname;
23 register char *homedir;
25 homedir = getenv("HOME");
28 * Most MSDOS users do not have $HOME defined,
29 * so if no $HOME then look for "_less" anywhere
30 * on search path (always begins at current directory).
34 extern char *searchpath();
35 pathname = searchpath(filename);
38 pathname = save(pathname);
41 pathname = (char *) calloc(strlen(homedir)+strlen(filename)+2,
45 sprintf(pathname, "%s\\%s", homedir, filename);
50 pathname = (char *) calloc(strlen(homedir)+strlen(filename)+2,
54 sprintf(pathname, "%s/%s", homedir, filename);
60 * Find out where the help file is.
65 register char *helpfile;
67 extern char *searchpath();
70 * Look in current directory.
72 if (access(HELPFILE,0) == 0)
75 * Find the basename of HELPFILE,
76 * and look for it in each directory in the search path.
78 if ((helpfile = strrchr(HELPFILE, '\\')) == NULL)
82 return (save(searchpath(helpfile)));
84 if ((helpfile = getenv("LESSHELP")) != NULL)
85 return (save(helpfile));
86 return (save(HELPFILE));
91 * Expand a string, substituting any "%" with the current filename,
92 * and any "#" with the previous filename.
98 register char *fr, *to;
103 * Make one pass to see how big a buffer we
104 * need to allocate for the expanded string.
107 for (fr = s; *fr != '\0'; fr++)
112 n += strlen(get_filename(curr_ifile));
115 if (old_ifile == NULL_IFILE)
117 error("No previous file", NULL_PARG);
120 n += strlen(get_filename(old_ifile));
128 e = (char *) ecalloc(n+1, sizeof(char));
131 * Now copy the string, expanding any "%" or "#".
134 for (fr = s; *fr != '\0'; fr++)
139 strcpy(to, get_filename(curr_ifile));
143 strcpy(to, get_filename(old_ifile));
156 * Try to determine if a file is "binary".
157 * This is just a guess, and we need not try too hard to make it accurate.
167 n = read(f, data, sizeof(data));
168 for (i = 0; i < n; i++)
169 if (binary_char(data[i]))
175 * Try to determine the size of a file by seeking to the end.
183 spos = lseek(f, (offset_t)0, 2);
184 if (spos == BAD_LSEEK)
185 return (NULL_POSITION);
186 return ((POSITION) spos);
190 * Expand a filename, substituting any environment variables, etc.
207 filename = fexpand(filename);
208 if (filename == NULL)
212 * We get the shell to expand the filename for us by passing
213 * an "echo" command to the shell and reading its output.
216 if (p == NULL || *p == '\0')
219 * Read the output of <echo filename>.
221 cmd = (char *) ecalloc(strlen(filename)+6, sizeof(char));
222 sprintf(cmd, "echo %s", filename);
226 * Read the output of <$SHELL -c "echo filename">.
228 cmd = (char *) ecalloc(strlen(p)+strlen(filename)+12, sizeof(char));
229 sprintf(cmd, "%s -c \"echo %s\"", p, filename);
239 gfilename = (char *) ecalloc(len, sizeof(char));
240 for (p = gfilename; ; p++)
242 if ((ch = getc(f)) == '\n' || ch == EOF)
244 if (p - gfilename >= len-1)
248 p = (char *) ecalloc(len, sizeof(char));
249 strcpy(p, gfilename);
252 p = gfilename + strlen(gfilename);
258 if (*gfilename == '\0')
269 return (fexpand(filename));
277 #include <sys/types.h>
278 #include <sys/stat.h>
281 * Returns NULL if the file can be opened and
282 * is an ordinary file, otherwise an error message
283 * (if it cannot be opened or is a directory, etc.)
292 if (stat(filename, &statbuf) < 0)
293 return (errno_message(filename));
298 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
300 static char is_dir[] = " is a directory";
301 m = (char *) ecalloc(strlen(filename) + sizeof(is_dir),
307 if ((statbuf.st_mode & S_IFMT) != S_IFREG)
309 static char not_reg[] = " is not a regular file";
310 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
321 * Return the size of a file, as cheaply as possible.
322 * In Unix, we can stat the file.
330 if (fstat(f, &statbuf) < 0)
332 * Can't stat; try seeking to the end.
334 return (seek_filesize(f));
336 return ((POSITION) statbuf.st_size);
342 * If we have no way to find out, just say the file is good.
352 * We can find the file size by seeking.
358 return (seek_filesize(f));