1 /*
2  * MOC - music on console
3  * Copyright (C) 2005 Damian Pietras <daper@daper.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  */
11 
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15 
16 #include <pthread.h>
17 #include <string.h>
18 #include <errno.h>
19 
20 #include "common.h"
21 #include "log.h"
22 
23 /* Various functions which some systems lack. */
24 
25 #ifndef HAVE_STRCASESTR
26 /* Case insensitive version of strstr(). */
strcasestr(const char * haystack,const char * needle)27 char *strcasestr (const char *haystack, const char *needle)
28 {
29 	char *haystack_i, *needle_i;
30 	char *c;
31 	char *res;
32 
33 	haystack_i = xstrdup (haystack);
34 	needle_i = xstrdup (needle);
35 
36 	c = haystack_i;
37 	while (*c) {
38 		*c = tolower (*c);
39 		c++;
40 	}
41 
42 	c = needle_i;
43 	while (*c) {
44 		*c = tolower (*c);
45 		c++;
46 	}
47 
48 	res = strstr (haystack_i, needle_i);
49 	free (haystack_i);
50 	free (needle_i);
51 	return res ? res - haystack_i + (char *)haystack : NULL;
52 }
53 #endif
54 
55 #ifndef HAVE_STRERROR_R
56 static pthread_mutex_t strerror_r_mutex = PTHREAD_MUTEX_INITIALIZER;
57 #endif
58 
59 #ifndef HAVE_STRERROR_R
strerror_r(int errnum,char * buf,size_t n)60 int strerror_r (int errnum, char *buf, size_t n)
61 {
62 	char *err_str;
63 	int ret_val = 0;
64 
65 	LOCK (strerror_r_mutex);
66 
67 	err_str = strerror (errnum);
68 	if (strlen (err_str) >= n) {
69 		errno = ERANGE;
70 		ret_val = -1;
71 	}
72 	else
73 		strcpy (buf, err_str);
74 
75 	UNLOCK (strerror_r_mutex);
76 
77 	return ret_val;
78 }
79 #endif
80 
compat_cleanup()81 void compat_cleanup ()
82 {
83 #ifndef HAVE_STRERROR_R
84 	int rc;
85 
86 	rc = pthread_mutex_destroy (&strerror_r_mutex);
87 	if (rc != 0)
88 		logit ("Can't destroy strerror_r_mutex: %s", strerror (rc));
89 #endif
90 }
91