Repository navigation
Expand file tree
/
Copy pathls.cpp
More file actions
157 lines (140 loc) · 4.03 KB
/
Copy pathls.cpp
File metadata and controls
157 lines (140 loc) · 4.03 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "headers.h"
static string permissionString(mode_t mode)
{
string s(10, '-');
switch (mode & S_IFMT)
{
case S_IFBLK: s[0] = 'b'; break;
case S_IFCHR: s[0] = 'c'; break;
case S_IFDIR: s[0] = 'd'; break;
case S_IFIFO: s[0] = 'p'; break;
case S_IFLNK: s[0] = 'l'; break;
case S_IFSOCK: s[0] = 's'; break;
default: s[0] = '-'; break;
}
const char *bits = "rwxrwxrwx";
mode_t masks[9] = {S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH};
for (int i = 0; i < 9; ++i)
if (mode & masks[i])
s[i + 1] = bits[i];
return s;
}
static void printLongEntry(const string &fullPath, const string &displayName)
{
struct stat st;
if (lstat(fullPath.c_str(), &st) != 0)
{
cerr << "ls: cannot access '" << displayName << "': " << strerror(errno) << endl;
return;
}
struct passwd *pw = getpwuid(st.st_uid);
struct group *gr = getgrgid(st.st_gid);
string when = ctime(&st.st_mtime);
if (!when.empty() && when.back() == '\n')
when.pop_back();
cout << permissionString(st.st_mode)
<< " " << st.st_nlink
<< " " << (pw ? pw->pw_name : to_string(st.st_uid))
<< " " << (gr ? gr->gr_name : to_string(st.st_gid))
<< " " << st.st_size
<< " " << when
<< " " << displayName << '\n';
}
static void collectNames(DIR *dir, bool showHidden, vector<string> &names)
{
struct dirent *entry;
while ((entry = readdir(dir)) != nullptr)
{
if (!showHidden && entry->d_name[0] == '.')
continue;
names.push_back(entry->d_name);
}
sort(names.begin(), names.end());
}
static long directoryBlocks(const string &path, const vector<string> &names)
{
long blocks = 0;
for (const string &name : names)
{
struct stat st;
if (lstat((path + "/" + name).c_str(), &st) == 0)
blocks += st.st_blocks;
}
return blocks / 2; // ls reports 1K blocks; st_blocks counts 512B units
}
static void printPath(const string &path, bool showHidden, bool showDetails, bool withHeader)
{
struct stat st;
if (stat(path.c_str(), &st) != 0)
{
cerr << "ls: cannot access '" << path << "': " << strerror(errno) << endl;
return;
}
if (!S_ISDIR(st.st_mode))
{
if (showDetails)
printLongEntry(path, path);
else
cout << path << '\n';
return;
}
DIR *dir = opendir(path.c_str());
if (!dir)
{
cerr << "ls: cannot open '" << path << "': " << strerror(errno) << endl;
return;
}
if (withHeader)
cout << path << ":\n";
vector<string> names;
collectNames(dir, showHidden, names);
closedir(dir);
if (showDetails)
{
cout << "total " << directoryBlocks(path, names) << '\n';
for (const string &name : names)
printLongEntry(path + "/" + name, name);
}
else
{
for (const string &name : names)
cout << name << '\n';
}
}
void listDirectory(vector<string> args)
{
bool showHidden = false;
bool showDetails = false;
vector<string> paths;
for (size_t i = 1; i < args.size(); ++i)
{
const string &arg = args[i];
if (arg.size() > 1 && arg[0] == '-')
{
for (size_t j = 1; j < arg.size(); ++j)
{
if (arg[j] == 'a')
showHidden = true;
else if (arg[j] == 'l')
showDetails = true;
else
cerr << "ls: invalid option -- '" << arg[j] << "'" << endl;
}
}
else
{
paths.push_back(arg == "~" ? string(SHELL_DIRECTORY_PATH) : arg);
}
}
if (paths.empty())
{
printPath(".", showHidden, showDetails, false);
return;
}
for (size_t i = 0; i < paths.size(); ++i)
{
printPath(paths[i], showHidden, showDetails, paths.size() > 1);
if (i + 1 < paths.size())
cout << '\n';
}
}