-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcd.c
More file actions
28 lines (26 loc) · 761 Bytes
/
cd.c
File metadata and controls
28 lines (26 loc) · 761 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
27
28
#include "headers.h"
void cd(char *token, char *present_dir)
{
token = strtok(NULL, " \n\t\r");
if (token == NULL)
chdir(present_dir);
else if (token[0] == '~' && strlen(token) >= 1)
{
static char buffer[4096];
char *p;
char *str = token;
char *orig = "~";
char *rep = present_dir;
p = strstr(str, orig);
strncpy(buffer, str, p - str);
buffer[p - str] = '\0';
sprintf(buffer + (p - str), "%s%s", rep, p + strlen(orig));
if (chdir(buffer) != 0)
printf("directory doesn't exist\n");
}
else if (strcmp(token, "~") == 0)
chdir(present_dir);
else if (chdir(token) != 0)
printf("directory doesn't exist\n");
return;
}