1 /*	$NetBSD: fsaccess_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2012  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: fsaccess_test.c,v 1.13 2007/06/19 23:46:59 tbox Exp  */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 
30 #include <sys/types.h>		/* Non-portable. */
31 #include <sys/stat.h>		/* Non-portable. */
32 
33 #include <isc/fsaccess.h>
34 #include <isc/result.h>
35 
36 #define PATH "/tmp/fsaccess"
37 
38 int
39 main(void) {
40 	isc_fsaccess_t access;
41 	isc_result_t result;
42 	FILE *fp;
43 	int n;
44 
45 	n = remove(PATH);
46 	if (n != 0 && errno != ENOENT) {
47 		fprintf(stderr, "unable to remove(%s)\n", PATH);
48 		exit(1);
49 	}
50 	fp = fopen(PATH, "w");
51 	if (fp == NULL) {
52 		fprintf(stderr, "unable to fopen(%s)\n", PATH);
53 		exit(1);
54 	}
55 	n = chmod(PATH, 0);
56 	if (n != 0) {
57 		fprintf(stderr, "unable chmod(%s, 0)\n", PATH);
58 		exit(1);
59 	}
60 
61 	access = 0;
62 
63 	isc_fsaccess_add(ISC_FSACCESS_OWNER | ISC_FSACCESS_GROUP,
64 			 ISC_FSACCESS_READ | ISC_FSACCESS_WRITE,
65 			 &access);
66 
67 	printf("fsaccess=%d\n", access);
68 
69 	isc_fsaccess_add(ISC_FSACCESS_OTHER, ISC_FSACCESS_READ, &access);
70 
71 	printf("fsaccess=%d\n", access);
72 
73 	result = isc_fsaccess_set(PATH, access);
74 	if (result != ISC_R_SUCCESS)
75 		fprintf(stderr, "result = %s\n", isc_result_totext(result));
76 	(void)fclose(fp);
77 
78 	return (0);
79 }
80