-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompat.c
90 lines (73 loc) · 1.64 KB
/
compat.c
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
/*
* MOC - music on console
* Copyright (C) 2005 Damian Pietras <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <pthread.h>
#include <log.h>
#include <string.h>
#include <errno.h>
#include "common.h"
/* Various functions which some systems lack. */
#ifndef HAVE_STRCASESTR
/* Case insensitive version of strstr(). */
char *strcasestr (const char *haystack, const char *needle)
{
char *haystack_i, *needle_i;
char *c;
char *res;
haystack_i = xstrdup (haystack);
needle_i = xstrdup (needle);
c = haystack_i;
while (*c) {
*c = tolower (*c);
c++;
}
c = needle_i;
while (*c) {
*c = tolower (*c);
c++;
}
res = strstr (haystack_i, needle_i);
free (haystack_i);
free (needle_i);
return res ? res - haystack_i + (char *)haystack : NULL;
}
#endif
#ifndef HAVE_STRERROR_R
static pthread_mutex_t strerror_r_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#ifndef HAVE_STRERROR_R
int strerror_r (int errnum, char *buf, size_t n)
{
char *err_str;
int ret_val = 0;
LOCK (strerror_r_mutex);
err_str = strerror (errnum);
if (strlen (err_str) >= n) {
errno = ERANGE;
ret_val = -1;
}
else
strcpy (buf, err_str);
UNLOCK (strerror_r_mutex);
return ret_val;
}
#endif
void compat_cleanup ()
{
#ifndef HAVE_STRERROR_R
int rc;
rc = pthread_mutex_destroy (&strerror_r_mutex);
if (rc != 0)
logit ("Can't destroy strerror_r_mutex: %s", strerror (rc));
#endif
}