forked from uobikiemukot/recterm
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutil.c
194 lines (161 loc) · 3.91 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (C) 2014 haru <uobikiemukot at gmail dot com>
* Copyright (C) 2014 Hayaki Saito <user@zuse.jp>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#include "yaft.h"
/* error functions */
void error(char *str)
{
perror(str);
exit(EXIT_FAILURE);
}
void fatal(char *str)
{
fprintf(stderr, "%s\n", str);
exit(EXIT_FAILURE);
}
/* wrapper of C functions */
void *ecalloc(size_t nmemb, size_t size)
{
void *ptr;
#if defined(HAVE_SYS_ERRNO_H) || (HAVE_ERRNO_H)
errno = 0;
#endif
#ifdef HAVE_CALLOC
if ((ptr = calloc(nmemb, size)) == NULL)
error("calloc");
#else
if ((ptr = malloc(nmemb * size)) == NULL)
error("malloc");
memset(ptr, 0, nmemb * size);
#endif
return ptr;
}
void *erealloc(void *ptr, size_t size)
{
void *new;
#if defined(HAVE_SYS_ERRNO_H) || (HAVE_ERRNO_H)
errno = 0;
#endif
if ((new = realloc(ptr, size)) == NULL)
error("realloc");
return new;
}
static long estrtol(const char *nptr, char **endptr, int base)
{
long int ret;
#if defined(HAVE_SYS_ERRNO_H) || (HAVE_ERRNO_H)
errno = 0;
#endif
ret = strtol(nptr, endptr, base);
#ifdef HAVE_LIMITS_H
if (ret == LONG_MIN || ret == LONG_MAX) {
perror("strtol");
return 0;
}
#endif
return ret;
}
/* parse_arg functions */
void reset_parm(struct parm_t *pt)
{
int i;
pt->argc = 0;
for (i = 0; i < MAX_ARGS; i++)
pt->argv[i] = NULL;
}
void add_parm(struct parm_t *pt, char *cp)
{
if (pt->argc >= MAX_ARGS)
return;
if (DEBUG)
fprintf(stderr, "argv[%d]: %s\n",
pt->argc, (cp == NULL) ? "NULL": cp);
pt->argv[pt->argc] = cp;
pt->argc++;
}
void parse_arg(char *buf, struct parm_t *pt, int delim, int (is_valid)(int c))
{
/*
v..........v d v.....v d v.....v ... d
(valid char) (delimiter)
argv[0] argv[1] argv[2] ... argv[argc - 1]
*/
size_t i, length;
char *cp, *vp;
if (buf == NULL)
return;
length = strlen(buf);
if (DEBUG)
fprintf(stderr, "parse_arg()\nlength:%u\n", (unsigned) length);
vp = NULL;
for (i = 0; i < length; i++) {
cp = buf + i;
if (vp == NULL && is_valid(*cp))
vp = cp;
if (*cp == delim) {
*cp = '\0';
add_parm(pt, vp);
vp = NULL;
}
if (i == (length - 1) && (vp != NULL || *cp == '\0'))
add_parm(pt, vp);
}
if (DEBUG)
fprintf(stderr, "argc:%d\n", pt->argc);
}
/* other functions */
int my_ceil(int val, int div)
{
return (val + div - 1) / div;
}
int dec2num(char *str)
{
if (str == NULL)
return 0;
return estrtol(str, NULL, 10);
}
int hex2num(char *str)
{
if (str == NULL)
return 0;
return estrtol(str, NULL, 16);
}
/* emacs Local Variables: */
/* emacs mode: c */
/* emacs tab-width: 4 */
/* emacs indent-tabs-mode: nil */
/* emacs c-basic-offset: 4 */
/* emacs End: */
/* vim: set expandtab ts=4 : */
/* EOF */