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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*
* Utilities
*
* Copyright (C) 2010 by Multi-Tech Systems
*
* Author: James Maki <jmaki@multitech.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <linux/limits.h>
#include "utils.h"
#include "log.h"
char *basename(const char *path)
{
const char *cp = strrchr(path, '/');
if(cp) {
return (char *) cp + 1;
} else {
return (char *) path;
}
}
int tokcmp(const char *cmd, const char *pattern)
{
int len = strlen(cmd);
if (len > strlen(pattern)) {
return -1;
}
return memcmp(pattern, cmd, len);
}
char *strrstrip(char *str)
{
char *end;
char *prev;
if (!str || !*str) {
return NULL;
}
prev = end = str + strlen(str);
while (end > str && isspace(*(end - 1))) {
end--;
}
*end = '\0';
return prev == end ? NULL : str;
}
ssize_t safe_read(int fd, void *buf, size_t count)
{
ssize_t n;
do {
n = read(fd, buf, count);
} while (n < 0 && errno == EINTR);
return n;
}
ssize_t safe_readn(int fd, void *buf, size_t len)
{
ssize_t cc;
ssize_t total;
total = 0;
while (len) {
cc = safe_read(fd, buf, len);
if (cc < 0) {
if (total) {
return total;
}
return cc;
}
total += cc;
buf = buf + cc;
len -= cc;
}
return total;
}
ssize_t safe_write(int fd, const void *buf, size_t count)
{
ssize_t n;
do {
n = write(fd, buf, count);
} while (n < 0 && errno == EINTR);
return n;
}
ssize_t full_write(int fd, const void *buf, size_t len)
{
ssize_t cc;
ssize_t total;
total = 0;
while (len) {
cc = safe_write(fd, buf, len);
if (cc < 0) {
if (total) {
return total;
}
return cc;
}
total += cc;
buf = ((const char *)buf) + cc;
len -= cc;
}
return total;
}
int user_yesno(int def, const char *fmt, ...)
{
char buf[128];
char *line;
va_list ap;
while (1) {
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("? [%c/%c] ",
def == USER_YESNO_YES ? 'Y' : 'y',
def == USER_YESNO_NO ? 'N' : 'n');
line = fgets(buf, sizeof(buf), stdin);
if (!line) {
return -1;
}
switch (toupper(*line)) {
case '\n':
return def;
case 'Y':
return USER_YESNO_YES;
case 'N':
return USER_YESNO_NO;
}
}
return -1;
}
int systemf(const char *fmt, ...)
{
int err;
va_list ap;
char *buf;
va_start(ap, fmt);
err = vasprintf(&buf, fmt, ap);
va_end(ap);
if (err < 0) {
log_error("out of memory");
return -1;
}
err = system(buf);
free(buf);
return err;
}
FILE *popenf(const char *mode, const char *fmt, ...)
{
int err;
va_list ap;
char *buf;
FILE *pipe;
va_start(ap, fmt);
err = vasprintf(&buf, fmt, ap);
va_end(ap);
if (err < 0) {
log_error("out of memory");
return NULL;
}
pipe = popen(buf, mode);
free(buf);
return pipe;
}
char *shell_path_expand(const char *path)
{
char buf[PATH_MAX + 1];
FILE *file;
char *line;
file = popenf("r", "echo -n \"%s\"", path);
if (!file) {
log_error("popen failed");
return NULL;
}
line = fgets(buf, sizeof(buf), file);
pclose(file);
if (!line) {
log_error("no line read");
return NULL;
}
return strdup(line);
}
char *device_lock(const char *path)
{
int tmp;
char *name = basename(path);
int fd;
char str[32];
char *lock;
tmp = asprintf(&lock, "/var/lock/LCK..%s", name);
if (tmp < 0) {
log_error("out of memory");
return NULL;
}
fd = open(lock, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
if (fd < 0) {
log_error("failed to create %s: %m", lock);
free(lock);
return NULL;
}
snprintf(str, sizeof(str), "%10d\n", getpid());
full_write(fd, str, strlen(str));
close(fd);
return lock;
}
int indexOfChar(const char *array, int len, char character)
{
int i;
for (i = 0; i < len; i++) {
if (array[i] == character) {
return i;
}
}
return -1;
}
|