#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#define ON 1
#define OFF 0
#define SIZE 512
main(int argc, char *argv[])
{
void fatal(char *);
DIR *dir;
int i, j;
char permission[10];
char list[SIZE];
char buff[SIZE];
char file[SIZE + 1];
struct dirent *directory;
struct stat file_stat;
struct passwd *user_information;
struct group *group_information;
if(argc != 2)
{
puts("사용법 : a.out directory_name");
exit(0);
}
for(i = 0;i < 10;i++)
permission[i] = '\0';
if((dir = opendir(argv[1])) == NULL)
fatal("opendir");
while(directory = readdir(dir))
{
if(strcmp(directory->d_name, ".") == 0 ||
strcmp(directory->d_name, "..") == 0 ||
strncmp(directory->d_name, ".", 1) == 0)
continue;
if(directory->d_ino == 0)
continue;
sprintf(file, "%s/%s", argv[1], directory->d_name);//절대경로를 얻음
if(lstat(file, &file_stat) < 0)
fatal("stat");
switch(file_stat.st_mode & S_IFMT)
{
case S_IFREG : list[0] = '-'; break;
case S_IFDIR : list[0] = 'd'; break;
case S_IFIFO : list[0] = 'p'; break;
case S_IFLNK : list[0] = 'l'; break;
}
list[1] = '\0';
for(i = 0, j = 0;i < 3;i++, j += 3)//파일의 권한 알아내기
{
if(file_stat.st_mode & (S_IREAD >> (i * 3)))
permission[j] = 'r';
else permission[j] = '-';
if(file_stat.st_mode & (S_IWRITE >> (i * 3)))
permission[j+1] = 'w';
else permission[j+1] = '-';
if(file_stat.st_mode & (S_IEXEC >> (i * 3)))
permission[j+2] = 'x';
else permission[j+2] = '-';
}
permission[9] = '\0';
/* 권한 출력 */
strcat(list, permission);
sprintf(buff, "%5d ", file_stat.st_nlink);
strcat(list, buff);
/* 사용자 이름 출력 */
user_information = getpwuid(file_stat.st_uid);
strcat(list, user_information->pw_name);
/* 그룹 이름 출력 */
group_information = getgrgid(file_stat.st_gid);
sprintf(buff, " %s", group_information->gr_name);
strcat(list, buff);
/* 파일 크기 출력 */
sprintf(buff, " %10d", file_stat.st_size);
strcat(list, buff);
/* 파일 접근 시간 출력 */
sprintf(buff, " %s", ctime(&file_stat.st_mtime));
buff[strlen(buff) - 1] = '\0';
strcat(list, buff);
printf("%s %s\n", list, directory->d_name);
}
putchar('\n');
closedir(dir);
}
void
fatal(char *error_name)
{
perror(error_name);
exit(1);
}
'EMBEDDED > SYSTEM Proc' 카테고리의 다른 글
[Normal] ls -R(재귀함수사용) (0) | 2012.07.13 |
---|---|
[Normal] ls -F 파일의 종류 표시하기 (0) | 2012.07.13 |
[analysis] lint, splint (0) | 2012.07.13 |
[Process] n개의 프로세스 체인을 만드는 프로그램 (0) | 2012.07.13 |
[Normal] 프로그램 cpu사용률을 출력하는 exit 핸드러를 가지는 프로그램 (0) | 2012.07.13 |