-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
96 lines (83 loc) · 2.05 KB
/
utils.c
File metadata and controls
96 lines (83 loc) · 2.05 KB
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
#include "utils.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "err.h"
void set_close_on_exec(int file_descriptor, bool value)
{
int flags = fcntl(file_descriptor, F_GETFD);
ASSERT_SYS_OK(flags);
if (value)
flags |= FD_CLOEXEC;
else
flags &= ~FD_CLOEXEC;
ASSERT_SYS_OK(fcntl(file_descriptor, F_SETFD, flags));
}
char** split_string(const char* s)
{
size_t len = strlen(s);
int spaces = 0;
for (int i = 0; i < len; ++i)
if (s[i] == ' ')
spaces++;
char** parts = calloc(spaces + 2, sizeof(char*));
parts[spaces + 1] = NULL;
int p = 0;
int b = 0;
for (int i = 0; i < len; ++i) {
if (s[i] == ' ') {
parts[p++] = strndup(s + b, i - b);
b = i + 1;
}
}
parts[p++] = strndup(s + b, len - b);
assert(p == spaces + 1);
return parts;
}
void free_split_string(char** parts)
{
for (int i = 0; parts[i] != NULL; ++i)
free(parts[i]);
free(parts);
}
bool read_line(char* buffer, size_t size_of_buffer, FILE* file)
{
if (size_of_buffer < 2)
fatal("Buffer too small: %d\n", size_of_buffer);
char* line = NULL;
size_t n_bytes;
ssize_t n_chars = getline(&line, &n_bytes, file);
if (n_chars == -1) {
free(line);
if (ferror(file))
syserr("Getline failed.");
assert(feof(file));
buffer[0] = '\0';
return false;
}
if (n_chars == 0) {
free(line);
assert(feof(file));
buffer[0] = '\0';
return false;
}
size_t len = strlen(line);
if (len < n_chars)
fatal("Null character in input.");
assert(n_chars == len);
if (len + 1 > size_of_buffer)
fatal("Line too long: %d > %d.", len, size_of_buffer - 1);
memcpy(buffer, line, len + 1);
free(line);
return true;
}