interim commit while overhauling pio

This commit is contained in:
2009-01-05 07:38:55 +00:00
parent afefb7b85a
commit b037bfcaf5
6 changed files with 443 additions and 76 deletions

View File

@ -247,10 +247,10 @@ qse_fio_off_t qse_fio_seek (
SEEK_END
};
#if !defined(_LP64) && defined(SYS__llseek)
#if defined(QSE_LLSEEK)
loff_t tmp;
if (syscall (SYS__llseek, fio->handle,
if (QSE_LLSEEK (fio->handle,
(unsigned long)(offset>>32),
(unsigned long)(offset&0xFFFFFFFFlu),
&tmp,
@ -261,12 +261,10 @@ qse_fio_off_t qse_fio_seek (
return (qse_fio_off_t)tmp;
#elif defined(SYS_lseek)
return syscall (SYS_lseek, fio->handle, offset, seek_map[origin]);
#elif !defined(_LP64) && defined(HAVE_LSEEK64)
return lseek64 (fio->handle, offset, seek_map[origin]);
#elif defined(HAVE_LSEEK64)
return QSE_LSEEK64 (fio->handle, offset, seek_map[origin]);
#else
return lseek (fio->handle, offset, seek_map[origin]);
return QSE_LSEEK (fio->handle, offset, seek_map[origin]);
#endif
#endif
@ -283,7 +281,7 @@ int qse_fio_truncate (qse_fio_t* fio, qse_fio_off_t size)
return 0;
#else
return QSE_TRUNCATE (fio->handle, size);
return QSE_FTRUNCATE (fio->handle, size);
#endif
}
@ -424,10 +422,6 @@ int qse_fio_chmod (qse_fio_t* fio, int mode)
if (!(mode & QSE_FIO_WUSR)) flags = FILE_ATTRIBUTE_READONLY;
return (SetFileAttributes (name, flags) == FALSE)? -1: 0;
#else
#if defined(SYS_fchmod)
return syscall (SYS_fchmod, fio->handle, mode);
#else
return fchmod (fio->handle, mode);
#endif
return QSE_FCHMOD (fio->handle, mode);
#endif
}

View File

@ -7,19 +7,19 @@
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#include <tchar.h>
#else
#include "syscall.h"
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#endif
#define HNDIN(pio) ((pio)->handle[QSE_PIO_HANDLE_IN])
#define HNDOUT(pio) ((pio)->handle[QSE_PIO_HANDLE_OUT])
#define HNDERR(pio) ((pio)->handle[QSE_PIO_HANDLE_ERR])
qse_pio_t* qse_pio_open (
qse_mmgr_t* mmgr, qse_size_t ext,
const qse_char_t* path, int flags, int mode)
const qse_char_t* path, int flags)
{
qse_pio_t* pio;
@ -36,7 +36,7 @@ qse_pio_t* qse_pio_open (
pio = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_pio_t) + ext);
if (pio == QSE_NULL) return QSE_NULL;
if (qse_pio_init (pio, mmgr, path, flags, mode) == QSE_NULL)
if (qse_pio_init (pio, mmgr, path, flags) == QSE_NULL)
{
QSE_MMGR_FREE (mmgr, pio);
return QSE_NULL;
@ -51,23 +51,148 @@ void qse_pio_close (qse_pio_t* pio)
QSE_MMGR_FREE (pio->mmgr, pio);
}
qse_pio_t* qse_pio_init (
qse_pio_t* pio, qse_mmgr_t* mmgr,
const qse_char_t* path, int flags, int mode)
const qse_char_t* path, int flags)
{
qse_pio_hnd_t handle;
qse_pio_pid_t pid;
qse_pio_hnd_t handle[6] = { -1, -1, -1, -1, -1, -1 };
int i, minidx = -1, maxidx = -1;
QSE_ASSERT (QSE_COUNTOF(pio->hanlde) == QSE_COUNTOF(handle));
QSE_MEMSET (pio, 0, QSE_SIZEOF(*pio));
pio->mmgr = mmgr;
#ifdef _WIN32
handle = -1;
/* TODO: XXXXXXXXXXXXXXXXX */
#else
handle = -1;
if (flags & QSE_PIO_WRITEIN)
{
if (QSE_PIPE(&handle[0]) == -1) goto oops;
minidx = 0; maxidx = 1;
}
if (flags & QSE_PIO_READOUT)
{
if (QSE_PIPE(&handle[2]) == -1) goto oops;
if (minidx == -1) minidx = 2;
maxidx = 3;
}
if (flags & QSE_PIO_READERR)
{
if (QSE_PIPE(&handle[4]) == -1) goto oops;
if (minidx == -1) minidx = 4;
maxidx = 5;
}
if (maxidx == -1) goto oops;
pid = QSE_FORK();
if (pid == -1) goto oops;
if (pid == 0)
{
/* child */
if (flags & QSE_PIO_WRITEIN)
{
/* child should read */
QSE_CLOSE (handle[1]);
if (QSE_DUP2 (handle[0], 0) == -1) goto child_oops;
QSE_CLOSE (handle[0]);
}
if (flags & QSE_PIO_READOUT)
{
/* child should write */
QSE_CLOSE (handle[2]);
if (QSE_DUP2 (handle[3], 1) == -1) goto child_oops;
if (flags & QSE_PIO_ERRTOOUT)
{
if (QSE_DUP2 (handle[3], 2) == -1) goto child_oops;
}
QSE_CLOSE (handle[3]);
}
if (flags & QSE_PIO_READERR)
{
/* child should write */
QSE_CLOSE (handle[4]);
if (QSE_DUP2 (handle[5], 2) == -1) goto child_oops;
if (flags & QSE_PIO_OUTTOERR)
{
if (QSE_DUP2 (handle[5], 1) == -1) goto child_oops;
}
QSE_CLOSE (handle[5]);
}
/* TODO: ... */
//execl ("/bin/sh", "sh", "-c", "cat", QSE_NULL);
execl ("/bin/sh", "sh", "-c", "cat -aksksks", QSE_NULL);
child_oops:
QSE_EXIT(127);
}
/* parent */
pio->child = pid;
if (flags & QSE_PIO_WRITEIN)
{
/*
* 012345
* rw----
* X
* WRITE => 1
*/
QSE_CLOSE (handle[0]); handle[0] = -1;
}
if (flags & QSE_PIO_READOUT)
{
/*
* 012345
* --rw--
* X
* READ => 2
*/
QSE_CLOSE (handle[3]); handle[3] = -1;
}
if (flags & QSE_PIO_READERR)
{
/*
* 012345
* ----rw
* X
* READ => 4
*/
QSE_CLOSE (handle[5]); handle[5] = -1;
}
#endif
pio->handle = handle;
HNDIN(pio) = handle[1];
HNDOUT(pio) = handle[2];
HNDERR(pio) = handle[4];
for (i = minidx; i < maxidx; i++)
{
qse_printf (QSE_T("%d ==> %d\n"), i, handle[i]);
}
return pio;
oops:
for (i = minidx; i < maxidx; i++) QSE_CLOSE (handle[i]);
return QSE_NULL;
}
void qse_pio_fini (qse_pio_t* pio)
@ -75,21 +200,60 @@ void qse_pio_fini (qse_pio_t* pio)
#ifdef _WIN32
CloseHandle (pio->handle);
#else
QSE_CLOSE (pio->handle);
int i, status;
if (HNDOUT(pio) != -1)
{
QSE_CLOSE (HNDOUT(pio));
HNDOUT(pio) = -1;
}
if (HNDIN(pio) != -1)
{
QSE_CLOSE (HNDIN(pio));
HNDIN(pio) = -1;
}
while (QSE_WAITPID (pio->child, &status, 0) == -1)
{
if (errno != EINTR) break;
}
#endif
}
qse_pio_hnd_t qse_pio_gethandle (qse_pio_t* pio)
int qse_pio_wait (qse_pio_t* pio)
{
return pio->handle;
int status;
#if 0
if (opt & QSE_PIO_NOWAIT)
{
opt |= WNOHANG;
}
while (1)
{
n = waitpid (pio->child, &status, opt);
if (n == 0) break;
/* TODO: .... */
}
output => return code...
output => termination cause...
#endif
return -1;
}
void qse_pio_sethandle (qse_pio_t* pio, qse_pio_hnd_t handle)
/*
qse_pio_hnd_t qse_pio_gethandle (qse_pio_t* pio, int readorwrite)
{
pio->handle = handle;
return handle[xxx];
}
*/
qse_ssize_t qse_pio_read (qse_pio_t* pio, void* buf, qse_size_t size)
qse_ssize_t qse_pio_read (
qse_pio_t* pio, void* buf, qse_size_t size, int flags)
{
#ifdef _WIN32
DWORD count;
@ -97,12 +261,25 @@ qse_ssize_t qse_pio_read (qse_pio_t* pio, void* buf, qse_size_t size)
if (ReadFile(pio->handle, buf, size, &count, QSE_NULL) == FALSE) return -1;
return (qse_ssize_t)count;
#else
qse_pio_hnd_t handle = -1;
if (flags == 0) flags = QSE_PIO_OUT;
if (flags & QSE_PIO_ERR) handle = HNDERR(pio);
if (flags & QSE_PIO_OUT) handle = HNDOUT(pio);
if (handle == -1)
{
/* the stream is already closed */
return (qse_ssize_t)-1;
}
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
return QSE_READ (pio->handle, buf, size);
return QSE_READ (handle, buf, size);
#endif
}
qse_ssize_t qse_pio_write (qse_pio_t* pio, const void* data, qse_size_t size)
qse_ssize_t qse_pio_write (
qse_pio_t* pio, const void* data, qse_size_t size, int flags)
{
#ifdef _WIN32
DWORD count;
@ -110,8 +287,37 @@ qse_ssize_t qse_pio_write (qse_pio_t* pio, const void* data, qse_size_t size)
if (WriteFile(pio->handle, data, size, &count, QSE_NULL) == FALSE) return -1;
return (qse_ssize_t)count;
#else
qse_pio_hnd_t handle;
if (flags == 0) flags = QSE_PIO_IN;
if (flags & QSE_PIO_IN) handle = HNDIN(pio);
if (handle == -1)
{
/* the stream is already closed */
return (qse_ssize_t)-1;
}
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
return QSE_WRITE (pio->handle, data, size);
return QSE_WRITE (handle, data, size);
#endif
}
void qse_pio_end (qse_pio_t* pio, int flags)
{
if ((flags & QSE_PIO_IN) && HNDIN(pio) != -1)
{
QSE_CLOSE (HNDIN(pio));
HNDIN(pio) = -1;
}
if ((flags & QSE_PIO_ERR) && HNDERR(pio) != -1)
{
QSE_CLOSE (HNDERR(pio));
HNDERR(pio) = -1;
}
if ((flags & QSE_PIO_OUT) && HNDOUT(pio) != -1)
{
QSE_CLOSE (HNDOUT(pio));
HNDOUT(pio) = -1;
}
}

View File

@ -1,9 +1,17 @@
#ifndef _QSE_LIB_CMN_SYSCALL_H_
#define _QSE_LIB_CMN_SYSCALL_H_
#if defined(HAVE_UNISTD_H)
/* This file defines unix/linux system calls */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#if defined(QSE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H)
#include <sys/syscall.h>
@ -33,6 +41,30 @@
#define QSE_WRITE(handle,buf,size) write(handle,buf,size)
#endif
#if !defined(_LP64) && defined(SYS__llseek)
#define QSE_LLSEEK(handle,hoffset,loffset,out,whence) \
syscall(SYS__llseek,handle,hoffset,loffset,out,whence)
#elif !defined(_LP64) && defined(HAVE__LLSEEK)
#define QSE_LLSEEK(handle,hoffset,loffset,out,whence) \
_llseek(handle,hoffset,loffset,out,whence)
#endif
#if defined(SYS_lseek65)
#define QSE_LSEEK64(handle,offset,whence) \
syscall(SYS_lseek64,handle,offset,whence)
#elif defined(HAVE_lseek64)
#define QSE_LSEEK64(handle,offset,whence) \
lseek64(handle,offset,whence)
#endif
#if defined(SYS_lseek)
#define QSE_LSEEK(handle,offset,whence) \
syscall(SYS_lseek,handle,offset,whence)
#else
#define QSE_LSEEK(handle,offset,whence) \
lseek(handle,offset,whence)
#endif
#if !defined(_LP64) && defined(SYS_ftruncate64)
#define QSE_FTRUNCATE(handle,size) syscall(SYS_ftruncate64,handle,size)
#elif defined(SYS_ftruncate)
@ -43,10 +75,71 @@
#define QSE_FTRUNCATE(handle,size) ftruncate(handle,size)
#endif
#if defined(SYS_fchmod)
#define QSE_FCHMOD(handle,mode) syscall(SYS_fchmod,handle,mode)
#else
#define QSE_FCHMOD(handle,mode) fchmod(handle,mode)
#endif
#ifdef SYS_dup2
#define QSE_DUP2(ofd,nfd) syscall(SYS_dup2,ofd,nfd)
#else
#define QSE_DUP2(ofd,nfd) dup2(ofd,nfd)
#endif
#ifdef SYS_pipe
#define QSE_PIPE(pfds) syscall(SYS_pipe,pfds)
#else
#define QSE_PIPE(pfds) pipe(pfds)
#endif
#ifdef SYS_exit
#define QSE_EXIT(code) syscall(SYS_exit,code)
#else
#define QSE_EXIT(code) _exit(code)
#endif
#ifdef SYS_fork
#define QSE_FORK() syscall(SYS_fork)
#else
#define QSE_FORK() fork()
#endif
#ifdef SYS_waitpid
#define QSE_WAITPID(pid,status,options) syscall(SYS_waitpid,pid,status,options)
#else
#define QSE_WAITPID(pid,status,options) waitpid(pid,status,options)
#endif
#ifdef SYS_getpid
#define QSE_GETPID() syscall(SYS_getpid)
#else
#define QSE_GETPID() getpid()
#endif
#ifdef SYS_getuid
#define QSE_GETUID() syscall(SYS_getuid)
#else
#define QSE_GETUID() getuid()
#endif
#ifdef SYS_geteuid
#define QSE_GETEUID() syscall(SYS_geteuid)
#else
#define QSE_GETEUID() geteuid()
#endif
#ifdef SYS_getgid
#define QSE_GETGID() syscall(SYS_getgid)
#else
#define QSE_GETGID() getgid()
#endif
#ifdef SYS_getegid
#define QSE_GETEGID() syscall(SYS_getegid)
#else
#define QSE_GETEGID() getegid()
#endif
#endif