-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec3.c
More file actions
76 lines (68 loc) · 1.71 KB
/
Copy pathspec3.c
File metadata and controls
76 lines (68 loc) · 1.71 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
#include "headers.h"
void warp(char *path, const char *home_dir, char *prev_dir)
{
char full_path[1024]; // Assuming a maximum path length of 1024 characters
if (path[0] == '~' || path[0] == '-' || path[0] == '.' || path[1] == '.')
{
if (path[0] == '~')
{
// Get home directory
snprintf(full_path, sizeof(full_path), "%s%s", home_dir, path + 1);
}
else if (path[0] == '-')
{
if (prev_dir)
{
snprintf(full_path, sizeof(full_path), "%s", prev_dir);
}
else
{
fprintf(stderr, "warp: OLDPWD not set\n");
return;
}
}
else if (path[1] == '.')
{
if (chdir("..") == 0)
{
getcwd(full_path, sizeof(full_path));
}
else
{
perror("warp: Error navigating to parent directory");
return;
}
}
else if (path[0] == '.')
{
getcwd(full_path, sizeof(full_path));
}
}
else
{
// Handle relative or absolute paths
if (path[0] == '/')
{
// Absolute path
snprintf(full_path, sizeof(full_path), "%s", path);
}
else
{
// Relative path
getcwd(full_path, sizeof(full_path));
snprintf(full_path, sizeof(full_path), "%s/%s", full_path, path);
}
}
// Change the directory
if (prev_dir)
{
snprintf(prev_dir, sizeof(full_path), "%s", getcwd(NULL, 0));
}
if (chdir(full_path) == 0)
{
}
else
{
perror("warp");
}
}