/*
 * Parse flags for fopen & Co.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1995 by Ralf Baechle
 */
#include <sys/types.h>
#include <sys/file.h>
#include <arc/errno.h>
#include <stand/stdio.h>

/*
 * Return the flags for a given mode string.
 * Return 0 on error and set errno.
 *
 * Just ignores everything behind a '+' and b flags.
 */
int
__sflags(const char *mode, int *flagp)
{
	int flags;

	if(mode == NULL)
	{
		errno = EINVAL;
		return 0;
	}
	switch (mode[0])
	{
	case 'r':
		flags = O_RDONLY;
		break;
	case 'w':
		flags = O_WRONLY | O_CREAT | O_TRUNC;
		break;
	case 'a':
		flags = O_WRONLY | O_CREAT | O_APPEND;
		break;
	default:
		errno = EINVAL;
		return 0;
	}

	if (mode[1] == '+' ||
	    (mode[1] == 'b' && mode[2] == '+'))
	{
		flags = (flags & ~O_ACCMODE) | O_RDWR;
	}

	*flagp = flags;
	return 1;
}
