-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.c
37 lines (32 loc) · 845 Bytes
/
dir.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
#include "dir.h"
/*
* 실행 파일이 위치한 폴더에 있는 파일들과 폴더들을 출력한다.
*/
bool print_dir(){
DIR* dir = opendir(".");
struct dirent *ent;
struct stat stat;
char* ent_dname;
char* format;
char path[1025];
int i = 0;
if(!dir){
fprintf(stderr, "[ERROR] Can't open directory");
return false;
}
ent = readdir(dir);
while (ent){
ent_dname = ent->d_name;
lstat(ent_dname, &stat);
if(S_ISDIR(stat.st_mode)) format = "%s/";
else if(S_IXUSR & stat.st_mode) format = "%s*";
else format = "%s ";
sprintf(path, format, ent->d_name);
printf("%-20s", path);
if(++i % 5 == 0) printf("\n");
ent = readdir(dir);
}
if (i % 5 != 0) printf ("\n");
closedir(dir);
return true;
}