Alien-Libjio

 view release on metacpan or  search on metacpan

libjio/libjio/ansi.c  view on Meta::CPAN


/*
 * ANSI C API wrappers
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>

#include "libjio.h"
#include "common.h"
#include "trans.h"


/*
 * To avoid completely useless code duplication, this functions rely on the
 * UNIX wrappers from unix.c.
 *
 * The API is not nice, and I wouldn't recommend it for any serious I/O.
 *
 * Note that it is still experimental and it hasn't received too much testing
 * (that's why it's not even documented), so use it at your own risk.
 */


/* fopen() wrapper */
struct jfs *jfopen(const char *path, const char *mode)
{
	int flags;
	int pos_at_the_beginning;
	struct jfs *fs;

	if (strlen(mode) < 1)
		return NULL;

	if (mode[0] == 'r') {
		pos_at_the_beginning = 1;
		if (strlen(mode) > 1 && strchr(mode, '+'))
			flags = O_RDWR;
		else
			flags = O_RDONLY;
	} else if (mode[0] == 'a') {
		if (strlen(mode) > 1 && strchr(mode, '+'))
			pos_at_the_beginning = 1;
		else
			pos_at_the_beginning = 0;
		flags = O_RDWR | O_CREAT | O_APPEND;
	} else if (mode[0] == 'w') {
		pos_at_the_beginning = 1;
		flags = O_RDWR | O_CREAT | O_TRUNC;
	} else {
		return NULL;
	}

	fs = jopen(path, flags, 0666, 0);
	if (fs == NULL)
		return NULL;

	if (pos_at_the_beginning)
		lseek(fs->fd, 0, SEEK_SET);
	else
		lseek(fs->fd, 0, SEEK_END);

	return fs;
}

/* fclose() wrapper */
int jfclose(struct jfs *stream)
{
	int rv;
	rv = jclose(stream);
	free(stream);

	if (rv == 0)
		return 0;
	else
		return EOF;



( run in 1.554 second using v1.01-cache-2.11-cpan-64ef6c95b5d )