1 /* +-------------------------------------------------------------------+ */
2 /* | Copyright 1991, David Koblas. | */
3 /* | Permission to use, copy, modify, and distribute this software | */
4 /* | and its documentation for any purpose and without fee is hereby | */
5 /* | granted, provided that the above copyright notice appear in all | */
6 /* | copies and that both that copyright notice and this permission | */
7 /* | notice appear in supporting documentation. This software is | */
8 /* | provided "as is" without express or implied warranty. | */
9 /* +-------------------------------------------------------------------+ */
10
11 #include "defs.h"
12 #include <errno.h>
13
14 char *
strtolower(char * in)15 strtolower(char *in)
16 {
17 char *i;
18
19 for (i = in; *i; ++i)
20 *i = tolower(*i);
21 return in;
22 }
23
24 array_t *
array_alloc(void)25 array_alloc(void)
26 {
27 array_t *array = malloc(sizeof(array_t));
28
29 if (!array || !(array->data = malloc(sizeof(void **) * ARRAY_CHUNK)))
30 fatal(1, "failed to allocate array");
31 array->capacity = ARRAY_CHUNK;
32 array->size = 0;
33 return array;
34 }
35
36 void
array_free(array_t * array)37 array_free(array_t * array)
38 {
39 free(array->data);
40 free(array);
41 }
42
43 array_t *
array_free_contents(array_t * array)44 array_free_contents(array_t * array)
45 {
46 size_t i;
47
48 for (i = 0; i < array->size; ++i)
49 free(array->data[i]);
50 array->size = 0;
51 return array;
52 }
53
54 void *
array_push(array_t * array,void * object)55 array_push(array_t * array, void *object)
56 {
57 if (array->size + 1 >= array->capacity) {
58 array->capacity += ARRAY_CHUNK;
59 if (!
60 (array->data =
61 realloc(array->data, sizeof(void **) * array->capacity)))
62 fatal(1, "failed to extend array");
63 }
64 return (array->data[array->size++] = object);
65 }
66
67 void *
array_pop(array_t * array)68 array_pop(array_t * array)
69 {
70 if (array->size == 0)
71 return NULL;
72 return array->data[--array->size];
73 }
74
75 int
array_extend(array_t * array,size_t capacity)76 array_extend(array_t * array, size_t capacity)
77 {
78 if (capacity < array->capacity)
79 return 0;
80 array->capacity = capacity;
81 array->data = realloc(array->data, sizeof(void **) * array->capacity);
82 return 1;
83 }
84
85 #ifdef malloc
86 #undef malloc
87 void *
rpl_malloc(size_t n)88 rpl_malloc(size_t n)
89 {
90 if (n == 0)
91 n = 1;
92 return malloc(n);
93 }
94 #endif
95
96 #ifdef realloc
97 #undef realloc
98 void *
rpl_realloc(void * ptr,size_t n)99 rpl_realloc(void *ptr, size_t n)
100 {
101 if (n == 0)
102 n = 1;
103 return realloc(ptr, n);
104 }
105 #endif
106
107 /* from man strtol(1) */
108 /* NOLINTNEXTLINE(runtime/int) */
109 long
strtolong(const char * str,int base)110 strtolong(const char *str, int base)
111 {
112 char *endptr;
113 /* NOLINTNEXTLINE(runtime/int) */
114 long val;
115
116 errno = 0; /* To distinguish success/failure after call */
117 val = strtol(str, &endptr, base); /* base 10 */
118
119 /* Check for various possible errors */
120
121 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
122 || (errno != 0 && val == 0))
123 fatal(1, "Number out of range");
124
125 if (endptr == str)
126 fatal(1, "No digits were found");
127
128 if (val < 0)
129 fatal(1, "Number out of range");
130
131 return val;
132 }
133