Annotation of researchv9/cmd/touch.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <sys/types.h>
        !             3: #include <sys/stat.h>
        !             4: 
        !             5: long lseek();
        !             6: 
        !             7: main(argc,argv)
        !             8: int argc;
        !             9: char *argv[];
        !            10: {
        !            11:        int i;
        !            12:        static int force = 1;
        !            13:        int status = 0;
        !            14: 
        !            15:        if (argc < 2) {
        !            16:                fprintf(stderr, "usage: touch files\n");
        !            17:                exit(1);
        !            18:        }
        !            19:        for(i = 1; i < argc; ++i)
        !            20:                if (strcmp(argv[i], "-c") == 0)
        !            21:                        force = 0;
        !            22:                else
        !            23:                        status += touch(force, argv[i]);
        !            24:        exit(status);
        !            25: }
        !            26: 
        !            27: touch(force, name)
        !            28: int force;
        !            29: char *name;
        !            30: {
        !            31:        struct stat stbuff;
        !            32:        char junk[1];
        !            33:        int fd;
        !            34: 
        !            35:        stbuff.st_size = 0;
        !            36:        if (stat(name,&stbuff) < 0 && force == 0) {
        !            37:                fprintf(stderr, "touch: %s: cannot stat\n", name);
        !            38:                return (1);
        !            39:        }
        !            40:        if (stbuff.st_size == 0) {
        !            41:                if ((fd = creat(name, 0666)) < 0) {
        !            42:                        fprintf(stderr, "touch: %s: cannot create\n", name);
        !            43:                        return (1);
        !            44:                }
        !            45:                close(fd);
        !            46:                return (0);
        !            47:        }
        !            48:        if ((fd = open(name, 2)) < 0) {
        !            49:                fprintf(stderr, "touch: %s: cannot open\n", name);
        !            50:                return (1);
        !            51:        }
        !            52:        if(read(fd, junk, 1) < 1) {
        !            53:                fprintf(stderr, "touch: %s: read error\n", name);
        !            54:                close(fd);
        !            55:                return (1);
        !            56:        }
        !            57:        lseek(fd, 0L, 0);
        !            58:        if(write(fd, junk, 1) < 1 ) {
        !            59:                fprintf(stderr, "touch: %s: write error\n", name);
        !            60:                close(fd);
        !            61:                return (1);
        !            62:        }
        !            63:        close(fd);
        !            64:        return (0);
        !            65: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.