LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1077|回复: 6

请问:有没有人写过用C实现ls命令,急!

[复制链接]
发表于 2004-11-1 10:38:48 | 显示全部楼层 |阅读模式
发表于 2004-11-1 12:02:38 | 显示全部楼层

读一下它的源代码不就是了

在coreutils中
发表于 2004-11-1 21:17:52 | 显示全部楼层
how can i read that??
sry,i'm a new linuxer~~  
发表于 2004-11-3 10:19:06 | 显示全部楼层
* list.c -  C version of a simple UNIX ls utility */

/* c89 list.c -o list */

/* need types.h and dir.h for definitions of scandir and alphasort */
#include <sys/types.h>
#include <sys/dir.h>

/* definition for getwd ie MAXPATHLEN etc */
#include <sys/param.h>

#include <stdio.h>

#define FALSE 0
#define TRUE !FALSE

/* prototype std lib functions */
extern  int alphasort();

/* variable to store current path */
char pathname[MAXPATHLEN];

main()
{ int count,i;
  struct direct **files;
  int file_select();

  if (getwd(pathname) == NULL )
   { printf("Error getting path\n);
     exit(1);
   }
  printf("Current Working Directory = %s\n",pathname);

  count =
    scandir(pathname, &files, file_select, alphasort);


  /* If no files found, make a non-selectable menu item */
  if (count <= 0) {
    printf("No files in this directory\n");
    exit(0);
  }

  printf("Number of files = %d\n",count);

  for (i=1;i<count+1;++i)
   { printf("%s   ",files[i-1]->d_name);
     if ( (i % 4) == 0) printf("\n");
   }
   
  printf("\n"); /* flush buffer */
}


int
file_select(struct direct   *entry)
{  /* ignore . and .. entries */
      if ((strcmp(entry->d_name, ".") == 0) ||
            (strcmp(entry->d_name, "..") == 0))
                return (FALSE);
     else
       return (TRUE);
}
发表于 2004-11-3 21:03:58 | 显示全部楼层

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7. #include <time.h>

  8. static int get_info(const char * filename)
  9. {
  10.   struct stat statbuf;
  11.   if(stat(filename,&statbuf)==-1)
  12.     {
  13.       printf("%s\n",filename);
  14.       return(1);
  15.      }
  16.   if(S_ISDIR(statbuf.st_mode))
  17.     printf("%s\t Directory\tmodified at %s",filename,ctime(&statbuf.st_mtime));
  18.   if(S_ISREG(statbuf.st_mode))
  19.     printf("%s\tsize : %ld bytes\tmodified at %s",filename,statbuf.st_size,ctime(&statbuf.st_mtime));
  20.   return(0);
  21. }

  22. int main(int argc,char **argv)
  23. {
  24.   DIR * dirp;
  25.   struct dirent * direntp;

  26.   if((dirp=opendir(argv[1])))
  27.     {
  28.       while((direntp=readdir(dirp))!=NULL)
  29. get_info(direntp->d_name);
  30. /* printf("%s\t",direntp->d_name);*/
  31.       closedir(dirp);
  32.       exit(1);
  33.     }
  34.   else
  35.     {
  36.       printf("Error\n");
  37.       exit(1);
  38.     }
  39. }
复制代码


其实程序还有一点问题,在当前目录下使用效果很好,但是如果ls的是一个其他的路径,就无法显示一些文件的具体信息,具体来说,就是stat (filename,&statbuf)会等于-1,如果只是希望显示目录名和文件名,可以把get_info函数删掉,把mian函数里加注释的那一句去掉注释就可以了
发表于 2004-11-3 21:28:05 | 显示全部楼层
牙刷,怎么每次我布置作业你们都来问人哦!
这有啥子意思啊?
发表于 2004-11-4 11:57:30 | 显示全部楼层
楼上的没有必要为这些人生气,学习计算机绝对不是一件轻松的事,如果不是真正的喜欢,很难学得好。你强迫他学反而效果不好,顺其自然吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表