blob: 19734517b13bddfc7c606301019e2f93ae9ffb5d [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
20#ifndef HAVE_VASPRINTF
21int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
22{
23 int r;
24 va_list p2;
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010025 char buf[128];
Dan Fandrich21a542d2009-10-27 11:05:00 +010026
27 va_copy(p2, p);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010028 r = vsnprintf(buf, 128, format, p);
Dan Fandrich21a542d2009-10-27 11:05:00 +010029 va_end(p);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010030
Denys Vlasenkob5fc5112013-02-07 16:06:54 +010031 /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
32
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010033 if (r < 128) {
34 va_end(p2);
Denys Vlasenkob5fc5112013-02-07 16:06:54 +010035 *string_ptr = strdup(buf);
Denys Vlasenko272d85c2013-02-10 23:03:38 +010036 return (*string_ptr ? r : -1);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010037 }
38
Denys Vlasenkob5fc5112013-02-07 16:06:54 +010039 *string_ptr = malloc(r+1);
40 r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1);
Dan Fandrich21a542d2009-10-27 11:05:00 +010041 va_end(p2);
42
43 return r;
44}
45#endif
46
Denys Vlasenko47061b42011-04-17 23:14:19 +020047#ifndef HAVE_DPRINTF
48/* dprintf is now part of POSIX.1, but was only added in 2008 */
49int dprintf(int fd, const char *format, ...)
Dan Fandrich21a542d2009-10-27 11:05:00 +010050{
51 va_list p;
52 int r;
53 char *string_ptr;
54
55 va_start(p, format);
56 r = vasprintf(&string_ptr, format, p);
57 va_end(p);
58 if (r >= 0) {
59 r = full_write(fd, string_ptr, r);
60 free(string_ptr);
61 }
62 return r;
63}
64#endif
65
Dan Fandrichfe4e23f2009-11-01 04:01:30 +010066#ifndef HAVE_MEMRCHR
67/* Copyright (C) 2005 Free Software Foundation, Inc.
68 * memrchr() is a GNU function that might not be available everywhere.
69 * It's basically the inverse of memchr() - search backwards in a
70 * memory block for a particular character.
71 */
72void* FAST_FUNC memrchr(const void *s, int c, size_t n)
73{
74 const char *start = s, *end = s;
75
76 end += n - 1;
77
78 while (end >= start) {
79 if (*end == (char)c)
80 return (void *) end;
81 end--;
82 }
83
84 return NULL;
85}
86#endif
87
88#ifndef HAVE_MKDTEMP
89/* This is now actually part of POSIX.1, but was only added in 2008 */
90char* FAST_FUNC mkdtemp(char *template)
91{
92 if (mktemp(template) == NULL || mkdir(template, 0700) != 0)
93 return NULL;
94 return template;
95}
96#endif
97
98#ifndef HAVE_STRCASESTR
99/* Copyright (c) 1999, 2000 The ht://Dig Group */
100char* FAST_FUNC strcasestr(const char *s, const char *pattern)
101{
102 int length = strlen(pattern);
103
104 while (*s) {
105 if (strncasecmp(s, pattern, length) == 0)
106 return (char *)s;
107 s++;
108 }
109 return 0;
110}
111#endif
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700112
113#ifndef HAVE_STRSEP
114/* Copyright (C) 2004 Free Software Foundation, Inc. */
115char* FAST_FUNC strsep(char **stringp, const char *delim)
116{
117 char *start = *stringp;
118 char *ptr;
119
120 if (!start)
121 return NULL;
122
123 if (!*delim)
124 ptr = start + strlen(start);
125 else {
126 ptr = strpbrk(start, delim);
127 if (!ptr) {
128 *stringp = NULL;
129 return start;
130 }
131 }
132
133 *ptr = '\0';
134 *stringp = ptr + 1;
135
136 return start;
137}
138#endif
Dan Fandrichdc506762011-02-12 22:26:57 -0800139
140#ifndef HAVE_STPCPY
141char* FAST_FUNC stpcpy(char *p, const char *to_add)
142{
143 while ((*p = *to_add) != '\0') {
144 p++;
145 to_add++;
146 }
147 return p;
148}
149#endif
Timo Teras0a5b3102011-06-29 02:19:58 +0200150
151#ifndef HAVE_GETLINE
152ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
153{
154 int ch;
155 char *line = *lineptr;
156 size_t alloced = *n;
157 size_t len = 0;
158
159 do {
160 ch = fgetc(stream);
161 if (ch == EOF)
162 break;
163 if (len + 1 >= alloced) {
164 alloced += alloced/4 + 64;
165 line = xrealloc(line, alloced);
166 }
167 line[len++] = ch;
168 } while (ch != '\n');
169
170 if (len == 0)
171 return -1;
172
173 line[len] = '\0';
174 *lineptr = line;
175 *n = alloced;
176 return len;
177}
178#endif