Repository navigation
Expand file tree
/
Copy pathwarp.c
More file actions
73 lines (67 loc) · 1.59 KB
/
Copy pathwarp.c
File metadata and controls
73 lines (67 loc) · 1.59 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
#include "headers.h"
void warpHelper(char *directory)
{
if (directory == NULL)
{
printError("warp: missing argument\n");
return;
}
char current_directory[DEF_SIZE];
if (getcwd(current_directory, sizeof(current_directory)) == NULL)
{
printError("getcwd error");
return;
}
if (strcmp(directory, "-") == 0)
{
if (prev_directory == NULL)
{
printError("warp: no previous directory\n");
return;
}
directory = prev_directory;
}
else if (directory[0] == '~' && (directory[1] == '/' || directory[1] == '\0'))
{
// handling ~/dir case
char expanded_path[DEF_SIZE];
snprintf(expanded_path, sizeof(expanded_path), "%s%s", global_home, directory + 1);
directory = expanded_path;
}
if (chdir(directory) != 0)
{
printError("warp error");
}
else
{
prev_directory = strdup(current_directory);
if (prev_directory == NULL)
{
printError("Memory allocation error");
exit(EXIT_FAILURE);
}
}
char working_directory[DEF_SIZE];
if (getcwd(working_directory, sizeof(working_directory)) == NULL)
{
printError("getcwd error");
return;
}
printf("%s\n", working_directory);
}
void warp(Command *cmd)
{
if (cmd->argc == 0)
{
printError("warp: missing argument\n");
return;
}
else if (cmd->argc == 1)
{
warpHelper("~");
}
for (int i = 1; i < cmd->argc; i++)
{
warpHelper(cmd->argv[i]);
}
}