免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 859 | 回复: 0
打印 上一主题 下一主题

大家好! 请帮个忙? 多谢!有关C 语言的!!!!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-02-28 14:36 |只看该作者 |倒序浏览
我的代码 贴给你!!!#include<unistd.h>;
#include<sys/stat.h>;
#include<stdio.h>;
#include<dirent.h>;
#include<string.h>;
#include<sys/types.h>;
#define BLKSIZE 1024
char *get_perms(struct stat *sbuf,char *perms);
void list(char *);
void printout(char *,char *);
int main(int argc,char *argv[])
{
        struct stat sbuf;
        if(argc<2){
          list(".";
          exit(0);
        while(--argc){
          if(stat(*++argv,&sbuf)<0){
          perror(*argv);
          continue;
     }
        if((sbuf.st_mode & S_IFMT)==S_IFDIR)
         list(*argv);
        else
         printout(".",*argv);
     }
        exit(0);
  }
}
void list(char *name)
{
        DIR *dp;
        struct dirent *dir;
        if ((dp=opendir(name))==NULL){
           fprintf(stderr,"%s:cannot open.\n",name);
           return;
   }
        while((dir=readdir(dp))!=NULL)
           printout(name,dir->;d_name);
           closedir(dp);
  }
char *get_perms(struct stat *sbuf,char *perms)

{
        static char *modes[]={"---","--x","-w-","r--","r-x","rw-","rwx"};
        int i,j;
        *perms='\0';
        for(i=2;i>;=0;i--){
        j=(sbuf->;st_mode>;>;(i*3))& 07;
        strcat(perms,modes[j]);
}
if((sbuf->;st_mode & S_ISUID)!=0)
        perms[2]='s';
if((sbuf->;st_mode & S_ISGID)!=0)
        perms[5]='s';
if((sbuf->;st_mode & S_ISUID)!=0)
        perms[8]='t';
        return perms;
}

  void printout(char *dir,char *name)
      {
        int i,j;
        char perms[10];
        struct stat sbuf;
        char newname[BLKSIZE];
        sprintf(newname,"%s/%s",dir,name);
        stat(newname,&sbuf);
        printf("%5d",(sbuf.st_size+BLKSIZE-1)/BLKSIZE);
        switch(sbuf.st_mode & S_IFMT){
        case S_IFREG:putchar('_');break;
        case S_IFDIR:putchar('d');break;
        case S_IFCHR:putchar('c');break;
        case S_IFBLK:putchar('b');break;
        case S_IFIFO:putchar('l');break;
#ifdef S_IFSOCK
        case S_IFLNK:putchar('l');break;
#endif
#ifdef S_IFSOCK
        case S_IFSOCK:putchar('l');break;
#endif
        default:putchar('?');break;
    }
        get_perms(&sbuf,perms);

  printf("%s%3d%5d/%-5d",perms,sbuf.st_nlink,sbuf.st_uid,sbuf.st_gid);
        printf("%7d%.20s",sbuf.st_size,ctime(&sbuf.st_mtime));
        printf("%s\n",name);
  }

  

******************************
gcc -o file1 file1.c
./file1


显示:
Segmentation fault

请教问什么这样!请教!! 多谢!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP