鱼C论坛

 找回密码
 立即注册
查看: 2907|回复: 2

[技术交流] linux下边自己写的一个列出指定目录的c文件

[复制链接]
发表于 2011-8-20 17:32:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
只加了一个选项-d    {:7_158:}来列出目录,其它的选项我会在后续完善!

文件名   只列出当前目录
文件名 -d  目录名     列出指定目录

运行环境LINUX

#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/stat.h>



int main( int argc, char **argv )
{
        
        void list_dir ( char *);

        char         dir_name[20]={0};
        int         list_dir_option = 0;
        while ( *++argv != NULL && **argv == '-' ) {
        /*
        **dispose the parameter next '-'
        **
        */
        
                switch (*++*argv) {
                        case 'd':
                                list_dir_option = 1;
                                break;
                        default :
                                printf("Parameter is error !\n");
                                exit (0);
                }
        }
        if ( *argv != NULL ) {
                strcpy ( dir_name, *argv );
                if ( list_dir_option )
                        list_dir ( dir_name );
                else 
                        printf("Please input the parameter !\n");
        }
        else if ( *argv == NULL ) {
                if ( !list_dir_option )
                        list_dir (".");
                else 
                        printf("Please input directory !\n");
        }
        else
                printf("Parameter is error !\n");
}




/*
**To list the directory
*/
void
list_dir ( char *dir_names )
{
        DIR         *dp;
        struct         dirent         *entry;
        struct         stat        statbuf;

        if ( ( dp = opendir( dir_names ) ) == NULL ) {
                fprintf ( stderr, "cannot open the directory:%s\n",dir_names );
                return;
        }
        chdir ( dir_names );        /*into directory*/
        /*
        **Loop ergdic this directory and list the dir or file
        */
        while ( ( entry = readdir ( dp ) ) != NULL ) {
                /*
                **To detection the dir
                */
                lstat (entry->d_name, &statbuf );
                if ( S_ISDIR (statbuf.st_mode) ) {
                        /*
                        **skip "." and ".."
                        */
                        if ( strcmp ( entry->d_name, "." ) == 0 ||
                             strcmp ( entry->d_name, "..") == 0 )
                                continue;
                        printf("%s/\t<----------------------[DIR]\n", entry->d_name );
                }
                else
                        printf("%s\n", entry->d_name );
                }
        chdir ("..");
        closedir (dp);
}
list2.png

list1.png


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-9-16 13:46:53 | 显示全部楼层
个人感觉是不是调用系统命令像:ll,la之类的也可以啊。。。
要是带参数的话就用strcat连接起来,注意空格的话也可以吧
没有试过。。猜的。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2016-4-14 10:43:22 | 显示全部楼层
没学过
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-11 16:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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