-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
49 lines (44 loc) · 1.2 KB
/
utils.c
File metadata and controls
49 lines (44 loc) · 1.2 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
int count_orrurrence(const char *string, char c) {
int i = 0;
int len = sizeof(string);
int count = 0;
for (i = 0; i < len && string[i] != '\0'; i++) {
if (string[i] == c) {
count++;
}
}
return count;
}
void exit_on_failure(int result, char *message) {
if (result < 0) {
if (message) {
perror(message);
}
exit(1);
}
}
int getLine(char *prompt, char *buff, size_t sz) {
int ch, extra;
// Get line with buffer overrun protection.
if (prompt != NULL) {
printf("%s", prompt);
fflush(stdout);
}
if (fgets(buff, sz, stdin) == NULL)
return NO_INPUT;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff) - 1] != '\n') {
extra = 0;
if (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : READLINE_OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff) - 1] = 0;
return READLINE_OK;
}