Do not use .Xo/.Xc to work around ancient roff limits.
[netbsd-mini2440.git] / libexec / cron / compat.c
blob75a07285a0153f52b0a5c6b5833905101408f188
1 /* Copyright 1988,1990,1993 by Paul Vixie
2 * All rights reserved
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
11 * user.
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static char rcsid[] = "$Id: compat.c,v 1.1 1994/01/05 20:40:12 jtc Exp $";
20 #endif
22 /* vix 30dec93 [broke this out of misc.c - see RCS log for history]
23 * vix 15jan87 [added TIOCNOTTY, thanks csg@pyramid]
27 #include "cron.h"
28 #include "externs.h"
29 #ifdef NEED_GETDTABLESIZE
30 # include <limits.h>
31 #endif
32 #if defined(NEED_SETSID) && defined(BSD)
33 # include <sys/ioctl.h>
34 #endif
35 #include <errno.h>
38 /* the code does not depend on any of vfork's
39 * side-effects; it just uses it as a quick
40 * fork-and-exec.
42 #ifdef NEED_VFORK
43 PID_T
44 vfork() {
45 return (fork());
47 #endif
50 #ifdef NEED_STRDUP
51 char *
52 strdup(str)
53 char *str;
55 char *temp;
57 temp = malloc(strlen(str) + 1);
58 (void) strcpy(temp, str);
59 return temp;
61 #endif
64 #ifdef NEED_STRERROR
65 char *
66 strerror(error)
67 int error;
69 extern char *sys_errlist[];
70 extern int sys_nerr;
71 static char buf[32];
73 if ((error <= sys_nerr) && (error > 0)) {
74 return sys_errlist[error];
77 sprintf(buf, "Unknown error: %d", error);
78 return buf;
80 #endif
83 #ifdef NEED_STRCASECMP
84 int
85 strcasecmp(left, right)
86 char *left;
87 char *right;
89 while (*left && (MkLower(*left) == MkLower(*right))) {
90 left++;
91 right++;
93 return MkLower(*left) - MkLower(*right);
95 #endif
98 #ifdef NEED_SETSID
99 int
100 setsid()
102 int newpgrp;
103 # if defined(BSD)
104 int fd;
105 # if defined(POSIX)
106 newpgrp = setpgid((pid_t)0, getpid());
107 # else
108 newpgrp = setpgrp(0, getpid());
109 # endif
110 if ((fd = open("/dev/tty", 2)) >= 0)
112 (void) ioctl(fd, TIOCNOTTY, (char*)0);
113 (void) close(fd);
115 # else /*BSD*/
116 newpgrp = setpgrp();
118 (void) close(STDIN); (void) open("/dev/null", 0);
119 (void) close(STDOUT); (void) open("/dev/null", 1);
120 (void) close(STDERR); (void) open("/dev/null", 2);
121 # endif /*BSD*/
122 return newpgrp;
124 #endif /*NEED_SETSID*/
127 #ifdef NEED_GETDTABLESIZE
129 getdtablesize() {
130 #ifdef _SC_OPEN_MAX
131 return sysconf(_SC_OPEN_MAX);
132 #else
133 return _POSIX_OPEN_MAX;
134 #endif
136 #endif
139 #ifdef NEED_FLOCK
140 /* The following flock() emulation snarfed intact *) from the HP-UX
141 * "BSD to HP-UX porting tricks" maintained by
142 * system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson))
143 * from the version "last updated: 11-Jan-1993"
144 * Snarfage done by Jarkko Hietaniemi <Jarkko.Hietaniemi@hut.fi>
145 * *) well, almost, had to K&R the function entry, HPUX "cc"
146 * does not grok ANSI function prototypes */
149 * flock (fd, operation)
151 * This routine performs some file locking like the BSD 'flock'
152 * on the object described by the int file descriptor 'fd',
153 * which must already be open.
155 * The operations that are available are:
157 * LOCK_SH - get a shared lock.
158 * LOCK_EX - get an exclusive lock.
159 * LOCK_NB - don't block (must be ORed with LOCK_SH or LOCK_EX).
160 * LOCK_UN - release a lock.
162 * Return value: 0 if lock successful, -1 if failed.
164 * Note that whether the locks are enforced or advisory is
165 * controlled by the presence or absence of the SETGID bit on
166 * the executable.
168 * Note that there is no difference between shared and exclusive
169 * locks, since the 'lockf' system call in SYSV doesn't make any
170 * distinction.
172 * The file "<sys/file.h>" should be modified to contain the definitions
173 * of the available operations, which must be added manually (see below
174 * for the values).
177 /* this code has been reformatted by vixie */
180 flock(fd, operation)
181 int fd;
182 int operation;
184 int i;
186 switch (operation) {
187 case LOCK_SH: /* get a shared lock */
188 case LOCK_EX: /* get an exclusive lock */
189 i = lockf (fd, F_LOCK, 0);
190 break;
192 case LOCK_SH|LOCK_NB: /* get a non-blocking shared lock */
193 case LOCK_EX|LOCK_NB: /* get a non-blocking exclusive lock */
194 i = lockf (fd, F_TLOCK, 0);
195 if (i == -1)
196 if ((errno == EAGAIN) || (errno == EACCES))
197 errno = EWOULDBLOCK;
198 break;
200 case LOCK_UN: /* unlock */
201 i = lockf (fd, F_ULOCK, 0);
202 break;
204 default: /* can't decipher operation */
205 i = -1;
206 errno = EINVAL;
207 break;
210 return (i);
212 #endif /*NEED_FLOCK*/
215 #ifdef NEED_SETENV
217 setenv(name, value, overwrite)
218 char *name, *value;
219 int overwrite;
221 char *tmp;
223 if (overwrite && getenv(name))
224 return -1;
226 if (!(tmp = malloc(strlen(name) + strlen(value) + 2))) {
227 errno = ENOMEM;
228 return -1;
231 sprintf("%s=%s", name, value);
232 return putenv(tmp); /* intentionally orphan 'tmp' storage */
234 #endif