thr_open.c revision 56698
113546Sjulian/*
235509Sjb * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
313546Sjulian * All rights reserved.
413546Sjulian *
513546Sjulian * Redistribution and use in source and binary forms, with or without
613546Sjulian * modification, are permitted provided that the following conditions
713546Sjulian * are met:
813546Sjulian * 1. Redistributions of source code must retain the above copyright
913546Sjulian *    notice, this list of conditions and the following disclaimer.
1013546Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1113546Sjulian *    notice, this list of conditions and the following disclaimer in the
1213546Sjulian *    documentation and/or other materials provided with the distribution.
1313546Sjulian * 3. All advertising materials mentioning features or use of this software
1413546Sjulian *    must display the following acknowledgement:
1513546Sjulian *	This product includes software developed by John Birrell.
1613546Sjulian * 4. Neither the name of the author nor the names of any co-contributors
1713546Sjulian *    may be used to endorse or promote products derived from this software
1813546Sjulian *    without specific prior written permission.
1913546Sjulian *
2013546Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
2113546Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2213546Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2349439Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2413546Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2513546Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2613546Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2713546Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2813546Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2913546Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3013546Sjulian * SUCH DAMAGE.
3113546Sjulian *
3250476Speter * $FreeBSD: head/lib/libkse/thread/thr_open.c 56698 2000-01-27 23:07:25Z jasone $
3325402Sjb *
3413546Sjulian */
3513546Sjulian#include <stdarg.h>
3617706Sjulian#include <unistd.h>
3713546Sjulian#include <fcntl.h>
3813546Sjulian#include <dirent.h>
3925402Sjb#include <errno.h>
4013546Sjulian#ifdef _THREAD_SAFE
4113546Sjulian#include <pthread.h>
4213546Sjulian#include "pthread_private.h"
4313546Sjulian
4413546Sjulianint
4556698Sjasone_open(const char *path, int flags,...)
4613546Sjulian{
4753812Salfred	int	fd;
4853812Salfred	int	mode = 0;
4953812Salfred	va_list	ap;
5013546Sjulian
5113546Sjulian	/* Check if the file is being created: */
5213546Sjulian	if (flags & O_CREAT) {
5313546Sjulian		/* Get the creation mode: */
5413546Sjulian		va_start(ap, flags);
5513546Sjulian		mode = va_arg(ap, int);
5613546Sjulian		va_end(ap);
5713546Sjulian	}
5817706Sjulian	/* Open the file: */
5917706Sjulian	if ((fd = _thread_sys_open(path, flags, mode)) < 0) {
6013546Sjulian	}
6113546Sjulian	/* Initialise the file descriptor table entry: */
6213546Sjulian	else if (_thread_fd_table_init(fd) != 0) {
6313546Sjulian		/* Quietly close the file: */
6413546Sjulian		_thread_sys_close(fd);
6513546Sjulian
6613546Sjulian		/* Reset the file descriptor: */
6713546Sjulian		fd = -1;
6813546Sjulian	}
6913546Sjulian
7013546Sjulian	/* Return the file descriptor or -1 on error: */
7113546Sjulian	return (fd);
7213546Sjulian}
7355838Sjasone
7456698Sjasoneint
7556698Sjasoneopen(const char *path, int flags,...)
7656698Sjasone{
7756698Sjasone	int	ret;
7856698Sjasone	int	mode = 0;
7956698Sjasone	va_list	ap;
8056698Sjasone
8156698Sjasone	_thread_enter_cancellation_point();
8256698Sjasone
8356698Sjasone	/* Check if the file is being created: */
8456698Sjasone	if (flags & O_CREAT) {
8556698Sjasone		/* Get the creation mode: */
8656698Sjasone		va_start(ap, flags);
8756698Sjasone		mode = va_arg(ap, int);
8856698Sjasone		va_end(ap);
8956698Sjasone	}
9056698Sjasone
9156698Sjasone	ret = _open(path, flags, mode);
9256698Sjasone	_thread_leave_cancellation_point();
9356698Sjasone
9456698Sjasone	return ret;
9556698Sjasone}
9613546Sjulian#endif
97