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