-
Notifications
You must be signed in to change notification settings - Fork 0
/
which.c
67 lines (56 loc) · 1.18 KB
/
which.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct path{
char** paths;
int length;
};
void freePath(struct path p){
int i;
for(i=0; i<p.length;i++){
free(p.paths[i]);
}
free(p.paths);
return;
}
int compLength(char* raw){
int i, ans = 0, len = strlen(raw);
for(i=0;i<len;i++){
switch(raw[i]){
case ':': ans++;break;
default: break;
}
}
return ++ans;
}
struct path parsePath(char* raw){
int i,pp = 0;
char* ref;
struct path res;
res.length = compLength(raw);
res.paths = malloc(sizeof(char*) * res.length);
ref = raw;
for(i = 0; i<strlen(raw); i++){
switch(raw[i]){
case ':':
res.paths[pp] = calloc(1, 1 + ((raw + i) - ref));
strncpy(res.paths[pp], ref+1, ((raw + i) - ref) - 1);
pp++;
ref = raw + i;
break;
default: break;
}
}
//Copy the last string
res.paths[pp] = calloc(1, 1 + ((raw + i) - ref));
strncpy(res.paths[pp], ref+1, ((raw + i) - ref) - 1);
return res;
}
int main(int argc, char** argv){
int i;
struct path p;
char* path = getenv("PATH");
if(argc != 1){
return 1;
}
}