/* @(#)open-unlk.c 1.2 90/01/03 NFS Rev 2 Testsuite
* 1.3 Lachman ONC Test Suite source
* tests operation on open file which has been unlinked.
char wbuf
[TBUFSIZ
], rbuf
[TBUFSIZ
];
#define TMSG "This is a test message written to the unlinked file\n"
char *tname
= "nfstestXXXXXX";
if ((fd
= open(tname
, O_CREAT
|O_TRUNC
|O_RDWR
, 0777)) < 0) {
fprintf(stderr
, "can't create %s: ", tname
);
if ((fd
= creat(tname
, 0777)) < 0) {
fprintf(stderr
, "can't create %s: ", tname
);
if ((fd
= open(tname
, 2)) < 0) {
fprintf(stderr
, "can't reopen %s: ", tname
);
printf("nfsjunk files before unlink:\n ");
printf("%s open; unlink ret = %d\n", tname
, ret
);
printf("nfsjunk files after unlink:\n ");
if ((ret
= write(fd
, wbuf
, TBUFSIZ
)) != TBUFSIZ
) {
fprintf(stderr
, "write ret %d; expected %d\n", ret
, TBUFSIZ
);
if ((lret
= lseek(fd
, 0L, 0)) != 0L) {
fprintf(stderr
, "lseek ret %ld; expected 0\n", lret
);
if ((ret
= read(fd
, rbuf
, TBUFSIZ
)) != TBUFSIZ
) {
fprintf(stderr
, "read ret %d; expected %d\n", ret
, TBUFSIZ
);
if (strcmp(wbuf
, rbuf
) != NULL
) {
printf("read data not same as written data\n");
printf(" written: '%s'\n read: '%s'\n", wbuf
, rbuf
);
printf("data compare ok\n");
if (unlink(tname
) == 0) {
printf("Error: second unlink succeeded!??\n");
} else if (errno
!= ENOENT
) {
perror("unexpected error on second unlink");
perror("error on close");
printf("nfsjunk files after close:\n ");
if ((ret
= close(fd
)) == 0) {
fprintf(stderr
, "second close didn't return error!??\n");
printf("test completed successfully.\n");