首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

求各位大神的解答,linux一个小程序的有关问题,至今仍困扰着小弟我

2013-03-29 
求各位大神的解答,linux一个小程序的问题,至今仍困扰着我!其实这个程序就是想实现ls -al的功能 ,但遇到一

求各位大神的解答,linux一个小程序的问题,至今仍困扰着我!
其实这个程序就是想实现ls -al  的功能 ,但遇到一个问题,让我迟迟没有解决,上代码吧,大家复制代码后试试?
      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<unistd.h>
      4 #include<sys/types.h>
      5 #include<sys/stat.h>
      6 #include<dirent.h>
      7 #include<string.h>
      8 
      9 
     10 #define N_BITS 3
     11 #define MAX_PATH 80
     12 
     13 void output_dir_info(const char *new_docunment);
     14 void output_file_info(struct stat file);
     15 
     16 int main(int argc,char **argv)
     17 {
     18         char *docunment_path;
     19         docunment_path=(char *)malloc(sizeof(char)*MAX_PATH);
     20         getcwd(docunment_path,MAX_PATH);
     21         if(argc==1)                          //read current docunment   
     22                 output_dir_info(docunment_path);
     23         else if(argc==2)                     //panduan pramamter
     24         {
     25                 struct stat stat_file;
     26                 stat(argv[1],&stat_file);
     27                 if(S_ISREG(stat_file.st_mode))         //a file
     28                 {
     29                         output_file_info(stat_file);
     30                 }
     31                 else if(S_ISDIR(stat_file.st_mode))                             //a docunment
     32                 {


     33                         char *new_docunment;
     34                         new_docunment=(char *)malloc(sizeof(docunment_pa        th)+1+sizeof(argv[1])+1);
     35                         sprintf(new_docunment,"%s/%s\0",docunment_path,a        rgv[1]);
     36                         output_dir_info(new_docunment);              //read dir
     37                 }
     38         }
     39         else                            //input error
     40         {
     41                 fprintf(stderr,"Usage :%s [file_name or dir_name]\n",arg        v[0]);
     42         }
     43 
     44         return 0;
     45 }
     46 void output_dir_info(const char *docunment)                                            //read dir
     47 {
     48         DIR *dirp=NULL;
     49         dirp=opendir(docunment);
     51         struct dirent *entp=NULL;
     52 
     53         while(NULL!=(entp=readdir(dirp)))
     54         {
     55                 if(strcmp(entp->d_name,"..")==0||strcmp(entp->d_name,"."        )==0)
     56                 {
     57                         continue;
     58                 }


     59                 char *file_path=(char *)malloc(sizeof(docunment)+1+sizeof(entp->d_name)+1);
     60                 sprintf(file_path,"%s/%s\0",docunment,entp->d_name);
     61                 struct stat stat_file;
     62                 stat(file_path,&stat_file);
     63                 output_file_info(stat_file);
     64         }
     65 }
     66 void output_file_info(struct stat file)                //print file
     67 {
     68         unsigned int i ,mask=0700;
     69         unsigned num_temp;
     70         static char *perm[]={"---","--x","-w-","-wx","r--","r-x","rw-","        rwx"};
     71         for(i=3;i;i--)
     72         {
     73                 num_temp=(file.st_mode&mask)>>(i-1)*N_BITS;
     74                 printf("%3s",perm[num_temp]);
     75                 mask>>=N_BITS;
     76         }
     77         printf("\t%d\t%d\t%ld\t%ld\n",file.st_nlink,file.st_uid,file.st_        size,file.st_atime);
     78 }

       程序运行时传递一个“目录”作为参数。 问题主要出在line47-line51,当运行到47行时,用gdb单步调试发现char *docunment = 当前的目录路径,但一到line51后,char *docunment的值就不对了??一直没搞清是为什么??  求各位大神帮小弟一把! 非常感谢
                                                                 linux
[解决办法]
你59行分配的空间不足吧
 char *file_path=(char *)malloc(sizeof(docunment)+1+sizeof(entp->d_name)+1);

那个sizeof(document)是指针本身的大小,通常是4

你这儿应该改为strlen(document)

热点排行