ls.c (927B)
1 #include <dirent.h> 2 #include <sys/stat.h> 3 #include <errno.h> 4 #include <stdio.h> 5 #include <string.h> 6 #include <stdlib.h> 7 8 int 9 main(void) 10 { 11 char buf[256]; 12 DIR *dirp; 13 struct dirent *dirent; 14 struct stat stbuf; 15 int ret; 16 17 dirp = opendir("/"); 18 if (!dirp) 19 return -errno; 20 dirent = readdir(dirp); 21 printf("%-12s%-12s%-12s%-12s\n", "FILENAME", "TYPE", "UID", "GID"); 22 while (dirent) { 23 if (!strcmp(dirent->d_name, ".") 24 || !strcmp(dirent->d_name, "..")) 25 goto out; 26 bzero(buf, 256); 27 buf[0] = '/'; 28 strncpy(buf + 1, dirent->d_name, 255); 29 buf[255] = '\0'; 30 ret = stat(buf, &stbuf); 31 if (ret < 0) { 32 fprintf(stderr, "%s\n", strerror(errno)); 33 break; 34 } 35 printf("%-12s%-12s%-12d%-12d\n", 36 dirent->d_name, 37 S_ISREG(stbuf.st_mode) ? "FILE" : "DIR", 38 stbuf.st_uid, 39 stbuf.st_gid); 40 out: 41 dirent = readdir(dirp); 42 } 43 ret = closedir(dirp); 44 if (ret < 0) 45 return -errno; 46 return 0; 47 } 48