xref: /netbsd-src/bin/setfacl/file.c (revision 9aa2a9c323eb12a08584c70d6ea91d316703d3fe)
1 /*	$NetBSD: file.c,v 1.1 2020/05/16 18:31:45 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Chris D. Faulhaber
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #if 0
31 __FBSDID("$FreeBSD: head/bin/setfacl/file.c 333065 2018-04-27 15:25:24Z emaste $");
32 #else
33 __RCSID("$NetBSD: file.c,v 1.1 2020/05/16 18:31:45 christos Exp $");
34 #endif
35 
36 #include <sys/types.h>
37 #include <sys/acl.h>
38 
39 #include <err.h>
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include "setfacl.h"
44 
45 /*
46  * read acl text from a file and return the corresponding acl
47  */
48 acl_t
get_acl_from_file(const char * filename)49 get_acl_from_file(const char *filename)
50 {
51 	FILE *file;
52 	size_t len;
53 	char buf[BUFSIZ+1];
54 
55 	if (filename == NULL)
56 		err(1, "(null) filename in get_acl_from_file()");
57 
58 	if (strcmp(filename, "-") == 0) {
59 		if (have_stdin)
60 			err(1, "cannot specify more than one stdin");
61 		file = stdin;
62 		have_stdin = true;
63 	} else {
64 		file = fopen(filename, "r");
65 		if (file == NULL)
66 			err(1, "fopen() %s failed", filename);
67 	}
68 
69 	len = fread(buf, (size_t)1, sizeof(buf) - 1, file);
70 	buf[len] = '\0';
71 	if (ferror(file) != 0) {
72 		fclose(file);
73 		err(1, "error reading from %s", filename);
74 	} else if (feof(file) == 0) {
75 		fclose(file);
76 		errx(1, "line too long in %s", filename);
77 	}
78 
79 	fclose(file);
80 
81 	return (acl_from_text(buf));
82 }
83