Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

compat.c

Go to the documentation of this file.
00001 /* 
00002    Copyright (C) Andrew Tridgell 1998
00003    Copyright (C) 2002 by Martin Pool
00004    
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009    
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014    
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /**
00021  * @file compat.c
00022  *
00023  * Reimplementations of standard functions for platforms that don't
00024  * have them.
00025  **/
00026 
00027 
00028 
00029 #include "rsync.h"
00030 
00031 
00032 #ifndef HAVE_STRDUP
00033  char *strdup(char *s)
00034 {
00035   int l = strlen(s) + 1;
00036   char *ret = (char *)malloc(l);
00037   if (ret)
00038     strcpy(ret,s);
00039   return ret;
00040 }
00041 #endif
00042 
00043 #ifndef HAVE_GETCWD
00044  char *getcwd(char *buf, int size)
00045 {
00046         return getwd(buf);
00047 }
00048 #endif
00049 
00050 
00051 #ifndef HAVE_WAITPID
00052  pid_t waitpid(pid_t pid, int *statptr, int options)
00053 {
00054 #ifdef HAVE_WAIT4
00055         return wait4(pid, statptr, options, NULL);
00056 #else
00057         /* If wait4 is also not available, try wait3 for SVR3 variants */
00058         /* Less ideal because can't actually request a specific pid */
00059         /* At least the WNOHANG option is supported */
00060         /* Code borrowed from apache fragment written by dwd@bell-labs.com */
00061         int tmp_pid, dummystat;;
00062         if (kill(pid, 0) == -1) {
00063                 errno = ECHILD;
00064                 return -1;
00065         }
00066         if (statptr == NULL)
00067                 statptr = &dummystat;
00068         while (((tmp_pid = wait3(statptr, options, 0)) != pid) &&
00069                     (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
00070             ;
00071         return tmp_pid;
00072 #endif
00073 }
00074 #endif
00075 
00076 
00077 #ifndef HAVE_MEMMOVE
00078  void *memmove(void *dest, const void *src, size_t n)
00079 {
00080         bcopy((char *) src, (char *) dest, n);
00081         return dest;
00082 }
00083 #endif
00084 
00085 #ifndef HAVE_STRPBRK
00086 /**
00087  * Find the first ocurrence in @p s of any character in @p accept.
00088  *
00089  * Derived from glibc 
00090  **/
00091  char *strpbrk(const char *s, const char *accept)
00092 {
00093         while (*s != '\0')  {
00094                 const char *a = accept;
00095                 while (*a != '\0') {
00096                         if (*a++ == *s) return (char *)s;
00097                 }
00098                 ++s;
00099         }
00100 
00101         return NULL;
00102 }
00103 #endif
00104 
00105 
00106 #ifndef HAVE_STRLCPY
00107 /**
00108  * Like strncpy but does not 0 fill the buffer and always null 
00109  * terminates.
00110  *
00111  * @param bufsize is the size of the destination buffer.
00112  *
00113  * @return index of the terminating byte.
00114  **/
00115  size_t strlcpy(char *d, const char *s, size_t bufsize)
00116 {
00117         size_t len = strlen(s);
00118         size_t ret = len;
00119         if (bufsize <= 0) return 0;
00120         if (len >= bufsize) len = bufsize-1;
00121         memcpy(d, s, len);
00122         d[len] = 0;
00123         return ret;
00124 }
00125 #endif
00126 
00127 #ifndef HAVE_STRLCAT
00128 /**
00129  * Like strncat() but does not 0 fill the buffer and always null 
00130  * terminates.
00131  *
00132  * @param bufsize length of the buffer, which should be one more than
00133  * the maximum resulting string length.
00134  **/
00135  size_t strlcat(char *d, const char *s, size_t bufsize)
00136 {
00137         size_t len1 = strlen(d);
00138         size_t len2 = strlen(s);
00139         size_t ret = len1 + len2;
00140 
00141         if (len1+len2 >= bufsize) {
00142                 len2 = bufsize - (len1+1);
00143         }
00144         if (len2 > 0) {
00145                 memcpy(d+len1, s, len2);
00146                 d[len1+len2] = 0;
00147         }
00148         return ret;
00149 }
00150 #endif
00151 
00152 #ifdef REPLACE_INET_NTOA
00153  char *rep_inet_ntoa(struct in_addr ip)
00154 {
00155         unsigned char *p = (unsigned char *)&ip.s_addr;
00156         static char buf[18];
00157 #if WORDS_BIGENDIAN
00158         snprintf(buf, 18, "%d.%d.%d.%d", 
00159                  (int)p[0], (int)p[1], (int)p[2], (int)p[3]);
00160 #else
00161         snprintf(buf, 18, "%d.%d.%d.%d", 
00162                  (int)p[3], (int)p[2], (int)p[1], (int)p[0]);
00163 #endif
00164         return buf;
00165 }
00166 #endif
00167 
00168 #ifdef REPLACE_INET_ATON
00169  int inet_aton(const char *cp, struct in_addr *inp)
00170 {
00171         unsigned int a1, a2, a3, a4;
00172         unsigned long ret;
00173 
00174         if (strcmp(cp, "255.255.255.255") == 0) {
00175                 inp->s_addr = (unsigned) -1;
00176                 return 0;
00177         }
00178 
00179         if (sscanf(cp, "%u.%u.%u.%u", &a1, &a2, &a3, &a4) != 4 ||
00180             a1 > 255 || a2 > 255 || a3 > 255 || a4 > 255) {
00181                 return 0;
00182         }
00183 
00184         ret = (a1 << 24) | (a2 << 16) | (a3 << 8) | a4;
00185 
00186         inp->s_addr = htonl(ret);
00187         
00188         if (inp->s_addr == (unsigned) -1) {
00189                 return 0;
00190         }
00191         return 1;
00192 }
00193 #endif
00194 
00195 /* some systems don't take the 2nd argument */
00196 int sys_gettimeofday(struct timeval *tv)
00197 {
00198 #if HAVE_GETTIMEOFDAY_TZ
00199         return gettimeofday(tv, NULL);
00200 #else
00201         return gettimeofday(tv);
00202 #endif
00203 }

Generated on Tue Apr 16 12:37:36 2002 for rsync by doxygen1.2.15