blob: cdc63c24dcbd53f7afdd760020c5ea8c0368a341 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller31741252003-05-19 00:13:38 +10002 * Copyright (c) 1999-2003 Damien Miller. All rights reserved.
Damien Miller040f3832000-04-03 14:50:43 +10003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Miller040f3832000-04-03 14:50:43 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
Damien Miller11fa2cc2000-08-16 10:35:58 +100025#include "includes.h"
Damien Millera8ed44b2003-01-10 09:53:12 +110026#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller31741252003-05-19 00:13:38 +100028RCSID("$Id: bsd-misc.c,v 1.13 2003/05/18 14:13:39 djm Exp $");
Damien Millere9cf3572001-02-09 12:55:35 +110029
Damien Millera8ed44b2003-01-10 09:53:12 +110030/*
31 * NB. duplicate __progname in case it is an alias for argv[0]
32 * Otherwise it may get clobbered by setproctitle()
33 */
Ben Lindstrom49a79c02000-11-17 03:47:20 +000034char *get_progname(char *argv0)
35{
36#ifdef HAVE___PROGNAME
37 extern char *__progname;
38
Damien Millera8ed44b2003-01-10 09:53:12 +110039 return xstrdup(__progname);
Ben Lindstrom49a79c02000-11-17 03:47:20 +000040#else
41 char *p;
42
43 if (argv0 == NULL)
Damien Miller31741252003-05-19 00:13:38 +100044 return ("unknown"); /* XXX */
Ben Lindstrom49a79c02000-11-17 03:47:20 +000045 p = strrchr(argv0, '/');
46 if (p == NULL)
47 p = argv0;
48 else
49 p++;
Damien Millera8ed44b2003-01-10 09:53:12 +110050
Damien Miller31741252003-05-19 00:13:38 +100051 return (xstrdup(p));
Ben Lindstrom49a79c02000-11-17 03:47:20 +000052#endif
53}
54
Damien Millere72b7af1999-12-30 15:08:44 +110055#ifndef HAVE_SETLOGIN
56int setlogin(const char *name)
57{
Damien Miller31741252003-05-19 00:13:38 +100058 return (0);
Damien Millere72b7af1999-12-30 15:08:44 +110059}
60#endif /* !HAVE_SETLOGIN */
61
62#ifndef HAVE_INNETGR
63int innetgr(const char *netgroup, const char *host,
64 const char *user, const char *domain)
65{
Damien Miller31741252003-05-19 00:13:38 +100066 return (0);
Damien Millere72b7af1999-12-30 15:08:44 +110067}
68#endif /* HAVE_INNETGR */
69
70#if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
71int seteuid(uid_t euid)
72{
Damien Miller31741252003-05-19 00:13:38 +100073 return (setreuid(-1, euid));
Damien Millere72b7af1999-12-30 15:08:44 +110074}
75#endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
Damien Millerecbb26d2000-07-15 14:59:14 +100076
Kevin Stevescb17e992001-04-09 14:50:52 +000077#if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
78int setegid(uid_t egid)
79{
Damien Miller31741252003-05-19 00:13:38 +100080 return(setresgid(-1, egid, -1));
Kevin Stevescb17e992001-04-09 14:50:52 +000081}
82#endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
83
Damien Miller11fa2cc2000-08-16 10:35:58 +100084#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
85const char *strerror(int e)
Damien Millerecbb26d2000-07-15 14:59:14 +100086{
Damien Miller11fa2cc2000-08-16 10:35:58 +100087 extern int sys_nerr;
88 extern char *sys_errlist[];
89
Ben Lindstrom46e55aa2001-03-13 23:38:20 +000090 if ((e >= 0) && (e < sys_nerr))
Damien Miller31741252003-05-19 00:13:38 +100091 return (sys_errlist[e]);
92
93 return ("unlisted error");
Damien Millerecbb26d2000-07-15 14:59:14 +100094}
Damien Miller11fa2cc2000-08-16 10:35:58 +100095#endif
Ben Lindstrom42202bc2001-01-15 02:34:37 +000096
97#ifndef HAVE_UTIMES
98int utimes(char *filename, struct timeval *tvp)
99{
100 struct utimbuf ub;
101
Ben Lindstrom100d5862002-07-08 21:09:41 +0000102 ub.actime = tvp[0].tv_sec;
103 ub.modtime = tvp[1].tv_sec;
Ben Lindstrom42202bc2001-01-15 02:34:37 +0000104
Damien Miller31741252003-05-19 00:13:38 +1000105 return (utime(filename, &ub));
Ben Lindstrom42202bc2001-01-15 02:34:37 +0000106}
107#endif
Tim Rice4bd2a192002-05-07 19:51:31 -0700108
109#ifndef HAVE_TRUNCATE
Damien Miller31741252003-05-19 00:13:38 +1000110int truncate(const char *path, off_t length)
Tim Rice4bd2a192002-05-07 19:51:31 -0700111{
112 int fd, ret, saverrno;
113
114 fd = open(path, O_WRONLY);
115 if (fd < 0)
Damien Miller31741252003-05-19 00:13:38 +1000116 return (-1);
Tim Rice4bd2a192002-05-07 19:51:31 -0700117
118 ret = ftruncate(fd, length);
119 saverrno = errno;
Damien Miller31741252003-05-19 00:13:38 +1000120 close(fd);
Tim Rice4bd2a192002-05-07 19:51:31 -0700121 if (ret == -1)
122 errno = saverrno;
Damien Miller31741252003-05-19 00:13:38 +1000123
Tim Rice4bd2a192002-05-07 19:51:31 -0700124 return(ret);
125}
126#endif /* HAVE_TRUNCATE */
127
Ben Lindstrom837461b2002-06-12 16:57:14 +0000128#if !defined(HAVE_SETGROUPS) && defined(SETGROUPS_NOOP)
129/*
130 * Cygwin setgroups should be a noop.
131 */
132int
Ben Lindstrom0e23ebc2002-06-13 21:34:57 +0000133setgroups(size_t size, const gid_t *list)
Ben Lindstrom837461b2002-06-12 16:57:14 +0000134{
Damien Miller31741252003-05-19 00:13:38 +1000135 return (0);
Ben Lindstrom837461b2002-06-12 16:57:14 +0000136}
137#endif
138
Tim Rice4e4dc562003-03-18 10:21:40 -0800139#if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
140int nanosleep(const struct timespec *req, struct timespec *rem)
141{
142 int rc, saverrno;
143 extern int errno;
144 struct timeval tstart, tstop, tremain, time2wait;
145
146 TIMESPEC_TO_TIMEVAL(&time2wait, req)
147 (void) gettimeofday(&tstart, NULL);
148 rc = select(0, NULL, NULL, NULL, &time2wait);
149 if (rc == -1) {
150 saverrno = errno;
151 (void) gettimeofday (&tstop, NULL);
152 errno = saverrno;
153 tremain.tv_sec = time2wait.tv_sec -
154 (tstop.tv_sec - tstart.tv_sec);
155 tremain.tv_usec = time2wait.tv_usec -
156 (tstop.tv_usec - tstart.tv_usec);
157 tremain.tv_sec += tremain.tv_usec / 1000000L;
158 tremain.tv_usec %= 1000000L;
159 } else {
160 tremain.tv_sec = 0;
161 tremain.tv_usec = 0;
162 }
163 TIMEVAL_TO_TIMESPEC(&tremain, rem)
164
165 return(rc);
166}
167
168#endif
169