os_oflags.c 2.22 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
unknown's avatar
unknown committed
4
 * Copyright (c) 1997-2002
unknown's avatar
unknown committed
5 6 7 8 9 10
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
unknown's avatar
unknown committed
11
static const char revid[] = "$Id: os_oflags.c,v 11.9 2002/01/11 15:53:00 bostic Exp $";
unknown's avatar
unknown committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#endif

#include "db_int.h"

/*
 * __db_oflags --
 *	Convert open(2) flags to DB flags.
 *
 * PUBLIC: u_int32_t __db_oflags __P((int));
 */
u_int32_t
__db_oflags(oflags)
	int oflags;
{
	u_int32_t dbflags;

	dbflags = 0;

	if (oflags & O_CREAT)
		dbflags |= DB_CREATE;

	if (oflags & O_TRUNC)
		dbflags |= DB_TRUNCATE;

	/*
	 * !!!
	 * Convert POSIX 1003.1 open(2) mode flags to DB flags.  This isn't
	 * an exact science as few POSIX implementations have a flag value
	 * for O_RDONLY, it's simply the lack of a write flag.
	 */
#ifndef	O_ACCMODE
#define	O_ACCMODE	(O_RDONLY | O_RDWR | O_WRONLY)
#endif
	switch (oflags & O_ACCMODE) {
	case O_RDWR:
	case O_WRONLY:
		break;
	default:
		dbflags |= DB_RDONLY;
		break;
	}
	return (dbflags);
}

/*
 * __db_omode --
 *	Convert a permission string to the correct open(2) flags.
 *
 * PUBLIC: int __db_omode __P((const char *));
 */
int
__db_omode(perm)
	const char *perm;
{
	int mode;

#ifdef DB_WIN32
unknown's avatar
unknown committed
76
#ifndef	S_IRUSR
unknown's avatar
unknown committed
77
#define	S_IRUSR	S_IREAD		/* R for owner */
unknown's avatar
unknown committed
78 79
#endif
#ifndef	S_IWUSR
unknown's avatar
unknown committed
80
#define	S_IWUSR	S_IWRITE	/* W for owner */
unknown's avatar
unknown committed
81 82
#endif
#ifndef	S_IRGRP
unknown's avatar
unknown committed
83
#define	S_IRGRP	0		/* R for group */
unknown's avatar
unknown committed
84 85
#endif
#ifndef	S_IWGRP
unknown's avatar
unknown committed
86
#define	S_IWGRP	0		/* W for group */
unknown's avatar
unknown committed
87 88
#endif
#ifndef	S_IROTH
unknown's avatar
unknown committed
89
#define	S_IROTH	0		/* R for other */
unknown's avatar
unknown committed
90 91
#endif
#ifndef	S_IWOTH
unknown's avatar
unknown committed
92
#define	S_IWOTH	0		/* W for other */
unknown's avatar
unknown committed
93
#endif
unknown's avatar
unknown committed
94
#else
unknown's avatar
unknown committed
95
#ifndef	S_IRUSR
unknown's avatar
unknown committed
96 97 98 99 100 101 102
#define	S_IRUSR	0000400		/* R for owner */
#define	S_IWUSR	0000200		/* W for owner */
#define	S_IRGRP	0000040		/* R for group */
#define	S_IWGRP	0000020		/* W for group */
#define	S_IROTH	0000004		/* R for other */
#define	S_IWOTH	0000002		/* W for other */
#endif
unknown's avatar
unknown committed
103
#endif /* DB_WIN32 */
unknown's avatar
unknown committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	mode = 0;
	if (perm[0] == 'r')
		mode |= S_IRUSR;
	if (perm[1] == 'w')
		mode |= S_IWUSR;
	if (perm[2] == 'r')
		mode |= S_IRGRP;
	if (perm[3] == 'w')
		mode |= S_IWGRP;
	if (perm[4] == 'r')
		mode |= S_IROTH;
	if (perm[5] == 'w')
		mode |= S_IWOTH;
	return (mode);
}