리눅스에서, ls 명령을 C로 짤려고 하는데요..
맘대로 안 되네요..
제가 원하는 건 파일 이름을 출력하고
그 옆에 파일 사이즈하고 변경시간을 출력하는 건데요..
밑에 있는데로 하면, 파일이름은 출력되는데,,
파일사이즈와 변경시간은?? 아무리 생각해봐도~~.
stat structure에 st_mtime과 st_size라는 게 있던데,,,
이걸 어떻게 출력하죠?
readdir 함수에서, 디렉토리안의 상세 정보를 원한다면
stat에 대한 호출이 필요하다고, 책에 나와있긴 한데,,
도대체 어디에서 호출해 줘야 하는지...
아래에서, print 함수를 좀 봐주십시오..
답변 부탁드립니다... 안녕히~~.
#include "unistd.h"
#include "stdio.h"
#include "dirent.h"
#include "string.h"
#include "sys/stat.h"
#include "sys/types.h"
#include "stdlib.h"
void print(char* dir)
{
DIR* dp;
struct dirent* entry;
struct stat statbuf;
// time_t time;
// off_t size;
if((dp=opendir(dir))==NULL){
fprintf(stderr,"cannot open directory: %s",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL){
statbuf.st_mtime;
statbuf.st_size;
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==0 ||
strcmp("..",entry->d_name)==0)
continue;
// time=S_ISDIR(statbuf.st_mtime);
//size=S_ISDIR(statbuf.st_size);
printf("%s directory",
entry->d_name);
}
else{
// time=S_ISDIR(statbuf.st_mtime);
//size=S_ISDIR(statbuf.st_size);
printf("%s file",
entry->d_name);
}
}
closedir(dp);
}
int main(int argc, char* argv<>)
{
char* topdir;
if(argc>=2)
topdir=argv[1];
print(topdir);
exit(0);
}