zoukankan      html  css  js  c++  java
  • C 语言实现 Linux touch 命令

    参考教程:C 语言实现 Linux touch 命令

    其他参考:

    C语言动态变量和静态变量的区别

    linux系统下的 C 编程,头文件相关;哪里找-> sys/types.h, sys/stat.h

    parameter和argument的区别

     

    命令行选项解析函数(C语言):getopt()和getopt_long()  (学习getopt()函数即可) 

    getopt函数 (配合学习,适合最后看,学习完本教程后,再看基本就能理解)

     

    代码:(相比教程中少用了头文件 和 修改函数返回值为ok)

    #include <unistd.h>
    #include <getopt.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    
    #include <stdbool.h>
    #include <stdlib.h>
    
    #define CH_ATIME 1
    #define CH_MTIME 2
    
    #define MODE_RW_UGO (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
    
    // acess time and modify time
    static int change_times;
    // -c
    static bool no_create;
    // new a/m time
    static struct timespec newtime[2];
    
    static bool mytouch(const char *file)
    {
        bool ok;
        int fd = -1;
        if ( no_create != 1)
            fd = open(file, O_CREAT | O_WRONLY, MODE_RW_UGO);
    
        if (change_times != (CH_ATIME | CH_MTIME))
        {
            if (change_times == CH_ATIME)
                newtime[1].tv_nsec = UTIME_OMIT;
            else
                newtime[0].tv_nsec = UTIME_OMIT;
        }
        ok = (utimensat(AT_FDCWD, file,  newtime, 0) == 0);
        return ok;
    }
    
    int main(int argc, char **argv)
    {
        int c;
        bool ok = true;
    
        no_create = false;
        change_times = 0;
    
        while ((c = getopt(argc, argv, "acm")) != -1)
        {
            switch (c)
            {
            case 'a':
                change_times |=  CH_ATIME;
                break;
    
            case 'c':
                no_create = true;
                break;
    
            case 'm':
                change_times |= CH_MTIME;
                break;
    
            default:
                printf("fault option!");
            }
        }    
    
        if (change_times == 0)
            change_times = CH_ATIME | CH_MTIME;
        
        newtime[0].tv_nsec = UTIME_NOW;
        newtime[1].tv_nsec = UTIME_NOW;
    
        if (argc == optind)
            printf("missing file operand
    ");
    
        for (; optind < argc; ++optind)
            ok &= mytouch(argv[optind]);
    
        exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);    
    }

    使用教程中的例子测试:

    ./mytouch shiyanlou
    stat shiyanlou

     

    附上之前的学习代码:

    #include <stdio.h>
    #include <unistd.h>
    #include <getopt.h>
    
    int main(int argc, char **argv){
        int opt;
        char *optstring = "a::b:c::d";
        while((opt = getopt(argc, argv, optstring)) != -1){
            printf("opt = %c		", opt);
            printf("optarg = %s		", optarg);
            printf("optind = %d		", optind);
            printf("argv[optind] = %s
    ", argv[optind]);
        }
    }
  • 相关阅读:
    FOJ2250 不可能弹幕结界
    寻找最大值
    Haybale Guessing
    MG loves string
    Curious Cupid
    Anton and Permutation
    TLE
    Jzzhu and Numbers
    Divisible Group Sums
    The merchant
  • 原文地址:https://www.cnblogs.com/exciting/p/11018394.html
Copyright © 2011-2022 走看看