1 /* $scrotwm: linux.c,v 1.1 2009/01/22 23:12:27 marco Exp $ */
5 #include <sys/socket.h>
17 * All the workarounds for glibc stupidity are piled into this file...
20 /* --------------------------------------------------------------------------- */
21 /* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
24 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
26 * Permission to use, copy, modify, and distribute this software for any
27 * purpose with or without fee is hereby granted, provided that the above
28 * copyright notice and this permission notice appear in all copies.
30 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
31 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
32 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
33 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
35 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
36 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 * Copy src to string dst of size siz. At most siz-1 characters
41 * will be copied. Always NUL terminates (unless siz == 0).
42 * Returns strlen(src); if retval >= siz, truncation occurred.
45 strlcpy(char *dst
, const char *src
, size_t siz
)
51 /* Copy as many bytes as will fit */
52 if (n
!= 0 && --n
!= 0) {
54 if ((*d
++ = *s
++) == 0)
59 /* Not enough room in dst, add NUL and traverse rest of src */
62 *d
= '\0'; /* NUL-terminate dst */
67 return(s
- src
- 1); /* count does not include NUL */
70 /* --------------------------------------------------------------------------- */
71 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
74 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
76 * Permission to use, copy, modify, and distribute this software for any
77 * purpose with or without fee is hereby granted, provided that the above
78 * copyright notice and this permission notice appear in all copies.
80 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
81 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
82 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
83 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
84 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
85 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
86 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
90 * Appends src to string dst of size siz (unlike strncat, siz is the
91 * full size of dst, not space left). At most siz-1 characters
92 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
93 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
94 * If retval >= siz, truncation occurred.
97 strlcat(char *dst
, const char *src
, size_t siz
)
104 /* Find the end of dst and adjust bytes left but don't go past end */
105 while (n
-- != 0 && *d
!= '\0')
111 return(dlen
+ strlen(s
));
121 return(dlen
+ (s
- src
)); /* count does not include NUL */
124 /* --------------------------------------------------------------------------- */
125 /* $NetBSD: fgetln.c,v 1.3 2007/08/07 02:06:58 lukem Exp $ */
128 * Copyright (c) 1998 The NetBSD Foundation, Inc.
129 * All rights reserved.
131 * This code is derived from software contributed to The NetBSD Foundation
132 * by Christos Zoulas.
134 * Redistribution and use in source and binary forms, with or without
135 * modification, are permitted provided that the following conditions
137 * 1. Redistributions of source code must retain the above copyright
138 * notice, this list of conditions and the following disclaimer.
139 * 2. Redistributions in binary form must reproduce the above copyright
140 * notice, this list of conditions and the following disclaimer in the
141 * documentation and/or other materials provided with the distribution.
142 * 3. Neither the name of The NetBSD Foundation nor the names of its
143 * contributors may be used to endorse or promote products derived
144 * from this software without specific prior written permission.
146 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
147 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
148 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
149 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
150 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
151 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
152 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
153 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
154 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
155 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
156 * POSSIBILITY OF SUCH DAMAGE.
164 static char *buf
= NULL
;
165 static size_t bufsiz
= 0;
171 if ((buf
= malloc(bufsiz
)) == NULL
)
175 if (fgets(buf
, bufsiz
, fp
) == NULL
)
179 while ((ptr
= strchr(&buf
[*len
], '\n')) == NULL
) {
180 size_t nbufsiz
= bufsiz
+ BUFSIZ
;
181 char *nbuf
= realloc(buf
, nbufsiz
);
193 if (fgets(&buf
[bufsiz
], BUFSIZ
, fp
) == NULL
)
199 *len
= (ptr
- buf
) + 1;
203 /* --------------------------------------------------------------------------- */
204 /* $OpenBSD: fparseln.c,v 1.6 2005/08/02 21:46:23 espie Exp $ */
205 /* $NetBSD: fparseln.c,v 1.7 1999/07/02 15:49:12 simonb Exp $ */
208 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
210 * Redistribution and use in source and binary forms, with or without
211 * modification, are permitted provided that the following conditions
213 * 1. Redistributions of source code must retain the above copyright
214 * notice, this list of conditions and the following disclaimer.
215 * 2. Redistributions in binary form must reproduce the above copyright
216 * notice, this list of conditions and the following disclaimer in the
217 * documentation and/or other materials provided with the distribution.
218 * 3. All advertising materials mentioning features or use of this software
219 * must display the following acknowledgement:
220 * This product includes software developed by Christos Zoulas.
221 * 4. The name of the author may not be used to endorse or promote products
222 * derived from this software without specific prior written permission.
224 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
225 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
226 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
227 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
230 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
233 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
236 #define FPARSELN_UNESCESC 0x01
237 #define FPARSELN_UNESCCONT 0x02
238 #define FPARSELN_UNESCCOMM 0x04
239 #define FPARSELN_UNESCREST 0x08
240 #define FPARSELN_UNESCALL 0x0f
242 static int isescaped(const char *, const char *, int);
245 * Return true if the character in *p that belongs to a string
246 * that starts in *sp, is escaped by the escape character esc.
249 isescaped(const char *sp
, const char *p
, int esc
)
254 /* No escape character */
258 /* Count the number of escape characters that precede ours */
259 for (ne
= 0, cp
= p
; --cp
>= sp
&& *cp
== esc
; ne
++)
262 /* Return true if odd number of escape characters */
263 return (ne
& 1) != 0;
268 * Read a line from a file parsing continuations ending in \
269 * and eliminating trailing newlines, or comments starting with
273 fparseln(FILE *fp
, size_t *size
, size_t *lineno
, const char str
[3],
276 static const char dstr
[3] = { '\\', '\\', '#' };
277 char *buf
= NULL
, *ptr
, *cp
, esc
, con
, nl
, com
;
289 * XXX: it would be cool to be able to specify the newline character,
290 * but unfortunately, fgetln does not let us
300 if ((ptr
= fgetln(fp
, &s
)) == NULL
)
303 if (s
&& com
) { /* Check and eliminate comments */
304 for (cp
= ptr
; cp
< ptr
+ s
; cp
++)
305 if (*cp
== com
&& !isescaped(ptr
, cp
, esc
)) {
307 cnt
= s
== 0 && buf
== NULL
;
312 if (s
&& nl
) { /* Check and eliminate newlines */
316 s
--; /* forget newline */
319 if (s
&& con
) { /* Check and eliminate continuations */
322 if (*cp
== con
&& !isescaped(ptr
, cp
, esc
)) {
323 s
--; /* forget escape */
328 if (s
== 0 && buf
!= NULL
)
331 if ((cp
= realloc(buf
, len
+ s
+ 1)) == NULL
) {
337 (void) memcpy(buf
+ len
, ptr
, s
);
342 if ((flags
& FPARSELN_UNESCALL
) != 0 && esc
&& buf
!= NULL
&&
343 strchr(buf
, esc
) != NULL
) {
345 while (cp
[0] != '\0') {
348 while (cp
[0] != '\0' && cp
[0] != esc
)
350 if (cp
[0] == '\0' || cp
[1] == '\0')
355 skipesc
+= (flags
& FPARSELN_UNESCCOMM
);
357 skipesc
+= (flags
& FPARSELN_UNESCCONT
);
359 skipesc
+= (flags
& FPARSELN_UNESCESC
);
360 if (cp
[1] != com
&& cp
[1] != con
&& cp
[1] != esc
)
361 skipesc
= (flags
& FPARSELN_UNESCREST
);
378 /* --------------------------------------------------------------------------- */
379 /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
382 * Copyright (c) 2004 Ted Unangst and Todd Miller
383 * All rights reserved.
385 * Permission to use, copy, modify, and distribute this software for any
386 * purpose with or without fee is hereby granted, provided that the above
387 * copyright notice and this permission notice appear in all copies.
389 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
390 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
391 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
392 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
393 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
394 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
395 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
403 strtonum(const char *numstr
, long long minval
, long long maxval
,
404 const char **errstrp
)
414 { "invalid", EINVAL
},
415 { "too small", ERANGE
},
416 { "too large", ERANGE
},
424 ll
= strtoll(numstr
, &ep
, 10);
425 if (numstr
== ep
|| *ep
!= '\0')
427 else if ((ll
== LLONG_MIN
&& errno
== ERANGE
) || ll
< minval
)
429 else if ((ll
== LLONG_MAX
&& errno
== ERANGE
) || ll
> maxval
)
433 *errstrp
= ev
[error
].errstr
;
434 errno
= ev
[error
].err
;
441 /* $OpenBSD: fmt_scaled.c,v 1.10 2009/06/20 15:00:04 martynas Exp $ */
444 * Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
446 * Redistribution and use in source and binary forms, with or without
447 * modification, are permitted provided that the following conditions
449 * 1. Redistributions of source code must retain the above copyright
450 * notice, this list of conditions and the following disclaimer.
451 * 2. Redistributions in binary form must reproduce the above copyright
452 * notice, this list of conditions and the following disclaimer in the
453 * documentation and/or other materials provided with the distribution.
454 * 3. The name of the author may not be used to endorse or promote products
455 * derived from this software without specific prior written permission.
457 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
458 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
459 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
460 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
461 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
462 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
463 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
464 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
465 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
466 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
470 * fmt_scaled: Format numbers scaled for human comprehension
471 * scan_scaled: Scan numbers in this format.
473 * "Human-readable" output uses 4 digits max, and puts a unit suffix at
474 * the end. Makes output compact and easy-to-read esp. on huge disks.
475 * Formatting code was originally in OpenBSD "df", converted to library routine.
476 * Scanning code written for OpenBSD libutil.
489 NONE
= 0, KILO
= 1, MEGA
= 2, GIGA
= 3, TERA
= 4, PETA
= 5, EXA
= 6
492 /* These three arrays MUST be in sync! XXX make a struct */
493 static unit_type units
[] = { NONE
, KILO
, MEGA
, GIGA
, TERA
, PETA
, EXA
};
494 static char scale_chars
[] = "BKMGTPE";
495 static long long scale_factors
[] = {
500 1024LL*1024*1024*1024,
501 1024LL*1024*1024*1024*1024,
502 1024LL*1024*1024*1024*1024*1024,
504 #define SCALE_LENGTH (sizeof(units)/sizeof(units[0]))
506 #define MAX_DIGITS (SCALE_LENGTH * 3) /* XXX strlen(sprintf("%lld", -1)? */
508 /* Convert the given input string "scaled" into numeric in "result".
509 * Return 0 on success, -1 and errno set on error.
512 scan_scaled(char *scaled
, long long *result
)
516 unsigned int i
, ndigits
= 0, fract_digits
= 0;
517 long long scale_fact
= 1, whole
= 0, fpart
= 0;
519 /* Skip leading whitespace */
520 while (isascii(*p
) && isspace(*p
))
523 /* Then at most one leading + or - */
524 while (*p
== '-' || *p
== '+') {
532 } else if (*p
== '+') {
542 /* Main loop: Scan digits, find decimal point, if present.
543 * We don't allow exponentials, so no scientific notation
544 * (but note that E for Exa might look like e to some!).
545 * Advance 'p' to end, to get scale factor.
547 for (; isascii(*p
) && (isdigit(*p
) || *p
=='.'); ++p
) {
549 if (fract_digits
> 0) { /* oops, more than one '.' */
557 i
= (*p
) - '0'; /* whew! finally a digit we can use */
558 if (fract_digits
> 0) {
559 if (fract_digits
>= MAX_DIGITS
-1)
560 /* ignore extra fractional digits */
562 fract_digits
++; /* for later scaling */
565 } else { /* normal digit */
566 if (++ndigits
>= MAX_DIGITS
) {
580 /* If no scale factor given, we're done. fraction is discarded. */
586 /* Validate scale factor, and scale whole and fraction by it. */
587 for (i
= 0; i
< SCALE_LENGTH
; i
++) {
589 /* Are we there yet? */
590 if (*p
== scale_chars
[i
] ||
591 *p
== tolower(scale_chars
[i
])) {
593 /* If it ends with alphanumerics after the scale char, bad. */
594 if (isalnum(*(p
+1))) {
598 scale_fact
= scale_factors
[i
];
600 /* scale whole part */
603 /* truncate fpart so it does't overflow.
604 * then scale fractional part.
606 while (fpart
>= LLONG_MAX
/ scale_fact
) {
611 if (fract_digits
> 0) {
612 for (i
= 0; i
< fract_digits
-1; i
++)
624 /* Format the given "number" into human-readable form in "result".
625 * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
626 * Return 0 on success, -1 and errno set if error.
629 fmt_scaled(long long number
, char *result
)
631 long long abval
, fract
= 0;
633 unit_type unit
= NONE
;
635 abval
= llabs(number
);
637 /* Not every negative long long has a positive representation.
638 * Also check for numbers that are just too darned big to format
640 if (abval
< 0 || abval
/ 1024 >= scale_factors
[SCALE_LENGTH
-1]) {
645 /* scale whole part; get unscaled fraction */
646 for (i
= 0; i
< SCALE_LENGTH
; i
++) {
647 if (abval
/1024 < scale_factors
[i
]) {
649 fract
= (i
== 0) ? 0 : abval
% scale_factors
[i
];
650 number
/= scale_factors
[i
];
652 fract
/= scale_factors
[i
- 1];
657 fract
= (10 * fract
+ 512) / 1024;
658 /* if the result would be >= 10, round main number */
668 strlcpy(result
, "0B", FMT_SCALED_STRSIZE
);
669 else if (unit
== NONE
|| number
>= 100 || number
<= -100) {
676 (void)snprintf(result
, FMT_SCALED_STRSIZE
, "%lld%c",
677 number
, scale_chars
[unit
]);
679 (void)snprintf(result
, FMT_SCALED_STRSIZE
, "%lld.%1lld%c",
680 number
, fract
, scale_chars
[unit
]);
685 /* --------------------------------------------------------------------------- */
688 * Copyright (c) 2002,2004 Damien Miller <djm@mindrot.org>
690 * Permission to use, copy, modify, and distribute this software for any
691 * purpose with or without fee is hereby granted, provided that the above
692 * copyright notice and this permission notice appear in all copies.
694 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
695 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
696 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
697 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
698 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
699 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
700 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
703 /* Get effective user and group identification of locally-connected
707 getpeereid(int s
, uid_t
*euid
, gid_t
*gid
)
710 socklen_t len
= sizeof(cred
);
712 if (getsockopt(s
, SOL_SOCKET
, SO_PEERCRED
, &cred
, &len
) < 0)
722 * This is the original version of the program in the man page.
723 * Copy-and-paste whatever you need from it.
726 main(int argc
, char **argv
)
728 char *cinput
= "1.5K", buf
[FMT_SCALED_STRSIZE
];
729 long long ninput
= 10483892, result
;
731 if (scan_scaled(cinput
, &result
) == 0)
732 printf("\"%s\" -> %lld\n", cinput
, result
);
736 if (fmt_scaled(ninput
, buf
) == 0)
737 printf("%lld -> \"%s\"\n", ninput
, buf
);
739 fprintf(stderr
, "%lld invalid (%s)\n", ninput
, strerror(errno
));