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