]>
code.delx.au - spectrwm/blob - linux/linux.c
14 * All the workarounds for glibc stupidity are piled into this file...
17 /* --------------------------------------------------------------------------- */
18 /* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
21 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
23 * Permission to use, copy, modify, and distribute this software for any
24 * purpose with or without fee is hereby granted, provided that the above
25 * copyright notice and this permission notice appear in all copies.
27 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
28 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
30 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
32 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
33 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 * Copy src to string dst of size siz. At most siz-1 characters
38 * will be copied. Always NUL terminates (unless siz == 0).
39 * Returns strlen(src); if retval >= siz, truncation occurred.
42 strlcpy(char *dst
, const char *src
, size_t siz
)
48 /* Copy as many bytes as will fit */
49 if (n
!= 0 && --n
!= 0) {
51 if ((*d
++ = *s
++) == 0)
56 /* Not enough room in dst, add NUL and traverse rest of src */
59 *d
= '\0'; /* NUL-terminate dst */
64 return(s
- src
- 1); /* count does not include NUL */
67 /* --------------------------------------------------------------------------- */
68 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
71 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
73 * Permission to use, copy, modify, and distribute this software for any
74 * purpose with or without fee is hereby granted, provided that the above
75 * copyright notice and this permission notice appear in all copies.
77 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
78 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
79 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
80 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
81 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
82 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
83 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
87 * Appends src to string dst of size siz (unlike strncat, siz is the
88 * full size of dst, not space left). At most siz-1 characters
89 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
90 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
91 * If retval >= siz, truncation occurred.
94 strlcat(char *dst
, const char *src
, size_t siz
)
101 /* Find the end of dst and adjust bytes left but don't go past end */
102 while (n
-- != 0 && *d
!= '\0')
108 return(dlen
+ strlen(s
));
118 return(dlen
+ (s
- src
)); /* count does not include NUL */
121 /* --------------------------------------------------------------------------- */
122 /* $NetBSD: fgetln.c,v 1.3 2007/08/07 02:06:58 lukem Exp $ */
125 * Copyright (c) 1998 The NetBSD Foundation, Inc.
126 * All rights reserved.
128 * This code is derived from software contributed to The NetBSD Foundation
129 * by Christos Zoulas.
131 * Redistribution and use in source and binary forms, with or without
132 * modification, are permitted provided that the following conditions
134 * 1. Redistributions of source code must retain the above copyright
135 * notice, this list of conditions and the following disclaimer.
136 * 2. Redistributions in binary form must reproduce the above copyright
137 * notice, this list of conditions and the following disclaimer in the
138 * documentation and/or other materials provided with the distribution.
139 * 3. Neither the name of The NetBSD Foundation nor the names of its
140 * contributors may be used to endorse or promote products derived
141 * from this software without specific prior written permission.
143 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
144 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
145 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
146 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
147 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
148 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
149 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
150 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
151 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
152 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
153 * POSSIBILITY OF SUCH DAMAGE.
161 static char *buf
= NULL
;
162 static size_t bufsiz
= 0;
168 if ((buf
= malloc(bufsiz
)) == NULL
)
172 if (fgets(buf
, bufsiz
, fp
) == NULL
)
176 while ((ptr
= strchr(&buf
[*len
], '\n')) == NULL
) {
177 size_t nbufsiz
= bufsiz
+ BUFSIZ
;
178 char *nbuf
= realloc(buf
, nbufsiz
);
190 if (fgets(&buf
[bufsiz
], BUFSIZ
, fp
) == NULL
)
196 *len
= (ptr
- buf
) + 1;
200 /* --------------------------------------------------------------------------- */
201 /* $OpenBSD: fparseln.c,v 1.6 2005/08/02 21:46:23 espie Exp $ */
202 /* $NetBSD: fparseln.c,v 1.7 1999/07/02 15:49:12 simonb Exp $ */
205 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
207 * Redistribution and use in source and binary forms, with or without
208 * modification, are permitted provided that the following conditions
210 * 1. Redistributions of source code must retain the above copyright
211 * notice, this list of conditions and the following disclaimer.
212 * 2. Redistributions in binary form must reproduce the above copyright
213 * notice, this list of conditions and the following disclaimer in the
214 * documentation and/or other materials provided with the distribution.
215 * 3. All advertising materials mentioning features or use of this software
216 * must display the following acknowledgement:
217 * This product includes software developed by Christos Zoulas.
218 * 4. The name of the author may not be used to endorse or promote products
219 * derived from this software without specific prior written permission.
221 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
222 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
224 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
225 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
229 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
230 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
233 #define FPARSELN_UNESCESC 0x01
234 #define FPARSELN_UNESCCONT 0x02
235 #define FPARSELN_UNESCCOMM 0x04
236 #define FPARSELN_UNESCREST 0x08
237 #define FPARSELN_UNESCALL 0x0f
239 static int isescaped(const char *, const char *, int);
242 * Return true if the character in *p that belongs to a string
243 * that starts in *sp, is escaped by the escape character esc.
246 isescaped(const char *sp
, const char *p
, int esc
)
251 /* No escape character */
255 /* Count the number of escape characters that precede ours */
256 for (ne
= 0, cp
= p
; --cp
>= sp
&& *cp
== esc
; ne
++)
259 /* Return true if odd number of escape characters */
260 return (ne
& 1) != 0;
265 * Read a line from a file parsing continuations ending in \
266 * and eliminating trailing newlines, or comments starting with
270 fparseln(FILE *fp
, size_t *size
, size_t *lineno
, const char str
[3],
273 static const char dstr
[3] = { '\\', '\\', '#' };
274 char *buf
= NULL
, *ptr
, *cp
, esc
, con
, nl
, com
;
286 * XXX: it would be cool to be able to specify the newline character,
287 * but unfortunately, fgetln does not let us
297 if ((ptr
= fgetln(fp
, &s
)) == NULL
)
300 if (s
&& com
) { /* Check and eliminate comments */
301 for (cp
= ptr
; cp
< ptr
+ s
; cp
++)
302 if (*cp
== com
&& !isescaped(ptr
, cp
, esc
)) {
304 cnt
= s
== 0 && buf
== NULL
;
309 if (s
&& nl
) { /* Check and eliminate newlines */
313 s
--; /* forget newline */
316 if (s
&& con
) { /* Check and eliminate continuations */
319 if (*cp
== con
&& !isescaped(ptr
, cp
, esc
)) {
320 s
--; /* forget escape */
325 if (s
== 0 && buf
!= NULL
)
328 if ((cp
= realloc(buf
, len
+ s
+ 1)) == NULL
) {
334 (void) memcpy(buf
+ len
, ptr
, s
);
339 if ((flags
& FPARSELN_UNESCALL
) != 0 && esc
&& buf
!= NULL
&&
340 strchr(buf
, esc
) != NULL
) {
342 while (cp
[0] != '\0') {
345 while (cp
[0] != '\0' && cp
[0] != esc
)
347 if (cp
[0] == '\0' || cp
[1] == '\0')
352 skipesc
+= (flags
& FPARSELN_UNESCCOMM
);
354 skipesc
+= (flags
& FPARSELN_UNESCCONT
);
356 skipesc
+= (flags
& FPARSELN_UNESCESC
);
357 if (cp
[1] != com
&& cp
[1] != con
&& cp
[1] != esc
)
358 skipesc
= (flags
& FPARSELN_UNESCREST
);
375 /* --------------------------------------------------------------------------- */
376 /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
379 * Copyright (c) 2004 Ted Unangst and Todd Miller
380 * All rights reserved.
382 * Permission to use, copy, modify, and distribute this software for any
383 * purpose with or without fee is hereby granted, provided that the above
384 * copyright notice and this permission notice appear in all copies.
386 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
387 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
388 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
389 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
390 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
391 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
392 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
400 strtonum(const char *numstr
, long long minval
, long long maxval
,
401 const char **errstrp
)
411 { "invalid", EINVAL
},
412 { "too small", ERANGE
},
413 { "too large", ERANGE
},
421 ll
= strtoll(numstr
, &ep
, 10);
422 if (numstr
== ep
|| *ep
!= '\0')
424 else if ((ll
== LLONG_MIN
&& errno
== ERANGE
) || ll
< minval
)
426 else if ((ll
== LLONG_MAX
&& errno
== ERANGE
) || ll
> maxval
)
430 *errstrp
= ev
[error
].errstr
;
431 errno
= ev
[error
].err
;