]> code.delx.au - spectrwm/blob - osx/osx.c
add osx support
[spectrwm] / osx / osx.c
1 /* $scrotwm$ */
2
3 #include <sys/types.h>
4 #include <sys/cdefs.h>
5
6 #include <errno.h>
7 #include <errno.h>
8 #include <limits.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "util.h"
14
15 /* --------------------------------------------------------------------------- */
16 /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
17
18 /*
19 * Copyright (c) 2004 Ted Unangst and Todd Miller
20 * All rights reserved.
21 *
22 * Permission to use, copy, modify, and distribute this software for any
23 * purpose with or without fee is hereby granted, provided that the above
24 * copyright notice and this permission notice appear in all copies.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
27 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
29 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
31 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
32 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 */
34
35 #define INVALID 1
36 #define TOOSMALL 2
37 #define TOOLARGE 3
38
39 long long
40 strtonum(const char *numstr, long long minval, long long maxval,
41 const char **errstrp)
42 {
43 long long ll = 0;
44 char *ep;
45 int error = 0;
46 struct errval {
47 const char *errstr;
48 int err;
49 } ev[4] = {
50 { NULL, 0 },
51 { "invalid", EINVAL },
52 { "too small", ERANGE },
53 { "too large", ERANGE },
54 };
55
56 ev[0].err = errno;
57 errno = 0;
58 if (minval > maxval)
59 error = INVALID;
60 else {
61 ll = strtoll(numstr, &ep, 10);
62 if (numstr == ep || *ep != '\0')
63 error = INVALID;
64 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
65 error = TOOSMALL;
66 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
67 error = TOOLARGE;
68 }
69 if (errstrp != NULL)
70 *errstrp = ev[error].errstr;
71 errno = ev[error].err;
72 if (error)
73 ll = 0;
74
75 return (ll);
76 }