Linux C编程练习

1、定制自己的ls命令

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void do_ls(char*dir_entry);
void do_ls_filename(char*dir_entry);
void do_stat( char* filename );
void show_list( char* filename, struct stat* statinfo );
void mode_to_letters( mode_t filemode, char str[] );
void show_time( time_t filetime );
char* format_time( char* dsttime, const char* srctime );

//用户id转名称
char* uid_to_name( uid_t uid );
//组id转名称
char* gid_to_name( gid_t gid );

int flag;
int main(int argc,char*argv[])
{
    char*dir=".";
    if(argc>2)
    {
        dir=argv[3];  
    }
    //printf("%s\n",dir );
    //printf("%c\n",argv[2][1] );
    switch(argv[2][1]){
        case 'l':flag=0;do_ls(argv[3]);break;
        case 'A':flag=1;do_ls(argv[3]);break;
        case 'n':flag=2;do_ls(argv[3]);break;
        case 'a':flag=3;do_ls(argv[3]);break;
        case 'm':flag=4;do_ls(argv[3]);break;
    }
}

void do_ls(char* dir_entry) {
    DIR* pDir;
    struct dirent* pCurDir;
    if( ( pDir = opendir( dir_entry ) ) == NULL ){
        perror( "read dir" );
        exit( -1 );
    }
    else {
        while( ( pCurDir = readdir( pDir ) ) != NULL ) {
            //printf("%s\n",pCurDir->d_name );
            do_stat( pCurDir->d_name );
        }
        closedir( pDir );
    }
}

//得到文件信息
void do_stat( char* filename ){
    struct stat statinfo;
    if ( stat( filename, &statinfo ) == -1 ) {
        printf( "打开%s失败\n", filename );
        exit( -1 );
    }else {

        show_list( filename, &statinfo );
    }
}

//显示文件列表
void show_list( char* filename, struct stat* statinfo ) {
    mode_t st_mode = statinfo->st_mode;

    char str[10];
    mode_to_letters( st_mode, str );
    switch(flag){
        case 0:{
            printf( "%s\t", str );                         //文件权限和文件类型信息
            printf( "%ld\t", statinfo->st_nlink ); //该文件上硬链接个数 
            printf( "%s\t\t", uid_to_name( statinfo->st_uid ) ); //UID号转用户名
            printf( "%s\t", gid_to_name( statinfo->st_gid ) ); //GID号转组名
            printf( "%10ld", statinfo->st_size ); //文件大小
            show_time( statinfo->st_mtime ); //最后一次修改时间
            printf( "\t%s", filename );

            printf( "\n" );
                };break;
        case 1:{
            printf("%s ",filename );   
        };break;
        case 2:{
            printf( "%s\t", str );                         //文件权限和文件类型信息
            printf( "%ld\t", statinfo->st_nlink ); //该文件上硬链接个数 
            printf( "%d\t\t", statinfo->st_uid  ); //UID号
            printf( "%d\t", statinfo->st_gid  ); //GID号
            printf( "%10ld", statinfo->st_size ); //文件大小
            show_time( statinfo->st_mtime ); //最后一次修改时间
            printf( "\t%s", filename );
            printf( "\n" );
        };break;
        case 3:
            printf("%s ", filename);break;
        case 4:
            printf("%s,",filename);break;
    }
    printf("\n");
}

char* uid_to_name( uid_t uid ){
    return getpwuid( uid )->pw_name;
}

char* gid_to_name( gid_t gid ){
    return getgrgid( gid )->gr_name;
}

void mode_to_letters( mode_t filemode, char str[] ) {

    strcpy( str, "----------" );
    if( S_ISREG( filemode ) ) str[0] = '-';
    if( S_ISDIR( filemode ) ) str[0] = 'd';
    if( S_ISLNK( filemode ) ) str[0] = 'l';

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

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

    //其他组权限位
    if( filemode & S_IROTH ) str[7] = 'r';
    if( filemode & S_IWOTH ) str[8] = 'w';
    if( filemode & S_IXOTH ) str[9] = 'x';
}

void show_time( time_t filetime ) {
    struct tm* ptm;
    ptm = localtime( &filetime );

    int month = ptm->tm_mon + 1;
    int day = ptm->tm_mday;
    int hour = ptm->tm_hour;
    int min = ptm->tm_min;

    char srchour[3] = "0";
    char srcmin[3] = "0";
    char dsthour[3] = "0";
    char dstmin[3] = "0";
    sprintf( srchour, "%d", hour );
    sprintf( srcmin, "%d", min );
    format_time( dsthour, srchour );
    format_time( dstmin, srcmin );

    printf( "%4d月%4d%4s:%2s", month, day, dsthour, dstmin );
}

char* format_time( char* dsttime, const char* srctime ) {
    if( strlen( srctime ) < 2 ) {
        return strcat( dsttime, srctime );
    }
    return strcpy( dsttime, srctime );
}

2、遍历当前目录,输出目录和文件详细信息

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#define MAX 102
void printdir(char*dir,int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;

    if((dp=opendir(dir))==NULL)
    {

        //fprintf(stderr,"can't not open directory:%s\n",dir);
        return;
    }

    chdir(dir);
    while((entry=readdir(dp)) != NULL)
    {
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode))
        {
            if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            printdir(entry->d_name,depth+4);
        }
        else 
            printf("%*s%s\n",depth,"",entry->d_name);
    }
    closedir(dp);
}


int main()
{
    char buffer[MAX];
    char *p=getcwd(buffer,MAX);
    printf("The current directory:%s\n",buffer);
    printdir(buffer,0);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weifuliu/article/details/80559318