-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
98 lines (88 loc) · 1.88 KB
/
utils.c
File metadata and controls
98 lines (88 loc) · 1.88 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
97
98
#include "includes.h"
#include "constants.h"
#include "types.h"
void trim(char *str)
{
/*
Trims leading and trailing whitespaces,
modifies given string
*/
int len = strlen(str);
int cur; // iterator/cursor
for (cur = len - 1; cur >= 0; cur--)
{
if (!isspace(str[cur]))
{
break;
}
str[cur] = '\0';
}
len = strlen(str);
for (cur = 0; cur < len; cur++)
{
if (!isspace(str[cur]))
{
break;
}
}
if (cur != len)
{
// leading spaces were present
memmove(str, str + cur, strlen(str));
}
}
void tokenise(char *str, char *delim, char **tokens, int *num_tokens)
{
/*
Tokenises the string according to given `delim`
and stores them in array of strings `tokens`.
*/
int i = 0;
if (str != NULL)
{
char *token = strtok(str, delim);
while (token)
{
tokens[i] = token;
i++;
token = strtok(NULL, delim);
}
}
tokens[i] = NULL;
*num_tokens = i;
}
void replace_tilde_with_home(char *path)
{
/*
Convenience function to replace "~"
with the absolute path of shell home (if present)
*/
if ('~' == path[0])
{
char temp[MAX_PATH_LEN];
// save the relative path, without "~"
strcpy(temp, path + 1);
// Store absolute path of shell home
strcpy(path, SHELL_HOME_PATH);
// Append relative path
strcat(path, temp);
}
}
/*
// DEPRECATED
void read_input(char **input)
{
*input = NULL;
size_t input_len = 0;
if (getline(input, &input_len, stdin) == -1)
{
if (!feof(stdin))
{
// getline not at EOF, so other error
perror("Error while taking input");
exit(EXIT_FAILURE);
}
}
*input[strlen(*input) - 1] = '\0';
}
*/