-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.c
More file actions
35 lines (33 loc) · 777 Bytes
/
cd.c
File metadata and controls
35 lines (33 loc) · 777 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
29
30
31
32
33
34
35
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#define clear() printf("\033[H\033[J")
void cd(char home[], char *str)
{
if (strlen(str) == 0)
{
str = (char *)malloc(2);
str[0] = '~';
str[1] = '\0';
}
int i;
if (str[0] == '~')
{
int sz = strlen(home) + strlen(str) - 1;
char *temp = str;
str = (char *)malloc(strlen(home) + strlen(str));
for (i = sz - 1; i >= strlen(home); i--)
{
str[i] = temp[i - strlen(home) + 1];
}
for (i = 0; i < strlen(home); i++)
str[i] = home[i];
str[sz] = '\0';
}
int x = chdir(str);
if (x == -1)
perror("Error ");
}