Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / distrib / utils / more / os.c
blobae1e522765a1a56c0cf87faf991cf6705b04b547
1 /* $NetBSD: os.c,v 1.7 2003/10/13 14:34:25 agc Exp $ */
3 /*
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)os.c 8.1 (Berkeley) 6/6/93";
37 #else
38 __RCSID("$NetBSD: os.c,v 1.7 2003/10/13 14:34:25 agc Exp $");
39 #endif
40 #endif /* not lint */
43 * Operating system dependent routines.
45 * Most of the stuff in here is based on Unix, but an attempt
46 * has been made to make things work on other operating systems.
47 * This will sometimes result in a loss of functionality, unless
48 * someone rewrites code specifically for the new operating system.
50 * The makefile provides defines to decide whether various
51 * Unix features are present.
54 #include <sys/param.h>
55 #include <sys/stat.h>
56 #include <sys/file.h>
57 #include <signal.h>
58 #include <setjmp.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <stdlib.h>
62 #include <unistd.h>
63 #include <errno.h>
65 #include "less.h"
66 #include "extern.h"
67 #include "pathnames.h"
69 int reading;
71 static jmp_buf read_label;
74 * Pass the specified command to a shell to be executed.
75 * Like plain "system()", but handles resetting terminal modes, etc.
77 void
78 lsystem(cmd)
79 char *cmd;
81 int inp;
82 char cmdbuf[256];
83 char *shell;
86 * Print the command which is to be executed,
87 * unless the command starts with a "-".
89 if (cmd[0] == '-')
90 cmd++;
91 else
93 lower_left();
94 clear_eol();
95 putstr("!");
96 putstr(cmd);
97 putstr("\n");
101 * De-initialize the terminal and take out of raw mode.
103 deinit();
104 flush();
105 raw_mode(0);
108 * Restore signals to their defaults.
110 init_signals(0);
113 * Force standard input to be the terminal, "/dev/tty",
114 * even if less's standard input is coming from a pipe.
116 inp = dup(0);
117 (void)close(0);
118 if (open(_PATH_TTY, O_RDONLY, 0) < 0)
119 (void)dup(inp);
122 * Pass the command to the system to be executed.
123 * If we have a SHELL environment variable, use
124 * <$SHELL -c "command"> instead of just <command>.
125 * If the command is empty, just invoke a shell.
127 if ((shell = getenv("SHELL")) != NULL && *shell != '\0')
129 if (*cmd == '\0')
130 cmd = shell;
131 else
133 (void)snprintf(cmdbuf, sizeof(cmdbuf), "%s -c \"%s\"",
134 shell, cmd);
135 cmd = cmdbuf;
138 if (*cmd == '\0')
139 cmd = "sh";
141 (void)system(cmd);
144 * Restore standard input, reset signals, raw mode, etc.
146 (void)close(0);
147 (void)dup(inp);
148 (void)close(inp);
150 init_signals(1);
151 raw_mode(1);
152 init();
153 screen_trashed = 1;
154 #if defined(SIGWINCH) || defined(SIGWIND)
156 * Since we were ignoring window change signals while we executed
157 * the system command, we must assume the window changed.
159 winch(SIGWINCH);
160 #endif
164 * Like read() system call, but is deliberately interruptable.
165 * A call to intread() from a signal handler will interrupt
166 * any pending iread().
169 iread(fd, buf, len)
170 int fd;
171 char *buf;
172 int len;
174 int n;
176 if (setjmp(read_label))
178 * We jumped here from intread.
180 return (READ_INTR);
182 flush();
183 reading = 1;
184 n = read(fd, buf, len);
185 reading = 0;
186 if (n < 0)
187 return (-1);
188 return (n);
191 void
192 intread()
194 (void)sigsetmask(0L);
195 longjmp(read_label, 1);
199 * Expand a filename, substituting any environment variables, etc.
200 * The implementation of this is necessarily very operating system
201 * dependent. This implementation is unabashedly only for Unix systems.
203 char *
204 glob(filename)
205 char *filename;
207 FILE *f;
208 char *p;
209 int ch;
210 char *cmd;
211 static char buffer[MAXPATHLEN];
213 if (filename[0] == '#')
214 return (filename);
217 * We get the shell to expand the filename for us by passing
218 * an "echo" command to the shell and reading its output.
220 p = getenv("SHELL");
221 if (p == NULL || *p == '\0')
224 * Read the output of <echo filename>.
226 asprintf(&cmd, "echo \"%s\"", filename);
227 if (cmd == NULL)
228 return (filename);
229 } else
232 * Read the output of <$SHELL -c "echo filename">.
234 asprintf(&cmd, "%s -c \"echo %s\"", p, filename);
235 if (cmd == NULL)
236 return (filename);
239 if ((f = popen(cmd, "r")) == NULL)
240 return (filename);
241 free(cmd);
243 for (p = buffer; p < &buffer[sizeof(buffer)-1]; p++)
245 if ((ch = getc(f)) == '\n' || ch == EOF)
246 break;
247 *p = ch;
249 *p = '\0';
250 (void)pclose(f);
251 return(buffer);
254 char *
255 bad_file(filename, message, len)
256 char *filename, *message;
257 u_int len;
259 struct stat statbuf;
261 if (stat(filename, &statbuf) < 0) {
262 (void)snprintf(message, len, "%s: %s", filename,
263 strerror(errno));
264 return(message);
266 if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
267 static char is_dir[] = " is a directory";
269 strtcpy(message, filename, (int)(len-sizeof(is_dir)-1));
270 (void)strlcat(message, is_dir, len);
271 return(message);
273 return((char *)NULL);
277 * Copy a string, truncating to the specified length if necessary.
278 * Unlike strncpy(), the resulting string is guaranteed to be null-terminated.
280 void
281 strtcpy(to, from, len)
282 char *to, *from;
283 int len;
285 (void)strncpy(to, from, (int)len);
286 to[len-1] = '\0';