亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1612 | 回復(fù): 0
打印 上一主題 下一主題

C寫的實現(xiàn)wc和ls函數(shù) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-10-09 12:38 |只看該作者 |倒序瀏覽
以下是用linux C寫的實現(xiàn)wc功能的函數(shù)
#include
#define BEGIN 1;
int main(int argc, char *argv[])
{
    int characters, lines, words, state;
    char c;
    state = characters = lines = words = 0;
    while((c = getchar()) != '0') {
        characters++;
        if(c == '\n') {
            lines++;
            state = 0;
            continue;
        } else if(c == ' ') {
            state = 0;
            continue;
        } else if(c == '\t') {
            state = 0;
            continue;
        } else {
            if(state == 0) {
                state = BEGIN;
                words++;
            }
            continue;
        }
    }
    printf("%d characters. %d words. %d lines.\n", characters, words, lines);
}
以下是用linux C寫的實現(xiàn)ls部分功能的函數(shù)。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//用lstat函數(shù)獲取文件的類型
int get_mode(char *file)
{
    struct stat buf;
    char *ptr;
    if(lstat(file, &buf) pw_name);
    strcpy(name2, getpwuid(buf.st_gid)->pw_name);
    printf(" %s %s", name1, name2);
    return 0;
}
//瀏覽文件夾的內(nèi)容
struct dirent **scan_dir(int *n)
{
    struct dirent **namelist;
   
    (*n) = scandir(".", &namelist, 0, alphasort);
    if ((*n) d_name[0] != '.') {
            printf("%-s  ", namelist->d_name);
        }
        
        free(namelist);
    }
    printf("\n");
    free(namelist);
    return 0;
}
//實現(xiàn)ls -l的命令
int l_ls()
{
    struct dirent **namelist;
    int n, i;
    long all_size = 0, size;
    char ch[40] = {0};
    namelist = scan_dir(&n);
   
    //得到文件總的大小
    for(i = 0; i d_name[0] != '.') {
            size = get_size(namelist->d_name);
            all_size += size;
        }
    }
    //打印信息
    printf("總用量:%ld\n", all_size);
    for(i = 0; i d_name[0] != '.') {
            get_mode(namelist->d_name);
            get_rwx(namelist->d_name);
            get_nlink(namelist->d_name);
            
            get_name(namelist->d_name);
            printf(" %5ld", get_size(namelist->d_name));
            get_time(namelist->d_name, ch);
            printf(" %s", ch);
            printf(" %s", namelist->d_name);
            printf("\n");
        }
        free(namelist);
    }
    free(namelist);
    return 0;
}
//實現(xiàn)ls -al的命令
int al_ls()
{
    struct dirent **namelist;
    int n, i;
    long all_size = 0, size;
    char ch[40] = {0};
   
    namelist = scan_dir(&n);
   
    //得到文件總的大小
    for(i = 0; i d_name);
        all_size += size;
    }
    printf("總用量:%ld\n", all_size);
    for(i = 0; i d_name);
        get_rwx(namelist->d_name);
        get_nlink(namelist->d_name);   
        get_name(namelist->d_name);
        all_size += size;
        printf(" %5ld", get_size(namelist->d_name));
        get_time(namelist->d_name, ch);
        printf(" %s", ch);
        printf(" %s", namelist->d_name);
        printf("\n");
        free(namelist);
    }
    free(namelist);
    return 0;
}
//實現(xiàn)ls -a的命令
int a_ls()
{
    struct dirent **namelist;
    int i, n;
    namelist = scan_dir(&n);
    for(i = 0; i d_name);
        free(namelist);
    }
    printf("\n");
    free(namelist);
    return 0;
}
//實現(xiàn)ls -A的命令
int A_ls()
{
    struct dirent **namelist;
    int i, n, test1, test2;
    namelist = scan_dir(&n);
    for(i = 0; i d_name) == 0;
        test2 = strcmp("..", namelist->d_name) == 0;
        if(test1 || test2){
            free(namelist);
            continue;
        }
        printf("%-s  ", namelist->d_name);
        free(namelist);
    }
    printf("\n");
    free(namelist);
    return 0;
}
//瀏覽指定目錄下的文件
int dir_ls(char *name)
{
    struct dirent **namelist;
    char file[20] = {0};
    int i, n;
    strcpy(file, "./");
    strcat(file, name);
    printf("%s\n", name);
    n = scandir(file, &namelist, 0, alphasort);
    for(i = 0; i d_name[0] == '.') {
            free(namelist);
            continue;
        }
        printf("%-s  ", namelist->d_name);
        free(namelist);
    }
    printf("\n");
    free(namelist);
    return 0;
}
//實現(xiàn)多個輸入文件時的輸出name_ls
int name_ls(int argc, char **argv)
{
    int i;
    struct stat buf;
    for(i = 1; i  2) {
        name_ls(argc, argv);
    } else {
        perror("Input error!\n");
    }
    return 0;
}
               
               
               

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u3/104213/showart_2066203.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP