http://zhoulifa.bokee.com/用C语言自己编写一个ls程序

24 篇文章 0 订阅

这是自己用C语言编写的一个ls程序,源代码如下:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

void do_ls(char *);
void dostat(char *);
void show_file_info( char *, struct stat *);
void mode_to_letters( int , char * );
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );

/************关于本文档********************************************
*filename:用C语言自己编写一个ls程序
*purpose:自己编写的一个ls程序
*wrote by: zhoulifa(zhoulifa@163.com) 周立 发(http://zhoulifa.bokee.com)
Linux爱好者 Linux知识 传播者 SOHO族 开发者 最擅长C语言 编程
*date time:2006-07-16 16:00:00
*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途
* 但请遵循GPL。
*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力
*********************************************************************/
int main(int argc, char **argv)
{
  if ( argc == 1 )
    do_ls( "." );
  else
    while ( --argc ){
      printf("%s:/n", *++argv );
      chdir(*argv); //切换到指定目录后再显示该目录的内容
      do_ls( *argv );
      chdir(""); //再回到当前工作目录来
    }
 return 0;
}

void do_ls( char * dirname )
{
  DIR    *dir_ptr;
  struct dirent  *direntp;

  if ( ( dir_ptr = opendir( dirname ) ) == NULL ) {/*打开目录*/
    fprintf(stderr,"ls1: cannot open %s, not a directory. treat as a file shown below:/n", dirname);
    dostat( dirname ); //如果不是目录就当作文件来显示其属性
 }
  else
  {
    while ( ( direntp = readdir( dir_ptr ) ) != NULL )
      dostat( direntp->d_name );/*逐个显示目录里文件信息*/
    closedir(dir_ptr);
  }
}

void dostat( char *filename )
{
  struct stat info;

  if ( stat(filename, &info) == -1 )    /*取文件信息失败*/
    perror( filename );
  else          /*显示文件信息*/
    show_file_info( filename, &info );
}

void show_file_info( char *filename, struct stat *info_p )
{
  char    modestr[11];

  mode_to_letters( info_p->st_mode, modestr );

  printf( "%s"    , modestr );
  printf( "%4d "  , (int) info_p->st_nlink); 
  printf( "%-8s " , uid_to_name(info_p->st_uid) );
  printf( "%-8s " , gid_to_name(info_p->st_gid) );
  printf( "%8ld " , (long)info_p->st_size);
  printf( "%.12s ", 4+ctime(&info_p->st_mtime));
  printf( "%s/n"  , filename );

}

void mode_to_letters( int mode, char * str )
{
    strcpy( str, "----------" );           /* default=无参数 */

    if ( S_ISDIR(mode) )  str[0] = 'd';    /* 目录       */
    if ( S_ISCHR(mode) )  str[0] = 'c';    /* 字符设备   */
    if ( S_ISBLK(mode) )  str[0] = 'b';    /* 块设备     */

    if ( mode & S_IRUSR ) str[1] = 'r';    /* 用户权限  */
    if ( mode & S_IWUSR ) str[2] = 'w';
    if ( mode & S_IXUSR ) str[3] = 'x';

    if ( mode & S_IRGRP ) str[4] = 'r';    /* 组权限 */
    if ( mode & S_IWGRP ) str[5] = 'w';
    if ( mode & S_IXGRP ) str[6] = 'x';

    if ( mode & S_IROTH ) str[7] = 'r';    /* 其人的 权限 */
    if ( mode & S_IWOTH ) str[8] = 'w';
    if ( mode & S_IXOTH ) str[9] = 'x';
}

char *uid_to_name( uid_t uid )
{
  struct  passwd *pw_ptr;
  static  char numstr[10];

  if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){
    sprintf(numstr,"%d", uid);
    return numstr;
  }
  else
    return pw_ptr->pw_name ;
}

char *gid_to_name( gid_t gid )
{
  struct group *grp_ptr;
  static  char numstr[10];

  if ( ( grp_ptr = getgrgid(gid) ) == NULL ){
    sprintf(numstr,"%d", gid);
    return numstr;
  }
  else
    return grp_ptr->gr_name;
}

运行结果如下:

./a.out /
/:
drwxr-xr-x  22 root     root         4096 Jul 13 12:37 .
drwxr-xr-x  22 root     root         4096 Jul 13 12:37 ..
drwxr-xr-x   2 root     root        49152 May 21 03:00 lost+found
drwxr-xr-x 130 root     root         8192 Jul 16 15:01 etc
drwxr-xr-x   3 root     root         4096 May 21 03:01 media
drwxr-xr-x   2 root     root         4096 May 21 03:01 cdrom
-rw-r--r--   1 root     root      6773545 Jul 13 08:06 initrd.img
drwxr-xr-x  15 root     root         4096 Jun  9 08:40 var
drwxr-xr-x  18 root     root         8192 Jul 13 12:37 lib
drwxr-xr-x  11 root     root         4096 May 21 03:08 usr
drwxr-xr-x   2 root     root         4096 Jul 14 09:41 bin
drwxr-xr-x   3 root     root         4096 Jul 13 12:37 boot
drwxr-xr-x  15 root     root        15260 Jul 16 14:37 dev
drwxr-xr-x   5 root     root         4096 Jun  1 09:22 home
drwxr-xr-x   6 root     root         4096 Jul 10 11:30 mnt
dr-xr-xr-x 185 root     root            0 Jul 16 22:37 proc
drwxr-xr-x  36 root     root         4096 Jul  9 15:45 root
drwxr-xr-x   2 root     root         8192 Jul 14 09:44 sbin
drwxrwxrwx  17 root     root         4096 Jul 16 16:15 tmp
drwxr-xr-x  10 root     root            0 Jul 16 22:37 sys
drwxr-xr-x   2 root     root         4096 May 21 03:02 srv
drwxr-xr-x   5 root     root         4096 May 29 13:03 opt
drwxr-xr-x   2 root     root         4096 May 21 03:02 initrd
-rw-r--r--   1 root     root      1414702 Jul  8 05:22 vmlinuz
drwxr-xr-x  34 administrator administrator     4096 Jul 12 15:31 data
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值