Annotation of 43BSDReno/sys/nfs/TEST/unix-tests/special/open-unlk.c, revision 1.1

1.1     ! root        1: /*     @(#)open-unlk.c 1.2 90/01/03 NFS Rev 2 Testsuite
        !             2:  *     1.3 Lachman ONC Test Suite source
        !             3:  *
        !             4:  *  tests operation on open file which has been unlinked.
        !             5:  *  steps taken:
        !             6:  *     1.  create file
        !             7:  *     2.  open for read/write
        !             8:  *     3.  unlink file
        !             9:  *     4.  write data
        !            10:  *     5.  rewind
        !            11:  *     6.  read data back
        !            12:  */
        !            13: 
        !            14: #include <stdio.h>
        !            15: #ifdef SVR3
        !            16: #include <fcntl.h>
        !            17: #else
        !            18: #include <sys/file.h>
        !            19: #endif
        !            20: #include <errno.h>
        !            21: extern errno;
        !            22: #define TBUFSIZ 100
        !            23: char wbuf[TBUFSIZ], rbuf[TBUFSIZ];
        !            24: #define TMSG "This is a test message written to the unlinked file\n"
        !            25: 
        !            26: main(argc, argv)
        !            27: int argc;
        !            28: char *argv[];
        !            29: {
        !            30:        int fd, ret;
        !            31:        char *tname = "nfstestXXXXXX";
        !            32:        int errcount = 0;
        !            33:        long lret;
        !            34:        extern long lseek();
        !            35: 
        !            36:        setbuf(stdout, NULL);
        !            37:        mktemp(tname);
        !            38: #ifdef O_RDWR
        !            39:        if ((fd = open(tname, O_CREAT|O_TRUNC|O_RDWR, 0777)) < 0) {
        !            40:                fprintf(stderr, "can't create %s: ", tname);
        !            41:                xxit("open");
        !            42:        }
        !            43: #else
        !            44:        if ((fd = creat(tname, 0777)) < 0) {
        !            45:                fprintf(stderr, "can't create %s: ", tname);
        !            46:                xxit("creat");
        !            47:        }
        !            48:        close(fd);
        !            49:        if ((fd = open(tname, 2)) < 0) {
        !            50:                fprintf(stderr, "can't reopen %s: ", tname);
        !            51:                unlink(tname);
        !            52:                xxit("open");
        !            53:        }
        !            54: #endif /* O_RDWR */
        !            55:        printf("nfsjunk files before unlink:\n  ");
        !            56:        system("ls -al .nfs*");
        !            57:        ret = unlink(tname);
        !            58:        printf("%s open; unlink ret = %d\n", tname, ret);
        !            59:        if (ret)
        !            60:                xxit(" unlink");
        !            61:        printf("nfsjunk files after unlink:\n  ");
        !            62:        system("ls -al .nfs*");
        !            63:        strcpy(wbuf, TMSG);
        !            64:        if ((ret = write(fd, wbuf, TBUFSIZ)) != TBUFSIZ) {
        !            65:                fprintf(stderr, "write ret %d; expected %d\n", ret, TBUFSIZ);
        !            66:                if (ret < 0)
        !            67:                        perror(" write");
        !            68:                exit(1);
        !            69:        }
        !            70:        if ((lret = lseek(fd, 0L, 0)) != 0L) {
        !            71:                fprintf(stderr, "lseek ret %ld; expected 0\n", lret);
        !            72:                if (lret < 0)
        !            73:                        perror(" lseek");
        !            74:                exit(1);
        !            75:        }
        !            76:        if ((ret = read(fd, rbuf, TBUFSIZ)) != TBUFSIZ) {
        !            77:                fprintf(stderr, "read ret %d; expected %d\n", ret, TBUFSIZ);
        !            78:                if (ret < 0)
        !            79:                        perror(" read");
        !            80:                exit(1);
        !            81:        }
        !            82:        if (strcmp(wbuf, rbuf) != NULL) {
        !            83:                errcount++;
        !            84:                printf("read data not same as written data\n");
        !            85:                printf(" written: '%s'\n read:    '%s'\n", wbuf, rbuf);
        !            86:        } else {
        !            87:                printf("data compare ok\n");
        !            88:        }
        !            89: 
        !            90:        if (unlink(tname) == 0) {
        !            91:                errcount++;
        !            92:                printf("Error: second unlink succeeded!??\n");
        !            93:        } else if (errno != ENOENT) {
        !            94:                errcount++;
        !            95:                perror("unexpected error on second unlink");
        !            96:        }
        !            97: 
        !            98:        if (ret = close(fd)) {
        !            99:                errcount++;
        !           100:                perror("error on close");
        !           101:        }
        !           102: 
        !           103:        printf("nfsjunk files after close:\n  ");
        !           104:        system("ls -al .nfs*");
        !           105: 
        !           106:        if ((ret = close(fd)) == 0) {
        !           107:                errcount++;
        !           108:                fprintf(stderr, "second close didn't return error!??\n");
        !           109:        }
        !           110: 
        !           111:        if (errcount == 0)
        !           112:                printf("test completed successfully.\n");
        !           113:        exit(errcount);
        !           114: }
        !           115: 
        !           116: xxit(s)
        !           117: char *s;
        !           118: {
        !           119:        perror(s);
        !           120:        exit(1);
        !           121: }

unix.superglobalmegacorp.com

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