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