12311Sjkh/* Copyright 1988,1990,1993,1994 by Paul Vixie
22311Sjkh * All rights reserved
32311Sjkh *
42311Sjkh * Distribute freely, except: don't remove my name from the source or
52311Sjkh * documentation (don't take credit for my work), mark your changes (don't
62311Sjkh * get me blamed for your possible bugs), don't alter or remove this
72311Sjkh * notice.  May be sold if buildable source is provided to buyer.  No
82311Sjkh * warrantee of any kind, express or implied, is included with this
92311Sjkh * software; use at your own risk, responsibility for damages (if any) to
102311Sjkh * anyone resulting from the use of this software rests entirely with the
112311Sjkh * user.
122311Sjkh *
132311Sjkh * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
142311Sjkh * I'll try to keep a version up to date.  I can be reached as follows:
152311Sjkh * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
162311Sjkh */
172311Sjkh
182311Sjkh#if !defined(lint) && !defined(LINT)
1950479Speterstatic char rcsid[] = "$FreeBSD: stable/11/usr.sbin/cron/lib/compat.c 358255 2020-02-23 03:13:38Z kevans $";
202311Sjkh#endif
212311Sjkh
222311Sjkh/* vix 30dec93 [broke this out of misc.c - see RCS log for history]
232311Sjkh * vix 15jan87 [added TIOCNOTTY, thanks csg@pyramid]
242311Sjkh */
252311Sjkh
262311Sjkh
272311Sjkh#include "cron.h"
282311Sjkh#ifdef NEED_GETDTABLESIZE
292311Sjkh# include <limits.h>
302311Sjkh#endif
312311Sjkh#if defined(NEED_SETSID) && defined(BSD)
322311Sjkh# include <sys/ioctl.h>
332311Sjkh#endif
342311Sjkh#include <errno.h>
3569793Sobrien#include <paths.h>
362311Sjkh
372311Sjkh
382311Sjkh#ifdef NEED_STRDUP
392311Sjkhchar *
402311Sjkhstrdup(str)
412311Sjkh	char	*str;
422311Sjkh{
432311Sjkh	char	*temp;
442311Sjkh
4520573Spst	if ((temp = malloc(strlen(str) + 1)) == NULL) {
4620573Spst		errno = ENOMEM;
4720573Spst		return NULL;
4820573Spst	}
492311Sjkh	(void) strcpy(temp, str);
502311Sjkh	return temp;
512311Sjkh}
522311Sjkh#endif
532311Sjkh
542311Sjkh
552311Sjkh#ifdef NEED_STRERROR
562311Sjkhchar *
572311Sjkhstrerror(error)
582311Sjkh	int error;
592311Sjkh{
602311Sjkh	extern char *sys_errlist[];
612311Sjkh	extern int sys_nerr;
622311Sjkh	static char buf[32];
632311Sjkh
642311Sjkh	if ((error <= sys_nerr) && (error > 0)) {
652311Sjkh		return sys_errlist[error];
662311Sjkh	}
672311Sjkh
682311Sjkh	sprintf(buf, "Unknown error: %d", error);
692311Sjkh	return buf;
702311Sjkh}
712311Sjkh#endif
722311Sjkh
732311Sjkh
742311Sjkh#ifdef NEED_STRCASECMP
752311Sjkhint
762311Sjkhstrcasecmp(left, right)
772311Sjkh	char	*left;
782311Sjkh	char	*right;
792311Sjkh{
802311Sjkh	while (*left && (MkLower(*left) == MkLower(*right))) {
812311Sjkh		left++;
822311Sjkh		right++;
832311Sjkh	}
842311Sjkh	return MkLower(*left) - MkLower(*right);
852311Sjkh}
862311Sjkh#endif
872311Sjkh
882311Sjkh
892311Sjkh#ifdef NEED_SETSID
902311Sjkhint
912311Sjkhsetsid()
922311Sjkh{
932311Sjkh	int	newpgrp;
942311Sjkh# if defined(BSD)
952311Sjkh	int	fd;
962311Sjkh#  if defined(POSIX)
972311Sjkh	newpgrp = setpgid((pid_t)0, getpid());
982311Sjkh#  else
992311Sjkh	newpgrp = setpgrp(0, getpid());
1002311Sjkh#  endif
10169793Sobrien	if ((fd = open(_PATH_TTY, 2)) >= 0)
1022311Sjkh	{
1032311Sjkh		(void) ioctl(fd, TIOCNOTTY, (char*)0);
1042311Sjkh		(void) close(fd);
1052311Sjkh	}
1062311Sjkh# else /*BSD*/
1072311Sjkh	newpgrp = setpgrp();
1082311Sjkh
10969793Sobrien	(void) close(STDIN);	(void) open(_PATH_DEVNULL, 0);
11069793Sobrien	(void) close(STDOUT);	(void) open(_PATH_DEVNULL, 1);
11169793Sobrien	(void) close(STDERR);	(void) open(_PATH_DEVNULL, 2);
1122311Sjkh# endif /*BSD*/
1132311Sjkh	return newpgrp;
1142311Sjkh}
1152311Sjkh#endif /*NEED_SETSID*/
1162311Sjkh
1172311Sjkh
1182311Sjkh#ifdef NEED_GETDTABLESIZE
1192311Sjkhint
1202311Sjkhgetdtablesize() {
1212311Sjkh#ifdef _SC_OPEN_MAX
1222311Sjkh	return sysconf(_SC_OPEN_MAX);
1232311Sjkh#else
1242311Sjkh	return _POSIX_OPEN_MAX;
1252311Sjkh#endif
1262311Sjkh}
1272311Sjkh#endif
1282311Sjkh
1292311Sjkh
1302311Sjkh#ifdef NEED_FLOCK
1312311Sjkh/* The following flock() emulation snarfed intact *) from the HP-UX
1322311Sjkh * "BSD to HP-UX porting tricks" maintained by
1332311Sjkh * system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson))
1342311Sjkh * from the version "last updated: 11-Jan-1993"
1352311Sjkh * Snarfage done by Jarkko Hietaniemi <Jarkko.Hietaniemi@hut.fi>
1362311Sjkh * *) well, almost, had to K&R the function entry, HPUX "cc"
1372311Sjkh * does not grok ANSI function prototypes */
13820573Spst
1392311Sjkh/*
1402311Sjkh * flock (fd, operation)
1412311Sjkh *
1422311Sjkh * This routine performs some file locking like the BSD 'flock'
1432311Sjkh * on the object described by the int file descriptor 'fd',
1442311Sjkh * which must already be open.
1452311Sjkh *
1462311Sjkh * The operations that are available are:
1472311Sjkh *
1482311Sjkh * LOCK_SH  -  get a shared lock.
1492311Sjkh * LOCK_EX  -  get an exclusive lock.
1502311Sjkh * LOCK_NB  -  don't block (must be ORed with LOCK_SH or LOCK_EX).
1512311Sjkh * LOCK_UN  -  release a lock.
1522311Sjkh *
1532311Sjkh * Return value: 0 if lock successful, -1 if failed.
1542311Sjkh *
1552311Sjkh * Note that whether the locks are enforced or advisory is
1562311Sjkh * controlled by the presence or absence of the SETGID bit on
1572311Sjkh * the executable.
1582311Sjkh *
1592311Sjkh * Note that there is no difference between shared and exclusive
1602311Sjkh * locks, since the 'lockf' system call in SYSV doesn't make any
1612311Sjkh * distinction.
1622311Sjkh *
1632311Sjkh * The file "<sys/file.h>" should be modified to contain the definitions
1642311Sjkh * of the available operations, which must be added manually (see below
1652311Sjkh * for the values).
1662311Sjkh */
1672311Sjkh
1682311Sjkh/* this code has been reformatted by vixie */
1692311Sjkh
1702311Sjkhint
1712311Sjkhflock(fd, operation)
1722311Sjkh	int fd;
1732311Sjkh	int operation;
1742311Sjkh{
1752311Sjkh	int i;
1762311Sjkh
1772311Sjkh	switch (operation) {
1782311Sjkh	case LOCK_SH:		/* get a shared lock */
1792311Sjkh	case LOCK_EX:		/* get an exclusive lock */
1802311Sjkh		i = lockf (fd, F_LOCK, 0);
1812311Sjkh		break;
1822311Sjkh
1832311Sjkh	case LOCK_SH|LOCK_NB:	/* get a non-blocking shared lock */
1842311Sjkh	case LOCK_EX|LOCK_NB:	/* get a non-blocking exclusive lock */
1852311Sjkh		i = lockf (fd, F_TLOCK, 0);
1862311Sjkh		if (i == -1)
1872311Sjkh			if ((errno == EAGAIN) || (errno == EACCES))
1882311Sjkh				errno = EWOULDBLOCK;
1892311Sjkh		break;
1902311Sjkh
1912311Sjkh	case LOCK_UN:		/* unlock */
1922311Sjkh		i = lockf (fd, F_ULOCK, 0);
1932311Sjkh		break;
19420573Spst
1952311Sjkh	default:		/* can't decipher operation */
1962311Sjkh		i = -1;
1972311Sjkh		errno = EINVAL;
1982311Sjkh		break;
1992311Sjkh	}
20020573Spst
2012311Sjkh	return (i);
2022311Sjkh}
2032311Sjkh#endif /*NEED_FLOCK*/
2042311Sjkh
2052311Sjkh
2062311Sjkh#ifdef NEED_SETENV
2072311Sjkhint
2082311Sjkhsetenv(name, value, overwrite)
2092311Sjkh	char *name, *value;
2102311Sjkh	int overwrite;
2112311Sjkh{
2122311Sjkh	char *tmp;
2132311Sjkh
2142311Sjkh	if (overwrite && getenv(name))
2152311Sjkh		return -1;
2162311Sjkh
2172311Sjkh	if (!(tmp = malloc(strlen(name) + strlen(value) + 2))) {
2182311Sjkh		errno = ENOMEM;
2192311Sjkh		return -1;
2202311Sjkh	}
2212311Sjkh
22220573Spst	sprintf(tmp, "%s=%s", name, value);
2232311Sjkh	return putenv(tmp);	/* intentionally orphan 'tmp' storage */
2242311Sjkh}
2252311Sjkh#endif
226