LinuxSir.cn,穿越时空的Linuxsir!

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

ls ?

[复制链接]
发表于 2004-8-20 02:08:20 | 显示全部楼层 |阅读模式
比如我想得到文件下的文件list,我们平时就可以使用到ls命令

可在c语言下,我想查看某个目录下有哪些文件并且list出来,怎办 ?

ls可以调用的吗,,,,,, ??? 谢谢大家
发表于 2004-8-20 07:50:20 | 显示全部楼层
system("ls pathname");
发表于 2004-8-20 08:19:44 | 显示全部楼层
man opendir
man readdir
发表于 2004-8-20 11:08:42 | 显示全部楼层

一个例文件的演示程序

int listfile( char *path )
{
        DIR        *dirp;
        struct dirent        *pDirEnt;
        int        ret=-1,i;
       
        dirp = opendir( path );
        i=0;
        while ( (pDirEnt = readdir( dirp )) != NULL ) {
                if ( strncmp(pDirEnt->d_name,".",1) != 0 ) {
                        ret=0;
                        mvaddstr(i, 0, pDirEnt->d_name);
                        i++;
                }
        }
        closedir( dirp );
        refresh();

        return(ret);
}
 楼主| 发表于 2004-8-20 20:38:34 | 显示全部楼层
看不懂是什么意思啊 ?
发表于 2004-8-21 07:50:04 | 显示全部楼层

这是我编的一个完整的程序,你慢慢看吧.

[PHP]
#include <stdio.h>
#include <curses.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#define        LINE        4
#define        COL        4

void        get_date();
void        get_time();
void        show_time();
void        initial();
void        exit_program();
int        show_msg();
int        extract_fd();
int        copyfile();
void        testexit();
int        listfile();
int        rmfileindir();

int main(void)
{
        int        year,month,day;
        char        today[9],cweek[3];
        int        i=0,ret=0;
        char        TmpDir[100], DataDir[100], SBdir[20], ErrDir[100];
        char        file_source[100],file_target[100];
        char        Command[100], Message[100],SendTo[50];

        initial();

        get_date(&year, &month, &day, cweek);
        sprintf(today, "%4d%02d%02d", year, month, day);

        sprintf(DataDir,"%s/data",getenv("HOME"));
        sprintf(TmpDir,"%s/tmp",getenv("HOME"));
        sprintf(ErrDir,"%s/unsuccessful",getenv("HOME"));
        sprintf(SBdir, "sb/sb%4d%02d", year, month);
        strcpy(SendTo,"XX:/whereis/the/backup/data/");
      
        rmfileindir(TmpDir);

        mvaddstr(LINE+i,COL,"┌─────────────────────────────────┐");
        ++i;
        mvaddstr(LINE+i,COL,"│ 日期:                  XXXXXXXX光盘数据备份系统          星期    │");
        ++i;
        mvaddstr(LINE+i,COL,"├─────────────────────────────────┤");
        ++i;
        mvaddstr(LINE+i,COL,"│                                                                  │");
        ++i;
        mvaddstr(LINE+i,COL,"│                                                                  │");
        ++i;
        mvaddstr(LINE+i,COL,"│                                                                  │");
        ++i;
        mvaddstr(LINE+i,COL,"│   ####   #    #  #####   ######    ##    #    #   ####   #    #  │");
        ++i;
        mvaddstr(LINE+i,COL,"│  #    #  ##   #  #    #  #        #  #   ##  ##  #    #  ##   #  │");
        ++i;
        mvaddstr(LINE+i,COL,"│  #    #  # #  #  #    #  #####   #    #  # ## #  #    #  # #  #  │");
        ++i;
        mvaddstr(LINE+i,COL,"│  #    #  #  # #  #    #  #       ######  #    #  #    #  #  # #  │");
        ++i;
        mvaddstr(LINE+i,COL,"│  #    #  #   ##  #    #  #       #    #  #    #  #    #  #   ##  │");
        ++i;
        mvaddstr(LINE+i,COL,"│   ####   #    #  #####   ######  #    #  #    #   ####   #    #  │");
        ++i;
        mvaddstr(LINE+i,COL,"│                                                                  │");
        ++i;
        mvaddstr(LINE+i,COL,"│                                                                  │");
        ++i;
        mvaddstr(LINE+i,COL,"│                                                                  │");
        ++i;
        mvaddstr(LINE+i,COL,"├───┬────────────────────────┬────┤");
        ++i;
        mvaddstr(LINE+i,COL,"│状态栏│                                                │        │");
        ++i;
        mvaddstr(LINE+i,COL,"└───┴────────────────────────┴────┘");
        mvaddstr(LINE+1,COL+9,today);
        mvaddstr(LINE+1,COL+64,cweek);
        refresh();
        show_time(stdscr);

        sprintf(file_source, "%s/%s/B107%s01.txt",DataDir,SBdir,today);
        sprintf(file_target, "%s/B107%s01.txt",TmpDir,today);

        ret=copyfile(file_source,file_target);
        if ( ret == 0 ) {
                sprintf(file_source, "%s/%s/W107%s01.txt",DataDir,SBdir,today);
                sprintf(file_target, "%s/W107%s01.txt",TmpDir,today);
                ret=copyfile(file_source,file_target);
                if ( ret == 0 )
                        show_msg("XX数据准备完毕!",1);
                else {
                        show_msg("准备数据出错!回车退出程序",1);
                        exit_program(ret);
                }
        } else
                show_msg("本日无XX数据或准备数据出错!",1);

        show_msg("请插入XX软盘, 回车开始拷盘.",1);
        ret=extract_fd(TmpDir);
        if ( ret != 0 )
                exit_program(ret);

        show_msg("请插入YY业务软盘, 回车开始拷盘.",1);
        ret=extract_fd(TmpDir);
        if ( ret != 0 )
                exit_program(ret);

        sprintf(file_source, "%s/dat1.file",TmpDir,today);
        sprintf(file_target, "%s/dat1_%s.file",TmpDir,today);
        ret=rename(file_source, file_target);
        sprintf(file_source, "%s/dat2.file",TmpDir,today);
        sprintf(file_target, "%s/dat2_%s.file",TmpDir,today);
        ret=rename(file_source, file_target);
        sprintf(file_source, "%s/sys2.file",TmpDir,today);
        sprintf(file_target, "%s/sys2_%s.file",TmpDir,today);
        ret=rename(file_source, file_target);

        listfile(TmpDir);
        show_msg("读盘成功, 正在传送到主机, 请等待...",0);

        sprintf(Command,"su - root -c \"rcp %s/*%s* %s\" 1 2>/dev/null",TmpDir,today,SendTo);

        ret=system(Command);
        if ( ret != 0 ) {
                ret=system(Command);
                if ( ret != 0 ) {
                        sprintf(Command,"cp %s/*%s* %s/", TmpDir,today,ErrDir);
                        system(Command);
                        show_msg("传送失败! 请与系统管理员联系.",1);
                        exit_program(ret);
                }
        } else
                show_msg("传送完毕, 回车退出.",1);

        exit_program(0);
}

void show_time(WINDOW *oldwin)
{
        int hour, minute, second;
        int oldrow, oldcol;
        chtype oldattr;

        get_time( &hour, &minute, &second );

        getyx(oldwin, oldrow, oldcol);

        mvprintw(LINE+16,COL+60,"%02d:%02d:%02d",hour,minute,second);
        refresh();
        wmove( oldwin, oldrow, oldcol );
        wrefresh(oldwin);
}

void get_date(int *year, int *month, int *day, char *cweek)
{
        int week;
        char *cweeks="日一二三四五六";
        time_t t;
        struct tm *local_time;

        t = time(0);
        local_time = localtime(&t);

        *year  = 1900 + local_time->tm_year;
        *month = local_time->tm_mon+1;
        *day   = local_time->tm_mday;
        week   = local_time->tm_wday;

        strncpy(cweek,cweeks+week*2,2);
        cweek[2]='\0';

        return ;
}

       
void get_time(int *hour, int *minute, int *second)
{
        time_t t;
        struct tm *local_time;

        t = time(0);
        local_time = localtime(&t);

        *hour   = local_time->tm_hour;
        *minute = local_time->tm_min;
        *second = local_time->tm_sec;

        return ;
}

void initial(void)
{
  initscr();
  cbreak();
  nonl();
  noecho();
  intrflush(stdscr,FALSE);
  keypad(stdscr,TRUE);
  refresh();
}

void exit_program(int flag)
{
        endwin();
        exit(flag);
}

int show_msg(char *message, int bz)
{
  WINDOW *msgwin;

  msgwin=newwin(1,48,LINE+16,COL+10);
  mvwaddstr(msgwin,0,0,message);
  touchwin(msgwin);
  wrefresh(msgwin);
  if( bz==1 ) {
        halfdelay(10);
        for ( ; getch() == -1; )
                show_time(msgwin);
        cbreak();
  }
}

int extract_fd(char *targetdir)
{
        int        ret=0;
        char        Message[50],Command[100];

        sprintf(Command,"cd %s; tar x6 2>/dev/null", targetdir);
        show_msg("正在拷盘, 请稍候...",0);
        ret=system(Command);
        if ( ret != 0 ) {
                sprintf(Message, "[%d]读软盘错, 回车重试.", ret);
                show_msg(Message,1);
                show_msg("正在拷盘, 请稍候...",0);
                ret=system(Command);
                if ( ret != 0 ) {
                        sprintf(Message, "[%d]读软盘错, 请与系统管理员联系!", ret);
                        show_msg(Message,1);
                }
        }

        return(ret);
}

int copyfile(char *sourcefile, char *targetfile)
{
        int        ret=0;
        FILE        *fp_source, *fp_target;
        int        ch;

        fp_source=fopen(sourcefile,"r");
        if ( fp_source == NULL )
                ret=-1;
        if ( ret == 0 ) {
                fp_target=fopen(targetfile,"w");
                if ( fp_target == NULL )
                        ret=-2;
        }

        if ( ret == 0 )
                while ( (ch=fgetc(fp_source)) != EOF )
                        fputc(ch,fp_target);

        fclose(fp_source);
        if ( ret != -1 )
                fclose(fp_target);

        return(ret);
}

int listfile( char *path )
{
        DIR        *dirp;
        struct dirent        *pDirEnt;
        int        ret=-1,i=0;
        char        BlankLine[80];

        strcpy(BlankLine,"                                                                  ");
        for( i=0; i < 12; i++ )
                mvaddstr(LINE+3+i,COL+2,BlankLine);
        dirp = opendir( path );
        i=0;
        while ( (pDirEnt = readdir( dirp )) != NULL ) {
                if ( strncmp(pDirEnt->d_name,".",1) != 0 ) {
                        ret=0;
                        mvaddstr(LINE+3+i, COL+2, pDirEnt->d_name);
                        i++;
                }
        }
        closedir( dirp );
        refresh();

        return(ret);
}

int rmfileindir( char *path )
{
        DIR        *dirp;
        struct dirent        *pDirEnt;
        int        ret=-1;
        char        filename[100];

        dirp = opendir( path );
        while ( (pDirEnt = readdir( dirp )) != NULL ) {
                if ( strncmp(pDirEnt->d_name,".",1) != 0 ) {
                        sprintf(filename,"%s/%s",path,pDirEnt->d_name);
                        ret=remove(filename);
                }
        }
        closedir( dirp );
        refresh();

        return(ret);
}
[/PHP]
发表于 2004-8-22 17:24:35 | 显示全部楼层
代码帖乱了,在show_time()中
  1. mvprintw(LINE+16,COL+60,"%02d<img src="images/smilies/biglove.gif" border="0" alt="">02d<img src="images/smilies/biglove.gif" border="0" alt="">02d",hour,minute,second);
复制代码
这样的代码都有,会误了新手的。应该是mvprintw(LINE+16,COL+60,"%2d: %2d: %2d",hour,minute,second);

另外还得跟着你事先建好$HOME/tmp、$HOME/data等目录,不然一直就是段错误。
发表于 2004-8-23 09:49:11 | 显示全部楼层
给你们一个ls的例子吧:

  1. /*  We start with the appropriate headers and then a function, printdir,
  2.     which prints out the current directory.
  3.     It will recurse for subdirectories, using the depth parameter is used for indentation.  */

  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <dirent.h>
  7. #include <string.h>
  8. #include <sys/stat.h>

  9. void printdir(char *dir, int depth)
  10. {
  11.     DIR *dp;
  12.     struct dirent *entry;
  13.     struct stat statbuf;

  14.     if((dp = opendir(dir)) == NULL) {
  15.         fprintf(stderr,"cannot open directory: %s\n", dir);
  16.         return;
  17.     }
  18.     chdir(dir);
  19.     while((entry = readdir(dp)) != NULL) {
  20.         lstat(entry->d_name,&statbuf);
  21.         if(S_ISDIR(statbuf.st_mode)) {
  22.             /* Found a directory, but ignore . and .. */
  23.             if(strcmp(".",entry->d_name) == 0 ||
  24.                 strcmp("..",entry->d_name) == 0)
  25.                 continue;
  26.             printf("%*s%s/\n",depth,"",entry->d_name);
  27.             /* Recurse at a new indent level */
  28.             printdir(entry->d_name,depth+4);
  29.         }
  30.         else printf("%*s%s\n",depth,"",entry->d_name);
  31.     }
  32.     chdir("..");
  33.     closedir(dp);
  34. }

  35. /*  Now we move onto the main function.  */

  36. int main(int argc, char* argv[])
  37. {
  38.     char *topdir, pwd[2]=".";
  39.     if (argc != 2)
  40.         topdir=pwd;
  41.     else
  42.         topdir=argv[1];

  43.     printf("Directory scan of %s\n",topdir);
  44.     printdir(topdir,0);
  45.     printf("done.\n");

  46.     exit(0);
  47. }
复制代码
发表于 2004-8-23 15:40:16 | 显示全部楼层
最初由 lanche 发表
代码帖乱了,在show_time()中

  1. mvprintw(LINE+16,COL+60,"%02d<img src="images/smilies/biglove.gif" border="0" alt="">02d<img src="images/smilies/biglove.gif" border="0" alt="">02d",hour,minute,second);
复制代码

这样的代码都有,会误了新手的。应该是mvprintw(LINE+16,COL+60,"%2d: %2d: %2d",hour,minute,second);



对不起,没有注意到:p
发表于 2004-8-23 19:59:38 | 显示全部楼层

to lanche

我常遇到段错误,但一直不知道是什么原因造成的,你能告诉我通常什么情况下会出现段错误吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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