-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
218 lines (198 loc) · 4.73 KB
/
ls.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @file ls.c
* @author Ivan Turasov
* @brief Test version of ls utility with -l flag
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#define NORMAL_COLOR "\x1B[0m"
#define GREEN "\x1B[32m"
#define BLUE "\x1B[34m"
#define DEBUG 0
/**
* @brief Display file type
*
* @param mode file mode (struct stat)
*/
void print_filetype(mode_t mode)
{
switch (mode & S_IFMT)
{
case S_IFREG:
putchar('-');
break;
case S_IFDIR:
putchar('d');
break;
case S_IFLNK:
putchar('l');
break;
case S_IFCHR:
putchar('c');
break;
case S_IFBLK:
putchar('b');
break;
case S_IFSOCK:
putchar('s');
break;
case S_IFIFO:
putchar('f');
break;
}
}
/**
* @brief Display file permissions
*
* @param mode file mode (struct stat)
*/
void print_permissions(mode_t mode)
{
putchar((mode & S_IRUSR) ? 'r' : '-');
putchar((mode & S_IWUSR) ? 'w' : '-');
putchar((mode & S_IXUSR) ? 'x' : '-');
putchar((mode & S_IRGRP) ? 'r' : '-');
putchar((mode & S_IWGRP) ? 'w' : '-');
putchar((mode & S_IXGRP) ? 'x' : '-');
putchar((mode & S_IROTH) ? 'r' : '-');
putchar((mode & S_IWOTH) ? 'w' : '-');
putchar((mode & S_IXOTH) ? 'x' : '-');
}
/**
* @brief Print time of file creation
*
* @param mod_time file time (struct stat)
*/
void print_time(time_t mod_time)
{
// get current time with year
time_t curr_time;
time(&curr_time);
struct tm *t = localtime(&curr_time);
const int curr_mon = t->tm_mon;
const int curr_yr = 1970 + t->tm_year;
// get mod time and year
t = localtime(&mod_time);
const int mod_mon = t->tm_mon;
const int mod_yr = 1970 + t->tm_year;
// determine format based on years
const char *format = ((mod_yr == curr_yr) && (mod_mon >= (curr_mon - 6)))
? "%b %e %H:%M"
: "%b %e %Y";
char time_buf[128];
strftime(time_buf, sizeof(time_buf), format, t);
printf("%s ", time_buf);
}
/**
* @brief Show extended attributes of the file
*
* @param path Path to the file
*/
void list_extended(char *path)
{
struct stat file_stat;
if(lstat(path, &file_stat) != 0)
{
if (DEBUG)
{
printf("lstat error\n");
}
exit(0);
}
print_filetype(file_stat.st_mode);
print_permissions(file_stat.st_mode);
printf(" %d ", file_stat.st_nlink);
printf("%7s ", getpwuid(file_stat.st_uid)->pw_name);
printf("%7s ", getgrgid(file_stat.st_gid)->gr_name);
printf("%7ld ", (long)file_stat.st_size);
print_time(file_stat.st_mtime);
}
/**
* @brief Print contents of the directory
*
* @param path Path to the directory
* @param lflag Extended attributes flag
*/
void print_dir_content(char *path, int lflag)
{
DIR *current_dir = opendir(path);
struct dirent *dir;
if (current_dir)
{
while ((dir = readdir(current_dir)) != NULL)
{
if (lflag)
{
list_extended(dir->d_name);
}
switch (dir->d_type)
{
case DT_DIR:
printf(BLUE);
printf("%s\n", dir->d_name);
break;
case DT_REG:
printf(NORMAL_COLOR);
printf("%s\n", dir->d_name);
break;
case DT_LNK:
printf(GREEN);
printf("%s\n", dir->d_name);
break;
default:
printf(NORMAL_COLOR);
printf("%s\n", dir->d_name);
break;
}
printf(NORMAL_COLOR);
}
closedir(current_dir);
}
}
/**
* @brief Print usage instructions
*
* @param argv0
*/
static void usage(char *argv0)
{
fprintf(stdout, "\nDisplay contents of the directory\n"
"Usage: %s [options] <path>\n\n"
"Options:\n"
"\t-l List in long format.\n"
"\t-h Print this help message.\n",
argv0);
exit(-1);
}
int main(int argc, char *argv[])
{
int option_index = 1, c;
int lflag = 0;
while ((c = getopt(argc, argv, ":lh")) != -1)
{
switch (c)
{
case 'l':
lflag = 1;
option_index ++;
break;
case 'h':
usage(argv[0]);
break;
default:
printf("Unknown option '%c'\n", c);
usage(argv[0]);
break;
}
}
print_dir_content(argv[option_index], lflag);
return 0;
}