Martin gave his approval to use GPLv3 with this code.
[rsync/rsync.git] / lib / compat.c
1 /*
2  * Reimplementations of standard functions for platforms that don't have them.
3  *
4  * Copyright (C) 1998 Andrew Tridgell
5  * Copyright (C) 2002 Martin Pool
6  * Copyright (C) 2004, 2005, 2006 Wayne Davison
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 3 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, visit the http://fsf.org website.
19  */
20
21 #include "rsync.h"
22
23 #ifndef HAVE_STRDUP
24  char *strdup(char *s)
25 {
26         int len = strlen(s) + 1;
27         char *ret = (char *)malloc(len);
28         if (ret)
29                 memcpy(ret, s, len);
30         return ret;
31 }
32 #endif
33
34 #ifndef HAVE_GETCWD
35  char *getcwd(char *buf, int size)
36 {
37         return getwd(buf);
38 }
39 #endif
40
41
42 #ifndef HAVE_WAITPID
43  pid_t waitpid(pid_t pid, int *statptr, int options)
44 {
45 #ifdef HAVE_WAIT4
46         return wait4(pid, statptr, options, NULL);
47 #else
48         /* If wait4 is also not available, try wait3 for SVR3 variants */
49         /* Less ideal because can't actually request a specific pid */
50         /* At least the WNOHANG option is supported */
51         /* Code borrowed from apache fragment written by dwd@bell-labs.com */
52         int tmp_pid, dummystat;;
53         if (kill(pid, 0) == -1) {
54                 errno = ECHILD;
55                 return -1;
56         }
57         if (statptr == NULL)
58                 statptr = &dummystat;
59         while (((tmp_pid = wait3(statptr, options, 0)) != pid) &&
60                     (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
61             ;
62         return tmp_pid;
63 #endif
64 }
65 #endif
66
67
68 #ifndef HAVE_MEMMOVE
69  void *memmove(void *dest, const void *src, size_t n)
70 {
71         bcopy((char *) src, (char *) dest, n);
72         return dest;
73 }
74 #endif
75
76 #ifndef HAVE_STRPBRK
77 /**
78  * Find the first ocurrence in @p s of any character in @p accept.
79  *
80  * Derived from glibc
81  **/
82  char *strpbrk(const char *s, const char *accept)
83 {
84         while (*s != '\0')  {
85                 const char *a = accept;
86                 while (*a != '\0') {
87                         if (*a++ == *s) return (char *)s;
88                 }
89                 ++s;
90         }
91
92         return NULL;
93 }
94 #endif
95
96
97 #ifndef HAVE_STRLCPY
98 /**
99  * Like strncpy but does not 0 fill the buffer and always null
100  * terminates.
101  *
102  * @param bufsize is the size of the destination buffer.
103  *
104  * @return index of the terminating byte.
105  **/
106  size_t strlcpy(char *d, const char *s, size_t bufsize)
107 {
108         size_t len = strlen(s);
109         size_t ret = len;
110         if (bufsize > 0) {
111                 if (len >= bufsize)
112                         len = bufsize-1;
113                 memcpy(d, s, len);
114                 d[len] = 0;
115         }
116         return ret;
117 }
118 #endif
119
120 #ifndef HAVE_STRLCAT
121 /**
122  * Like strncat() but does not 0 fill the buffer and always null
123  * terminates.
124  *
125  * @param bufsize length of the buffer, which should be one more than
126  * the maximum resulting string length.
127  **/
128  size_t strlcat(char *d, const char *s, size_t bufsize)
129 {
130         size_t len1 = strlen(d);
131         size_t len2 = strlen(s);
132         size_t ret = len1 + len2;
133
134         if (len1 < bufsize - 1) {
135                 if (len2 >= bufsize - len1)
136                         len2 = bufsize - len1 - 1;
137                 memcpy(d+len1, s, len2);
138                 d[len1+len2] = 0;
139         }
140         return ret;
141 }
142 #endif
143
144 /* some systems don't take the 2nd argument */
145 int sys_gettimeofday(struct timeval *tv)
146 {
147 #ifdef HAVE_GETTIMEOFDAY_TZ
148         return gettimeofday(tv, NULL);
149 #else
150         return gettimeofday(tv);
151 #endif
152 }