1 /*
2  * Replacements for common but usually nonstandard functions that aren't
3  * supplied by all platforms.
4  *
5  * Copyright (C) 2009 by Dan Fandrich <dan@coneharvesters.com>, et. al.
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 #include "libbb.h"
10 
11 #ifndef HAVE_STRCHRNUL
strchrnul(const char * s,int c)12 char* FAST_FUNC strchrnul(const char *s, int c)
13 {
14 	while (*s != '\0' && *s != c)
15 		s++;
16 	return (char*)s;
17 }
18 #endif
19 
20 #ifndef HAVE_USLEEP
usleep(unsigned usec)21 int FAST_FUNC usleep(unsigned usec)
22 {
23 	struct timespec ts;
24 	ts.tv_sec = usec / 1000000u;
25 	ts.tv_nsec = (usec % 1000000u) * 1000u;
26 	/*
27 	 * If a signal has non-default handler, nanosleep returns early.
28 	 * Our version of usleep doesn't return early
29 	 * if interrupted by such signals:
30 	 *
31 	 */
32 	while (nanosleep(&ts, &ts) != 0)
33 		continue;
34 	return 0;
35 }
36 #endif
37 
38 #ifndef HAVE_VASPRINTF
vasprintf(char ** string_ptr,const char * format,va_list p)39 int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
40 {
41 	int r;
42 	va_list p2;
43 	char buf[128];
44 
45 	va_copy(p2, p);
46 	r = vsnprintf(buf, 128, format, p);
47 	va_end(p);
48 
49 	/* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
50 
51 	if (r < 128) {
52 		va_end(p2);
53 		*string_ptr = strdup(buf);
54 		return (*string_ptr ? r : -1);
55 	}
56 
57 	*string_ptr = malloc(r+1);
58 	r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1);
59 	va_end(p2);
60 
61 	return r;
62 }
63 #endif
64 
65 #ifndef HAVE_DPRINTF
66 /* dprintf is now part of POSIX.1, but was only added in 2008 */
dprintf(int fd,const char * format,...)67 int dprintf(int fd, const char *format, ...)
68 {
69 	va_list p;
70 	int r;
71 	char *string_ptr;
72 
73 	va_start(p, format);
74 	r = vasprintf(&string_ptr, format, p);
75 	va_end(p);
76 	if (r >= 0) {
77 		r = full_write(fd, string_ptr, r);
78 		free(string_ptr);
79 	}
80 	return r;
81 }
82 #endif
83 
84 #ifndef HAVE_MEMRCHR
85 /* Copyright (C) 2005 Free Software Foundation, Inc.
86  * memrchr() is a GNU function that might not be available everywhere.
87  * It's basically the inverse of memchr() - search backwards in a
88  * memory block for a particular character.
89  */
memrchr(const void * s,int c,size_t n)90 void* FAST_FUNC memrchr(const void *s, int c, size_t n)
91 {
92 	const char *start = s, *end = s;
93 
94 	end += n - 1;
95 
96 	while (end >= start) {
97 		if (*end == (char)c)
98 			return (void *) end;
99 		end--;
100 	}
101 
102 	return NULL;
103 }
104 #endif
105 
106 #ifndef HAVE_MKDTEMP
107 /* This is now actually part of POSIX.1, but was only added in 2008 */
mkdtemp(char * template)108 char* FAST_FUNC mkdtemp(char *template)
109 {
110 	if (mktemp(template) == NULL || mkdir(template, 0700) != 0)
111 		return NULL;
112 	return template;
113 }
114 #endif
115 
116 #ifndef HAVE_STRCASESTR
117 /* Copyright (c) 1999, 2000 The ht://Dig Group */
strcasestr(const char * s,const char * pattern)118 char* FAST_FUNC strcasestr(const char *s, const char *pattern)
119 {
120 	int length = strlen(pattern);
121 
122 	while (*s) {
123 		if (strncasecmp(s, pattern, length) == 0)
124 			return (char *)s;
125 		s++;
126 	}
127 	return 0;
128 }
129 #endif
130 
131 #ifndef HAVE_STRSEP
132 /* Copyright (C) 2004 Free Software Foundation, Inc. */
strsep(char ** stringp,const char * delim)133 char* FAST_FUNC strsep(char **stringp, const char *delim)
134 {
135 	char *start = *stringp;
136 	char *ptr;
137 
138 	if (!start)
139 		return NULL;
140 
141 	if (!*delim)
142 		ptr = start + strlen(start);
143 	else {
144 		ptr = strpbrk(start, delim);
145 		if (!ptr) {
146 			*stringp = NULL;
147 			return start;
148 		}
149 	}
150 
151 	*ptr = '\0';
152 	*stringp = ptr + 1;
153 
154 	return start;
155 }
156 #endif
157 
158 #ifndef HAVE_STPCPY
stpcpy(char * p,const char * to_add)159 char* FAST_FUNC stpcpy(char *p, const char *to_add)
160 {
161 	while ((*p = *to_add) != '\0') {
162 		p++;
163 		to_add++;
164 	}
165 	return p;
166 }
167 #endif
168 
169 #ifndef HAVE_GETLINE
getline(char ** lineptr,size_t * n,FILE * stream)170 ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
171 {
172 	int ch;
173 	char *line = *lineptr;
174 	size_t alloced = *n;
175 	size_t len = 0;
176 
177 	do {
178 		ch = fgetc(stream);
179 		if (ch == EOF)
180 			break;
181 		if (len + 1 >= alloced) {
182 			alloced += alloced/4 + 64;
183 			line = xrealloc(line, alloced);
184 		}
185 		line[len++] = ch;
186 	} while (ch != '\n');
187 
188 	if (len == 0)
189 		return -1;
190 
191 	line[len] = '\0';
192 	*lineptr = line;
193 	*n = alloced;
194 	return len;
195 }
196 #endif
197 
198 #ifndef HAVE_TTYNAME_R
ttyname_r(int fd,char * buf,size_t buflen)199 int ttyname_r(int fd, char *buf, size_t buflen)
200 {
201 	int r;
202 	char path[sizeof("/proc/self/fd/%d") + sizeof(int)*3];
203 
204 	if (!isatty(fd))
205 		return errno == EINVAL ? ENOTTY : errno;
206 	sprintf(path, "/proc/self/fd/%d", fd);
207 	r = readlink(path, buf, buflen);
208 	if (r < 0)
209 		return errno;
210 	if (r >= buflen)
211 		return ERANGE;
212 	buf[r] = '\0';
213 	return 0;
214 }
215 #endif
216