summaryrefslogtreecommitdiffstats
path: root/special/excltest.c
blob: f74eaf9ebf79b6e03e020a33ea6d0047e623e6c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 *	@(#)excltest.c	1.2 98/10/26 Connectathon Testsuite
 *	1.3 Lachman ONC Test Suite source
 *
 * test exclusive create
 */

#if defined (DOS) || defined (WIN32)
#define DOSorWIN32
#include "../tests.h"
#endif

#ifndef DOSorWIN32
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#if defined(SVR3) || defined(SVR4)
#include <fcntl.h>
#else
#include <sys/file.h>
#endif
#endif /* DOSorWIN32 */

int
main(argc, argv)
	int argc;
	char *argv[];
{
	char *testfile = "exctest.file";
	int count;
	int res, i;

	if (argc > 2) {
		fprintf(stderr, "usage: %s [count]\n", argv[0]);
		exit(1);
	}
	if (argc == 2) 
		count = atoi(argv[1]);
	else
		count = 2;

	unlink(testfile);
	for (i = 0; i < count; i++) {
		res = open(testfile, O_CREAT | O_EXCL, 0777);
		if (i == 0) {
			if (res < 0) {
				perror(testfile);
				exit(1);
			}
		} else {
			if (res >= 0) {
				fprintf(stderr, "exclusive create succeeded\n");
				exit(1);
			} else if (errno != EEXIST) {
				perror(testfile);
				exit(1);
			}
		}
	}

	exit(0);
}