Prima

 view release on metacpan or  search on metacpan

api/file.c  view on Meta::CPAN

#include "apricot.h"
#include "guts.h"

#ifdef __cplusplus
extern "C" {
#endif

FILE*
prima_open_file( const char *text, Bool is_utf8, const char * mode)
{
	int fd, o, m;
	const char * omode = mode;
	char *cwd = NULL;
	FILE * ret;

	(void)cwd;

	switch ( *mode++ ) {
	case 'r':
		m = O_RDONLY;
		o = 0;
		break;
	case 'w':
		m = O_WRONLY;
		o = O_CREAT | O_TRUNC;
		break;
	case 'a':
		m = O_WRONLY;
		o = O_CREAT | O_APPEND;
		break;
	default:
		errno = EINVAL;
		return NULL;
	}

	if ( *mode == 'b' ) {
		mode++;
#ifdef O_BINARY
		o |= O_BINARY;
#endif
	}
	if ( *mode == '+' ) m = O_RDWR;

#if defined(PERL_IMPLICIT_SYS)
	if (
		(*text != '/') &&
		!(isalpha(text[0]) && text[1] == ':')
	) {
		cwd = apc_fs_getcwd();
		apc_fs_chdir(PerlEnv_get_childdir(), false);
	}
#endif

	if (( fd = apc_fs_open_file( text, is_utf8, m | o, 0666)) < 0) {
		free(cwd);
		return NULL;
	}

#if defined(PERL_IMPLICIT_SYS)
	if (cwd) {
		apc_fs_chdir(cwd, true);
		free(cwd);
	}
#endif

	if (!( ret = fdopen( fd, omode ))) {
		close(fd);
		return NULL;
	}

	if ( o & O_APPEND )
		fseek( ret, 0, SEEK_END);
	else
		fseek( ret, 0, SEEK_SET);

	return ret;
}

#ifdef __cplusplus
}
#endif

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.496 second using v1.00-cache-2.02-grep-82fe00e-cpan-dad7e4baca0 )