1139969Simp/*-
274465Srwatson * Copyright (c) 2001 Chris D. Faulhaber
374465Srwatson * All rights reserved.
474465Srwatson *
574465Srwatson * Redistribution and use in source and binary forms, with or without
674465Srwatson * modification, are permitted provided that the following conditions
774465Srwatson * are met:
874465Srwatson * 1. Redistributions of source code must retain the above copyright
974465Srwatson *    notice, this list of conditions and the following disclaimer.
1074465Srwatson * 2. Redistributions in binary form must reproduce the above copyright
1174465Srwatson *    notice, this list of conditions and the following disclaimer in the
1274465Srwatson *    documentation and/or other materials provided with the distribution.
1374465Srwatson *
1474465Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1574465Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1674465Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17204819Sjoel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18204819Sjoel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19204819Sjoel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20204819Sjoel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21204819Sjoel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22204819Sjoel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23204819Sjoel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24204819Sjoel * SUCH DAMAGE.
2574465Srwatson */
2674465Srwatson
2799110Sobrien#include <sys/cdefs.h>
2899110Sobrien__FBSDID("$FreeBSD$");
2999110Sobrien
3074465Srwatson#include <sys/types.h>
3174465Srwatson#include <sys/acl.h>
3274465Srwatson
3374465Srwatson#include <err.h>
3474465Srwatson#include <stdio.h>
3574465Srwatson#include <string.h>
3674465Srwatson
3774465Srwatson#include "setfacl.h"
3874465Srwatson
3987254Sjedgar/*
4087254Sjedgar * read acl text from a file and return the corresponding acl
4187254Sjedgar */
4274465Srwatsonacl_t
4374465Srwatsonget_acl_from_file(const char *filename)
4474465Srwatson{
4574465Srwatson	FILE *file;
4674465Srwatson	char buf[BUFSIZ];
4774465Srwatson
4887254Sjedgar	if (filename == NULL)
4987254Sjedgar		err(1, "(null) filename in get_acl_from_file()");
5074465Srwatson
5174465Srwatson	bzero(&buf, sizeof(buf));
5274465Srwatson
5387254Sjedgar	if (strcmp(filename, "-") == 0) {
5487254Sjedgar		if (have_stdin != 0)
5587254Sjedgar			err(1, "cannot specify more than one stdin");
5674465Srwatson		file = stdin;
5774465Srwatson		have_stdin = 1;
5874465Srwatson	} else {
5974465Srwatson		file = fopen(filename, "r");
6087254Sjedgar		if (file == NULL)
6187254Sjedgar			err(1, "fopen() %s failed", filename);
6274465Srwatson	}
6374465Srwatson
6476881Skris	fread(buf, sizeof(buf), (size_t)1, file);
6587254Sjedgar	if (ferror(file) != 0) {
6674465Srwatson		fclose(file);
6787254Sjedgar		err(1, "error reading from %s", filename);
6887254Sjedgar	} else if (feof(file) == 0) {
6974465Srwatson		fclose(file);
7087254Sjedgar		errx(1, "line too long in %s", filename);
7174465Srwatson	}
7274465Srwatson
7374465Srwatson	fclose(file);
7474465Srwatson
7587254Sjedgar	return (acl_from_text(buf));
7674465Srwatson}
77