-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
64 lines (49 loc) · 1.08 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "util.h"
int min(int a, int b) {
if (a > b) {
return b;
}
return a;
}
int checkError(int descriptor, char *description) {
if (descriptor == -1) {
printf("%s error %d: %s\n", description, errno, strerror(errno));
return -1;
}
return descriptor;
}
void prompt(char **input_ptr, char *prefix, int history) {
if (*input_ptr) {
free(*input_ptr);
*input_ptr = NULL;
}
*input_ptr = readline(prefix);
if (history && *input_ptr && **input_ptr) {
add_history(*input_ptr);
}
}
int startsWith(char *s1, char *s2) {
return strncmp(s1, s2, strlen(s2)) == 0;
}
int equals(char *s1, char *s2) {
return strcmp(s1, s2) == 0;
}
void leftShift(char *str, int offset) {
char *trail = str;
str += offset;
while (*str) {
*(trail++) = *(str++);
}
*trail = 0;
}
int readMessage(int fd) {
memset(message, 0, MAX_MESSAGE_LENGTH * sizeof(char));
return read(fd, message, MAX_MESSAGE_LENGTH);
}