-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ls.cpp
72 lines (64 loc) · 1.68 KB
/
Ls.cpp
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
#include "Ls.h"
void Ls::dirwalk(const char *dir)
{
char name[MAX_PATH];
dirent *dp;
DIR *dfd;
if ((dfd = opendir(dir)) == NULL) {
fprintf(stderr, "dirwalk: can't open %s\n", dir);
return;
}
while ((dp = readdir(dfd)) != NULL) {
if (strcmp(dp->d_name, ".") == 0
|| strcmp(dp->d_name, "..") == 0)
continue; /* skip self and parent */
if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name))
fprintf(stderr, "dirwalk: name %s %s too long\n",
dir, dp->d_name);
else {
sprintf(name, "%s/%s", dir, dp->d_name);
this->fsize(name);
}
}
closedir(dfd);
}
void Ls::fsize(const char *name)
{
struct stat stbuf;
if (stat(name, &stbuf) == -1) {
fprintf(stderr, "\nCan't access %s\n\n", name);
return;
}
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
if (samoTekuci) {
samoTekuci = false;
dirwalk(name);
}
else //else da ne bi stampao i tekuci direktorijum
printf("<DIR> %s\n", File::getFileName(name).c_str());
}
else { //stampanje podataka o datoteci
char* t = ctime(&stbuf.st_mtime); /*ukidanje prelaska u novi red*/size_t ln = strlen(t) - 1; if (*t && t[ln] == '\n')t[ln] = '\0';
printf("%12ld KB %s %s\n", stbuf.st_size / (1024/*KB*/), t, File::getFileName(name).c_str());
}
}
void Ls::run(string Command){
Command = Commands::getCommandLine(Command);
DIR* dir;
struct dirent *element;
string elem;
string dirName = Command::tekuci->getFullPath();
if (Command == "-l") {
fsize(dirName.c_str());
samoTekuci = true;/*dozvoljavamo ispis*/
return;
}
else if (dir = opendir(dirName.c_str())) { //obican pregled fajlova
while (element = readdir(dir))
{
elem = element->d_name;
cout << elem << endl;
}
}
closedir(dir);
}