1 /* $NetBSD: os.c,v 1.7 2003/10/13 14:34:25 agc Exp $ */
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
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
33 #include <sys/cdefs.h>
36 static char sccsid
[] = "@(#)os.c 8.1 (Berkeley) 6/6/93";
38 __RCSID("$NetBSD: os.c,v 1.7 2003/10/13 14:34:25 agc Exp $");
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>
67 #include "pathnames.h"
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.
86 * Print the command which is to be executed,
87 * unless the command starts with a "-".
101 * De-initialize the terminal and take out of raw mode.
108 * Restore signals to their defaults.
113 * Force standard input to be the terminal, "/dev/tty",
114 * even if less's standard input is coming from a pipe.
118 if (open(_PATH_TTY
, O_RDONLY
, 0) < 0)
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')
133 (void)snprintf(cmdbuf
, sizeof(cmdbuf
), "%s -c \"%s\"",
144 * Restore standard input, reset signals, raw mode, etc.
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.
164 * Like read() system call, but is deliberately interruptable.
165 * A call to intread() from a signal handler will interrupt
166 * any pending iread().
176 if (setjmp(read_label
))
178 * We jumped here from intread.
184 n
= read(fd
, buf
, len
);
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.
211 static char buffer
[MAXPATHLEN
];
213 if (filename
[0] == '#')
217 * We get the shell to expand the filename for us by passing
218 * an "echo" command to the shell and reading its output.
221 if (p
== NULL
|| *p
== '\0')
224 * Read the output of <echo filename>.
226 asprintf(&cmd
, "echo \"%s\"", filename
);
232 * Read the output of <$SHELL -c "echo filename">.
234 asprintf(&cmd
, "%s -c \"echo %s\"", p
, filename
);
239 if ((f
= popen(cmd
, "r")) == NULL
)
243 for (p
= buffer
; p
< &buffer
[sizeof(buffer
)-1]; p
++)
245 if ((ch
= getc(f
)) == '\n' || ch
== EOF
)
255 bad_file(filename
, message
, len
)
256 char *filename
, *message
;
261 if (stat(filename
, &statbuf
) < 0) {
262 (void)snprintf(message
, len
, "%s: %s", filename
,
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
);
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.
281 strtcpy(to
, from
, len
)
285 (void)strncpy(to
, from
, (int)len
);