-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
70 lines (59 loc) · 1.12 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "co/err.h"
#include "co/util.h"
#include "inc/def.h"
#define SIZE (MAX_STRING_SIZE)
int
util_mkdir(const char *path)
{
char command[SIZE];
snprintf(command, SIZE, "mkdir -p -- '%s'", path);
if (system(command) != 0)
ERR(CO_SYS, "comamnd <%s> failed", command);
return CO_OK;
}
int
util_eq(const char *a, const char *b)
{
return strncmp(a, b, SIZE) == 0;
}
char *
util_fgets(char *s, FILE * stream)
{
char *c;
if (fgets(s, SIZE, stream) == NULL)
return NULL;
if ((c = strchr(s, '\n')) != NULL)
*c = '\0';
return s;
}
char *
util_strcpy(char *a, const char *b)
{
return strncpy(a, b, SIZE - 1);
}
enum { NO, YES };
static int
commentp(const char *s)
{
while (*s != '\0') {
if (*s == '#')
return YES;
if (!isblank(*s))
return NO;
s++;
}
return YES;
}
char *
util_comment_fgets(char *s, FILE * stream)
{
do
if (util_fgets(s, stream) == NULL)
return NULL;
while (commentp(s) == YES);
return s;
}