forked from ashish7802/cpp-cli-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
26 lines (24 loc) · 750 Bytes
/
Copy pathutils.c
File metadata and controls
26 lines (24 loc) · 750 Bytes
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
#include "utils.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char* trim(char* str) {
char* end;
while (isspace((unsigned char)*str)) str++;
if (*str == 0) return str;
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
return str;
}
char** split_string(char* str, int* count) {
char** tokens = malloc(10 * sizeof(char*)); // assume max 10 tokens
char* token = strtok(str, " ");
*count = 0;
while (token != NULL && *count < 10) {
tokens[*count] = strdup(token); // duplicate the token
(*count)++;
token = strtok(NULL, " ");
}
return tokens;
}