Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / cron / compat.c
bloba9dd7da9b8b46d091ce695694fb321430024624a
1 /* $NetBSD: compat.c,v 1.4 2005/03/16 02:53:55 xtraeme Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * All rights reserved
6 * Distribute freely, except: don't remove my name from the source or
7 * documentation (don't take credit for my work), mark your changes (don't
8 * get me blamed for your possible bugs), don't alter or remove this
9 * notice. May be sold if buildable source is provided to buyer. No
10 * warrantee of any kind, express or implied, is included with this
11 * software; use at your own risk, responsibility for damages (if any) to
12 * anyone resulting from the use of this software rests entirely with the
13 * user.
15 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
16 * I'll try to keep a version up to date. I can be reached as follows:
17 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
20 #include <sys/cdefs.h>
21 #if !defined(lint) && !defined(LINT)
22 #if 0
23 static char rcsid[] = "Id: compat.c,v 1.6 1994/01/15 20:43:43 vixie Exp";
24 #else
25 __RCSID("$NetBSD: compat.c,v 1.4 2005/03/16 02:53:55 xtraeme Exp $");
26 #endif
27 #endif
29 /* vix 30dec93 [broke this out of misc.c - see RCS log for history]
30 * vix 15jan87 [added TIOCNOTTY, thanks csg@pyramid]
34 #include "cron.h"
35 #ifdef NEED_GETDTABLESIZE
36 # include <limits.h>
37 #endif
38 #if defined(NEED_SETSID) && defined(BSD)
39 # include <sys/ioctl.h>
40 #endif
41 #include <errno.h>
44 /* the code does not depend on any of vfork's
45 * side-effects; it just uses it as a quick
46 * fork-and-exec.
48 #ifdef NEED_VFORK
49 PID_T
50 vfork(void) {
51 return (fork());
53 #endif
56 #ifdef NEED_STRDUP
57 char *
58 strdup(char *str)
60 char *temp;
62 temp = malloc(strlen(str) + 1);
63 (void) strcpy(temp, str); /* safe */
64 return temp;
66 #endif
69 #ifdef NEED_STRERROR
70 char *
71 strerror(int error)
73 extern char *sys_errlist[];
74 extern int sys_nerr;
75 static char buf[32];
77 if ((error <= sys_nerr) && (error > 0)) {
78 return sys_errlist[error];
81 sprintf(buf, "Unknown error: %d", error);
82 return buf;
84 #endif
87 #ifdef NEED_STRCASECMP
88 int
89 strcasecmp(char *left, char *right)
91 while (*left && (MkLower(*left) == MkLower(*right))) {
92 left++;
93 right++;
95 return MkLower(*left) - MkLower(*right);
97 #endif
100 #ifdef NEED_SETSID
102 setsid(void)
104 int newpgrp;
105 # if defined(BSD)
106 int fd;
107 # if defined(POSIX)
108 newpgrp = setpgid((pid_t)0, getpid());
109 # else
110 newpgrp = setpgrp(0, getpid());
111 # endif
112 if ((fd = open("/dev/tty", 2)) >= 0)
114 (void) ioctl(fd, TIOCNOTTY, (char*)0);
115 (void) close(fd);
117 # else /*BSD*/
118 newpgrp = setpgrp();
120 (void) close(STDIN); (void) open("/dev/null", 0);
121 (void) close(STDOUT); (void) open("/dev/null", 1);
122 (void) close(STDERR); (void) open("/dev/null", 2);
123 # endif /*BSD*/
124 return newpgrp;
126 #endif /*NEED_SETSID*/
129 #ifdef NEED_GETDTABLESIZE
131 getdtablesize(void) {
132 #ifdef _SC_OPEN_MAX
133 return sysconf(_SC_OPEN_MAX);
134 #else
135 return _POSIX_OPEN_MAX;
136 #endif
138 #endif
141 #ifdef NEED_FLOCK
142 /* The following flock() emulation snarfed intact *) from the HP-UX
143 * "BSD to HP-UX porting tricks" maintained by
144 * system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson))
145 * from the version "last updated: 11-Jan-1993"
146 * Snarfage done by Jarkko Hietaniemi <Jarkko.Hietaniemi@hut.fi>
147 * *) well, almost, had to K&R the function entry, HPUX "cc"
148 * does not grok ANSI function prototypes */
151 * flock (fd, operation)
153 * This routine performs some file locking like the BSD 'flock'
154 * on the object described by the int file descriptor 'fd',
155 * which must already be open.
157 * The operations that are available are:
159 * LOCK_SH - get a shared lock.
160 * LOCK_EX - get an exclusive lock.
161 * LOCK_NB - don't block (must be ORed with LOCK_SH or LOCK_EX).
162 * LOCK_UN - release a lock.
164 * Return value: 0 if lock successful, -1 if failed.
166 * Note that whether the locks are enforced or advisory is
167 * controlled by the presence or absence of the SETGID bit on
168 * the executable.
170 * Note that there is no difference between shared and exclusive
171 * locks, since the 'lockf' system call in SYSV doesn't make any
172 * distinction.
174 * The file "<sys/file.h>" should be modified to contain the definitions
175 * of the available operations, which must be added manually (see below
176 * for the values).
179 /* this code has been reformatted by vixie */
182 flock(int fd, 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(char *name, char *value, int overwrite)
219 char *tmp;
221 if (overwrite && getenv(name))
222 return -1;
224 if (!(tmp = malloc(strlen(name) + strlen(value) + 2))) {
225 errno = ENOMEM;
226 return -1;
229 sprintf("%s=%s", name, value);
230 return putenv(tmp); /* intentionally orphan 'tmp' storage */
232 #endif