-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchdir.c
More file actions
75 lines (60 loc) · 1.73 KB
/
chdir.c
File metadata and controls
75 lines (60 loc) · 1.73 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
/*
* chdir.c - chdir stuff
*
* (C) 2000 Jordan DeLong
*/
#include <kos/fs.h>
#include <kos/limits.h>
#include <string.h>
#include "kosh.h"
/* the current directory */
static char cwd[NAME_MAX];
/* read a pathname based on the current directory and turn it into an abs one, we
don't check for validity, or really do anything except handle ..'s and .'s */
int makeabspath(char *buff, char *path, size_t size) {
int numtxts;
int i;
char *txts[32]; /* max of 32...should be nuff */
char *rslash;
numtxts = 0;
/* check if path is already absolute */
if (path[0] == '/') {
strncpy(buff, path, size);
return 0;
}
/* split the path into tokens */
for (numtxts = 0; numtxts < 32;) {
if ((txts[numtxts] = strsep(&path, "/")) == NULL)
break;
if (*txts[numtxts] != '\0')
numtxts++;
}
/* start from the current directory */
strncpy(buff, cwd, size);
for (i = 0; i < numtxts; i++) {
if (strcmp(txts[i], "..") == 0) {
if ((rslash = strrchr(buff, '/')) != NULL)
*rslash = '\0';
} else if (strcmp(txts[i], ".") == 0) {
/* do nothing */
} else {
if (buff[strlen(buff) - 1] != '/')
strncat(buff, "/", size - 1 - strlen(buff));
strncat(buff, txts[i], size - 1 - strlen(buff));
}
}
/* make sure it's not empty */
if (buff[0] == '\0') {
buff[0] = '/';
buff[1] = '\0';
}
return 0;
}
/* change the current directory (dir is an absolute path for now) */
int kosh_chdir(char *dir) {
char buff[NAME_MAX];
makeabspath(buff, dir, NAME_MAX);
strncpy(cwd, buff, NAME_MAX);
fs_chdir(cwd);
return 0;
}