Mail Archives: djgpp-workers/1996/08/12/10:43:00
As both of dup and dup2 call setmode, I fixed setmode.c instead of it.
The following implementations include dummy functions. At least, use for
my port bash.
------ setmode.c -------------------------------------------------------
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <libc/stubs.h>
#include <sys/exceptn.h>
#include <dpmi.h>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <libc/dosio.h>
void (*__setmode_stdio_hook)(int fd, int mode); /* BSS to zero */
int
setmode(int handle, int mode)
{
__dpmi_regs regs;
int oldmode, newmode;
regs.x.ax = 0x4400;
regs.x.bx = handle;
regs.x.dx = 0; /* Clear upper e-word for return */
__dpmi_int(0x21, ®s);
if (regs.x.flags & 1)
{
errno = __doserr_to_errno(regs.x.ax);
return -1;
}
oldmode = newmode = regs.x.dx;
if (mode & O_BINARY)
newmode |= 0x20;
else
newmode &= ~0x20;
if (oldmode & 0x80) /* Only for character dev */
{
regs.x.ax = 0x4401;
regs.x.bx = handle;
regs.x.dx = newmode & 0xff; /* Force upper byte zero */
__dpmi_int(0x21, ®s);
if (regs.x.flags & 1)
{
errno = __doserr_to_errno(regs.x.ax);
return -1;
}
if (handle == 0)
__djgpp_set_ctrl_c(!(mode & O_BINARY));
}
#if 0
oldmode = __file_handle_modes[handle] & (O_BINARY|O_TEXT);
__file_handle_modes[handle] &= ~(O_BINARY|O_TEXT);
__file_handle_modes[handle] |= (mode & (O_BINARY|O_TEXT));
#else
oldmode = __file_handle_modes[handle];
newmode = (oldmode & ~(O_BINARY|O_TEXT)) | (mode & (O_BINARY|O_TEXT));
__file_handle_set (handle, newmode);
oldmode &= (O_BINARY|O_TEXT);
#endif
return oldmode;
}
------ fcntl.c ---------------------------------------------------------
#include <dpmi.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#include <libc/dosio.h>
#define DEFAULT_OPEN_MAX 20
#define OPEN_MAX 256
static int
is_used_fd (int fd)
{
__dpmi_regs regs;
regs.x.ax = 0x4400;
regs.x.bx = fd;
__dpmi_int (0x21, ®s);
if (regs.x.flags & 1)
return 0;
return 1;
}
static void
grow_up_table (void)
{
__file_handle_set (DEFAULT_OPEN_MAX-1, __file_handle_modes[DEFAULT_OPEN_MAX-1]);
}
int
fcntl (int fd, int cmd, ...)
{
va_list ap;
int tofd;
switch (cmd)
{
case F_DUPFD:
va_start (ap, cmd);
tofd = va_arg (ap, int);
va_end (ap);
while (tofd < OPEN_MAX)
{
if (! is_used_fd (tofd))
break;
tofd++;
}
if (tofd >= OPEN_MAX)
{
errno = EMFILE;
return -1;
}
if (tofd >= DEFAULT_OPEN_MAX)
grow_up_table ();
return dup2 (fd, tofd);
case F_GETFD:
case F_SETFD:
case F_GETFL:
case F_SETFL:
return 0;
case F_GETLK:
case F_SETLK:
case F_SETLKW:
return -1;
}
return -1;
}
------ sys/resource.h --------------------------------------------------
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#ifndef __dj_include_sys_resource_h_
#define __dj_include_sys_resource_h_
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __dj_ENFORCE_ANSI_FREESTANDING
#ifndef __STRICT_ANSI__
#ifndef _POSIX_SOURCE
#include <time.h>
#define RUSAGE_SELF 0 /* calling process */
#define RUSAGE_CHILDREN -1 /* terminated child processes */
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* integral max resident set size */
long ru_ixrss; /* integral shared text memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
#define RLIMIT_CPU 0 /* cpu time in milliseconds */
#define RLIMIT_FSIZE 1 /* maximum file size */
#define RLIMIT_DATA 2 /* data size */
#define RLIMIT_STACK 3 /* stack size */
#define RLIMIT_CORE 4 /* core file size */
#define RLIMIT_RSS 5 /* resident set size */
#define RLIMIT_MEMLOCK 6 /* locked-in-memory address space */
#define RLIMIT_NPROC 7 /* number of processes */
#define RLIMIT_NOFILE 8 /* number of open files */
#define RLIM_NLIMITS 9 /* number of resource limits */
#define RLIM_INFINITY ((long) ((1UL << 31) - 1UL))
struct rlimit {
long rlim_cur; /* current (soft) limit */
long rlim_max; /* maximum value for rlim_cur */
};
int getrusage(int _who, struct rusage *_rusage);
int getrlimit(int _rltype, struct rlimit *_rlimit);
int setrlimit(int _rltype, const struct rlimit *_rlimit);
#endif /* !_POSIX_SOURCE */
#endif /* !__STRICT_ANSI__ */
#endif /* !__dj_ENFORCE_ANSI_FREESTANDING */
#ifndef __dj_ENFORCE_FUNCTION_CALLS
#endif /* !__dj_ENFORCE_FUNCTION_CALLS */
#ifdef __cplusplus
}
#endif
#endif /* !__dj_include_sys_resource_h_ */
------ _limits.c -------------------------------------------------------
#include <unistd.h>
#include <sys/resource.h>
extern unsigned int _stklen;
struct rlimit _limits[RLIM_NLIMITS];
int _limits_initialized = 0;
void
_limits_initialize (void)
{
int i;
if (!_limits_initialized)
{
_limits_initialized = 1;
_limits[RLIMIT_CPU].rlim_max = RLIM_INFINITY;
_limits[RLIMIT_FSIZE].rlim_max = RLIM_INFINITY;
_limits[RLIMIT_DATA].rlim_max = RLIM_INFINITY;
_limits[RLIMIT_STACK].rlim_max = (long) _stklen;
_limits[RLIMIT_CORE].rlim_max = RLIM_INFINITY;
_limits[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
_limits[RLIMIT_MEMLOCK].rlim_max = RLIM_INFINITY;
_limits[RLIMIT_NPROC].rlim_max = RLIM_INFINITY;
_limits[RLIMIT_NOFILE].rlim_max = sysconf (_SC_OPEN_MAX);
for (i = 0; i < RLIM_NLIMITS; i++)
_limits[i].rlim_cur = _limits[i].rlim_max;
}
}
------ getrlimit.c -----------------------------------------------------
#include <errno.h>
#include <sys/resource.h>
extern struct rlimit _limits[];
extern void _limits_initialize (void);
extern int _limits_initialized;
int
getrlimit (int _rltype, struct rlimit *_rlimit)
{
if (_rltype < 0 && _rltype >= RLIM_NLIMITS)
{
errno = EINVAL;
return -1;
}
if (!_limits_initialized)
_limits_initialize ();
*_rlimit = _limits[_rltype];
return 0;
}
------ setrlimit.c -----------------------------------------------------
#include <errno.h>
#include <sys/resource.h>
extern struct rlimit _limits[];
extern void _limits_initialize (void);
extern int _limits_initialized;
int
setrlimit (int _rltype, const struct rlimit *_rlimit)
{
if (_rlimit->rlim_cur > _rlimit->rlim_max)
{
errno = EINVAL;
return -1;
}
if (!_limits_initialized)
_limits_initialize ();
switch (_rltype)
{
case RLIMIT_CPU:
case RLIMIT_FSIZE:
case RLIMIT_DATA:
case RLIMIT_STACK:
case RLIMIT_CORE:
case RLIMIT_RSS:
case RLIMIT_MEMLOCK:
case RLIMIT_NPROC:
case RLIMIT_NOFILE:
errno = EPERM;
return -1;
default:
errno = EINVAL;
return -1;
}
return 0;
}
------------------------------------------------------------------------
------
Daisuke Aoyama
jack AT st DOT rim DOT or DOT jp
- Raw text -