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