File: compat.c

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (158 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "def.h"

/* compat.c:  Copyright 2007 Dennis Holmes  (dholmes@rahul.net)
 *            except as noted.
 */

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#include "compat.h"

#ifndef HAVE_CLOCK_GETTIME
int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
  int r = 0;

#if defined(HAVE_GETTIMEOFDAY)
  struct timeval tv;
  if( (r = gettimeofday(&tv, (struct timezone *)0)) == 0 ){
    tp->tv_sec = tv.tv_sec;
    tp->tv_nsec = tv.tv_usec * 1000;
  }
#else
#error No suitable precision timing functions appear to be available!
#error Please report this problem and identify your system platform.
#endif

  return r;
}
#endif


#ifndef HAVE_VSNPRINTF
int vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
  int r;
  char *buffer[4*MAXPATHLEN];  /* git-r-dun */

  if( (r = vsprintf(buffer, format, ap)) >= 0){
    strncpy(str, buffer, size-1);
    str[size] = '\0';
  }
  return r;
}
#endif


#ifndef HAVE_SNPRINTF
int snprintf(char *str, size_t size, const char *format, ...)
{
  int r;
  va_list ap;

  va_start(ap, format);
  r = vsnprintf(str, size, format, ap);
  va_end(ap);

  return r;
}
#endif


#ifndef HAVE_STRNSTR
#include <string.h>
/* FUNCTION PROGRAMER: Siberiaic Sang */
char *strnstr(const char *haystack, const char *needle, size_t haystacklen)
{
  char *p;
  ssize_t plen;
  ssize_t len = strlen(needle);

  if( *needle == '\0' )
    return (char *)haystack;

  plen = haystacklen;
  for( p = (char *)haystack;
       p != (char *)0;
       p = (char *)memchr(p + 1, *needle, plen-1) ){
    plen = haystacklen - (p - haystack);

    if( plen < len ) return (char *)0;

    if( strncmp(p, needle, len) == 0 )
      return (p);
  }
  return (char*) 0;
}
#endif


#ifndef HAVE_STRNCASECMP
#include <ctype.h>
int strncasecmp(const char *s1, const char *s2, size_t len)
{
  unsigned char c1, c2;
  int r = 0;

  for( ; r==0 && len && *s1 && *s2; s1++, s2++, len-- ){
    if( *s1 == *s2 ) continue;
    c1 = (unsigned char)tolower((int)*s1);
    c2 = (unsigned char)tolower((int)*s2);
    if( c1 == c2 ) continue;
    else if( c1 < c2 ) r = -1;
    else if( c1 > c2 ) r = 1;
  }
  if( r==0 && len ){
    if( !*s1 && !*s2 ) r = 0;
    else if( *s1 ) r = 1;
    else r = -1;
  }
  return r;
}
#endif

#ifndef HAVE_STRCASECMP
int strcasecmp(const char *s1, const char *s2)
{
  return strncasecmp(s1, s2, strlen(s1));
}
#endif


#ifndef HAVE_RANDOM
long random(void)
{
  long result;
  unsigned long maxlong = 0, i = RAND_MAX;

  maxlong--;
  maxlong /= 2;

  result = (long)rand();
  while( i < maxlong ){
    result = (result * 2UL*(RAND_MAX+1UL)) | rand();
    i *= 2UL*(RAND_MAX+1UL);
  }
  return (result < 0) ? -result : result;
}

void srandom(unsigned long seed)
{
  unsigned useed;

  if( sizeof(seed) > sizeof(useed) ){
    unsigned long mask = 0xffff;
    int i = 2;
    while( i < sizeof(useed) ){
      mask = (mask << 16) | 0xffff;
      i += 2;
    }
    useed = (unsigned)(seed & mask);
  }else useed = seed;

  srand(useed);
}
#endif