Annotation of 43BSDReno/usr.bin/touch/touch.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1988 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: char copyright[] =
        !            22: "@(#) Copyright (c) 1988 Regents of the University of California.\n\
        !            23:  All rights reserved.\n";
        !            24: #endif /* not lint */
        !            25: 
        !            26: #ifndef lint
        !            27: static char sccsid[] = "@(#)touch.c    4.8 (Berkeley) 6/1/90";
        !            28: #endif /* not lint */
        !            29: 
        !            30: /*
        !            31:  * Attempt to set the modify date of a file to the current date.  If the
        !            32:  * file exists, read and write its first character.  If the file doesn't
        !            33:  * exist, create it, unless -c option prevents it.  If the file is read-only,
        !            34:  * -f forces chmod'ing and touch'ing.
        !            35:  */
        !            36: #include <sys/types.h>
        !            37: #include <sys/file.h>
        !            38: #include <sys/stat.h>
        !            39: #include <stdio.h>
        !            40: 
        !            41: static int     dontcreate;     /* set if -c option */
        !            42: static int     force;          /* set if -f option */
        !            43: 
        !            44: main(argc, argv)
        !            45:        int argc;
        !            46:        char **argv;
        !            47: {
        !            48:        extern int optind;
        !            49:        int ch, retval;
        !            50: 
        !            51:        dontcreate = force = retval = 0;
        !            52:        while ((ch = getopt(argc, argv, "cf")) != EOF)
        !            53:                switch((char)ch) {
        !            54:                case 'c':
        !            55:                        dontcreate = 1;
        !            56:                        break;
        !            57:                case 'f':
        !            58:                        force = 1;
        !            59:                        break;
        !            60:                case '?':
        !            61:                default:
        !            62:                        usage();
        !            63:                }
        !            64:        if (!*(argv += optind))
        !            65:                usage();
        !            66:        do {
        !            67:                retval |= touch(*argv);
        !            68:        } while (*++argv);
        !            69:        exit(retval);
        !            70: }
        !            71: 
        !            72: touch(filename)
        !            73:        char *filename;
        !            74: {
        !            75:        struct stat statbuffer;
        !            76: 
        !            77:        if (stat(filename, &statbuffer) == -1) {
        !            78:                if (!dontcreate)
        !            79:                        return(readwrite(filename, 0L));
        !            80:                fprintf(stderr, "touch: %s: does not exist\n", filename);
        !            81:                return(1);
        !            82:        }
        !            83:        if ((statbuffer.st_mode & S_IFMT) != S_IFREG) {
        !            84:                fprintf(stderr, "touch: %s: can only touch regular files\n",
        !            85:                    filename);
        !            86:                return(1);
        !            87:        }
        !            88:        if (!access(filename, R_OK | W_OK))
        !            89:                return(readwrite(filename,statbuffer.st_size));
        !            90:        if (force) {
        !            91:                int retval;
        !            92: 
        !            93:                if (chmod(filename, 0666)) {
        !            94:                        fprintf(stderr, "touch: %s: couldn't chmod: ",
        !            95:                            filename);
        !            96:                        perror((char *)NULL);
        !            97:                        return(1);
        !            98:                }
        !            99:                retval = readwrite(filename, statbuffer.st_size);
        !           100:                if (chmod(filename, statbuffer.st_mode)) {
        !           101:                        fprintf(stderr, "touch: %s: couldn't chmod back: ",
        !           102:                            filename);
        !           103:                        perror((char *)NULL);
        !           104:                        return(1);
        !           105:                }
        !           106:                return(retval);
        !           107:        }
        !           108:        fprintf(stderr, "touch: %s: cannot touch\n", filename);
        !           109:        return(1);
        !           110: }
        !           111: 
        !           112: readwrite(filename, size)
        !           113:        char *filename;
        !           114:        off_t size;
        !           115: {
        !           116:        int filedescriptor;
        !           117:        char first;
        !           118:        off_t lseek();
        !           119: 
        !           120:        if (size) {
        !           121:                filedescriptor = open(filename, O_RDWR, 0);
        !           122:                if (filedescriptor == -1) {
        !           123: error:                 fprintf(stderr, "touch: %s: ", filename);
        !           124:                        perror((char *)NULL);
        !           125:                        return(1);
        !           126:                }
        !           127:                if (read(filedescriptor, &first, 1) != 1)
        !           128:                        goto error;
        !           129:                if (lseek(filedescriptor, 0L, 0) == -1)
        !           130:                        goto error;
        !           131:                if (write(filedescriptor, &first, 1) != 1)
        !           132:                        goto error;
        !           133:        } else {
        !           134:                filedescriptor = creat(filename, 0666);
        !           135:                if (filedescriptor == -1)
        !           136:                        goto error;
        !           137:        }
        !           138:        if (close(filedescriptor) == -1)
        !           139:                goto error;
        !           140:        return(0);
        !           141: }
        !           142: 
        !           143: usage()
        !           144: {
        !           145:        fprintf(stderr, "usage: touch [-cf] file ...\n");
        !           146:        exit(1);
        !           147: }

unix.superglobalmegacorp.com

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