-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
147 lines (136 loc) · 4.42 KB
/
Copy pathtree.c
File metadata and controls
147 lines (136 loc) · 4.42 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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
int compare_names(const void *a, const void *b){
const char **a_ptr = (const char **)a;
const char **b_ptr = (const char **)b;
return strcmp(*a_ptr, *b_ptr);
}
int path_concatenate(char *dest, size_t dest_size, const char *src, const char *add){
size_t src_len = strlen(src);
size_t add_len = strlen(add);
if (src_len + 1 + add_len>=dest_size){
return -1; //path doesn't fit in buffer
}
strcpy(dest, src);
if (src[src_len - 1] != '/'){
strcat(dest, "/");
}
strcat(dest, add);
return 0;
}
void tree_output(const char *path, int spacing, int print_size, int print_hidden){
DIR *d;
struct dirent *dirent;
char **names = NULL;
int num_ent = 0;
int i;
//open directory and count number of directory entries
d = opendir(path);
if (!d) {//if directory doesn't exist show error msg
perror(path);
return;
}
while ((dirent = readdir(d)) != NULL) {//iterate over directory entries
if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) continue;//skip over "." and ".."
if (!print_hidden && dirent->d_name[0] == '.') continue;//skip over hidden files if -a flag not passed
num_ent++;//increment number of entries for allocating
}
closedir(d);//close directory, freeing fd's
names = malloc(num_ent * sizeof(char *));
if (!names) {//make sure space was allocated successfully
perror("malloc");
return;
}
//open directory again to put names on heap array names
d = opendir(path);
if (!d) {//ensure path exists
perror(path);
free(names);
return;
}//iterate over names
i = 0;
while ((dirent = readdir(d)) != NULL){
if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) continue;
if (!print_hidden && dirent->d_name[0] == '.') continue;
names[i] = strdup(dirent->d_name);
if (!names[i]) {
perror("strdup");//ensure strings were copied
for (int j=0; j<i; j++) {
free(names[j]);
}
free(names);
closedir(d);
return;
}
i++;
}
closedir(d);
//sort entries
qsort(names, num_ent, sizeof(char *), compare_names);
//print entries
d = opendir(path);
if (!d){
perror(path);
free(names);
return;
}
for (i=0; i<num_ent; i++){
struct stat sb;
char full_path[10000];
if (path_concatenate(full_path, sizeof(full_path), path, names[i]) == 0){
if (lstat(full_path, &sb) == 0){
for (int j=0; j<spacing; j++) printf("| ");
printf("|-- %s", names[i]);
if (print_size) printf(" [size: %ld]", (long)sb.st_size);
printf("\n");
if (S_ISDIR(sb.st_mode)) {
tree_output(full_path, spacing+1, print_size, print_hidden);
}
}
}else{
fprintf(stderr, "Path too long");
}
free(names[i]);
}
free(names);
closedir(d);
}
int main(int argc, char *argv[]){
int print_hidden = 0;
int print_size = 0;
int arg_number = 1;
while (arg_number < argc && argv[arg_number][0] == '-'){
if (strcmp(argv[arg_number], "-a") == 0) print_hidden = 1;
else if (strcmp(argv[arg_number], "-s") == 0) print_size = 1;
else{
fprintf(stderr, "Invalid switch: %s\n", argv[arg_number]);
return 1;
}
arg_number++;
}
if (arg_number == argc){// print current directory if no arguments are given
tree_output(".", 0, print_size, print_hidden);
} else{
while (arg_number < argc){
struct stat sb;
if (lstat(argv[arg_number], &sb) == 0){
if (S_ISDIR(sb.st_mode)){
printf("%s\n", argv[arg_number]);
tree_output(argv[arg_number], 0, print_size, print_hidden);
} else{
printf("%s", argv[arg_number]);
if (print_size) printf(" [size: %ld]", (long)sb.st_size);
printf("\n");
}
} else{
perror(argv[arg_number]);
}
arg_number++;
}
}
return 0;
}