Anope IRC Services  Version 1.8
compat.c
Go to the documentation of this file.
1 /* Compatibility routines.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 
14 #include "services.h"
15 
16 /*************************************************************************/
17 
18 #if !HAVE_SNPRINTF
19 
20 /* [v]snprintf: Like [v]sprintf, but don't write more than len bytes
21  * (including null terminator). Return the number of bytes
22  * written.
23  */
24 
25 #if BAD_SNPRINTF
26 int vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
27 {
28  if (len <= 0)
29  return 0;
30  *buf = 0;
31  vsnprintf(buf, len, fmt, args);
32  buf[len - 1] = 0;
33  return strlen(buf);
34 }
35 #endif /* BAD_SNPRINTF */
36 
37 int snprintf(char *buf, size_t len, const char *fmt, ...)
38 {
39  va_list args;
40  int ret = 0;
41 
42  va_start(args, fmt);
43  ret = vsnprintf(buf, len, fmt, args);
44  va_end(args);
45  return ret;
46 }
47 
48 #endif /* !HAVE_SNPRINTF */
49 
50 /*************************************************************************/
51 
52 #if !HAVE_STRICMP && !HAVE_STRCASECMP
53 
54 /* stricmp, strnicmp: Case-insensitive versions of strcmp() and
55  * strncmp().
56  */
57 
58 int stricmp(const char *s1, const char *s2)
59 {
60  register int c;
61 
62  while ((c = tolower(*s1)) == tolower(*s2)) {
63  if (c == 0)
64  return 0;
65  s1++;
66  s2++;
67  }
68  if (c < tolower(*s2))
69  return -1;
70  return 1;
71 }
72 
73 int strnicmp(const char *s1, const char *s2, size_t len)
74 {
75  register int c;
76 
77  if (!len)
78  return 0;
79  while ((c = tolower(*s1)) == tolower(*s2) && len > 0) {
80  if (c == 0 || --len == 0)
81  return 0;
82  s1++;
83  s2++;
84  }
85  if (c < tolower(*s2))
86  return -1;
87  return 1;
88 }
89 #endif
90 
91 /*************************************************************************/
92 
93 #if !HAVE_STRDUP
94 char *strdup(const char *s)
95 {
96  char *new = calloc(strlen(s) + 1, 1);
97  if (new)
98  strcpy(new, s);
99  return new;
100 }
101 #endif
102 
103 /*************************************************************************/
104 
105 #if !HAVE_STRSPN
106 size_t strspn(const char *s, const char *accept)
107 {
108  size_t i = 0;
109 
110  while (*s && strchr(accept, *s))
111  ++i, ++s;
112  return i;
113 }
114 #endif
115 
116 /*************************************************************************/
117 
118 #if !HAVE_STRERROR
119 # if HAVE_SYS_ERRLIST
120 extern char *sys_errlist[];
121 # endif
122 
123 char *strerror(int errnum)
124 {
125 # if HAVE_SYS_ERRLIST
126  return sys_errlist[errnum];
127 # else
128  static char buf[20];
129  snprintf(buf, sizeof(buf), "Error %d", errnum);
130  return buf;
131 # endif
132 }
133 #endif
134 
135 /*************************************************************************/
136 
137 #if !HAVE_STRSIGNAL
138 /* Windows only supports 6 signals:
139  * SIGINT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, SIGTERM
140  * -- codemastr
141  */
142 char *strsignal(int signum)
143 {
144  static char buf[32];
145  switch (signum) {
146 #ifndef _WIN32
147  case SIGHUP:
148  strscpy(buf, "Hangup", sizeof(buf));
149  break;
150 #endif
151  case SIGINT:
152  strscpy(buf, "Interrupt", sizeof(buf));
153  break;
154 #ifndef _WIN32
155  case SIGQUIT:
156  strscpy(buf, "Quit", sizeof(buf));
157  break;
158 #endif
159 #ifdef SIGILL
160  case SIGILL:
161  strscpy(buf, "Illegal instruction", sizeof(buf));
162  break;
163 #endif
164 #ifdef SIGABRT
165  case SIGABRT:
166  strscpy(buf, "Abort", sizeof(buf));
167  break;
168 #endif
169 #if defined(SIGIOT) && (!defined(SIGABRT) || SIGIOT != SIGABRT)
170  case SIGIOT:
171  strscpy(buf, "IOT trap", sizeof(buf));
172  break;
173 #endif
174 #ifdef SIGBUS
175  case SIGBUS:
176  strscpy(buf, "Bus error", sizeof(buf));
177  break;
178 #endif
179  case SIGFPE:
180  strscpy(buf, "Floating point exception", sizeof(buf));
181  break;
182 #ifndef _WIN32
183  case SIGKILL:
184  strscpy(buf, "Killed", sizeof(buf));
185  break;
186  case SIGUSR1:
187  strscpy(buf, "User signal 1", sizeof(buf));
188  break;
189 #endif
190  case SIGSEGV:
191  strscpy(buf, "Segmentation fault", sizeof(buf));
192  break;
193 #ifndef _WIN32
194  case SIGUSR2:
195  strscpy(buf, "User signal 2", sizeof(buf));
196  break;
197  case SIGPIPE:
198  strscpy(buf, "Broken pipe", sizeof(buf));
199  break;
200  case SIGALRM:
201  strscpy(buf, "Alarm clock", sizeof(buf));
202  break;
203 #endif
204  case SIGTERM:
205  strscpy(buf, "Terminated", sizeof(buf));
206  break;
207 #ifndef _WIN32
208  case SIGSTOP:
209  strscpy(buf, "Suspended (signal)", sizeof(buf));
210  break;
211  case SIGTSTP:
212  strscpy(buf, "Suspended", sizeof(buf));
213  break;
214  case SIGIO:
215  strscpy(buf, "I/O error", sizeof(buf));
216  break;
217 #endif
218  default:
219  snprintf(buf, sizeof(buf), "Signal %d\n", signum);
220  break;
221  }
222  return buf;
223 }
224 #endif
225 
226 
227 /*************************************************************************/
int strnicmp(const char *s1, const char *s2, size_t len)
Definition: compat.c:73
int snprintf(char *buf, size_t len, const char *fmt,...)
Definition: compat.c:37
char * strdup(const char *s)
Definition: compat.c:94
E char * strscpy(char *d, const char *s, size_t len)
Definition: db-merger.c:1886
char * strsignal(int signum)
Definition: compat.c:142
Command * c
Definition: ns_recover.c:17
char * strerror(int errnum)
Definition: compat.c:123
#define tolower
Definition: services.h:190
E int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
int stricmp(const char *s1, const char *s2)
Definition: compat.c:58
size_t strspn(const char *s, const char *accept)
Definition: compat.c:106