Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 2 | * Copyright (c) 1999-2003 Damien Miller. All rights reserved. |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 3 | * |
| 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 Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 12 | * |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 25 | #include "includes.h" |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 26 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 28 | RCSID("$Id: bsd-misc.c,v 1.13 2003/05/18 14:13:39 djm Exp $"); |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 29 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 30 | /* |
| 31 | * NB. duplicate __progname in case it is an alias for argv[0] |
| 32 | * Otherwise it may get clobbered by setproctitle() |
| 33 | */ |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 34 | char *get_progname(char *argv0) |
| 35 | { |
| 36 | #ifdef HAVE___PROGNAME |
| 37 | extern char *__progname; |
| 38 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 39 | return xstrdup(__progname); |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 40 | #else |
| 41 | char *p; |
| 42 | |
| 43 | if (argv0 == NULL) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 44 | return ("unknown"); /* XXX */ |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 45 | p = strrchr(argv0, '/'); |
| 46 | if (p == NULL) |
| 47 | p = argv0; |
| 48 | else |
| 49 | p++; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 50 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 51 | return (xstrdup(p)); |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 52 | #endif |
| 53 | } |
| 54 | |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 55 | #ifndef HAVE_SETLOGIN |
| 56 | int setlogin(const char *name) |
| 57 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 58 | return (0); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 59 | } |
| 60 | #endif /* !HAVE_SETLOGIN */ |
| 61 | |
| 62 | #ifndef HAVE_INNETGR |
| 63 | int innetgr(const char *netgroup, const char *host, |
| 64 | const char *user, const char *domain) |
| 65 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 66 | return (0); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 67 | } |
| 68 | #endif /* HAVE_INNETGR */ |
| 69 | |
| 70 | #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) |
| 71 | int seteuid(uid_t euid) |
| 72 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 73 | return (setreuid(-1, euid)); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 74 | } |
| 75 | #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */ |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 76 | |
Kevin Steves | cb17e99 | 2001-04-09 14:50:52 +0000 | [diff] [blame] | 77 | #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) |
| 78 | int setegid(uid_t egid) |
| 79 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 80 | return(setresgid(-1, egid, -1)); |
Kevin Steves | cb17e99 | 2001-04-09 14:50:52 +0000 | [diff] [blame] | 81 | } |
| 82 | #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */ |
| 83 | |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 84 | #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR) |
| 85 | const char *strerror(int e) |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 86 | { |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 87 | extern int sys_nerr; |
| 88 | extern char *sys_errlist[]; |
| 89 | |
Ben Lindstrom | 46e55aa | 2001-03-13 23:38:20 +0000 | [diff] [blame] | 90 | if ((e >= 0) && (e < sys_nerr)) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 91 | return (sys_errlist[e]); |
| 92 | |
| 93 | return ("unlisted error"); |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 94 | } |
Damien Miller | 11fa2cc | 2000-08-16 10:35:58 +1000 | [diff] [blame] | 95 | #endif |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 96 | |
| 97 | #ifndef HAVE_UTIMES |
| 98 | int utimes(char *filename, struct timeval *tvp) |
| 99 | { |
| 100 | struct utimbuf ub; |
| 101 | |
Ben Lindstrom | 100d586 | 2002-07-08 21:09:41 +0000 | [diff] [blame] | 102 | ub.actime = tvp[0].tv_sec; |
| 103 | ub.modtime = tvp[1].tv_sec; |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 104 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 105 | return (utime(filename, &ub)); |
Ben Lindstrom | 42202bc | 2001-01-15 02:34:37 +0000 | [diff] [blame] | 106 | } |
| 107 | #endif |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 108 | |
| 109 | #ifndef HAVE_TRUNCATE |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 110 | int truncate(const char *path, off_t length) |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 111 | { |
| 112 | int fd, ret, saverrno; |
| 113 | |
| 114 | fd = open(path, O_WRONLY); |
| 115 | if (fd < 0) |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 116 | return (-1); |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 117 | |
| 118 | ret = ftruncate(fd, length); |
| 119 | saverrno = errno; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 120 | close(fd); |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 121 | if (ret == -1) |
| 122 | errno = saverrno; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 123 | |
Tim Rice | 4bd2a19 | 2002-05-07 19:51:31 -0700 | [diff] [blame] | 124 | return(ret); |
| 125 | } |
| 126 | #endif /* HAVE_TRUNCATE */ |
| 127 | |
Ben Lindstrom | 837461b | 2002-06-12 16:57:14 +0000 | [diff] [blame] | 128 | #if !defined(HAVE_SETGROUPS) && defined(SETGROUPS_NOOP) |
| 129 | /* |
| 130 | * Cygwin setgroups should be a noop. |
| 131 | */ |
| 132 | int |
Ben Lindstrom | 0e23ebc | 2002-06-13 21:34:57 +0000 | [diff] [blame] | 133 | setgroups(size_t size, const gid_t *list) |
Ben Lindstrom | 837461b | 2002-06-12 16:57:14 +0000 | [diff] [blame] | 134 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 135 | return (0); |
Ben Lindstrom | 837461b | 2002-06-12 16:57:14 +0000 | [diff] [blame] | 136 | } |
| 137 | #endif |
| 138 | |
Tim Rice | 4e4dc56 | 2003-03-18 10:21:40 -0800 | [diff] [blame] | 139 | #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP) |
| 140 | int 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 | |