blob: 8d90ca4e9aa3729fbb34e8f0ed58e173ee8579aa [file] [log] [blame]
Dan Fandrich21a542d2009-10-27 11:05:00 +01001/*
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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Dan Fandrich21a542d2009-10-27 11:05:00 +01008 */
Dan Fandrich21a542d2009-10-27 11:05:00 +01009#include "libbb.h"
10
11#ifndef HAVE_STRCHRNUL
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010012char* FAST_FUNC strchrnul(const char *s, int c)
Dan Fandrich21a542d2009-10-27 11:05:00 +010013{
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010014 while (*s != '\0' && *s != c)
15 s++;
Dan Fandrich21a542d2009-10-27 11:05:00 +010016 return (char*)s;
17}
18#endif
19
Bernhard Reutner-Fischerad167412014-04-13 16:37:57 +020020#ifndef HAVE_USLEEP
21int 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
Dan Fandrich21a542d2009-10-27 11:05:00 +010038#ifndef HAVE_VASPRINTF
39int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
40{
41 int r;
42 va_list p2;
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010043 char buf[128];
Dan Fandrich21a542d2009-10-27 11:05:00 +010044
45 va_copy(p2, p);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010046 r = vsnprintf(buf, 128, format, p);
Dan Fandrich21a542d2009-10-27 11:05:00 +010047 va_end(p);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010048
Denys Vlasenkob5fc5112013-02-07 16:06:54 +010049 /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
50
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010051 if (r < 128) {
52 va_end(p2);
Denys Vlasenkob5fc5112013-02-07 16:06:54 +010053 *string_ptr = strdup(buf);
Denys Vlasenko272d85c2013-02-10 23:03:38 +010054 return (*string_ptr ? r : -1);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010055 }
56
Denys Vlasenkob5fc5112013-02-07 16:06:54 +010057 *string_ptr = malloc(r+1);
58 r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1);
Dan Fandrich21a542d2009-10-27 11:05:00 +010059 va_end(p2);
60
61 return r;
62}
63#endif
64
Denys Vlasenko47061b42011-04-17 23:14:19 +020065#ifndef HAVE_DPRINTF
66/* dprintf is now part of POSIX.1, but was only added in 2008 */
67int dprintf(int fd, const char *format, ...)
Dan Fandrich21a542d2009-10-27 11:05:00 +010068{
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
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010084#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 */
90void* 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 */
108char* 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 */
118char* 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
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700130
131#ifndef HAVE_STRSEP
132/* Copyright (C) 2004 Free Software Foundation, Inc. */
133char* 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
Dan Fandrichdc506762011-02-12 22:26:57 -0800157
158#ifndef HAVE_STPCPY
159char* 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
Timo Teras0a5b3102011-06-29 02:19:58 +0200168
169#ifndef HAVE_GETLINE
170ssize_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