-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.c
More file actions
70 lines (57 loc) · 1.49 KB
/
list.c
File metadata and controls
70 lines (57 loc) · 1.49 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
//list.c
// a simple implementatio of the ls command
// directories are printed in yellow
//regular files in blue
//add new colors for file types by using colors
//macros and a filetype check
#include "headers.h"
int list()
{
DIR *d;
struct dirent *dir;
int file = 0;
struct stat fileStat;
//list the file in the current directory takes no argument
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
//open file in read only permisiion so command
// works across various acess rights
if ((file = open(dir->d_name, O_RDONLY)) < -1)
return 1;
if (fstat(file, &fileStat) < 0)
return 1;
printf(" | ");
//check if it is directory
if (S_ISDIR(fileStat.st_mode))
{
PRINT_YELLOW(dir->d_name);
}
else
{
PRINT_BLUE(dir->d_name);
}
printf("\n");
printf("Size: %d bytes ", fileStat.st_size);
printf("Inode: %d ", fileStat.st_ino);
printf("Permissions:");
printf((fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf((fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf((fileStat.st_mode & S_IRGRP) ? "r" : "-");
//only print user FIle stats uncomment for other permissions
/*
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-" );
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-" );
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-" );
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-" );
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-" );
*/
printf("\n");
}
closedir(d);
}
printf("\n");
return 0;
}