2 Copyright (C) Andrew Tridgell 1998
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 compatibility functions - replacing functions for platforms that don't
30 int l = strlen(s) + 1;
31 char *ret = (char *)malloc(l);
39 char *getcwd(char *buf, int size)
47 pid_t waitpid(pid_t pid, int *statptr, int options)
50 return wait4(pid, statptr, options, NULL);
52 /* If wait4 is also not available, try wait3 for SVR3 variants */
53 /* Less ideal because can't actually request a specific pid */
54 /* At least the WNOHANG option is supported */
55 /* Code borrowed from apache fragment written by dwd@bell-labs.com */
56 int tmp_pid, dummystat;;
57 if (kill(pid, 0) == -1) {
63 while (((tmp_pid = wait3(statptr, options, 0)) != pid) &&
64 (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
73 void *memmove(void *dest, const void *src, size_t n)
75 bcopy((char *) src, (char *) dest, n);
81 /* Find the first ocurrence in S of any character in ACCEPT.
84 char *strpbrk(const char *s, const char *accept)
87 const char *a = accept;
89 if (*a++ == *s) return (char *)s;
100 /* Like strncpy but does not 0 fill the buffer and always null
101 * terminates. bufsize is the size of the destination buffer.
103 * Returns the index of the terminating byte. */
104 size_t strlcpy(char *d, const char *s, size_t bufsize)
106 size_t len = strlen(s);
108 if (bufsize <= 0) return 0;
109 if (len >= bufsize) len = bufsize-1;
117 /* like strncat but does not 0 fill the buffer and always null
118 terminates. bufsize is the length of the buffer, which should
119 be one more than the maximum resulting string length */
120 size_t strlcat(char *d, const char *s, size_t bufsize)
122 size_t len1 = strlen(d);
123 size_t len2 = strlen(s);
124 size_t ret = len1 + len2;
126 if (len1+len2 >= bufsize) {
127 len2 = bufsize - (len1+1);
130 memcpy(d+len1, s, len2);
137 #ifdef REPLACE_INET_NTOA
138 char *rep_inet_ntoa(struct in_addr ip)
140 unsigned char *p = (unsigned char *)&ip.s_addr;
143 snprintf(buf, 18, "%d.%d.%d.%d",
144 (int)p[0], (int)p[1], (int)p[2], (int)p[3]);
146 snprintf(buf, 18, "%d.%d.%d.%d",
147 (int)p[3], (int)p[2], (int)p[1], (int)p[0]);
153 #ifdef REPLACE_INET_ATON
154 int inet_aton(const char *cp, struct in_addr *inp)
156 unsigned int a1, a2, a3, a4;
159 if (strcmp(cp, "255.255.255.255") == 0) {
160 inp->s_addr = (unsigned) -1;
164 if (sscanf(cp, "%u.%u.%u.%u", &a1, &a2, &a3, &a4) != 4 ||
165 a1 > 255 || a2 > 255 || a3 > 255 || a4 > 255) {
169 ret = (a1 << 24) | (a2 << 16) | (a3 << 8) | a4;
171 inp->s_addr = htonl(ret);
173 if (inp->s_addr == (unsigned) -1) {
180 /* some systems don't take the 2nd argument */
181 int sys_gettimeofday(struct timeval *tv)
183 #if HAVE_GETTIMEOFDAY_TZ
184 return gettimeofday(tv, NULL);
186 return gettimeofday(tv);