-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.c
More file actions
109 lines (101 loc) · 3.29 KB
/
Copy pathdir.c
File metadata and controls
109 lines (101 loc) · 3.29 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* "dir.c" - Gabriel Bauer (@ToCodeABluejay)
*
*Copyright (c) 2021 Gabriel Bauer (@ToCodeABluejay)
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/
#include "shell.h"
char *absdir(char *s)
{
static char dir[PATH_MAX];
if(!strncmp(s, "~", 1)) // In this context '~' is used to mean the users home directory
{ //So this sections builds a relative pathway from the starting point of the given home directory
unsigned long i=1; //In place of the beginning '~'
i+=strlen(s); //And so that '~/Documents' would be equivalent to '/home/jdoe/Documents'
char end[i];
for (i=0; i<=strlen(end); i++) end[i]=s[i+1];
end[i+1]='\0';
strcpy(dir, getenv("HOME"));
strcat(dir, end);
}
else
strcpy(dir, s);
strcpy(dir, realpath(dir, NULL));
return dir;
}
#ifndef __linux__
char *get_current_dir_name(void) //For all other UNIX-like platforms, we can just roll our own. It's fairly easy...
{
static char cwdir[PATH_MAX];
getcwd(cwdir, sizeof(cwdir));
return cwdir;
}
#endif
void cd(struct command *k)
{
int i;
char d[PATH_MAX];
if(!k->argv[1])
{
strcpy(d, getenv("HOME"));
i = chdir(d);
}
else
{
char *p = absdir(k->argv[1]);
if(!p)
{
fprintf(stderr, "Could change directory to [%s]!\n", k->argv[1]);
}
else
{
i = chdir(p);
}
}
if (i)
fprintf(stderr, "Could not change directory to %s!\n", k->argv[1]);
}
void ls(struct command *k)
{
DIR *dir;
struct dirent *contents;
char path[PATH_MAX];
if(!strlen(k->argv[1])) //If no argument is given, assume that we are working with the curent active
dir = opendir("."); // directory
else
dir = opendir(absdir(k->argv[1])); //We can uncomment the above if-else statement when
if (dir) // we can replace realpath() w/absdir()
{
int i=0; //This is a tracker to keep a tab on how many items have been printed
while ((contents = readdir(dir)))
{
if(strncmp(contents->d_name, ".",1))
{
if((!(i%3))&&i) //This is to set a max to the amount of items printed on a line to three
putchar('\n'); //Sometimes names can get long, and three might still be tough when they get too long
printf("%30s ", contents->d_name); //But I though this would be nice to keep everything
i++; //From getting too crowded
}
}
closedir(dir);
putchar('\n');
}
else
fprintf(stderr, "Could not open directory!\n");
}