-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
78 lines (62 loc) · 1.79 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
//
// Created by georgii on 25.03.2021.
//
#include "util.h"
#include <argp.h>
#include <string.h>
#include <malloc.h>
void get_hex_chars(uint8_t byte, char *hex) {
unsigned char firstNibble; // a Nibble is 4 bits, half a byte, one hexadecimal character
unsigned char secondNibble;
firstNibble = (byte >> 4); // isolate first 4 bits
if (firstNibble < 10U) {
hex[0] = (char) ('0' + firstNibble);
} else {
firstNibble -= 10U;
hex[0] = (char) ('A' + firstNibble);
}
secondNibble = (byte & 0x0F); // isolate last 4 bits
if (secondNibble < 10U) {
hex[1] = (char) ('0' + secondNibble);
} else {
secondNibble -= 10U;
hex[1] = (char) ('A' + secondNibble);
}
}
int parse(const char *cmd, char **args) {
const char *p = cmd;
int count = 0;
for (;;) {
while (isspace(*p)) p++;
if (count >= 3) {
return count;
}
if (*p == '\0') break;
if (*p == '"' || *p == '\'') {
int quote = *p++;
const char *begin = p;
while (*p && *p != quote) p++;
if (*p == '\0') return -1;
strncpy(args[count], begin, p - begin);
count++;
p++;
continue;
}
if (strchr("<>()|", *p)) {
args[count] = calloc(1, 256);
strncpy(args[count], p, 1);
count++;
p++;
continue;
}
if (isalnum(*p) || *p == '.' || *p == '/' || *p == '-' || *p == ':') {
const char *begin = p;
while (isalnum(*p) || *p == '.' || *p == '/' || *p == '-' || *p == ':') p++;
strncpy(args[count], begin, p - begin);
count++;
continue;
}
return -1;
}
return count;
}