-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandling_functions.c
executable file
·92 lines (76 loc) · 2.19 KB
/
handling_functions.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
#include "handling_functions.h"
int change_dir(const char *path) {
// cd with error handling
int cd = chdir(path);
if (cd == -1) {
perror("chdir error");
exit(errno);
}
// ls with error handling
int ls = list_content(path);
if (ls == -1) {
perror("ls error");
exit(errno);
}
return EXIT_SUCCESS;
}
int list_content(const char *path) {
// Open directory by path
struct dirent *de;
DIR *dr = opendir(path);
if (dr == NULL) {
fprintf(stderr, "Could not open current directory");
return EXIT_FAILURE;
}
printf("\nContent of %s:\n", path);
// Print directory content with its type
while ((de = readdir(dr)) != NULL) {
switch (de->d_type) {
case DT_UNKNOWN:
printf("%s (unknown)\n", de->d_name);
break;
case DT_REG:
printf("%s (file)\n", de->d_name);
break;
case DT_DIR:
printf("%s (directory)\n", de->d_name);
break;
case DT_FIFO:
printf("%s (FIFO)\n", de->d_name);
break;
case DT_SOCK:
printf("%s (socket)\n", de->d_name);
break;
case DT_CHR:
printf("%s (char device)\n", de->d_name);
break;
case DT_BLK:
printf("%s (block device)\n", de->d_name);
break;
case DT_LNK:
printf("%s (symlink)\n", de->d_name);
break;
default:
fprintf(stderr, "Invalid entry type");
return EXIT_FAILURE;
}
}
closedir(dr);
return EXIT_SUCCESS;
}
int create_new_thread(const char *path) {
// Create new thread with cd command and handle errors
pthread_t new_thread;
int ptc = pthread_create(&new_thread, NULL, change_dir, path);
if (ptc == -1) {
perror("pthread_create error");
exit(errno);
}
// Join new thread
int ptj = pthread_join(new_thread, NULL);
if (ptj == -1) {
perror("pthread_join error");
exit(errno);
}
return EXIT_SUCCESS;
}