리눅스 ls -l 정렬하기

rlaguddls10의 이미지

안녕하세요 리눅스를 공부하다가 궁금한게 생겨서 질문해봅니다.
파일의 정보를 출력해 주는 ls -l 커맨드를 구현해보았는데요.
ls -l의 기능은 정상적으로 수행되는데 여기서 파일의 이름이 알파벳순으로 정렬되도록 하려면 어떻게 수정해야할까요??
구조체를 사용해서 해보려고 했는데 타입이 자꾸 안 맞네요ㅠ

#include<stdio.h>
 #include<stdlib.h>
 #include<sys/stat.h>
 #include<sys/types.h>
 #include<dirent.h>
 #include<unistd.h>
 #include<pwd.h>
 #include<grp.h>
 #include<time.h>
 
 int main(int argc,char *argv[])
 {
   DIR *nowdir;
   struct dirent *redir;
   struct stat st;
   struct passwd *file_own;
   struct group *file_grp;
   struct tm *tm_ptr;
   char prlist[11];
   int i;
   mode_t modes;
 
   if(argc==2){
       if((nowdir=opendir(argv[1]))==NULL){
           perror("Directory Open ERROR");
           exit(1);
       }
   }
   else if((nowdir=opendir("."))==NULL){
       perror("Diretory Open ERROR");
       exit(1);
   }
 
   while((redir=readdir(nowdir))!=NULL)
   {
 
       for(i=0;i<10;i++)
           prlist[i]='-';
       prlist[10]='\0';
 
       stat(redir->d_name,&st);
       modes=st.st_mode;
 
       if(S_ISDIR(modes)) 
           prlist[0]='d';
       if(modes & S_IRUSR) 
           prlist[1]='r';
       if(modes & S_IWUSR)
           prlist[2]='w';
       if(modes & S_IXUSR)
           prlist[3]='x';
       if(modes & 0040)
           prlist[4]='r';
       if(modes & 0020)
           prlist[5]='w';
       if(modes & 0010)
           prlist[6]='x';
       if(modes & 0004)
           prlist[7]='r';
       if(modes & 0002)
           prlist[8]='w';
       if(modes & 0001)
           prlist[9]='x';
 
       file_own=getpwuid(st.st_uid);
       file_grp=getgrgid(st.st_gid);
       tm_ptr=gmtime(&st.st_mtime);
 
       printf("%s %2d %5s %5s %5d %d-%02d-%02d %d:%02d %-05s\n"
           ,prlist,st.st_nlink,file_own->pw_name,file_grp->gr_name,st.st_size
           ,tm_ptr->tm_year+1900,tm_ptr->tm_mon,tm_ptr->tm_mday,tm_ptr->tm_hour+9,tm_ptr->tm_min
           ,redir->d_name);
 
   }
}
익명 사용자의 이미지

먼저 전체 파일 리스트를 읽어 배열에 담아,
sort 해서 출력하면 될꺼 같네요
참고로 현재 올리신 소스는 디렉토리에 파일이 10개 이상일 때 오류 발생합니다.

bushi의 이미지

scandir()을 구현하는 방법에 대한 질문인가요, 아니면 scandir() 을 이용하는 방법에 대한 질문인가요.

첫번째 댓글이 달린 다음에 본문이 수정됐군요.

rlaguddls10의 이미지

구현하는 쪽이 맞을 것 같습니다
정렬함수 라이브러리를 사용하는게 아니라 파일 이름들을 버블소트를 이용해서 정렬하여 출력할 생각이라서요

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.