Run libcc.ansi through clang-format.

This commit is contained in:
David Given 2018-06-21 22:33:47 +02:00
parent 60b7d8de6e
commit 93f39e4bbf
157 changed files with 3836 additions and 3121 deletions

View file

@ -3,11 +3,12 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void __bad_assertion(const char *mess) { void __bad_assertion(const char* mess)
{
fputs(mess, stderr); fputs(mess, stderr);
abort(); abort();

View file

@ -1,5 +1,6 @@
#include <ctype.h> #include <ctype.h>
int tolower(int c) { int tolower(int c)
return isupper(c) ? c - 'A' + 'a' : c ; {
return isupper(c) ? c - 'A' + 'a' : c;
} }

View file

@ -1,5 +1,6 @@
#include <ctype.h> #include <ctype.h>
int toupper(int c) { int toupper(int c)
return islower(c) ? c - 'a' + 'A' : c ; {
return islower(c) ? c - 'a' + 'A' : c;
} }

View file

@ -4,44 +4,44 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <errno.h> #include <errno.h>
const char *_sys_errlist[] = { const char* _sys_errlist[] = {
"Error 0", "Error 0",
"Not owner", "Not owner",
"No such file or directory", "No such file or directory",
"No such process", "No such process",
"Interrupted system call", "Interrupted system call",
"I/O error", "I/O error",
"No such device or address", "No such device or address",
"Arg list too long", "Arg list too long",
"Exec format error", "Exec format error",
"Bad file number", "Bad file number",
"No children", "No children",
"No more processes", "No more processes",
"Not enough core", "Not enough core",
"Permission denied", "Permission denied",
"Bad address", "Bad address",
"Block device required", "Block device required",
"Mount device busy", "Mount device busy",
"File exists", "File exists",
"Cross-device link", "Cross-device link",
"No such device", "No such device",
"Not a directory", "Not a directory",
"Is a directory", "Is a directory",
"Invalid argument", "Invalid argument",
"File table overflow", "File table overflow",
"Too many open files", "Too many open files",
"Not a typewriter", "Not a typewriter",
"Text file busy", "Text file busy",
"File too large", "File too large",
"No space left on device", "No space left on device",
"Illegal seek", "Illegal seek",
"Read-only file system", "Read-only file system",
"Too many links", "Too many links",
"Broken pipe", "Broken pipe",
"Math argument", "Math argument",
"Result too large" "Result too large"
}; };
const int _sys_nerr = sizeof(_sys_errlist) / sizeof(_sys_errlist[0]); const int _sys_nerr = sizeof(_sys_errlist) / sizeof(_sys_errlist[0]);

View file

@ -3,15 +3,15 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <limits.h> #include <limits.h>
#include <locale.h> #include <locale.h>
extern struct lconv _lc; extern struct lconv _lc;
struct lconv * struct lconv*
localeconv(void) localeconv(void)
{ {
register struct lconv *lcp = &_lc; register struct lconv* lcp = &_lc;
lcp->decimal_point = "."; lcp->decimal_point = ".";
lcp->thousands_sep = ""; lcp->thousands_sep = "";

View file

@ -3,26 +3,28 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
struct lconv _lc; struct lconv _lc;
char * char* setlocale(int category, const char* locale)
setlocale(int category, const char *locale)
{ {
if (!locale) return "C"; if (!locale)
if (*locale && strcmp(locale, "C")) return (char *)NULL; return "C";
if (*locale && strcmp(locale, "C"))
return (char*)NULL;
switch(category) { switch (category)
case LC_ALL: {
case LC_CTYPE: case LC_ALL:
case LC_COLLATE: case LC_CTYPE:
case LC_TIME: case LC_COLLATE:
case LC_NUMERIC: case LC_TIME:
case LC_MONETARY: case LC_NUMERIC:
return *locale ? (char *)locale : "C"; case LC_MONETARY:
default: return *locale ? (char*)locale : "C";
return (char *)NULL; default:
return (char*)NULL;
} }
} }

View file

@ -4,20 +4,20 @@
void* calloc(size_t nmemb, size_t size) void* calloc(size_t nmemb, size_t size)
{ {
size_t bytes = nmemb * size; size_t bytes = nmemb * size;
void* ptr; void* ptr;
/* Test for overflow. /* Test for overflow.
* See http://stackoverflow.com/questions/1815367/multiplication-of-large-numbers-how-to-catch-overflow * See http://stackoverflow.com/questions/1815367/multiplication-of-large-numbers-how-to-catch-overflow
*/ */
if ((nmemb == 0) || (size == 0) || (nmemb > (SIZE_MAX / size))) if ((nmemb == 0) || (size == 0) || (nmemb > (SIZE_MAX / size)))
return NULL; return NULL;
ptr = malloc(bytes); ptr = malloc(bytes);
if (!ptr) if (!ptr)
return NULL; return NULL;
memset(ptr, 0, bytes); memset(ptr, 0, bytes);
return ptr; return ptr;
} }

View file

@ -3,7 +3,7 @@
#include <unistd.h> #include <unistd.h>
#include "malloc.h" #include "malloc.h"
block_t __mem_root = {&__mem_root, 0}; block_t __mem_root = { &__mem_root, 0 };
block_t* __mem_freelist = &__mem_root; block_t* __mem_freelist = &__mem_root;
/* Pulls more memory from the system. */ /* Pulls more memory from the system. */
@ -57,7 +57,7 @@ void* malloc(size_t size)
* The size field is already set. */ * The size field is already set. */
prev->next = p->next; prev->next = p->next;
__mem_freelist = prev; __mem_freelist = prev;
return (void*) (p+1); return (void*)(p + 1);
} }
else if (p->size > nblocks) else if (p->size > nblocks)
{ {
@ -67,7 +67,7 @@ void* malloc(size_t size)
p += p->size; /* p now points at our new block */ p += p->size; /* p now points at our new block */
p->size = nblocks; p->size = nblocks;
__mem_freelist = prev; __mem_freelist = prev;
return (void*) (p+1); return (void*)(p + 1);
} }
if (p == __mem_freelist) if (p == __mem_freelist)
@ -84,7 +84,7 @@ void* malloc(size_t size)
} }
} }
void free(void *ptr) void free(void* ptr)
{ {
block_t* h = BLOCKOF(ptr); block_t* h = BLOCKOF(ptr);
block_t* p; block_t* p;
@ -148,4 +148,3 @@ void free(void *ptr)
/* ...and update the ring pointer. */ /* ...and update the ring pointer. */
__mem_freelist = p; __mem_freelist = p;
} }

View file

@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
#include "malloc.h" #include "malloc.h"
void* realloc(void *ptr, size_t size) void* realloc(void* ptr, size_t size)
{ {
block_t* h; block_t* h;
size_t nblocks; size_t nblocks;

View file

@ -6,66 +6,76 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
static double static double
asin_acos(double x, int cosfl) asin_acos(double x, int cosfl)
{ {
int negative = x < 0; int negative = x < 0;
int i; int i;
double g; double g;
static double p[] = { static double p[] = {
-0.27368494524164255994e+2, -0.27368494524164255994e+2,
0.57208227877891731407e+2, 0.57208227877891731407e+2,
-0.39688862997540877339e+2, -0.39688862997540877339e+2,
0.10152522233806463645e+2, 0.10152522233806463645e+2,
-0.69674573447350646411e+0 -0.69674573447350646411e+0
}; };
static double q[] = { static double q[] = {
-0.16421096714498560795e+3, -0.16421096714498560795e+3,
0.41714430248260412556e+3, 0.41714430248260412556e+3,
-0.38186303361750149284e+3, -0.38186303361750149284e+3,
0.15095270841030604719e+3, 0.15095270841030604719e+3,
-0.23823859153670238830e+2, -0.23823859153670238830e+2,
1.0 1.0
}; };
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (negative) { if (negative)
{
x = -x; x = -x;
} }
if (x > 0.5) { if (x > 0.5)
{
i = 1; i = 1;
if (x > 1) { if (x > 1)
{
errno = EDOM; errno = EDOM;
return 0; return 0;
} }
g = 0.5 - 0.5 * x; g = 0.5 - 0.5 * x;
x = - sqrt(g); x = -sqrt(g);
x += x; x += x;
} }
else { else
{
/* ??? avoid underflow ??? */ /* ??? avoid underflow ??? */
i = 0; i = 0;
g = x * x; g = x * x;
} }
x += x * g * POLYNOM4(g, p) / POLYNOM5(g, q); x += x * g * POLYNOM4(g, p) / POLYNOM5(g, q);
if (cosfl) { if (cosfl)
if (! negative) x = -x; {
if (!negative)
x = -x;
} }
if ((cosfl == 0) == (i == 1)) { if ((cosfl == 0) == (i == 1))
{
x = (x + M_PI_4) + M_PI_4; x = (x + M_PI_4) + M_PI_4;
} }
else if (cosfl && negative && i == 1) { else if (cosfl && negative && i == 1)
{
x = (x + M_PI_2) + M_PI_2; x = (x + M_PI_2) + M_PI_2;
} }
if (! cosfl && negative) x = -x; if (!cosfl && negative)
x = -x;
return x; return x;
} }

View file

@ -6,10 +6,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
atan(double x) atan(double x)
@ -26,47 +26,52 @@ atan(double x)
-0.83758299368150059274e+0 -0.83758299368150059274e+0
}; };
static double q[] = { static double q[] = {
0.41066306682575781263e+2, 0.41066306682575781263e+2,
0.86157349597130242515e+2, 0.86157349597130242515e+2,
0.59578436142597344465e+2, 0.59578436142597344465e+2,
0.15024001160028576121e+2, 0.15024001160028576121e+2,
1.0 1.0
}; };
static double a[] = { static double a[] = {
0.0, 0.0,
0.52359877559829887307710723554658381, /* pi/6 */ 0.52359877559829887307710723554658381, /* pi/6 */
M_PI_2, M_PI_2,
1.04719755119659774615421446109316763 /* pi/3 */ 1.04719755119659774615421446109316763 /* pi/3 */
}; };
int neg = x < 0; int neg = x < 0;
int n; int n;
double g; double g;
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (neg) { if (neg)
{
x = -x; x = -x;
} }
if (x > 1.0) { if (x > 1.0)
x = 1.0/x; {
x = 1.0 / x;
n = 2; n = 2;
} }
else n = 0; else
n = 0;
if (x > 0.26794919243112270647) { /* 2-sqtr(3) */ if (x > 0.26794919243112270647)
{ /* 2-sqtr(3) */
n = n + 1; n = n + 1;
x = (((0.73205080756887729353*x-0.5)-0.5)+x)/ x = (((0.73205080756887729353 * x - 0.5) - 0.5) + x) / (1.73205080756887729353 + x);
(1.73205080756887729353+x);
} }
/* ??? avoid underflow ??? */ /* ??? avoid underflow ??? */
g = x * x; g = x * x;
x += x * g * POLYNOM3(g, p) / POLYNOM4(g, q); x += x * g * POLYNOM3(g, p) / POLYNOM4(g, q);
if (n > 1) x = -x; if (n > 1)
x = -x;
x += a[n]; x += a[n];
return neg ? -x : x; return neg ? -x : x;
} }

View file

@ -6,35 +6,41 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
atan2(double y, double x) atan2(double y, double x)
{ {
double absx, absy, val; double absx, absy, val;
if (x == 0 && y == 0) { if (x == 0 && y == 0)
{
errno = EDOM; errno = EDOM;
return 0; return 0;
} }
absy = y < 0 ? -y : y; absy = y < 0 ? -y : y;
absx = x < 0 ? -x : x; absx = x < 0 ? -x : x;
if (absy - absx == absy) { if (absy - absx == absy)
{
/* x negligible compared to y */ /* x negligible compared to y */
return y < 0 ? -M_PI_2 : M_PI_2; return y < 0 ? -M_PI_2 : M_PI_2;
} }
if (absx - absy == absx) { if (absx - absy == absx)
{
/* y negligible compared to x */ /* y negligible compared to x */
val = 0.0; val = 0.0;
} }
else val = atan(y/x); else
if (x > 0) { val = atan(y / x);
if (x > 0)
{
/* first or fourth quadrant; already correct */ /* first or fourth quadrant; already correct */
return val; return val;
} }
if (y < 0) { if (y < 0)
{
/* third quadrant */ /* third quadrant */
return val - M_PI; return val - M_PI;
} }

View file

@ -6,14 +6,14 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
double double
ceil(double x) ceil(double x)
{ {
double val; double val;
return modf(x, &val) > 0 ? val + 1.0 : val ; return modf(x, &val) > 0 ? val + 1.0 : val;
/* this also works if modf always returns a positive /* this also works if modf always returns a positive
fractional part fractional part
*/ */

View file

@ -6,11 +6,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
exp(double x) exp(double x)
@ -32,41 +31,46 @@ exp(double x)
0.63121894374398503557e-3, 0.63121894374398503557e-3,
0.75104028399870046114e-6 0.75104028399870046114e-6
}; };
double xn, g; double xn, g;
int n; int n;
int negative = x < 0; int negative = x < 0;
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (x < M_LN_MIN_D) { if (x < M_LN_MIN_D)
{
errno = ERANGE; errno = ERANGE;
return 0.0; return 0.0;
} }
if (x > M_LN_MAX_D) { if (x > M_LN_MAX_D)
{
errno = ERANGE; errno = ERANGE;
return HUGE_VAL; return HUGE_VAL;
} }
if (negative) x = -x; if (negative)
x = -x;
/* ??? avoid underflow ??? */ /* ??? avoid underflow ??? */
n = x * M_LOG2E + 0.5; /* 1/ln(2) = log2(e), 0.5 added for rounding */ n = x * M_LOG2E + 0.5; /* 1/ln(2) = log2(e), 0.5 added for rounding */
xn = n; xn = n;
{ {
double x1 = (long) x; double x1 = (long)x;
double x2 = x - x1; double x2 = x - x1;
g = ((x1-xn*0.693359375)+x2) - xn*(-2.1219444005469058277e-4); g = ((x1 - xn * 0.693359375) + x2) - xn * (-2.1219444005469058277e-4);
} }
if (negative) { if (negative)
{
g = -g; g = -g;
n = -n; n = -n;
} }
xn = g * g; xn = g * g;
x = g * POLYNOM2(xn, p); x = g * POLYNOM2(xn, p);
n += 1; n += 1;
return (ldexp(0.5 + x/(POLYNOM3(xn, q) - x), n)); return (ldexp(0.5 + x / (POLYNOM3(xn, q) - x), n));
} }

View file

@ -9,5 +9,5 @@
double double
fabs(double x) fabs(double x)
{ {
return x < 0 ? -x : x; return x < 0 ? -x : x;
} }

View file

@ -6,14 +6,14 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
double double
floor(double x) floor(double x)
{ {
double val; double val;
return modf(x, &val) < 0 ? val - 1.0 : val ; return modf(x, &val) < 0 ? val - 1.0 : val;
/* this also works if modf always returns a positive /* this also works if modf always returns a positive
fractional part fractional part
*/ */

View file

@ -6,29 +6,34 @@
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
double double(fmod)(double x, double y)
(fmod)(double x, double y) { /* compute fmod(x, y) */
{ /* compute fmod(x, y) */
double t; double t;
int n, neg; int n, neg;
int ychar, xchar; int ychar, xchar;
if (y == 0.0) { if (y == 0.0)
{
errno = EDOM; errno = EDOM;
return 0.0; return 0.0;
} }
/* fmod(finite, finite) */ /* fmod(finite, finite) */
if (y < 0.0) y = -y; if (y < 0.0)
if (x < 0.0) x = -x, neg = 1; y = -y;
else neg = 0; if (x < 0.0)
x = -x, neg = 1;
else
neg = 0;
t = frexp(y, &ychar); t = frexp(y, &ychar);
/* substract |y| until |x| < |y| */ /* substract |y| until |x| < |y| */
t = frexp(x, &xchar); t = frexp(x, &xchar);
for (n = xchar - ychar; 0 <= n; --n) { for (n = xchar - ychar; 0 <= n; --n)
/* try to substract |y|*2^n */ {
t = ldexp(y, n); /* try to substract |y|*2^n */
if (t <= x) x -= t; t = ldexp(y, n);
if (t <= x)
x -= t;
} }
return (neg ? -x : x); return (neg ? -x : x);
} }

View file

@ -10,25 +10,30 @@
/* $Id$ */ /* $Id$ */
double double
hypot(double x,double y) hypot(double x, double y)
{ {
/* Computes sqrt(x*x+y*y), avoiding overflow */ /* Computes sqrt(x*x+y*y), avoiding overflow */
if (x < 0) x = -x; if (x < 0)
if (y < 0) y = -y; x = -x;
if (x > y) { if (y < 0)
y = -y;
if (x > y)
{
double t = y; double t = y;
y = x; y = x;
x = t; x = t;
} }
/* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.0) */ /* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.0) */
if (y == 0.0) return 0.0; if (y == 0.0)
return 0.0;
x /= y; x /= y;
return y*sqrt(x*x+1.0); return y * sqrt(x * x + 1.0);
} }
struct complex { struct complex
double r,i; {
double r, i;
}; };
double double

View file

@ -4,8 +4,8 @@ __IsNan(double d)
#else #else
float f = d; float f = d;
if ((*((long *) &f) & 0x7f800000) == 0x7f800000 && if ((*((long*)&f) & 0x7f800000) == 0x7f800000 && (*((long*)&f) & 0x007fffff) != 0)
(*((long *) &f) & 0x007fffff) != 0) return 1; return 1;
#endif #endif
return 0; return 0;
} }

View file

@ -4,9 +4,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
double double
ldexp(double fl, int exp) ldexp(double fl, int exp)
@ -14,42 +14,52 @@ ldexp(double fl, int exp)
int sign = 1; int sign = 1;
int currexp; int currexp;
if (__IsNan(fl)) { if (__IsNan(fl))
{
errno = EDOM; errno = EDOM;
return fl; return fl;
} }
if (fl == 0.0) return 0.0; if (fl == 0.0)
if (fl<0) { return 0.0;
if (fl < 0)
{
fl = -fl; fl = -fl;
sign = -1; sign = -1;
} }
if (fl > DBL_MAX) { /* for infinity */ if (fl > DBL_MAX)
{ /* for infinity */
errno = ERANGE; errno = ERANGE;
return sign * fl; return sign * fl;
} }
fl = frexp(fl,&currexp); fl = frexp(fl, &currexp);
exp += currexp; exp += currexp;
if (exp > 0) { if (exp > 0)
if (exp > DBL_MAX_EXP) { {
if (exp > DBL_MAX_EXP)
{
errno = ERANGE; errno = ERANGE;
return sign * HUGE_VAL; return sign * HUGE_VAL;
} }
while (exp>30) { while (exp > 30)
fl *= (double) (1L << 30); {
fl *= (double)(1L << 30);
exp -= 30; exp -= 30;
} }
fl *= (double) (1L << exp); fl *= (double)(1L << exp);
} }
else { else
{
/* number need not be normalized */ /* number need not be normalized */
if (exp < DBL_MIN_EXP - DBL_MANT_DIG) { if (exp < DBL_MIN_EXP - DBL_MANT_DIG)
{
return 0.0; return 0.0;
} }
while (exp<-30) { while (exp < -30)
fl /= (double) (1L << 30); {
fl /= (double)(1L << 30);
exp += 30; exp += 30;
} }
fl /= (double) (1L << -exp); fl /= (double)(1L << -exp);
} }
return sign * fl; return sign * fl;
} }

View file

@ -6,10 +6,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
log(double x) log(double x)
@ -20,47 +20,55 @@ log(double x)
*/ */
static double a[] = { static double a[] = {
-0.64124943423745581147e2, -0.64124943423745581147e2,
0.16383943563021534222e2, 0.16383943563021534222e2,
-0.78956112887491257267e0 -0.78956112887491257267e0
}; };
static double b[] = { static double b[] = {
-0.76949932108494879777e3, -0.76949932108494879777e3,
0.31203222091924532844e3, 0.31203222091924532844e3,
-0.35667977739034646171e2, -0.35667977739034646171e2,
1.0 1.0
}; };
double znum, zden, z, w; double znum, zden, z, w;
int exponent; int exponent;
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (x < 0) { if (x < 0)
{
errno = EDOM; errno = EDOM;
return -HUGE_VAL; return -HUGE_VAL;
} }
else if (x == 0) { else if (x == 0)
{
errno = ERANGE; errno = ERANGE;
return -HUGE_VAL; return -HUGE_VAL;
} }
if (x <= DBL_MAX) { if (x <= DBL_MAX)
{
} }
else return x; /* for infinity and Nan */ else
return x; /* for infinity and Nan */
x = frexp(x, &exponent); x = frexp(x, &exponent);
if (x > M_1_SQRT2) { if (x > M_1_SQRT2)
{
znum = (x - 0.5) - 0.5; znum = (x - 0.5) - 0.5;
zden = x * 0.5 + 0.5; zden = x * 0.5 + 0.5;
} }
else { else
{
znum = x - 0.5; znum = x - 0.5;
zden = znum * 0.5 + 0.5; zden = znum * 0.5 + 0.5;
exponent--; exponent--;
} }
z = znum/zden; w = z * z; z = znum / zden;
x = z + z * w * (POLYNOM2(w,a)/POLYNOM3(w,b)); w = z * z;
x = z + z * w * (POLYNOM2(w, a) / POLYNOM3(w, b));
z = exponent; z = exponent;
x += z * (-2.121944400546905827679e-4); x += z * (-2.121944400546905827679e-4);
return x + z * 0.693359375; return x + z * 0.693359375;

View file

@ -6,22 +6,25 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
log10(double x) log10(double x)
{ {
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (x < 0) { if (x < 0)
{
errno = EDOM; errno = EDOM;
return -HUGE_VAL; return -HUGE_VAL;
} }
else if (x == 0) { else if (x == 0)
{
errno = ERANGE; errno = ERANGE;
return -HUGE_VAL; return -HUGE_VAL;
} }

View file

@ -6,94 +6,116 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
double double
pow(double x, double y) pow(double x, double y)
{ {
double y_intpart, y_fractpart, fp; double y_intpart, y_fractpart, fp;
int negexp, negx; int negexp, negx;
int ex, newexp; int ex, newexp;
unsigned long yi; unsigned long yi;
if (x == 1.0) return x; if (x == 1.0)
return x;
if (x == 0 && y <= 0) { if (x == 0 && y <= 0)
{
errno = EDOM; errno = EDOM;
return 0; return 0;
} }
if (y == 0) return 1.0; if (y == 0)
return 1.0;
if (y < 0) { if (y < 0)
{
y = -y; y = -y;
negexp = 1; negexp = 1;
} }
else negexp = 0; else
negexp = 0;
y_fractpart = modf(y, &y_intpart); y_fractpart = modf(y, &y_intpart);
if (y_fractpart != 0) { if (y_fractpart != 0)
if (x < 0) { {
if (x < 0)
{
errno = EDOM; errno = EDOM;
return 0; return 0;
} }
} }
negx = 0; negx = 0;
if (x < 0) { if (x < 0)
{
x = -x; x = -x;
negx = 1; negx = 1;
} }
if (y_intpart > ULONG_MAX) { if (y_intpart > ULONG_MAX)
if (negx && modf(y_intpart/2.0, &y_fractpart) == 0) { {
if (negx && modf(y_intpart / 2.0, &y_fractpart) == 0)
{
negx = 0; negx = 0;
} }
x = log(x); x = log(x);
/* Beware of overflow in the multiplication */ /* Beware of overflow in the multiplication */
if (x > 1.0 && y > DBL_MAX/x) { if (x > 1.0 && y > DBL_MAX / x)
{
errno = ERANGE; errno = ERANGE;
return HUGE_VAL; return HUGE_VAL;
} }
if (negexp) y = -y; if (negexp)
y = -y;
if (negx) return -exp(x*y); if (negx)
return -exp(x * y);
return exp(x * y); return exp(x * y);
} }
if (y_fractpart != 0) { if (y_fractpart != 0)
{
fp = exp(y_fractpart * log(x)); fp = exp(y_fractpart * log(x));
} }
else fp = 1.0; else
fp = 1.0;
yi = y_intpart; yi = y_intpart;
if (! (yi & 1)) negx = 0; if (!(yi & 1))
negx = 0;
x = frexp(x, &ex); x = frexp(x, &ex);
newexp = 0; newexp = 0;
for (;;) { for (;;)
if (yi & 1) { {
if (yi & 1)
{
fp *= x; fp *= x;
newexp += ex; newexp += ex;
} }
yi >>= 1; yi >>= 1;
if (yi == 0) break; if (yi == 0)
break;
x *= x; x *= x;
ex <<= 1; ex <<= 1;
if (x < 0.5) { if (x < 0.5)
{
x += x; x += x;
ex -= 1; ex -= 1;
} }
} }
if (negexp) { if (negexp)
fp = 1.0/fp; {
fp = 1.0 / fp;
newexp = -newexp; newexp = -newexp;
} }
if (negx) { if (negx)
{
return -ldexp(fp, newexp); return -ldexp(fp, newexp);
} }
return ldexp(fp, newexp); return ldexp(fp, newexp);

View file

@ -6,10 +6,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
static double static double
sinus(double x, int cos_flag) sinus(double x, int cos_flag)
@ -21,39 +21,44 @@ sinus(double x, int cos_flag)
static double r[] = { static double r[] = {
-0.16666666666666665052e+0, -0.16666666666666665052e+0,
0.83333333333331650314e-2, 0.83333333333331650314e-2,
-0.19841269841201840457e-3, -0.19841269841201840457e-3,
0.27557319210152756119e-5, 0.27557319210152756119e-5,
-0.25052106798274584544e-7, -0.25052106798274584544e-7,
0.16058936490371589114e-9, 0.16058936490371589114e-9,
-0.76429178068910467734e-12, -0.76429178068910467734e-12,
0.27204790957888846175e-14 0.27204790957888846175e-14
}; };
double y; double y;
int neg = 1; int neg = 1;
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (x < 0) { if (x < 0)
{
x = -x; x = -x;
neg = -1; neg = -1;
} }
if (cos_flag) { if (cos_flag)
{
neg = 1; neg = 1;
y = M_PI_2 + x; y = M_PI_2 + x;
} }
else y = x; else
y = x;
/* ??? avoid loss of significance, if y is too large, error ??? */ /* ??? avoid loss of significance, if y is too large, error ??? */
y = y * M_1_PI + 0.5; y = y * M_1_PI + 0.5;
if (y >= DBL_MAX/M_PI) return 0.0; if (y >= DBL_MAX / M_PI)
return 0.0;
/* Use extended precision to calculate reduced argument. /* Use extended precision to calculate reduced argument.
Here we used 12 bits of the mantissa for a1. Here we used 12 bits of the mantissa for a1.
Also split x in integer part x1 and fraction part x2. Also split x in integer part x1 and fraction part x2.
*/ */
@ -63,8 +68,10 @@ sinus(double x, int cos_flag)
double x1, x2; double x1, x2;
modf(y, &y); modf(y, &y);
if (modf(0.5*y, &x1)) neg = -neg; if (modf(0.5 * y, &x1))
if (cos_flag) y -= 0.5; neg = -neg;
if (cos_flag)
y -= 0.5;
x2 = modf(x, &x1); x2 = modf(x, &x1);
x = x1 - y * A1; x = x1 - y * A1;
x += x2; x += x2;
@ -73,7 +80,8 @@ sinus(double x, int cos_flag)
#undef A2 #undef A2
} }
if (x < 0) { if (x < 0)
{
neg = -neg; neg = -neg;
x = -x; x = -x;
} }
@ -82,7 +90,7 @@ sinus(double x, int cos_flag)
y = x * x; y = x * x;
x += x * y * POLYNOM7(y, r); x += x * y * POLYNOM7(y, r);
return neg==-1 ? -x : x; return neg == -1 ? -x : x;
} }
double double
@ -94,6 +102,7 @@ sin(double x)
double double
cos(double x) cos(double x)
{ {
if (x < 0) x = -x; if (x < 0)
x = -x;
return sinus(x, 1); return sinus(x, 1);
} }

View file

@ -6,10 +6,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
static double static double
sinh_cosh(double x, int cosh_flag) sinh_cosh(double x, int cosh_flag)
@ -27,42 +27,48 @@ sinh_cosh(double x, int cosh_flag)
}; };
static double q[] = { static double q[] = {
-0.21108770058106271242e+7, -0.21108770058106271242e+7,
0.36162723109421836460e+5, 0.36162723109421836460e+5,
-0.27773523119650701167e+3, -0.27773523119650701167e+3,
1.0 1.0
}; };
int negative = x < 0; int negative = x < 0;
double y = negative ? -x : x; double y = negative ? -x : x;
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (! cosh_flag && y <= 1.0) { if (!cosh_flag && y <= 1.0)
{
/* ??? check for underflow ??? */ /* ??? check for underflow ??? */
y = y * y; y = y * y;
return x + x * y * POLYNOM3(y, p)/POLYNOM3(y,q); return x + x * y * POLYNOM3(y, p) / POLYNOM3(y, q);
} }
if (y >= M_LN_MAX_D) { if (y >= M_LN_MAX_D)
/* exp(y) would cause overflow */ {
#define LNV 0.69316101074218750000e+0 /* exp(y) would cause overflow */
#define VD2M1 0.52820835025874852469e-4 #define LNV 0.69316101074218750000e+0
double w = y - LNV; #define VD2M1 0.52820835025874852469e-4
double w = y - LNV;
if (w < M_LN_MAX_D+M_LN2-LNV) { if (w < M_LN_MAX_D + M_LN2 - LNV)
{
x = exp(w); x = exp(w);
x += VD2M1 * x; x += VD2M1 * x;
} }
else { else
{
errno = ERANGE; errno = ERANGE;
x = HUGE_VAL; x = HUGE_VAL;
} }
} }
else { else
double z = exp(y); {
double z = exp(y);
x = 0.5 * (z + (cosh_flag ? 1.0 : -1.0)/z); x = 0.5 * (z + (cosh_flag ? 1.0 : -1.0) / z);
} }
return negative ? -x : x; return negative ? -x : x;
} }
@ -76,6 +82,7 @@ sinh(double x)
double double
cosh(double x) cosh(double x)
{ {
if (x < 0) x = -x; if (x < 0)
x = -x;
return sinh_cosh(x, 1); return sinh_cosh(x, 1);
} }

View file

@ -6,11 +6,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#define NITER 5 #define NITER 5
double double
sqrt(double x) sqrt(double x)
@ -18,25 +18,31 @@ sqrt(double x)
int exponent; int exponent;
double val; double val;
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (x <= 0) { if (x <= 0)
if (x < 0) errno = EDOM; {
if (x < 0)
errno = EDOM;
return 0; return 0;
} }
if (x > DBL_MAX) return x; /* for infinity */ if (x > DBL_MAX)
return x; /* for infinity */
val = frexp(x, &exponent); val = frexp(x, &exponent);
if (exponent & 1) { if (exponent & 1)
{
exponent--; exponent--;
val *= 2; val *= 2;
} }
val = ldexp(val + 1.0, exponent/2 - 1); val = ldexp(val + 1.0, exponent / 2 - 1);
/* was: val = (val + 1.0)/2.0; val = ldexp(val, exponent/2); */ /* was: val = (val + 1.0)/2.0; val = ldexp(val, exponent/2); */
for (exponent = NITER - 1; exponent >= 0; exponent--) { for (exponent = NITER - 1; exponent >= 0; exponent--)
{
val = (val + x / val) / 2.0; val = (val + x / val) / 2.0;
} }
return val; return val;

View file

@ -6,10 +6,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
tan(double x) tan(double x)
@ -21,56 +21,61 @@ tan(double x)
int negative = x < 0; int negative = x < 0;
int invert = 0; int invert = 0;
double y; double y;
static double p[] = { static double p[] = {
1.0, 1.0,
-0.13338350006421960681e+0, -0.13338350006421960681e+0,
0.34248878235890589960e-2, 0.34248878235890589960e-2,
-0.17861707342254426711e-4 -0.17861707342254426711e-4
}; };
static double q[] = { static double q[] = {
1.0, 1.0,
-0.46671683339755294240e+0, -0.46671683339755294240e+0,
0.25663832289440112864e-1, 0.25663832289440112864e-1,
-0.31181531907010027307e-3, -0.31181531907010027307e-3,
0.49819433993786512270e-6 0.49819433993786512270e-6
}; };
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (negative) x = -x; if (negative)
x = -x;
/* ??? avoid loss of significance, error if x is too large ??? */ /* ??? avoid loss of significance, error if x is too large ??? */
y = x * M_2_PI + 0.5; y = x * M_2_PI + 0.5;
if (y >= DBL_MAX/M_PI_2) return 0.0; if (y >= DBL_MAX / M_PI_2)
return 0.0;
/* Use extended precision to calculate reduced argument. /* Use extended precision to calculate reduced argument.
Here we used 12 bits of the mantissa for a1. Here we used 12 bits of the mantissa for a1.
Also split x in integer part x1 and fraction part x2. Also split x in integer part x1 and fraction part x2.
*/ */
#define A1 1.57080078125 #define A1 1.57080078125
#define A2 -4.454455103380768678308e-6 #define A2 -4.454455103380768678308e-6
{ {
double x1, x2; double x1, x2;
modf(y, &y); modf(y, &y);
if (modf(0.5*y, &x1)) invert = 1; if (modf(0.5 * y, &x1))
invert = 1;
x2 = modf(x, &x1); x2 = modf(x, &x1);
x = x1 - y * A1; x = x1 - y * A1;
x += x2; x += x2;
x -= y * A2; x -= y * A2;
#undef A1 #undef A1
#undef A2 #undef A2
} }
/* ??? avoid underflow ??? */ /* ??? avoid underflow ??? */
y = x * x; y = x * x;
x += x * y * POLYNOM2(y, p+1); x += x * y * POLYNOM2(y, p + 1);
y = POLYNOM4(y, q); y = POLYNOM4(y, q);
if (negative) x = -x; if (negative)
return invert ? -y/x : x/y; x = -x;
return invert ? -y / x : x / y;
} }

View file

@ -6,10 +6,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
tanh(double x) tanh(double x)
@ -25,31 +25,36 @@ tanh(double x)
-0.96437492777225469787e+0 -0.96437492777225469787e+0
}; };
static double q[] = { static double q[] = {
0.48402357071988688686e+4, 0.48402357071988688686e+4,
0.22337720718962312926e+4, 0.22337720718962312926e+4,
0.11274474380534949335e+3, 0.11274474380534949335e+3,
1.0 1.0
}; };
int negative = x < 0; int negative = x < 0;
if (__IsNan(x)) { if (__IsNan(x))
{
errno = EDOM; errno = EDOM;
return x; return x;
} }
if (negative) x = -x; if (negative)
x = -x;
if (x >= 0.5*M_LN_MAX_D) { if (x >= 0.5 * M_LN_MAX_D)
{
x = 1.0; x = 1.0;
} }
#define LN3D2 0.54930614433405484570e+0 /* ln(3)/2 */ #define LN3D2 0.54930614433405484570e+0 /* ln(3)/2 */
else if (x > LN3D2) { else if (x > LN3D2)
x = 0.5 - 1.0/(exp(x+x)+1.0); {
x = 0.5 - 1.0 / (exp(x + x) + 1.0);
x += x; x += x;
} }
else { else
{
/* ??? avoid underflow ??? */ /* ??? avoid underflow ??? */
double g = x*x; double g = x * x;
x += x * g * POLYNOM2(g, p)/POLYNOM3(g, q); x += x * g * POLYNOM2(g, p) / POLYNOM3(g, q);
} }
return negative ? -x : x; return negative ? -x : x;
} }

View file

@ -4,33 +4,32 @@
last edit: 11-Nov-1988 D A Gwyn last edit: 11-Nov-1988 D A Gwyn
*/ */
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
typedef void *pointer; /* (void *) if you have it */ typedef void* pointer; /* (void *) if you have it */
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
#endif #endif
int _close(int d); int _close(int d);
int int closedir(register DIR* dirp) /* stream from opendir */
closedir(register DIR *dirp) /* stream from opendir */
{ {
register int fd; register int fd;
if ( dirp == NULL || dirp->dd_buf == NULL ) if (dirp == NULL || dirp->dd_buf == NULL)
{ {
errno = EFAULT; errno = EFAULT;
return -1; /* invalid pointer */ return -1; /* invalid pointer */
} }
fd = dirp->dd_fd; /* bug fix thanks to R. Salz */ fd = dirp->dd_fd; /* bug fix thanks to R. Salz */
free( (pointer)dirp->dd_buf ); free((pointer)dirp->dd_buf);
free( (pointer)dirp ); free((pointer)dirp);
return _close( fd ); return _close(fd);
} }

View file

@ -29,91 +29,92 @@
to always work, you shouldn't be using this source file at all. to always work, you shouldn't be using this source file at all.
*/ */
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef BSD_SYSV #ifdef BSD_SYSV
#include <sys/_dir.h> /* BSD flavor, not System V */ #include <sys/_dir.h> /* BSD flavor, not System V */
#else #else
#if defined(UFS) #if defined(UFS)
#define DIRSIZ 14 /* 14 char filename in Version 7 */ #define DIRSIZ 14 /* 14 char filename in Version 7 */
#endif #endif
#define MAXNAMLEN 255 #define MAXNAMLEN 255
struct direct { struct direct
off_t d_off; /* offset of next disk directory entry */ {
u_long d_fileno; /* file number of entry */ off_t d_off; /* offset of next disk directory entry */
u_short d_reclen; /* length of this record */ u_long d_fileno; /* file number of entry */
u_short d_namlen; /* length of string in d_name */ u_short d_reclen; /* length of this record */
char d_name[MAXNAMLEN + 1]; /* name (up to MAXNAMLEN + 1) */ u_short d_namlen; /* length of string in d_name */
char d_name[MAXNAMLEN + 1]; /* name (up to MAXNAMLEN + 1) */
}; };
#undef MAXNAMLEN /* avoid conflict with SVR3 */ #undef MAXNAMLEN /* avoid conflict with SVR3 */
#define d_ino d_fileno /* compatability */ #define d_ino d_fileno /* compatability */
#ifdef d_ino /* 4.3BSD/NFS using d_fileno */ #ifdef d_ino /* 4.3BSD/NFS using d_fileno */
#undef d_ino /* (not absolutely necessary) */ #undef d_ino /* (not absolutely necessary) */
#else #else
#define d_fileno d_ino /* (struct direct) member */ #define d_fileno d_ino /* (struct direct) member */
#endif #endif
#endif #endif
#include <sys/dirent.h> #include <sys/dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef UNK #ifdef UNK
#ifndef UFS #ifndef UFS
#error UNK applies only to UFS #error UNK applies only to UFS
/* One could do something similar for getdirentries(), but I didn't bother. */ /* One could do something similar for getdirentries(), but I didn't bother. */
#endif #endif
#include <signal.h> #include <signal.h>
#endif #endif
#if defined(UFS) + defined(BFS) + defined(NFS) != 1 /* sanity check */ #if defined(UFS) + defined(BFS) + defined(NFS) != 1 /* sanity check */
#error exactly one of UFS, BFS, or NFS must be defined #error exactly one of UFS, BFS, or NFS must be defined
#endif #endif
#ifdef UFS #ifdef UFS
#define RecLen( dp ) (sizeof(struct direct)) /* fixed-length entries */ #define RecLen(dp) (sizeof(struct direct)) /* fixed-length entries */
#else /* BFS || NFS */ #else /* BFS || NFS */
#define RecLen( dp ) ((dp)->d_reclen) /* variable-length entries */ #define RecLen(dp) ((dp)->d_reclen) /* variable-length entries */
#endif #endif
#ifdef NFS #ifdef NFS
#ifdef BSD_SYSV #ifdef BSD_SYSV
#define getdirentries _getdirentries /* package hides this system call */ #define getdirentries _getdirentries /* package hides this system call */
#endif #endif
extern int getdirentries(int fd, char *buf, int nbytes, long *basep); extern int getdirentries(int fd, char* buf, int nbytes, long* basep);
static long dummy; /* getdirentries() needs basep */ static long dummy; /* getdirentries() needs basep */
#define GetBlock( fd, buf, n ) getdirentries( fd, buf, (unsigned)n, &dummy ) #define GetBlock(fd, buf, n) getdirentries(fd, buf, (unsigned)n, &dummy)
#else /* UFS || BFS */ #else /* UFS || BFS */
#ifdef BSD_SYSV #ifdef BSD_SYSV
#define read _read /* avoid emulation overhead */ #define read _read /* avoid emulation overhead */
#endif #endif
extern int read(); extern int read();
#define GetBlock( fd, buf, n ) read( fd, buf, (unsigned)n ) #define GetBlock(fd, buf, n) read(fd, buf, (unsigned)n)
#endif #endif
#ifdef UNK #ifdef UNK
extern int _getdents(); /* actual system call */ extern int _getdents(); /* actual system call */
#endif #endif
extern int _fstat(int fd, struct stat *buf); extern int _fstat(int fd, struct stat* buf);
extern off_t _lseek(int d, int offset, int whence); extern off_t _lseek(int d, int offset, int whence);
#ifndef DIRBLKSIZ #ifndef DIRBLKSIZ
#define DIRBLKSIZ 4096 /* directory file read buffer size */ #define DIRBLKSIZ 4096 /* directory file read buffer size */
#endif #endif
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
#endif #endif
#ifndef SEEK_CUR #ifndef SEEK_CUR
#define SEEK_CUR 1 #define SEEK_CUR 1
#endif #endif
#ifndef S_ISDIR /* macro to test for directory file */ #ifndef S_ISDIR /* macro to test for directory file */
#define S_ISDIR( mode ) (((mode) & S_IFMT) == S_IFDIR) #define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#endif #endif
#ifdef UFS #ifdef UFS
@ -124,166 +125,168 @@ extern off_t _lseek(int d, int offset, int whence);
*/ */
static int static int
NameLen( char name[] ) /* return # chars in embedded name */ NameLen(char name[]) /* return # chars in embedded name */
/* -> name embedded in struct direct */ /* -> name embedded in struct direct */
{ {
register char *s; /* -> name[.] */ register char* s; /* -> name[.] */
register char *stop = &name[DIRSIZ]; /* -> past end of name field */ register char* stop = &name[DIRSIZ]; /* -> past end of name field */
for ( s = &name[1]; /* (empty names are impossible) */ for (s = &name[1]; /* (empty names are impossible) */
*s != '\0' /* not NUL terminator */ *s != '\0' /* not NUL terminator */
&& ++s < stop; /* < DIRSIZ characters scanned */ && ++s < stop; /* < DIRSIZ characters scanned */
) )
; ;
return s - name; /* # valid characters in name */ return s - name; /* # valid characters in name */
} }
#else /* BFS || NFS */ #else /* BFS || NFS */
#define NameLen( name ) strlen( name ) /* names are always NUL-terminated */ #define NameLen(name) strlen(name) /* names are always NUL-terminated */
#endif #endif
#ifdef UNK #ifdef UNK
static enum { maybe, no, yes } state = maybe; static enum { maybe,
/* does _getdents() work? */ no,
yes } state
= maybe;
/* does _getdents() work? */
/*ARGSUSED*/ /*ARGSUSED*/
static void static void
sig_catch(int sig) /* sig must be SIGSYS */ sig_catch(int sig) /* sig must be SIGSYS */
{ {
state = no; /* attempted _getdents() faulted */ state = no; /* attempted _getdents() faulted */
} }
#endif #endif
int int getdents(int fildes, char* buf, unsigned nbyte) /* returns # bytes read;
getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
0 on EOF, -1 on error */ 0 on EOF, -1 on error */
/* fildes == directory file descriptor */ /* fildes == directory file descriptor */
/* *buf == where to put the (struct dirent)s */ /* *buf == where to put the (struct dirent)s */
/* nbyte == size of buf[] */ /* nbyte == size of buf[] */
{ {
int serrno; /* entry errno */ int serrno; /* entry errno */
off_t offset; /* initial directory file offset */ off_t offset; /* initial directory file offset */
struct stat statb; /* fstat() info */ struct stat statb; /* fstat() info */
union { union {
char dblk[DIRBLKSIZ]; char dblk[DIRBLKSIZ];
/* directory file block buffer */ /* directory file block buffer */
struct direct dummy; /* just for alignment */ struct direct dummy; /* just for alignment */
} u; /* (avoids having to malloc()) */ } u; /* (avoids having to malloc()) */
register struct direct *dp; /* -> u.dblk[.] */ register struct direct* dp; /* -> u.dblk[.] */
register struct dirent *bp; /* -> buf[.] */ register struct dirent* bp; /* -> buf[.] */
#ifdef UNK #ifdef UNK
switch ( state ) switch (state)
{ {
void (*shdlr)(); /* entry SIGSYS handler */ void (*shdlr)(); /* entry SIGSYS handler */
register int retval; /* return from _getdents() if any */ register int retval; /* return from _getdents() if any */
case yes: /* _getdents() is known to work */ case yes: /* _getdents() is known to work */
return _getdents( fildes, buf, nbyte ); return _getdents(fildes, buf, nbyte);
case maybe: /* first time only */ case maybe: /* first time only */
shdlr = signal( SIGSYS, sig_catch ); shdlr = signal(SIGSYS, sig_catch);
retval = _getdents( fildes, buf, nbyte ); /* try it */ retval = _getdents(fildes, buf, nbyte); /* try it */
(void)signal( SIGSYS, shdlr ); (void)signal(SIGSYS, shdlr);
if ( state == maybe ) /* SIGSYS did not occur */ if (state == maybe) /* SIGSYS did not occur */
{ {
state = yes; /* so _getdents() must have worked */ state = yes; /* so _getdents() must have worked */
return retval; return retval;
} }
/* else fall through into emulation */ /* else fall through into emulation */
/* case no:*/ /* fall through into emulation */ /* case no:*/ /* fall through into emulation */
} }
#endif #endif
if ( buf == NULL if (buf == NULL
#ifdef ATT_SPEC #ifdef ATT_SPEC
|| (unsigned long)buf % sizeof(long) != 0 /* ugh */ || (unsigned long)buf % sizeof(long) != 0 /* ugh */
#endif #endif
) { )
errno = EFAULT; /* invalid pointer */ {
errno = EFAULT; /* invalid pointer */
return -1; return -1;
} }
if ( _fstat( fildes, &statb ) != 0 ) if (_fstat(fildes, &statb) != 0)
return -1; /* errno set by fstat() */ return -1; /* errno set by fstat() */
if ( !S_ISDIR( statb.st_mode ) ) if (!S_ISDIR(statb.st_mode))
{ {
errno = ENOTDIR; /* not a directory */ errno = ENOTDIR; /* not a directory */
return -1; return -1;
} }
if ( (offset = _lseek( fildes, (off_t)0, SEEK_CUR )) < 0 ) if ((offset = _lseek(fildes, (off_t)0, SEEK_CUR)) < 0)
return -1; /* errno set by lseek() */ return -1; /* errno set by lseek() */
#ifdef BFS /* no telling what remote hosts do */ #ifdef BFS /* no telling what remote hosts do */
if ( (unsigned long)offset % DIRBLKSIZ != 0 ) if ((unsigned long)offset % DIRBLKSIZ != 0)
{ {
errno = ENOENT; /* file pointer probably misaligned */ errno = ENOENT; /* file pointer probably misaligned */
return -1; return -1;
} }
#endif #endif
serrno = errno; /* save entry errno */ serrno = errno; /* save entry errno */
for ( bp = (struct dirent *)buf; bp == (struct dirent *)buf; ) for (bp = (struct dirent*)buf; bp == (struct dirent*)buf;)
{ /* convert next directory block */ { /* convert next directory block */
int size; int size;
do size = GetBlock( fildes, u.dblk, DIRBLKSIZ ); do
while ( size == -1 && errno == EINTR ); size = GetBlock(fildes, u.dblk, DIRBLKSIZ);
while (size == -1 && errno == EINTR);
if ( size <= 0 ) if (size <= 0)
return size; /* EOF or error (EBADF) */ return size; /* EOF or error (EBADF) */
for ( dp = (struct direct *)u.dblk; for (dp = (struct direct*)u.dblk;
(char *)dp < &u.dblk[size]; (char*)dp < &u.dblk[size];
dp = (struct direct *)((char *)dp + RecLen( dp )) dp = (struct direct*)((char*)dp + RecLen(dp)))
) { {
#ifndef UFS #ifndef UFS
if ( dp->d_reclen <= 0 ) if (dp->d_reclen <= 0)
{ {
errno = EIO; /* corrupted directory */ errno = EIO; /* corrupted directory */
return -1; return -1;
} }
#endif #endif
if ( dp->d_fileno != 0 ) if (dp->d_fileno != 0)
{ /* non-empty; copy to user buffer */ { /* non-empty; copy to user buffer */
register int reclen = register int reclen = DIRENTSIZ(NameLen(dp->d_name));
DIRENTSIZ( NameLen( dp->d_name ) );
if ( (char *)bp + reclen > &buf[nbyte] ) if ((char*)bp + reclen > &buf[nbyte])
{ {
errno = EINVAL; errno = EINVAL;
return -1; /* buf too small */ return -1; /* buf too small */
} }
bp->d_ino = dp->d_fileno; bp->d_ino = dp->d_fileno;
bp->d_off = offset + ((char *)dp - u.dblk); bp->d_off = offset + ((char*)dp - u.dblk);
bp->d_reclen = reclen; bp->d_reclen = reclen;
(void)strncpy( bp->d_name, dp->d_name, (void)strncpy(bp->d_name, dp->d_name,
reclen - DIRENTBASESIZ reclen - DIRENTBASESIZ); /* adds NUL padding */
); /* adds NUL padding */
bp = (struct dirent *)((char *)bp + reclen); bp = (struct dirent*)((char*)bp + reclen);
}
} }
#ifndef BFS /* 4.2BSD screwed up; fixed in 4.3BSD */
if ( (char *)dp > &u.dblk[size] )
{
errno = EIO; /* corrupted directory */
return -1;
}
#endif
} }
errno = serrno; /* restore entry errno */ #ifndef BFS /* 4.2BSD screwed up; fixed in 4.3BSD */
return (char *)bp - buf; /* return # bytes read */ if ((char*)dp > &u.dblk[size])
{
errno = EIO; /* corrupted directory */
return -1;
}
#endif
}
errno = serrno; /* restore entry errno */
return (char*)bp - buf; /* return # bytes read */
} }

View file

@ -5,131 +5,130 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <grp.h> #include <grp.h>
#define O_RDONLY 0 #define O_RDONLY 0
int open(const char *path, int flags); int open(const char* path, int flags);
#if defined(__BSD4_2) #if defined(__BSD4_2)
typedef int off_t; /* see lseek(2) */ typedef int off_t; /* see lseek(2) */
#else #else
typedef long off_t; typedef long off_t;
#endif #endif
off_t _lseek(int d, off_t offset, int whence); off_t _lseek(int d, off_t offset, int whence);
int _read(int d, char *buf, int nbytes); int _read(int d, char* buf, int nbytes);
int _close(int d); int _close(int d);
#define RBUFSIZE 1024 #define RBUFSIZE 1024
static char _gr_file[] = "/etc/group"; static char _gr_file[] = "/etc/group";
static char _grbuf[256]; static char _grbuf[256];
static char _buffer[RBUFSIZE]; static char _buffer[RBUFSIZE];
static char *_pnt; static char* _pnt;
static char *_buf; static char* _buf;
static int _gfd = -1; static int _gfd = -1;
static int _bufcnt; static int _bufcnt;
static struct group grp; static struct group grp;
int int setgrent(void)
setgrent(void)
{ {
if (_gfd >= 0) if (_gfd >= 0)
_lseek(_gfd, 0L, 0); _lseek(_gfd, 0L, 0);
else else
_gfd = open(_gr_file, O_RDONLY); _gfd = open(_gr_file, O_RDONLY);
_bufcnt = 0; _bufcnt = 0;
return _gfd; return _gfd;
} }
void void endgrent(void)
endgrent(void)
{ {
if (_gfd >= 0) if (_gfd >= 0)
_close(_gfd); _close(_gfd);
_gfd = -1; _gfd = -1;
_bufcnt = 0; _bufcnt = 0;
} }
static int static int
getline(void) getline(void)
{ {
if (_gfd < 0 && setgrent() < 0) if (_gfd < 0 && setgrent() < 0)
return 0; return 0;
_buf = _grbuf; _buf = _grbuf;
do { do
if (--_bufcnt <= 0){ {
if ((_bufcnt = _read(_gfd, _buffer, RBUFSIZE)) <= 0) if (--_bufcnt <= 0)
return 0; {
else if ((_bufcnt = _read(_gfd, _buffer, RBUFSIZE)) <= 0)
_pnt = _buffer; return 0;
else
_pnt = _buffer;
} }
*_buf++ = *_pnt++; *_buf++ = *_pnt++;
} while (*_pnt != '\n'); } while (*_pnt != '\n');
_pnt++; _pnt++;
_bufcnt--; _bufcnt--;
*_buf = 0; *_buf = 0;
_buf = _grbuf; _buf = _grbuf;
return 1; return 1;
} }
static void static void
skip_period(void) skip_period(void)
{ {
while (*_buf && *_buf != ':') while (*_buf && *_buf != ':')
_buf++; _buf++;
*_buf++ = '\0'; *_buf++ = '\0';
} }
struct group * struct group*
getgrent(void) getgrent(void)
{ {
if (getline() == 0) if (getline() == 0)
return 0; return 0;
grp.gr_name = _buf; grp.gr_name = _buf;
skip_period(); skip_period();
grp.gr_passwd = _buf; grp.gr_passwd = _buf;
skip_period(); skip_period();
grp.gr_gid = atoi(_buf); grp.gr_gid = atoi(_buf);
skip_period(); skip_period();
return &grp; return &grp;
} }
struct group * struct group*
getgrnam(const char *name) getgrnam(const char* name)
{ {
struct group *g; struct group* g;
setgrent(); setgrent();
while ((g = getgrent()) != 0) while ((g = getgrent()) != 0)
if (!strcmp(g -> gr_name, name)) if (!strcmp(g->gr_name, name))
break; break;
endgrent(); endgrent();
if (g != 0) if (g != 0)
return g; return g;
else else
return 0; return 0;
} }
struct group * struct group*
getgrgid(int gid) getgrgid(int gid)
{ {
struct group *g; struct group* g;
setgrent(); setgrent();
while ((g = getgrent()) != 0) while ((g = getgrent()) != 0)
if (g -> gr_gid == gid) if (g->gr_gid == gid)
break; break;
endgrent(); endgrent();
if (g != 0) if (g != 0)
return g; return g;
else else
return 0; return 0;
} }

View file

@ -3,56 +3,66 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define ERR(s, c) if(opterr){\ #define ERR(s, c) \
fputs(argv[0], stderr);\ if (opterr) \
fputs(s, stderr);\ { \
fputc(c, stderr);\ fputs(argv[0], stderr); \
fputc('\n', stderr);} fputs(s, stderr); \
fputc(c, stderr); \
fputc('\n', stderr); \
}
int opterr = 1; int opterr = 1;
int optind = 1; int optind = 1;
int optopt; int optopt;
char *optarg; char* optarg;
int int getopt(int argc, char** argv, char* opts)
getopt(int argc, char **argv, char *opts)
{ {
static int sp = 1; static int sp = 1;
register c; register c;
register char *cp; register char* cp;
if (sp == 1) if (sp == 1)
if (optind >= argc || if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
argv[optind][0] != '-' || argv[optind][1] == '\0')
return EOF; return EOF;
else if (!strcmp(argv[optind], "--")) { else if (!strcmp(argv[optind], "--"))
{
optind++; optind++;
return EOF; return EOF;
} }
optopt = c = argv[optind][sp]; optopt = c = argv[optind][sp];
if (c == ':' || (cp=strchr(opts, c)) == NULL) { if (c == ':' || (cp = strchr(opts, c)) == NULL)
ERR (": illegal option -- ", c); {
if (argv[optind][++sp] == '\0') { ERR(": illegal option -- ", c);
if (argv[optind][++sp] == '\0')
{
optind++; optind++;
sp = 1; sp = 1;
} }
return '?'; return '?';
} }
if (*++cp == ':') { if (*++cp == ':')
if (argv[optind][sp+1] != '\0') {
optarg = &argv[optind++][sp+1]; if (argv[optind][sp + 1] != '\0')
else if (++optind >= argc) { optarg = &argv[optind++][sp + 1];
ERR (": option requires an argument -- ", c); else if (++optind >= argc)
{
ERR(": option requires an argument -- ", c);
sp = 1; sp = 1;
return '?'; return '?';
} else }
else
optarg = argv[optind++]; optarg = argv[optind++];
sp = 1; sp = 1;
} else { }
if (argv[optind][++sp] == '\0') { else
{
if (argv[optind][++sp] == '\0')
{
sp = 1; sp = 1;
optind++; optind++;
} }

View file

@ -3,22 +3,21 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <sgtty.h> #include <sgtty.h>
#include <fcntl.h> #include <fcntl.h>
int _open(const char *path, int flags); int _open(const char* path, int flags);
int _write(int d, const char *buf, int nbytes); int _write(int d, const char* buf, int nbytes);
int _read(int d, char *buf, int nbytes); int _read(int d, char* buf, int nbytes);
int _close(int d); int _close(int d);
int _stty(int, struct sgttyb *); int _stty(int, struct sgttyb*);
int _gtty(int, struct sgttyb *); int _gtty(int, struct sgttyb*);
char * char* getpass(const char* prompt)
getpass(const char *prompt)
{ {
int i = 0; int i = 0;
struct sgttyb tty, ttysave; struct sgttyb tty, ttysave;
@ -26,7 +25,8 @@ getpass(const char *prompt)
int fd; int fd;
void (*savesig)(int); void (*savesig)(int);
if ((fd = _open("/dev/tty", O_RDONLY)) < 0) fd = 0; if ((fd = _open("/dev/tty", O_RDONLY)) < 0)
fd = 0;
savesig = signal(SIGINT, SIG_IGN); savesig = signal(SIGINT, SIG_IGN);
_write(2, prompt, strlen(prompt)); _write(2, prompt, strlen(prompt));
_gtty(fd, &tty); _gtty(fd, &tty);
@ -39,7 +39,8 @@ getpass(const char *prompt)
pwdbuf[i - 1] = '\0'; pwdbuf[i - 1] = '\0';
_stty(fd, &ttysave); _stty(fd, &ttysave);
_write(2, "\n", 1); _write(2, "\n", 1);
if (fd != 0) _close(fd); if (fd != 0)
_close(fd);
signal(SIGINT, savesig); signal(SIGINT, savesig);
return(pwdbuf); return (pwdbuf);
} }

View file

@ -3,17 +3,19 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int getw(register FILE *stream) int getw(register FILE* stream)
{ {
register int cnt = sizeof(int); register int cnt = sizeof(int);
int w; int w;
register char *p = (char *) &w; register char* p = (char*)&w;
while (cnt--) { while (cnt--)
{
*p++ = getc(stream); *p++ = getc(stream);
} }
if (feof(stream) || ferror(stream)) return EOF; if (feof(stream) || ferror(stream))
return EOF;
return w; return w;
} }

View file

@ -3,28 +3,32 @@
/* no _-protected system-calls? */ /* no _-protected system-calls? */
unsigned int getpid(void); unsigned int getpid(void);
int access(char *, int); int access(char*, int);
char *mktemp(char *template) char* mktemp(char* template)
{ {
register int pid, k; register int pid, k;
register char *p; register char* p;
pid = getpid(); /* get process id as semi-unique number */ pid = getpid(); /* get process id as semi-unique number */
p = template; p = template;
while (*p) p++; /* find end of string */ while (*p)
p++; /* find end of string */
/* Replace XXXXXX at end of template with pid. */ /* Replace XXXXXX at end of template with pid. */
while (*--p == 'X') { while (*--p == 'X')
*p = '0' + (pid % 10); {
pid /= 10; *p = '0' + (pid % 10);
} pid /= 10;
p++;
for (k = 'a'; k <= 'z'; k++) {
*p = k;
if (access(template, 0) < 0) {
return template;
} }
} p++;
return("/"); for (k = 'a'; k <= 'z'; k++)
{
*p = k;
if (access(template, 0) < 0)
{
return template;
}
}
return ("/");
} }

View file

@ -4,64 +4,63 @@
last edit: 16-Jun-1987 D A Gwyn last edit: 16-Jun-1987 D A Gwyn
*/ */
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
typedef void *pointer; /* (void *) if you have it */ typedef void* pointer; /* (void *) if you have it */
extern int _open(const char *path, int flags, int mode); extern int _open(const char* path, int flags, int mode);
extern int _close(int d); extern int _close(int d);
extern int _fstat(int fd, struct stat *buf); extern int _fstat(int fd, struct stat* buf);
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
#endif #endif
#ifndef O_RDONLY #ifndef O_RDONLY
#define O_RDONLY 0 #define O_RDONLY 0
#endif #endif
#ifndef S_ISDIR /* macro to test for directory file */ #ifndef S_ISDIR /* macro to test for directory file */
#define S_ISDIR( mode ) (((mode) & S_IFMT) == S_IFDIR) #define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#endif #endif
DIR * DIR* opendir(const char* dirname) /* name of directory */
opendir(const char *dirname) /* name of directory */
{ {
register DIR *dirp; /* -> malloc'ed storage */ register DIR* dirp; /* -> malloc'ed storage */
register int fd; /* file descriptor for read */ register int fd; /* file descriptor for read */
struct stat sbuf; /* result of fstat() */ struct stat sbuf; /* result of fstat() */
if ( (fd = _open( dirname, O_RDONLY, 0 )) < 0 ) if ((fd = _open(dirname, O_RDONLY, 0)) < 0)
return NULL; /* errno set by open() */ return NULL; /* errno set by open() */
if ( _fstat( fd, &sbuf ) != 0 || !S_ISDIR( sbuf.st_mode ) ) if (_fstat(fd, &sbuf) != 0 || !S_ISDIR(sbuf.st_mode))
{ {
(void)_close( fd ); (void)_close(fd);
errno = ENOTDIR; errno = ENOTDIR;
return NULL; /* not a directory */ return NULL; /* not a directory */
} }
if ( (dirp = (DIR *)malloc( sizeof(DIR) )) == NULL if ((dirp = (DIR*)malloc(sizeof(DIR))) == NULL
|| (dirp->dd_buf = (char *)malloc( (unsigned)DIRBUF )) == NULL || (dirp->dd_buf = (char*)malloc((unsigned)DIRBUF)) == NULL)
) { {
register int serrno = errno; register int serrno = errno;
/* errno set to ENOMEM by sbrk() */ /* errno set to ENOMEM by sbrk() */
if ( dirp != NULL ) if (dirp != NULL)
free( (pointer)dirp ); free((pointer)dirp);
(void)_close( fd ); (void)_close(fd);
errno = serrno; errno = serrno;
return NULL; /* not enough memory */ return NULL; /* not enough memory */
} }
dirp->dd_fd = fd; dirp->dd_fd = fd;
dirp->dd_loc = dirp->dd_size = 0; /* refill needed */ dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
return dirp; return dirp;
} }

View file

@ -3,55 +3,57 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
#if defined(__BSD4_2) #if defined(__BSD4_2)
union wait { union wait {
int w_status; int w_status;
}; };
typedef union wait wait_arg; typedef union wait wait_arg;
#else #else
typedef int wait_arg; typedef int wait_arg;
#endif /* __BSD4_2 */ #endif /* __BSD4_2 */
#include "../stdio/loc_incl.h" #include "../stdio/loc_incl.h"
int _close(int d); int _close(int d);
#if defined(__USG) #if defined(__USG)
static static
#endif #endif
int _dup2(int oldd, int newd); /* not present in System 5 */ int
int _execl(const char *name, ... ); _dup2(int oldd, int newd); /* not present in System 5 */
int _execl(const char* name, ...);
int _fork(void); int _fork(void);
int _pipe(int fildes[2]); int _pipe(int fildes[2]);
int _wait(wait_arg *status); int _wait(wait_arg* status);
void _exit(int status); void _exit(int status);
static int pids[FOPEN_MAX]; static int pids[FOPEN_MAX];
FILE * FILE* popen(const char* command, const char* type)
popen(const char *command, const char *type)
{ {
int piped[2]; int piped[2];
int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2; int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2;
int pid; int pid;
if (Xtype == 2 || if (Xtype == 2 || _pipe(piped) < 0 || (pid = _fork()) < 0)
_pipe(piped) < 0 || return 0;
(pid = _fork()) < 0) return 0;
if (pid == 0) { if (pid == 0)
{
/* child */ /* child */
register int *p; register int* p;
for (p = pids; p < &pids[ FOPEN_MAX]; p++) { for (p = pids; p < &pids[FOPEN_MAX]; p++)
if (*p) _close(p - pids); {
if (*p)
_close(p - pids);
} }
_close(piped[Xtype]); _close(piped[Xtype]);
_dup2(piped[!Xtype], !Xtype); _dup2(piped[!Xtype], !Xtype);
_close(piped[!Xtype]); _close(piped[!Xtype]);
_execl("/bin/sh", "sh", "-c", command, (char *) 0); _execl("/bin/sh", "sh", "-c", command, (char*)0);
_exit(127); /* like system() ??? */ _exit(127); /* like system() ??? */
} }
pids[piped[Xtype]] = pid; pids[piped[Xtype]] = pid;
@ -59,14 +61,13 @@ popen(const char *command, const char *type)
return fdopen(piped[Xtype], type); return fdopen(piped[Xtype], type);
} }
#if defined(__BSD4_2) #if defined(__BSD4_2)
#define ret_val status.w_status #define ret_val status.w_status
#else #else
#define ret_val status #define ret_val status
#endif #endif
int int pclose(FILE* stream)
pclose(FILE *stream)
{ {
int fd = fileno(stream); int fd = fileno(stream);
wait_arg status; wait_arg status;
@ -75,17 +76,20 @@ pclose(FILE *stream)
void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN); void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN);
fclose(stream); fclose(stream);
while ((wret = _wait(&status)) != -1) { while ((wret = _wait(&status)) != -1)
if (wret == pids[fd]) break; {
if (wret == pids[fd])
break;
} }
if (wret == -1) ret_val = -1; if (wret == -1)
ret_val = -1;
signal(SIGINT, intsave); signal(SIGINT, intsave);
signal(SIGQUIT, quitsave); signal(SIGQUIT, quitsave);
pids[fd] = 0; pids[fd] = 0;
return ret_val; return ret_val;
} }
#if defined(__USG) #if defined(__USG)
int _dup(int fildes); int _dup(int fildes);
static int static int
@ -95,16 +99,21 @@ _dup2(int oldd, int newd)
int fdbuf[FOPEN_MAX]; int fdbuf[FOPEN_MAX];
/* ignore the error on the close() */ /* ignore the error on the close() */
tmp = errno; (void) _close(newd); errno = tmp; tmp = errno;
while ((fd = _dup(oldd)) != newd) { (void)_close(newd);
if (fd == -1) break; errno = tmp;
while ((fd = _dup(oldd)) != newd)
{
if (fd == -1)
break;
fdbuf[i++] = fd; fdbuf[i++] = fd;
} }
tmp = errno; tmp = errno;
while (--i >= 0) { while (--i >= 0)
{
_close(fdbuf[i]); _close(fdbuf[i]);
} }
errno = tmp; errno = tmp;
return -(fd == -1); return -(fd == -1);
} }
#endif /* __USG */ #endif /* __USG */

View file

@ -3,17 +3,18 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int putw(int w, register FILE* stream)
putw(int w, register FILE *stream)
{ {
register int cnt = sizeof(int); register int cnt = sizeof(int);
register char *p = (char *) &w; register char* p = (char*)&w;
while (cnt--) { while (cnt--)
{
putc(*p++, stream); putc(*p++, stream);
} }
if (ferror(stream)) return EOF; if (ferror(stream))
return EOF;
return w; return w;
} }

View file

@ -4,44 +4,41 @@
last edit: 25-Apr-1987 D A Gwyn last edit: 25-Apr-1987 D A Gwyn
*/ */
#include <errno.h> #include <errno.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
/* SVR3 system call, or emulation for getdents() */ /* SVR3 system call, or emulation for getdents() */
extern int getdents(int fildes, char *buf, unsigned nbyte); extern int getdents(int fildes, char* buf, unsigned nbyte);
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
#endif #endif
struct dirent * struct dirent*
readdir(register DIR *dirp) readdir(register DIR* dirp)
{ {
register struct dirent *dp; /* -> directory data */ register struct dirent* dp; /* -> directory data */
if ( dirp == NULL || dirp->dd_buf == NULL ) if (dirp == NULL || dirp->dd_buf == NULL)
{ {
errno = EFAULT; errno = EFAULT;
return NULL; /* invalid pointer */ return NULL; /* invalid pointer */
} }
do { do
if ( dirp->dd_loc >= dirp->dd_size ) /* empty or obsolete */ {
if (dirp->dd_loc >= dirp->dd_size) /* empty or obsolete */
dirp->dd_loc = dirp->dd_size = 0; dirp->dd_loc = dirp->dd_size = 0;
if ( dirp->dd_size == 0 /* need to refill buffer */ if (dirp->dd_size == 0 /* need to refill buffer */
&& (dirp->dd_size = && (dirp->dd_size = getdents(dirp->dd_fd, dirp->dd_buf, (unsigned)DIRBUF)) <= 0)
getdents( dirp->dd_fd, dirp->dd_buf, (unsigned)DIRBUF ) return NULL; /* EOF or error */
) <= 0
)
return NULL; /* EOF or error */
dp = (struct dirent *)&dirp->dd_buf[dirp->dd_loc]; dp = (struct dirent*)&dirp->dd_buf[dirp->dd_loc];
dirp->dd_loc += dp->d_reclen; dirp->dd_loc += dp->d_reclen;
} } while (dp->d_ino == 0L); /* don't rely on getdents() */
while ( dp->d_ino == 0L ); /* don't rely on getdents() */
return dp; return dp;
} }

View file

@ -8,30 +8,29 @@
rewinddir() to forget about buffered data. rewinddir() to forget about buffered data.
*/ */
#include <errno.h> #include <errno.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
extern off_t _lseek(int d, int offset, int whence); extern off_t _lseek(int d, int offset, int whence);
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
#endif #endif
#ifndef SEEK_SET #ifndef SEEK_SET
#define SEEK_SET 0 #define SEEK_SET 0
#endif #endif
void void rewinddir(register DIR* dirp)
rewinddir(register DIR *dirp)
{ {
if ( dirp == NULL || dirp->dd_buf == NULL ) if (dirp == NULL || dirp->dd_buf == NULL)
{ {
errno = EFAULT; errno = EFAULT;
return; /* invalid pointer */ return; /* invalid pointer */
} }
dirp->dd_loc = dirp->dd_size = 0; /* invalidate buffer */ dirp->dd_loc = dirp->dd_size = 0; /* invalidate buffer */
(void)_lseek( dirp->dd_fd, (off_t)0, SEEK_SET ); /* may set errno */ (void)_lseek(dirp->dd_fd, (off_t)0, SEEK_SET); /* may set errno */
} }

View file

@ -10,36 +10,35 @@
practically impossible to do right. Avoid using them! practically impossible to do right. Avoid using them!
*/ */
#include <errno.h> #include <errno.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
extern off_t _lseek(int d, int offset, int whence); extern off_t _lseek(int d, int offset, int whence);
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
#endif #endif
#ifndef SEEK_SET #ifndef SEEK_SET
#define SEEK_SET 0 #define SEEK_SET 0
#endif #endif
typedef int bool; /* Boolean data type */ typedef int bool; /* Boolean data type */
#define false 0 #define false 0
#define true 1 #define true 1
void void seekdir(register DIR* dirp, register off_t loc)
seekdir(register DIR *dirp, register off_t loc)
/* loc == position from telldir() */ /* loc == position from telldir() */
{ {
register bool rewind; /* "start over when stymied" flag */ register bool rewind; /* "start over when stymied" flag */
if ( dirp == NULL || dirp->dd_buf == NULL ) if (dirp == NULL || dirp->dd_buf == NULL)
{ {
errno = EFAULT; errno = EFAULT;
return; /* invalid pointer */ return; /* invalid pointer */
} }
/* A (struct dirent)'s d_off is an invented quantity on 4.nBSD /* A (struct dirent)'s d_off is an invented quantity on 4.nBSD
NFS-supporting systems, so it is not safe to lseek() to it. */ NFS-supporting systems, so it is not safe to lseek() to it. */
@ -52,58 +51,57 @@ seekdir(register DIR *dirp, register off_t loc)
or even to use binary search on the directory blocks. I or even to use binary search on the directory blocks. I
doubt that the extra code for that would be worthwhile. */ doubt that the extra code for that would be worthwhile. */
if ( dirp->dd_loc >= dirp->dd_size /* invalid index */ if (dirp->dd_loc >= dirp->dd_size /* invalid index */
|| ((struct dirent *)&dirp->dd_buf[dirp->dd_loc])->d_off > loc || ((struct dirent*)&dirp->dd_buf[dirp->dd_loc])->d_off > loc
/* too far along in buffer */ /* too far along in buffer */
) )
dirp->dd_loc = 0; /* reset to beginning of buffer */ dirp->dd_loc = 0; /* reset to beginning of buffer */
/* else save time by starting at current dirp->dd_loc */ /* else save time by starting at current dirp->dd_loc */
for ( rewind = true; ; ) for (rewind = true;;)
{ {
register struct dirent *dp; register struct dirent* dp;
/* See whether the matching entry is in the current buffer. */ /* See whether the matching entry is in the current buffer. */
if ( (dirp->dd_loc < dirp->dd_size /* valid index */ if ((dirp->dd_loc < dirp->dd_size /* valid index */
|| readdir( dirp ) != NULL /* next buffer read */ || readdir(dirp) != NULL /* next buffer read */
&& (dirp->dd_loc = 0, true) /* beginning of buffer set */ && (dirp->dd_loc = 0, true) /* beginning of buffer set */
) )
&& (dp = (struct dirent *)&dirp->dd_buf[dirp->dd_loc])->d_off && (dp = (struct dirent*)&dirp->dd_buf[dirp->dd_loc])->d_off
<= loc /* match possible in this buffer */ <= loc /* match possible in this buffer */
) { )
for ( /* dp initialized above */ ; {
(char *)dp < &dirp->dd_buf[dirp->dd_size]; for (/* dp initialized above */;
dp = (struct dirent *)((char *)dp + dp->d_reclen) (char*)dp < &dirp->dd_buf[dirp->dd_size];
) dp = (struct dirent*)((char*)dp + dp->d_reclen))
if ( dp->d_off == loc ) if (dp->d_off == loc)
{ /* found it! */ { /* found it! */
dirp->dd_loc = dirp->dd_loc = (char*)dp - dirp->dd_buf;
(char *)dp - dirp->dd_buf;
return; return;
}
rewind = false; /* no point in backing up later */
dirp->dd_loc = dirp->dd_size; /* set end of buffer */
}
else /* whole buffer past matching entry */
if ( !rewind )
{ /* no point in searching further */
errno = EINVAL;
return; /* no entry at specified loc */
} }
else { /* rewind directory and start over */
rewind = false; /* but only once! */
dirp->dd_loc = dirp->dd_size = 0; rewind = false; /* no point in backing up later */
dirp->dd_loc = dirp->dd_size; /* set end of buffer */
if ( _lseek( dirp->dd_fd, (off_t)0, SEEK_SET )
!= 0
)
return; /* errno already set (EBADF) */
if ( loc == 0 )
return; /* save time */
}
} }
else /* whole buffer past matching entry */
if (!rewind)
{ /* no point in searching further */
errno = EINVAL;
return; /* no entry at specified loc */
}
else
{ /* rewind directory and start over */
rewind = false; /* but only once! */
dirp->dd_loc = dirp->dd_size = 0;
if (_lseek(dirp->dd_fd, (off_t)0, SEEK_SET)
!= 0)
return; /* errno already set (EBADF) */
if (loc == 0)
return; /* save time */
}
}
} }

View file

@ -3,44 +3,48 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <signal.h> #include <signal.h>
#include <setjmp.h> #include <setjmp.h>
int _alarm(int n); int _alarm(int n);
void _pause(void); void _pause(void);
static jmp_buf setjmpbuf; static jmp_buf setjmpbuf;
static void static void
alfun(int sig) alfun(int sig)
{ {
longjmp(setjmpbuf, 1); longjmp(setjmpbuf, 1);
} /* used with sleep() below */ } /* used with sleep() below */
void void sleep(int n)
sleep(int n)
{ {
/* sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt. */ /* sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt. */
unsigned oldalarm = 0; unsigned oldalarm = 0;
void (*oldsig)(int) = 0; void (*oldsig)(int) = 0;
if (n <= 0) return; if (n <= 0)
if (setjmp(setjmpbuf)) { return;
if (setjmp(setjmpbuf))
{
signal(SIGALRM, oldsig); signal(SIGALRM, oldsig);
_alarm(oldalarm); _alarm(oldalarm);
return; return;
} }
oldalarm = _alarm(5000); /* Who cares how long, as long oldalarm = _alarm(5000); /* Who cares how long, as long
* as it is long enough * as it is long enough
*/ */
if (oldalarm > n) oldalarm -= n; if (oldalarm > n)
else if (oldalarm) { oldalarm -= n;
else if (oldalarm)
{
n = oldalarm; n = oldalarm;
oldalarm = 1; oldalarm = 1;
} }
oldsig = signal(SIGALRM, alfun); oldsig = signal(SIGALRM, alfun);
_alarm(n); _alarm(n);
for (;;) { for (;;)
{
/* allow for other handlers ... */ /* allow for other handlers ... */
_pause(); _pause();
} }

View file

@ -7,28 +7,27 @@
practically impossible to do right. Avoid using them! practically impossible to do right. Avoid using them!
*/ */
#include <errno.h> #include <errno.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
extern off_t _lseek(int d, int offset, int whence); extern off_t _lseek(int d, int offset, int whence);
#ifndef SEEK_CUR #ifndef SEEK_CUR
#define SEEK_CUR 1 #define SEEK_CUR 1
#endif #endif
off_t off_t telldir(register DIR* dirp) /* return offset of next entry */
telldir(register DIR *dirp) /* return offset of next entry */
{ {
if ( dirp == NULL || dirp->dd_buf == NULL ) if (dirp == NULL || dirp->dd_buf == NULL)
{ {
errno = EFAULT; errno = EFAULT;
return -1; /* invalid pointer */ return -1; /* invalid pointer */
} }
if ( dirp->dd_loc < dirp->dd_size ) /* valid index */ if (dirp->dd_loc < dirp->dd_size) /* valid index */
return ((struct dirent *)&dirp->dd_buf[dirp->dd_loc])->d_off; return ((struct dirent*)&dirp->dd_buf[dirp->dd_loc])->d_off;
else /* beginning of next directory block */ else /* beginning of next directory block */
return _lseek( dirp->dd_fd, (off_t)0, SEEK_CUR ); return _lseek(dirp->dd_fd, (off_t)0, SEEK_CUR);
} }

View file

@ -3,37 +3,38 @@
*/ */
/* $Id$ */ /* $Id$ */
#if defined(_POSIX_SOURCE) #if defined(_POSIX_SOURCE)
/* This can't be done in setjmp.e, since SIG_SETMASK is defined in /* This can't be done in setjmp.e, since SIG_SETMASK is defined in
* <signal.h>. This is a C-file, which can't be included. * <signal.h>. This is a C-file, which can't be included.
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <stddef.h> #include <stddef.h>
int _sigprocmask(int, sigset_t *, sigset_t *); int _sigprocmask(int, sigset_t*, sigset_t*);
static void static void
__testsigset(void) { __testsigset(void)
{
/* This switch compiles when a sigset_t has the right size. */ /* This switch compiles when a sigset_t has the right size. */
switch(0) { switch (0)
case 0: {
case sizeof(sigset_t) <= sizeof(long): break; case 0:
case sizeof(sigset_t) <= sizeof(long):
break;
} }
} }
void void __newsigset(sigset_t* p)
__newsigset(sigset_t *p)
{ {
/* The SIG_SETMASK is not significant */ /* The SIG_SETMASK is not significant */
_sigprocmask(SIG_SETMASK, NULL, p); _sigprocmask(SIG_SETMASK, NULL, p);
} }
void void __oldsigset(sigset_t* p)
__oldsigset(sigset_t *p)
{ {
_sigprocmask(SIG_SETMASK, p, NULL); _sigprocmask(SIG_SETMASK, p, NULL);
} }
#endif /* _POSIX_SOURCE */ #endif /* _POSIX_SOURCE */

View file

@ -4,13 +4,12 @@
*/ */
/* $Id$ */ /* $Id$ */
#if defined(_POSIX_SOURCE) #if defined(_POSIX_SOURCE)
#include <sys/types.h> #include <sys/types.h>
#endif #endif
#include <signal.h> #include <signal.h>
int int raise(int sig)
raise(int sig)
{ {
if (sig < 0 || sig > _NSIG) if (sig < 0 || sig > _NSIG)
return -1; return -1;

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
void void(clearerr)(FILE* stream)
(clearerr)(FILE *stream)
{ {
clearerr(stream); clearerr(stream);
} }

View file

@ -3,24 +3,24 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
struct __iobuf __stdin = { struct __iobuf __stdin = {
0, 0, _IOREAD, 0, 0, 0, _IOREAD, 0,
(unsigned char *)NULL, (unsigned char *)NULL, (unsigned char*)NULL, (unsigned char*)NULL,
}; };
struct __iobuf __stdout = { struct __iobuf __stdout = {
0, 1, _IOWRITE, 0, 0, 1, _IOWRITE, 0,
(unsigned char *)NULL, (unsigned char *)NULL, (unsigned char*)NULL, (unsigned char*)NULL,
}; };
struct __iobuf __stderr = { struct __iobuf __stderr = {
0, 2, _IOWRITE | _IOLBF, 0, 0, 2, _IOWRITE | _IOLBF, 0,
(unsigned char *)NULL, (unsigned char *)NULL, (unsigned char*)NULL, (unsigned char*)NULL,
}; };
FILE *__iotab[FOPEN_MAX] = { FILE* __iotab[FOPEN_MAX] = {
&__stdin, &__stdin,
&__stdout, &__stdout,
&__stderr, &__stderr,

View file

@ -3,25 +3,29 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include "loc_incl.h" #include "loc_incl.h"
/* gnum() is used to get the width and precision fields of a format. */ /* gnum() is used to get the width and precision fields of a format. */
static const char * static const char*
gnum(register const char *f, int *ip, va_list *app) gnum(register const char* f, int* ip, va_list* app)
{ {
register int i, c; register int i, c;
if (*f == '*') { if (*f == '*')
{
*ip = va_arg((*app), int); *ip = va_arg((*app), int);
f++; f++;
} else { }
else
{
i = 0; i = 0;
while ((c = *f - '0') >= 0 && c <= 9) { while ((c = *f - '0') >= 0 && c <= 9)
i = i*10 + c; {
i = i * 10 + c;
f++; f++;
} }
*ip = i; *ip = i;
@ -29,91 +33,121 @@ gnum(register const char *f, int *ip, va_list *app)
return f; return f;
} }
#if _EM_WSIZE == _EM_PSIZE #if _EM_WSIZE == _EM_PSIZE
#define set_pointer(flags) /* nothing */ #define set_pointer(flags) /* nothing */
#elif _EM_LSIZE == _EM_PSIZE #elif _EM_LSIZE == _EM_PSIZE
#define set_pointer(flags) (flags |= FL_LONG) #define set_pointer(flags) (flags |= FL_LONG)
#else #else
#error garbage pointer size #error garbage pointer size
#define set_pointer(flags) /* compilation might continue */ #define set_pointer(flags) /* compilation might continue */
#endif #endif
#define PUTC(c) \ #define PUTC(c) \
do { \ do \
{ \
int i = putc(c, stream); \ int i = putc(c, stream); \
if (i == EOF) \ if (i == EOF) \
{ \ { \
if (ferror(stream)) \ if (ferror(stream)) \
return -1; \ return -1; \
} \ } \
} while (0) } while (0)
/* print an ordinal number */ /* print an ordinal number */
static char * static char*
o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed) o_print(va_list* ap, int flags, char* s, char c, int precision, int is_signed)
{ {
long signed_val; long signed_val;
unsigned long unsigned_val; unsigned long unsigned_val;
char *old_s = s; char* old_s = s;
int base; int base;
switch (flags & (FL_SHORT | FL_LONG)) { switch (flags & (FL_SHORT | FL_LONG))
case FL_SHORT: {
if (is_signed) { case FL_SHORT:
signed_val = (short) va_arg(*ap, int); if (is_signed)
} else { {
unsigned_val = (unsigned short) va_arg(*ap, unsigned); signed_val = (short)va_arg(*ap, int);
} }
break; else
case FL_LONG: {
if (is_signed) { unsigned_val = (unsigned short)va_arg(*ap, unsigned);
signed_val = va_arg(*ap, long); }
} else { break;
unsigned_val = va_arg(*ap, unsigned long); case FL_LONG:
} if (is_signed)
break; {
default: signed_val = va_arg(*ap, long);
if (is_signed) { }
signed_val = va_arg(*ap, int); else
} else { {
unsigned_val = va_arg(*ap, unsigned int); unsigned_val = va_arg(*ap, unsigned long);
} }
break; break;
default:
if (is_signed)
{
signed_val = va_arg(*ap, int);
}
else
{
unsigned_val = va_arg(*ap, unsigned int);
}
break;
} }
if (is_signed) { if (is_signed)
if (signed_val < 0) { {
if (signed_val < 0)
{
*s++ = '-'; *s++ = '-';
signed_val = -signed_val; signed_val = -signed_val;
} else if (flags & FL_SIGN) *s++ = '+'; }
else if (flags & FL_SPACE) *s++ = ' '; else if (flags & FL_SIGN)
*s++ = '+';
else if (flags & FL_SPACE)
*s++ = ' ';
unsigned_val = signed_val; unsigned_val = signed_val;
} }
if ((flags & FL_ALT) && (c == 'o')) *s++ = '0'; if ((flags & FL_ALT) && (c == 'o'))
if (!unsigned_val) { *s++ = '0';
if (!precision) if (!unsigned_val)
{
if (!precision)
return s; return s;
} else if (((flags & FL_ALT) && (c == 'x' || c == 'X')) }
|| c == 'p') { else if (((flags & FL_ALT) && (c == 'x' || c == 'X'))
|| c == 'p')
{
*s++ = '0'; *s++ = '0';
*s++ = (c == 'X' ? 'X' : 'x'); *s++ = (c == 'X' ? 'X' : 'x');
} }
switch (c) { switch (c)
case 'b': base = 2; break; {
case 'o': base = 8; break; case 'b':
case 'd': base = 2;
case 'i': break;
case 'u': base = 10; break; case 'o':
case 'x': base = 8;
case 'X': break;
case 'p': base = 16; break; case 'd':
case 'i':
case 'u':
base = 10;
break;
case 'x':
case 'X':
case 'p':
base = 16;
break;
} }
s = _i_compute(unsigned_val, base, s, precision); s = _i_compute(unsigned_val, base, s, precision);
if (c == 'X') if (c == 'X')
while (old_s != s) { while (old_s != s)
{
*old_s = toupper(*old_s); *old_s = toupper(*old_s);
old_s++; old_s++;
} }
@ -121,20 +155,22 @@ o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
return s; return s;
} }
int int _doprnt(register const char* fmt, va_list ap, FILE* stream)
_doprnt(register const char *fmt, va_list ap, FILE *stream)
{ {
register char *s; register char* s;
register int j; register int j;
int i, c, width, precision, zfill, flags, between_fill; int i, c, width, precision, zfill, flags, between_fill;
int nrchars=0; int nrchars = 0;
const char *oldfmt; const char* oldfmt;
char *s1, buf[1025]; char *s1, buf[1025];
while (c = *fmt++) { while (c = *fmt++)
if (c != '%') { {
#ifdef CPM if (c != '%')
if (c == '\n') { {
#ifdef CPM
if (c == '\n')
{
PUTC('\r'); PUTC('\r');
} }
#endif #endif
@ -143,125 +179,164 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
continue; continue;
} }
flags = 0; flags = 0;
do { do
switch(*fmt) { {
case '-': flags |= FL_LJUST; break; switch (*fmt)
case '+': flags |= FL_SIGN; break; {
case ' ': flags |= FL_SPACE; break; case '-':
case '#': flags |= FL_ALT; break; flags |= FL_LJUST;
case '0': flags |= FL_ZEROFILL; break; break;
default: flags |= FL_NOMORE; continue; case '+':
flags |= FL_SIGN;
break;
case ' ':
flags |= FL_SPACE;
break;
case '#':
flags |= FL_ALT;
break;
case '0':
flags |= FL_ZEROFILL;
break;
default:
flags |= FL_NOMORE;
continue;
} }
fmt++; fmt++;
} while(!(flags & FL_NOMORE)); } while (!(flags & FL_NOMORE));
oldfmt = fmt; oldfmt = fmt;
fmt = gnum(fmt, &width, &ap); fmt = gnum(fmt, &width, &ap);
if (fmt != oldfmt) flags |= FL_WIDTHSPEC; if (fmt != oldfmt)
flags |= FL_WIDTHSPEC;
if (*fmt == '.') { if (*fmt == '.')
fmt++; oldfmt = fmt; {
fmt++;
oldfmt = fmt;
fmt = gnum(fmt, &precision, &ap); fmt = gnum(fmt, &precision, &ap);
if (precision >= 0) flags |= FL_PRECSPEC; if (precision >= 0)
flags |= FL_PRECSPEC;
} }
if ((flags & FL_WIDTHSPEC) && width < 0) { if ((flags & FL_WIDTHSPEC) && width < 0)
{
width = -width; width = -width;
flags |= FL_LJUST; flags |= FL_LJUST;
} }
if (!(flags & FL_WIDTHSPEC)) width = 0; if (!(flags & FL_WIDTHSPEC))
width = 0;
if (flags & FL_SIGN) flags &= ~FL_SPACE; if (flags & FL_SIGN)
flags &= ~FL_SPACE;
if (flags & FL_LJUST) flags &= ~FL_ZEROFILL;
if (flags & FL_LJUST)
flags &= ~FL_ZEROFILL;
s = s1 = buf; s = s1 = buf;
switch (*fmt) { switch (*fmt)
case 'h': flags |= FL_SHORT; fmt++; break; {
case 'l': flags |= FL_LONG; fmt++; break; case 'h':
case 'L': flags |= FL_LONGDOUBLE; fmt++; break; flags |= FL_SHORT;
fmt++;
break;
case 'l':
flags |= FL_LONG;
fmt++;
break;
case 'L':
flags |= FL_LONGDOUBLE;
fmt++;
break;
} }
switch (c = *fmt++) { switch (c = *fmt++)
default: {
#ifdef CPM default:
if (c == '\n') { #ifdef CPM
PUTC('\r'); if (c == '\n')
nrchars++; {
} PUTC('\r');
nrchars++;
}
#endif #endif
PUTC(c); PUTC(c);
nrchars++; nrchars++;
continue; continue;
case 'n': case 'n':
if (flags & FL_SHORT) if (flags & FL_SHORT)
*va_arg(ap, short *) = (short) nrchars; *va_arg(ap, short*) = (short)nrchars;
else if (flags & FL_LONG) else if (flags & FL_LONG)
*va_arg(ap, long *) = (long) nrchars; *va_arg(ap, long*) = (long)nrchars;
else else
*va_arg(ap, int *) = (int) nrchars; *va_arg(ap, int*) = (int)nrchars;
continue; continue;
case 's': case 's':
s1 = va_arg(ap, char *); s1 = va_arg(ap, char*);
if (s1 == NULL) if (s1 == NULL)
s1 = "(null)"; s1 = "(null)";
s = s1; s = s1;
while (precision || !(flags & FL_PRECSPEC)) { while (precision || !(flags & FL_PRECSPEC))
if (*s == '\0') {
break; if (*s == '\0')
s++; break;
precision--; s++;
} precision--;
break; }
case 'p': break;
set_pointer(flags); case 'p':
set_pointer(flags);
/* fallthrough */ /* fallthrough */
case 'b': case 'b':
case 'o': case 'o':
case 'u': case 'u':
case 'x': case 'x':
case 'X': case 'X':
if (!(flags & FL_PRECSPEC)) precision = 1; if (!(flags & FL_PRECSPEC))
else if (c != 'p') flags &= ~FL_ZEROFILL; precision = 1;
s = o_print(&ap, flags, s, c, precision, 0); else if (c != 'p')
break; flags &= ~FL_ZEROFILL;
case 'd': s = o_print(&ap, flags, s, c, precision, 0);
case 'i': break;
flags |= FL_SIGNEDCONV; case 'd':
if (!(flags & FL_PRECSPEC)) precision = 1; case 'i':
else flags &= ~FL_ZEROFILL; flags |= FL_SIGNEDCONV;
s = o_print(&ap, flags, s, c, precision, 1); if (!(flags & FL_PRECSPEC))
break; precision = 1;
case 'c': else
*s++ = va_arg(ap, int); flags &= ~FL_ZEROFILL;
break; s = o_print(&ap, flags, s, c, precision, 1);
break;
case 'c':
*s++ = va_arg(ap, int);
break;
#ifndef ACKCONF_NO_STDIO_FLOAT #ifndef ACKCONF_NO_STDIO_FLOAT
case 'G': case 'G':
case 'g': case 'g':
if ((flags & FL_PRECSPEC) && (precision == 0)) if ((flags & FL_PRECSPEC) && (precision == 0))
precision = 1; precision = 1;
case 'f': case 'f':
case 'E': case 'E':
case 'e': case 'e':
if (!(flags & FL_PRECSPEC)) if (!(flags & FL_PRECSPEC))
precision = 6; precision = 6;
if (precision >= sizeof(buf)) if (precision >= sizeof(buf))
precision = sizeof(buf) - 1; precision = sizeof(buf) - 1;
flags |= FL_SIGNEDCONV; flags |= FL_SIGNEDCONV;
s = _f_print(&ap, flags, s, c, precision); s = _f_print(&ap, flags, s, c, precision);
break; break;
#endif /* ACKCONF_NO_STDIO_FLOAT */ #endif /* ACKCONF_NO_STDIO_FLOAT */
case 'r': case 'r':
ap = va_arg(ap, va_list); ap = va_arg(ap, va_list);
fmt = va_arg(ap, char *); fmt = va_arg(ap, char*);
continue; continue;
} }
zfill = ' '; zfill = ' ';
if (flags & FL_ZEROFILL) zfill = '0'; if (flags & FL_ZEROFILL)
zfill = '0';
j = s - s1; j = s - s1;
/* between_fill is true under the following conditions: /* between_fill is true under the following conditions:
@ -274,35 +349,45 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
between_fill = 0; between_fill = 0;
if ((flags & FL_ZEROFILL) if ((flags & FL_ZEROFILL)
&& (((c == 'x' || c == 'X') && (flags & FL_ALT)) && (((c == 'x' || c == 'X') && (flags & FL_ALT))
|| (c == 'p') || (c == 'p')
|| ((flags & FL_SIGNEDCONV) || ((flags & FL_SIGNEDCONV)
&& ( *s1 == '+' || *s1 == '-' || *s1 == ' ')))) && (*s1 == '+' || *s1 == '-' || *s1 == ' '))))
between_fill++; between_fill++;
if ((i = width - j) > 0) if ((i = width - j) > 0)
if (!(flags & FL_LJUST)) { /* right justify */ if (!(flags & FL_LJUST))
{ /* right justify */
nrchars += i; nrchars += i;
if (between_fill) { if (between_fill)
if (flags & FL_SIGNEDCONV) { {
j--; nrchars++; if (flags & FL_SIGNEDCONV)
PUTC(*s1++); {
} else { j--;
j -= 2; nrchars += 2; nrchars++;
PUTC(*s1++); PUTC(*s1++);
PUTC(*s1++); }
else
{
j -= 2;
nrchars += 2;
PUTC(*s1++);
PUTC(*s1++);
} }
} }
do { do
{
PUTC(zfill); PUTC(zfill);
} while (--i); } while (--i);
} }
nrchars += j; nrchars += j;
while (--j >= 0) { while (--j >= 0)
{
PUTC(*s1++); PUTC(*s1++);
} }
if (i > 0) nrchars += i; if (i > 0)
nrchars += i;
while (--i >= 0) while (--i >= 0)
PUTC(zfill); PUTC(zfill);
} }

View file

@ -3,26 +3,26 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
#if _EM_WSIZE == _EM_PSIZE #if _EM_WSIZE == _EM_PSIZE
#define set_pointer(flags) /* nothing */ #define set_pointer(flags) /* nothing */
#elif _EM_LSIZE == _EM_PSIZE #elif _EM_LSIZE == _EM_PSIZE
#define set_pointer(flags) (flags |= FL_LONG) #define set_pointer(flags) (flags |= FL_LONG)
#else #else
#error garbage pointer size #error garbage pointer size
#define set_pointer(flags) /* compilation might continue */ #define set_pointer(flags) /* compilation might continue */
#endif #endif
#define NUMLEN 512 #define NUMLEN 512
#define NR_CHARS 256 #define NR_CHARS 256
static char Xtable[NR_CHARS]; static char Xtable[NR_CHARS];
static char inp_buf[NUMLEN]; static char inp_buf[NUMLEN];
/* Collect a number of characters which constitite an ordinal number. /* Collect a number of characters which constitite an ordinal number.
* When the type is 'i', the base can be 8, 10, or 16, depending on the * When the type is 'i', the base can be 8, 10, or 16, depending on the
@ -30,65 +30,85 @@ static char inp_buf[NUMLEN];
* according to the format of the number. At the end of the function, base * according to the format of the number. At the end of the function, base
* is then set to 0, so strtol() will get the right argument. * is then set to 0, so strtol() will get the right argument.
*/ */
static char * static char*
o_collect(register int c, register FILE *stream, char type, o_collect(register int c, register FILE* stream, char type,
int width, int *basep) int width, int* basep)
{ {
register char *bufp = inp_buf; register char* bufp = inp_buf;
register int base; register int base;
switch (type) { switch (type)
case 'i': /* i means octal, decimal or hexadecimal */ {
case 'p': case 'i': /* i means octal, decimal or hexadecimal */
case 'x': case 'p':
case 'X': base = 16; break; case 'x':
case 'd': case 'X':
case 'u': base = 10; break; base = 16;
case 'o': base = 8; break; break;
case 'b': base = 2; break; case 'd':
case 'u':
base = 10;
break;
case 'o':
base = 8;
break;
case 'b':
base = 2;
break;
} }
if (c == '-' || c == '+') { if (c == '-' || c == '+')
*bufp++ = c; {
if (--width)
c = getc(stream);
}
if (width && c == '0' && base == 16) {
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
if (c != 'x' && c != 'X') { }
if (type == 'i') base = 8;
if (width && c == '0' && base == 16)
{
*bufp++ = c;
if (--width)
c = getc(stream);
if (c != 'x' && c != 'X')
{
if (type == 'i')
base = 8;
} }
else if (width) { else if (width)
{
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
} }
} }
else if (type == 'i') base = 10; else if (type == 'i')
base = 10;
while (width) { while (width)
{
if (((base == 10) && isdigit(c)) if (((base == 10) && isdigit(c))
|| ((base == 16) && isxdigit(c)) || ((base == 16) && isxdigit(c))
|| ((base == 8) && isdigit(c) && (c < '8')) || ((base == 8) && isdigit(c) && (c < '8'))
|| ((base == 2) && isdigit(c) && (c < '2'))) { || ((base == 2) && isdigit(c) && (c < '2')))
{
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
} }
else break; else
break;
} }
if (width && c != EOF) ungetc(c, stream); if (width && c != EOF)
if (type == 'i') base = 0; ungetc(c, stream);
if (type == 'i')
base = 0;
*basep = base; *basep = base;
*bufp = '\0'; *bufp = '\0';
return bufp - 1; return bufp - 1;
} }
#ifndef ACKCONF_NO_STDIO_FLOAT #ifndef ACKCONF_NO_STDIO_FLOAT
/* The function f_collect() reads a string that has the format of a /* The function f_collect() reads a string that has the format of a
* floating-point number. The function returns as soon as a format-error * floating-point number. The function returns as soon as a format-error
* is encountered, leaving the offending character in the input. This means * is encountered, leaving the offending character in the input. This means
@ -97,353 +117,425 @@ o_collect(register int c, register FILE *stream, char type,
* not necessary, although the use of the width field can cause incomplete * not necessary, although the use of the width field can cause incomplete
* numbers to be passed to strtod(). (e.g. 1.3e+) * numbers to be passed to strtod(). (e.g. 1.3e+)
*/ */
static char * static char*
f_collect(register int c, register FILE *stream, register int width) f_collect(register int c, register FILE* stream, register int width)
{ {
register char *bufp = inp_buf; register char* bufp = inp_buf;
int digit_seen = 0; int digit_seen = 0;
if (c == '-' || c == '+') { if (c == '-' || c == '+')
{
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
} }
while (width && isdigit(c)) { while (width && isdigit(c))
{
digit_seen++; digit_seen++;
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
} }
if (width && c == '.') { if (width && c == '.')
*bufp++ = c; {
if(--width)
c = getc(stream);
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
if (--width)
c = getc(stream);
}
}
if (!digit_seen) {
if (width && c != EOF) ungetc(c, stream);
return inp_buf - 1;
}
else digit_seen = 0;
if (width && (c == 'e' || c == 'E')) {
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
if (width && (c == '+' || c == '-')) { while (width && isdigit(c))
*bufp++ = c; {
if (--width)
c = getc(stream);
}
while (width && isdigit(c)) {
digit_seen++; digit_seen++;
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
} }
if (!digit_seen) { }
if (width && c != EOF) ungetc(c,stream);
if (!digit_seen)
{
if (width && c != EOF)
ungetc(c, stream);
return inp_buf - 1;
}
else
digit_seen = 0;
if (width && (c == 'e' || c == 'E'))
{
*bufp++ = c;
if (--width)
c = getc(stream);
if (width && (c == '+' || c == '-'))
{
*bufp++ = c;
if (--width)
c = getc(stream);
}
while (width && isdigit(c))
{
digit_seen++;
*bufp++ = c;
if (--width)
c = getc(stream);
}
if (!digit_seen)
{
if (width && c != EOF)
ungetc(c, stream);
return inp_buf - 1; return inp_buf - 1;
} }
} }
if (width && c != EOF) ungetc(c, stream); if (width && c != EOF)
ungetc(c, stream);
*bufp = '\0'; *bufp = '\0';
return bufp - 1; return bufp - 1;
} }
#endif /* ACKCONF_NO_STDIO_FLOAT */ #endif /* ACKCONF_NO_STDIO_FLOAT */
/* /*
* the routine that does the scanning * the routine that does the scanning
*/ */
int int _doscan(register FILE* stream, const char* format, va_list ap)
_doscan(register FILE *stream, const char *format, va_list ap)
{ {
int done = 0; /* number of items done */ int done = 0; /* number of items done */
int nrchars = 0; /* number of characters read */ int nrchars = 0; /* number of characters read */
int conv = 0; /* # of conversions */ int conv = 0; /* # of conversions */
int base; /* conversion base */ int base; /* conversion base */
unsigned long val; /* an integer value */ unsigned long val; /* an integer value */
register char *str; /* temporary pointer */ register char* str; /* temporary pointer */
char *tmp_string; /* ditto */ char* tmp_string; /* ditto */
unsigned width; /* width of field */ unsigned width; /* width of field */
int flags; /* some flags */ int flags; /* some flags */
int reverse; /* reverse the checking in [...] */ int reverse; /* reverse the checking in [...] */
int kind; int kind;
register int ic; /* the input character */ register int ic; /* the input character */
#ifndef ACKCONF_NO_STDIO_FLOAT #ifndef ACKCONF_NO_STDIO_FLOAT
long double ld_val; long double ld_val;
#endif #endif
if (!*format) return 0; if (!*format)
return 0;
while (1) { while (1)
if (isspace(*format)) { {
if (isspace(*format))
{
while (isspace(*format)) while (isspace(*format))
format++; /* skip whitespace */ format++; /* skip whitespace */
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
while (isspace (ic)) { while (isspace(ic))
{
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
} }
if (ic != EOF) ungetc(ic,stream); if (ic != EOF)
ungetc(ic, stream);
nrchars--; nrchars--;
} }
if (!*format) break; /* end of format */ if (!*format)
break; /* end of format */
if (*format != '%') { if (*format != '%')
{
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
if (ic != *format++) { if (ic != *format++)
if (ic != EOF) ungetc(ic,stream); {
if (ic != EOF)
ungetc(ic, stream);
nrchars--; nrchars--;
break; /* error */ break; /* error */
} }
continue; continue;
} }
format++; format++;
if (*format == '%') { if (*format == '%')
{
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
if (ic == '%') { if (ic == '%')
{
format++; format++;
continue; continue;
} }
else break; else
break;
} }
flags = 0; flags = 0;
if (*format == '*') { if (*format == '*')
{
format++; format++;
flags |= FL_NOASSIGN; flags |= FL_NOASSIGN;
} }
if (isdigit (*format)) { if (isdigit(*format))
{
flags |= FL_WIDTHSPEC; flags |= FL_WIDTHSPEC;
for (width = 0; isdigit (*format);) for (width = 0; isdigit(*format);)
width = width * 10 + *format++ - '0'; width = width * 10 + *format++ - '0';
} }
switch (*format) { switch (*format)
case 'h': flags |= FL_SHORT; format++; break; {
case 'l': flags |= FL_LONG; format++; break; case 'h':
case 'L': flags |= FL_LONGDOUBLE; format++; break; flags |= FL_SHORT;
format++;
break;
case 'l':
flags |= FL_LONG;
format++;
break;
case 'L':
flags |= FL_LONGDOUBLE;
format++;
break;
} }
kind = *format; kind = *format;
if ((kind != 'c') && (kind != '[') && (kind != 'n')) { if ((kind != 'c') && (kind != '[') && (kind != 'n'))
do { {
do
{
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
} while (isspace(ic)); } while (isspace(ic));
if (ic == EOF) break; /* outer while */ if (ic == EOF)
} else if (kind != 'n') { /* %c or %[ */ break; /* outer while */
}
else if (kind != 'n')
{ /* %c or %[ */
ic = getc(stream); ic = getc(stream);
if (ic == EOF) break; /* outer while */ if (ic == EOF)
break; /* outer while */
nrchars++; nrchars++;
} }
switch (kind) { switch (kind)
default: {
/* not recognized, like %q */ default:
return conv || (ic != EOF) ? done : EOF; /* not recognized, like %q */
break; return conv || (ic != EOF) ? done : EOF;
case 'n': break;
if (!(flags & FL_NOASSIGN)) { /* silly, though */ case 'n':
if (flags & FL_SHORT) if (!(flags & FL_NOASSIGN))
*va_arg(ap, short *) = (short) nrchars; { /* silly, though */
else if (flags & FL_LONG) if (flags & FL_SHORT)
*va_arg(ap, long *) = (long) nrchars; *va_arg(ap, short*) = (short)nrchars;
else else if (flags & FL_LONG)
*va_arg(ap, int *) = (int) nrchars; *va_arg(ap, long*) = (long)nrchars;
} else
break; *va_arg(ap, int*) = (int)nrchars;
case 'p': /* pointer */ }
set_pointer(flags); break;
case 'p': /* pointer */
set_pointer(flags);
/* fallthrough */ /* fallthrough */
case 'b': /* binary */ case 'b': /* binary */
case 'd': /* decimal */ case 'd': /* decimal */
case 'i': /* general integer */ case 'i': /* general integer */
case 'o': /* octal */ case 'o': /* octal */
case 'u': /* unsigned */ case 'u': /* unsigned */
case 'x': /* hexadecimal */ case 'x': /* hexadecimal */
case 'X': /* ditto */ case 'X': /* ditto */
if (!(flags & FL_WIDTHSPEC) || width > NUMLEN) if (!(flags & FL_WIDTHSPEC) || width > NUMLEN)
width = NUMLEN; width = NUMLEN;
if (!width) return done; if (!width)
return done;
str = o_collect(ic, stream, kind, width, &base); str = o_collect(ic, stream, kind, width, &base);
if (str < inp_buf if (str < inp_buf
|| (str == inp_buf || (str == inp_buf
&& (*str == '-' && (*str == '-'
|| *str == '+'))) return done; || *str == '+')))
return done;
/* /*
* Although the length of the number is str-inp_buf+1 * Although the length of the number is str-inp_buf+1
* we don't add the 1 since we counted it already * we don't add the 1 since we counted it already
*/ */
nrchars += str - inp_buf; nrchars += str - inp_buf;
if (!(flags & FL_NOASSIGN)) {
if (kind == 'd' || kind == 'i')
val = strtol(inp_buf, &tmp_string, base);
else
val = strtoul(inp_buf, &tmp_string, base);
if (flags & FL_LONG)
*va_arg(ap, unsigned long *) = (unsigned long) val;
else if (flags & FL_SHORT)
*va_arg(ap, unsigned short *) = (unsigned short) val;
else
*va_arg(ap, unsigned *) = (unsigned) val;
}
break;
case 'c':
if (!(flags & FL_WIDTHSPEC))
width = 1;
if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char *);
if (!width) return done;
while (width && ic != EOF) {
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*str++ = (char) ic; {
if (--width) { if (kind == 'd' || kind == 'i')
ic = getc(stream); val = strtol(inp_buf, &tmp_string, base);
nrchars++; else
val = strtoul(inp_buf, &tmp_string, base);
if (flags & FL_LONG)
*va_arg(ap, unsigned long*) = (unsigned long)val;
else if (flags & FL_SHORT)
*va_arg(ap, unsigned short*) = (unsigned short)val;
else
*va_arg(ap, unsigned*) = (unsigned)val;
} }
} break;
case 'c':
if (width) { if (!(flags & FL_WIDTHSPEC))
if (ic != EOF) ungetc(ic,stream); width = 1;
nrchars--;
}
break;
case 's':
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char *);
if (!width) return done;
while (width && ic != EOF && !isspace(ic)) {
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*str++ = (char) ic; str = va_arg(ap, char*);
if (--width) { if (!width)
ic = getc(stream); return done;
nrchars++;
}
}
/* terminate the string */
if (!(flags & FL_NOASSIGN))
*str = '\0';
if (width) {
if (ic != EOF) ungetc(ic,stream);
nrchars--;
}
break;
case '[':
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!width) return done;
if ( *++format == '^' ) { while (width && ic != EOF)
reverse = 1; {
format++; if (!(flags & FL_NOASSIGN))
} else *str++ = (char)ic;
reverse = 0; if (--width)
{
for (str = Xtable; str < &Xtable[NR_CHARS] ic = getc(stream);
; str++) nrchars++;
*str = 0;
if (*format == ']') Xtable[*format++] = 1;
while (*format && *format != ']') {
Xtable[*format++] = 1;
if (*format == '-') {
format++;
if (*format
&& *format != ']'
&& *(format) >= *(format -2)) {
int c;
for( c = *(format -2) + 1
; c <= *format ; c++)
Xtable[c] = 1;
format++;
} }
else Xtable['-'] = 1;
} }
}
if (!*format || !(Xtable[ic] ^ reverse)) {
if (ic != EOF) ungetc(ic, stream);
return done;
}
if (!(flags & FL_NOASSIGN)) if (width)
str = va_arg(ap, char *); {
if (ic != EOF)
do { ungetc(ic, stream);
nrchars--;
}
break;
case 's':
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*str++ = (char) ic; str = va_arg(ap, char*);
if (--width) { if (!width)
ic = getc(stream); return done;
nrchars++;
while (width && ic != EOF && !isspace(ic))
{
if (!(flags & FL_NOASSIGN))
*str++ = (char)ic;
if (--width)
{
ic = getc(stream);
nrchars++;
}
} }
} while (width && ic != EOF && (Xtable[ic] ^ reverse)); /* terminate the string */
if (!(flags & FL_NOASSIGN))
*str = '\0';
if (width)
{
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
}
break;
case '[':
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!width)
return done;
if (width) { if (*++format == '^')
if (ic != EOF) ungetc(ic, stream); {
nrchars--; reverse = 1;
} format++;
if (!(flags & FL_NOASSIGN)) { /* terminate string */ }
*str = '\0'; else
} reverse = 0;
break;
#ifndef ACKCONF_NO_STDIO_FLOAT
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (!(flags & FL_WIDTHSPEC) || width > NUMLEN)
width = NUMLEN;
if (!width) return done; for (str = Xtable; str < &Xtable[NR_CHARS]; str++)
str = f_collect(ic, stream, width); *str = 0;
if (str < inp_buf if (*format == ']')
|| (str == inp_buf Xtable[*format++] = 1;
&& (*str == '-'
|| *str == '+'))) return done;
/* while (*format && *format != ']')
{
Xtable[*format++] = 1;
if (*format == '-')
{
format++;
if (*format
&& *format != ']'
&& *(format) >= *(format - 2))
{
int c;
for (c = *(format - 2) + 1; c <= *format; c++)
Xtable[c] = 1;
format++;
}
else
Xtable['-'] = 1;
}
}
if (!*format || !(Xtable[ic] ^ reverse))
{
if (ic != EOF)
ungetc(ic, stream);
return done;
}
if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char*);
do
{
if (!(flags & FL_NOASSIGN))
*str++ = (char)ic;
if (--width)
{
ic = getc(stream);
nrchars++;
}
} while (width && ic != EOF && (Xtable[ic] ^ reverse));
if (width)
{
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
}
if (!(flags & FL_NOASSIGN))
{ /* terminate string */
*str = '\0';
}
break;
#ifndef ACKCONF_NO_STDIO_FLOAT
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (!(flags & FL_WIDTHSPEC) || width > NUMLEN)
width = NUMLEN;
if (!width)
return done;
str = f_collect(ic, stream, width);
if (str < inp_buf
|| (str == inp_buf
&& (*str == '-'
|| *str == '+')))
return done;
/*
* Although the length of the number is str-inp_buf+1 * Although the length of the number is str-inp_buf+1
* we don't add the 1 since we counted it already * we don't add the 1 since we counted it already
*/ */
nrchars += str - inp_buf; nrchars += str - inp_buf;
if (!(flags & FL_NOASSIGN)) { if (!(flags & FL_NOASSIGN))
ld_val = strtod(inp_buf, &tmp_string); {
if (flags & FL_LONGDOUBLE) ld_val = strtod(inp_buf, &tmp_string);
*va_arg(ap, long double *) = (long double) ld_val; if (flags & FL_LONGDOUBLE)
else *va_arg(ap, long double*) = (long double)ld_val;
if (flags & FL_LONG) else if (flags & FL_LONG)
*va_arg(ap, double *) = (double) ld_val; *va_arg(ap, double*) = (double)ld_val;
else else
*va_arg(ap, float *) = (float) ld_val; *va_arg(ap, float*) = (float)ld_val;
} }
break; break;
#endif #endif
} /* end switch */ } /* end switch */
conv++; conv++;
if (!(flags & FL_NOASSIGN) && kind != 'n') done++; if (!(flags & FL_NOASSIGN) && kind != 'n')
done++;
format++; format++;
} }
return conv || (ic != EOF) ? done : EOF; return conv || (ic != EOF) ? done : EOF;

View file

@ -1,15 +1,15 @@
/* $Id$ */ /* $Id$ */
#include "loc_incl.h" #include "loc_incl.h"
#ifndef ACKCONF_NO_STDIO_FLOAT #ifndef ACKCONF_NO_STDIO_FLOAT
#include "../stdlib/ext_fmt.h" #include "../stdlib/ext_fmt.h"
void _dbl_ext_cvt(double value, struct EXTEND *e); void _dbl_ext_cvt(double value, struct EXTEND* e);
char *_ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int * sign, int ecvtflag); char* _ext_str_cvt(struct EXTEND* e, int ndigit, int* decpt, int* sign, int ecvtflag);
static char * static char*
cvt(long double value, int ndigit, int *decpt, int *sign, int ecvtflag) cvt(long double value, int ndigit, int* decpt, int* sign, int ecvtflag)
{ {
struct EXTEND e; struct EXTEND e;
@ -17,17 +17,15 @@ cvt(long double value, int ndigit, int *decpt, int *sign, int ecvtflag)
return _ext_str_cvt(&e, ndigit, decpt, sign, ecvtflag); return _ext_str_cvt(&e, ndigit, decpt, sign, ecvtflag);
} }
char * char* _ecvt(long double value, int ndigit, int* decpt, int* sign)
_ecvt(long double value, int ndigit, int *decpt, int *sign)
{ {
return cvt(value, ndigit, decpt, sign, 1); return cvt(value, ndigit, decpt, sign, 1);
} }
char * char* _fcvt(long double value, int ndigit, int* decpt, int* sign)
_fcvt(long double value, int ndigit, int *decpt, int *sign)
{ {
return cvt(value, ndigit, decpt, sign, 0); return cvt(value, ndigit, decpt, sign, 0);
} }
#endif /* ACKCONF_NO_STDIO_FLOAT */ #endif /* ACKCONF_NO_STDIO_FLOAT */

View file

@ -8,23 +8,25 @@
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
int int fclose(FILE* fp)
fclose(FILE *fp)
{ {
register int i, retval = 0; register int i, retval = 0;
for (i=0; i<FOPEN_MAX; i++) for (i = 0; i < FOPEN_MAX; i++)
if (fp == __iotab[i]) { if (fp == __iotab[i])
{
__iotab[i] = 0; __iotab[i] = 0;
break; break;
} }
if (i >= FOPEN_MAX) if (i >= FOPEN_MAX)
return EOF; return EOF;
if (fflush(fp)) retval = EOF; if (fflush(fp))
if (close(fileno(fp))) retval = EOF; retval = EOF;
if ( io_testflag(fp,_IOMYBUF) && fp->_buf ) if (close(fileno(fp)))
free((void *)fp->_buf); retval = EOF;
if (io_testflag(fp, _IOMYBUF) && fp->_buf)
free((void*)fp->_buf);
if (fp != stdin && fp != stdout && fp != stderr) if (fp != stdin && fp != stdout && fp != stderr)
free((void *)fp); free((void*)fp);
return retval; return retval;
} }

View file

@ -3,50 +3,54 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "../stdio/loc_incl.h" #include "../stdio/loc_incl.h"
FILE * FILE* fdopen(int fd, const char* mode)
fdopen(int fd, const char *mode)
{ {
register int i; register int i;
FILE *stream; FILE* stream;
int flags = 0; int flags = 0;
if (fd < 0) return (FILE *)NULL; if (fd < 0)
for (i = 0; __iotab[i] != 0 ; i++) return (FILE*)NULL;
if (i >= FOPEN_MAX-1) for (i = 0; __iotab[i] != 0; i++)
return (FILE *)NULL; if (i >= FOPEN_MAX - 1)
return (FILE*)NULL;
switch(*mode++) { switch (*mode++)
case 'r': {
flags |= _IOREAD | _IOREADING; case 'r':
break; flags |= _IOREAD | _IOREADING;
case 'a':
flags |= _IOAPPEND;
case 'w':
flags |= _IOWRITE | _IOWRITING;
break;
default:
return (FILE *)NULL;
}
while(*mode) {
switch(*mode++) {
case 'b':
continue;
case '+':
flags |= _IOREAD | _IOWRITE;
continue;
/* The sequence may be followed by aditional characters */
default:
break; break;
case 'a':
flags |= _IOAPPEND;
case 'w':
flags |= _IOWRITE | _IOWRITING;
break;
default:
return (FILE*)NULL;
}
while (*mode)
{
switch (*mode++)
{
case 'b':
continue;
case '+':
flags |= _IOREAD | _IOWRITE;
continue;
/* The sequence may be followed by aditional characters */
default:
break;
} }
break; break;
} }
if ((stream = (FILE *) malloc(sizeof(FILE))) == NULL) { if ((stream = (FILE*)malloc(sizeof(FILE))) == NULL)
return (FILE *)NULL; {
return (FILE*)NULL;
} }
if ((flags & _IOREAD) && (flags & _IOWRITE)) if ((flags & _IOREAD) && (flags & _IOWRITE))

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int(feof)(FILE* stream)
(feof)(FILE *stream)
{ {
return feof(stream); return feof(stream);
} }

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int(ferror)(FILE* stream)
(ferror)(FILE *stream)
{ {
return ferror(stream); return ferror(stream);
} }

View file

@ -8,67 +8,71 @@
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
int int fflush(FILE* stream)
fflush(FILE *stream)
{ {
int count, c1, i, retval = 0; int count, c1, i, retval = 0;
if (!stream) { if (!stream)
for(i= 0; i < FOPEN_MAX; i++) {
if (__iotab[i] && fflush(__iotab[i])) for (i = 0; i < FOPEN_MAX; i++)
retval = EOF; if (__iotab[i] && fflush(__iotab[i]))
return retval; retval = EOF;
return retval;
} }
if (!stream->_buf if (!stream->_buf
|| (!io_testflag(stream, _IOREADING) || (!io_testflag(stream, _IOREADING)
&& !io_testflag(stream, _IOWRITING))) && !io_testflag(stream, _IOWRITING)))
return 0; return 0;
if (io_testflag(stream, _IOREADING)) { if (io_testflag(stream, _IOREADING))
{
/* (void) fseek(stream, 0L, SEEK_CUR); */ /* (void) fseek(stream, 0L, SEEK_CUR); */
int adjust = 0; int adjust = 0;
if (stream->_buf && !io_testflag(stream,_IONBF)) if (stream->_buf && !io_testflag(stream, _IONBF))
adjust = stream->_count; adjust = stream->_count;
stream->_count = 0; stream->_count = 0;
lseek(fileno(stream), (off_t) adjust, SEEK_CUR); lseek(fileno(stream), (off_t)adjust, SEEK_CUR);
if (io_testflag(stream, _IOWRITE)) if (io_testflag(stream, _IOWRITE))
stream->_flags &= ~(_IOREADING | _IOWRITING); stream->_flags &= ~(_IOREADING | _IOWRITING);
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
return 0; return 0;
} else if (io_testflag(stream, _IONBF)) return 0; }
else if (io_testflag(stream, _IONBF))
return 0;
if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */ if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
stream->_flags &= ~_IOWRITING; stream->_flags &= ~_IOWRITING;
count = stream->_ptr - stream->_buf; count = stream->_ptr - stream->_buf;
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
if ( count <= 0 ) if (count <= 0)
return 0; return 0;
if (io_testflag(stream, _IOAPPEND)) { if (io_testflag(stream, _IOAPPEND))
if (lseek(fileno(stream), 0L, SEEK_END) == -1) { {
if (lseek(fileno(stream), 0L, SEEK_END) == -1)
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
} }
c1 = write(stream->_fd, (char *)stream->_buf, count); c1 = write(stream->_fd, (char*)stream->_buf, count);
stream->_count = 0; stream->_count = 0;
if ( count == c1 ) if (count == c1)
return 0; return 0;
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
void void __cleanup(void)
__cleanup(void)
{ {
register int i; register int i;
for(i= 0; i < FOPEN_MAX; i++) for (i = 0; i < FOPEN_MAX; i++)
if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING)) if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
(void) fflush(__iotab[i]); (void)fflush(__iotab[i]);
} }

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int fgetc(FILE* stream)
fgetc(FILE *stream)
{ {
return getc(stream); return getc(stream);
} }

View file

@ -3,12 +3,12 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int fgetpos(FILE* stream, fpos_t* pos)
fgetpos(FILE *stream, fpos_t *pos)
{ {
*pos = ftell(stream); *pos = ftell(stream);
if (*pos == -1) return -1; if (*pos == -1)
return -1;
return 0; return 0;
} }

View file

@ -3,24 +3,29 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
char * char* fgets(char* s, register int n, register FILE* stream)
fgets(char *s, register int n, register FILE *stream)
{ {
register int ch; register int ch;
register char *ptr; register char* ptr;
ptr = s; ptr = s;
while (--n > 0 && (ch = getc(stream)) != EOF) { while (--n > 0 && (ch = getc(stream)) != EOF)
{
*ptr++ = ch; *ptr++ = ch;
if ( ch == '\n') if (ch == '\n')
break; break;
} }
if (ch == EOF) { if (ch == EOF)
if (feof(stream)) { {
if (ptr == s) return NULL; if (feof(stream))
} else return NULL; {
if (ptr == s)
return NULL;
}
else
return NULL;
} }
*ptr = '\0'; *ptr = '\0';
return s; return s;

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int(fileno)(FILE* stream)
(fileno)(FILE *stream)
{ {
return stream->_fd; return stream->_fd;
} }

View file

@ -8,20 +8,23 @@
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
int int __fillbuf(register FILE* stream)
__fillbuf(register FILE *stream)
{ {
static unsigned char ch[FOPEN_MAX]; static unsigned char ch[FOPEN_MAX];
register int i; register int i;
stream->_count = 0; stream->_count = 0;
if (fileno(stream) < 0) return EOF; if (fileno(stream) < 0)
if (io_testflag(stream, _IOEOF)) return EOF; return EOF;
if (!io_testflag(stream, _IOREAD)) { if (io_testflag(stream, _IOEOF))
return EOF;
if (!io_testflag(stream, _IOREAD))
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
if (io_testflag(stream, _IOWRITING)) { if (io_testflag(stream, _IOWRITING))
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
@ -29,33 +32,40 @@ __fillbuf(register FILE *stream)
if (!io_testflag(stream, _IOREADING)) if (!io_testflag(stream, _IOREADING))
stream->_flags |= _IOREADING; stream->_flags |= _IOREADING;
if (!io_testflag(stream, _IONBF) && !stream->_buf) { if (!io_testflag(stream, _IONBF) && !stream->_buf)
stream->_buf = (unsigned char *) malloc(BUFSIZ); {
if (!stream->_buf) { stream->_buf = (unsigned char*)malloc(BUFSIZ);
if (!stream->_buf)
{
stream->_flags |= _IONBF; stream->_flags |= _IONBF;
} }
else { else
{
stream->_flags |= _IOMYBUF; stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ; stream->_bufsiz = BUFSIZ;
} }
} }
/* flush line-buffered output when filling an input buffer */ /* flush line-buffered output when filling an input buffer */
for (i = 0; i < FOPEN_MAX; i++) { for (i = 0; i < FOPEN_MAX; i++)
{
if (__iotab[i] && io_testflag(__iotab[i], _IOLBF)) if (__iotab[i] && io_testflag(__iotab[i], _IOLBF))
if (io_testflag(__iotab[i], _IOWRITING)) if (io_testflag(__iotab[i], _IOWRITING))
(void) fflush(__iotab[i]); (void)fflush(__iotab[i]);
} }
if (!stream->_buf) { if (!stream->_buf)
{
stream->_buf = &ch[fileno(stream)]; stream->_buf = &ch[fileno(stream)];
stream->_bufsiz = 1; stream->_bufsiz = 1;
} }
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
stream->_count = read(stream->_fd, (char *)stream->_buf, stream->_bufsiz); stream->_count = read(stream->_fd, (char*)stream->_buf, stream->_bufsiz);
if (stream->_count <= 0){ if (stream->_count <= 0)
if (stream->_count == 0) { {
if (stream->_count == 0)
{
stream->_flags |= _IOEOF; stream->_flags |= _IOEOF;
} }
else else

View file

@ -3,16 +3,16 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
#ifndef ACKCONF_NO_STDIO_FLOAT #ifndef ACKCONF_NO_STDIO_FLOAT
static char * static char*
_pfloat(long double r, register char *s, int n, int flags) _pfloat(long double r, register char* s, int n, int flags)
{ {
register char *s1; register char* s1;
int sign, dp; int sign, dp;
register int i; register int i;
@ -24,29 +24,34 @@ _pfloat(long double r, register char *s, int n, int flags)
else if (flags & FL_SPACE) else if (flags & FL_SPACE)
*s++ = ' '; *s++ = ' ';
if (dp<=0) if (dp <= 0)
*s++ = '0'; *s++ = '0';
for (i=dp; i>0; i--) for (i = dp; i > 0; i--)
if (*s1) *s++ = *s1++; if (*s1)
else *s++ = '0'; *s++ = *s1++;
if (((i=n) > 0) || (flags & FL_ALT)) else
*s++ = '0';
if (((i = n) > 0) || (flags & FL_ALT))
*s++ = '.'; *s++ = '.';
while (++dp <= 0) { while (++dp <= 0)
if (--i<0) {
if (--i < 0)
break; break;
*s++ = '0'; *s++ = '0';
} }
while (--i >= 0) while (--i >= 0)
if (*s1) *s++ = *s1++; if (*s1)
else *s++ = '0'; *s++ = *s1++;
else
*s++ = '0';
return s; return s;
} }
static char * static char*
_pscien(long double r, register char *s, int n, int flags) _pscien(long double r, register char* s, int n, int flags)
{ {
int sign, dp; int sign, dp;
register char *s1; register char* s1;
s1 = _ecvt(r, n + 1, &dp, &sign); s1 = _ecvt(r, n + 1, &dp, &sign);
if (sign) if (sign)
@ -60,30 +65,38 @@ _pscien(long double r, register char *s, int n, int flags)
if ((n > 0) || (flags & FL_ALT)) if ((n > 0) || (flags & FL_ALT))
*s++ = '.'; *s++ = '.';
while (--n >= 0) while (--n >= 0)
if (*s1) *s++ = *s1++; if (*s1)
else *s++ = '0'; *s++ = *s1++;
else
*s++ = '0';
*s++ = 'e'; *s++ = 'e';
if ( r != 0 ) --dp ; if (r != 0)
if ( dp<0 ) { --dp;
*s++ = '-' ; dp= -dp ; if (dp < 0)
} else { {
*s++ = '+' ; *s++ = '-';
dp = -dp;
} }
if (dp >= 100) { else
{
*s++ = '+';
}
if (dp >= 100)
{
*s++ = '0' + (dp / 100); *s++ = '0' + (dp / 100);
dp %= 100; dp %= 100;
} }
*s++ = '0' + (dp/10); *s++ = '0' + (dp / 10);
*s++ = '0' + (dp%10); *s++ = '0' + (dp % 10);
return s; return s;
} }
#define NDIGINEXP(exp) (((exp) >= 100 || (exp) <= -100) ? 3 : 2) #define NDIGINEXP(exp) (((exp) >= 100 || (exp) <= -100) ? 3 : 2)
#define LOW_EXP -4 #define LOW_EXP -4
#define USE_EXP(exp, ndigits) (((exp) < LOW_EXP + 1) || (exp >= ndigits + 1)) #define USE_EXP(exp, ndigits) (((exp) < LOW_EXP + 1) || (exp >= ndigits + 1))
static char * static char*
_gcvt(long double value, int ndigit, char *s, int flags) _gcvt(long double value, int ndigit, char* s, int flags)
{ {
int sign, dp; int sign, dp;
register char *s1, *s2; register char *s1, *s2;
@ -92,7 +105,8 @@ _gcvt(long double value, int ndigit, char *s, int flags)
s1 = _ecvt(value, ndigit, &dp, &sign); s1 = _ecvt(value, ndigit, &dp, &sign);
s2 = s; s2 = s;
if (sign) *s2++ = '-'; if (sign)
*s2++ = '-';
else if (flags & FL_SIGN) else if (flags & FL_SIGN)
*s2++ = '+'; *s2++ = '+';
else if (flags & FL_SPACE) else if (flags & FL_SPACE)
@ -102,78 +116,97 @@ _gcvt(long double value, int ndigit, char *s, int flags)
for (i = nndigit - 1; i > 0 && s1[i] == '0'; i--) for (i = nndigit - 1; i > 0 && s1[i] == '0'; i--)
nndigit--; nndigit--;
if (USE_EXP(dp,ndigit)) { if (USE_EXP(dp, ndigit))
{
/* Use E format */ /* Use E format */
dp--; dp--;
*s2++ = *s1++; *s2++ = *s1++;
if ((nndigit > 1) || (flags & FL_ALT)) *s2++ = '.'; if ((nndigit > 1) || (flags & FL_ALT))
while (--nndigit > 0) *s2++ = *s1++; *s2++ = '.';
while (--nndigit > 0)
*s2++ = *s1++;
*s2++ = 'e'; *s2++ = 'e';
if (dp < 0) { if (dp < 0)
{
*s2++ = '-'; *s2++ = '-';
dp = -dp; dp = -dp;
} }
else *s2++ = '+'; else
*s2++ = '+';
s2 += NDIGINEXP(dp); s2 += NDIGINEXP(dp);
*s2 = 0; *s2 = 0;
for (i = NDIGINEXP(dp); i > 0; i--) { for (i = NDIGINEXP(dp); i > 0; i--)
{
*--s2 = dp % 10 + '0'; *--s2 = dp % 10 + '0';
dp /= 10; dp /= 10;
} }
return s; return s;
} }
/* Use f format */ /* Use f format */
if (dp <= 0) { if (dp <= 0)
if (*s1 != '0') { {
if (*s1 != '0')
{
/* otherwise the whole number is 0 */ /* otherwise the whole number is 0 */
*s2++ = '0'; *s2++ = '0';
*s2++ = '.'; *s2++ = '.';
} }
while (dp < 0) { while (dp < 0)
{
dp++; dp++;
*s2++ = '0'; *s2++ = '0';
} }
} }
for (i = 1; i <= nndigit; i++) { for (i = 1; i <= nndigit; i++)
{
*s2++ = *s1++; *s2++ = *s1++;
if (i == dp) *s2++ = '.'; if (i == dp)
*s2++ = '.';
} }
if (i <= dp) { if (i <= dp)
while (i++ <= dp) *s2++ = '0'; {
while (i++ <= dp)
*s2++ = '0';
*s2++ = '.'; *s2++ = '.';
} }
if ((s2[-1]=='.') && !(flags & FL_ALT)) s2--; if ((s2[-1] == '.') && !(flags & FL_ALT))
s2--;
*s2 = '\0'; *s2 = '\0';
return s; return s;
} }
char * char* _f_print(va_list* ap, int flags, char* s, char c, int precision)
_f_print(va_list *ap, int flags, char *s, char c, int precision)
{ {
register char *old_s = s; register char* old_s = s;
long double ld_val; long double ld_val;
if (flags & FL_LONGDOUBLE) ld_val = va_arg(*ap, long double); if (flags & FL_LONGDOUBLE)
else ld_val = (long double) va_arg(*ap, double); ld_val = va_arg(*ap, long double);
else
ld_val = (long double)va_arg(*ap, double);
switch(c) { switch (c)
case 'f': {
s = _pfloat(ld_val, s, precision, flags); case 'f':
break; s = _pfloat(ld_val, s, precision, flags);
case 'e': break;
case 'E': case 'e':
s = _pscien(ld_val, s, precision , flags); case 'E':
break; s = _pscien(ld_val, s, precision, flags);
case 'g': break;
case 'G': case 'g':
s = _gcvt(ld_val, precision, s, flags); case 'G':
s += strlen(s); s = _gcvt(ld_val, precision, s, flags);
break; s += strlen(s);
break;
} }
if ( c == 'E' || c == 'G') { if (c == 'E' || c == 'G')
while (*old_s && *old_s != 'e') old_s++; {
if (*old_s == 'e') *old_s = 'E'; while (*old_s && *old_s != 'e')
old_s++;
if (*old_s == 'e')
*old_s = 'E';
} }
return s; return s;
} }
#endif /* ACKCONF_NO_STDIO_FLOAT */ #endif /* ACKCONF_NO_STDIO_FLOAT */

View file

@ -11,107 +11,136 @@
extern void (*_clean)(void); extern void (*_clean)(void);
static int static int
do_write(int d, char *buf, int nbytes) do_write(int d, char* buf, int nbytes)
{ {
int c; int c;
/* POSIX actually allows write() to return a positive value less /* POSIX actually allows write() to return a positive value less
than nbytes, so loop ... than nbytes, so loop ...
*/ */
while ((c = write(d, buf, nbytes)) > 0 && c < nbytes) { while ((c = write(d, buf, nbytes)) > 0 && c < nbytes)
{
nbytes -= c; nbytes -= c;
buf += c; buf += c;
} }
return c > 0; return c > 0;
} }
int int __flushbuf(int c, FILE* stream)
__flushbuf(int c, FILE * stream)
{ {
_clean = __cleanup; _clean = __cleanup;
if (fileno(stream) < 0) return EOF; if (fileno(stream) < 0)
if (!io_testflag(stream, _IOWRITE)) return EOF; return EOF;
if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF; if (!io_testflag(stream, _IOWRITE))
return EOF;
if (io_testflag(stream, _IOREADING) && !feof(stream))
return EOF;
stream->_flags &= ~_IOREADING; stream->_flags &= ~_IOREADING;
stream->_flags |= _IOWRITING; stream->_flags |= _IOWRITING;
if (!io_testflag(stream, _IONBF)) { if (!io_testflag(stream, _IONBF))
if (!stream->_buf) { {
if (stream == stdout && isatty(fileno(stdout))) { if (!stream->_buf)
if (!(stream->_buf = {
(unsigned char *) malloc(BUFSIZ))) { if (stream == stdout && isatty(fileno(stdout)))
{
if (!(stream->_buf = (unsigned char*)malloc(BUFSIZ)))
{
stream->_flags |= _IONBF; stream->_flags |= _IONBF;
} else { }
stream->_flags |= _IOLBF|_IOMYBUF; else
{
stream->_flags |= _IOLBF | _IOMYBUF;
stream->_bufsiz = BUFSIZ; stream->_bufsiz = BUFSIZ;
stream->_count = -1; stream->_count = -1;
} }
} else { }
if (!(stream->_buf = else
(unsigned char *) malloc(BUFSIZ))) { {
if (!(stream->_buf = (unsigned char*)malloc(BUFSIZ)))
{
stream->_flags |= _IONBF; stream->_flags |= _IONBF;
} else { }
else
{
stream->_flags |= _IOMYBUF; stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ; stream->_bufsiz = BUFSIZ;
if (!io_testflag(stream, _IOLBF)) if (!io_testflag(stream, _IOLBF))
stream->_count = BUFSIZ - 1; stream->_count = BUFSIZ - 1;
else stream->_count = -1; else
stream->_count = -1;
} }
} }
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
} }
} }
if (io_testflag(stream, _IONBF)) { if (io_testflag(stream, _IONBF))
{
char c1 = c; char c1 = c;
stream->_count = 0; stream->_count = 0;
if (io_testflag(stream, _IOAPPEND)) { if (io_testflag(stream, _IOAPPEND))
if (lseek(fileno(stream), 0L, SEEK_END) == -1) { {
if (lseek(fileno(stream), 0L, SEEK_END) == -1)
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
} }
if (write(fileno(stream), &c1, 1) != 1) { if (write(fileno(stream), &c1, 1) != 1)
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
return (unsigned char) c; return (unsigned char)c;
} else if (io_testflag(stream, _IOLBF)) { }
else if (io_testflag(stream, _IOLBF))
{
*stream->_ptr++ = c; *stream->_ptr++ = c;
/* stream->_count has been updated in putc macro. */ /* stream->_count has been updated in putc macro. */
if (c == '\n' || stream->_count == -stream->_bufsiz) { if (c == '\n' || stream->_count == -stream->_bufsiz)
{
int count = -stream->_count; int count = -stream->_count;
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
stream->_count = 0; stream->_count = 0;
if (io_testflag(stream, _IOAPPEND)) { if (io_testflag(stream, _IOAPPEND))
if (lseek(fileno(stream), 0L, SEEK_END) == -1) { {
if (lseek(fileno(stream), 0L, SEEK_END) == -1)
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
} }
if (! do_write(fileno(stream), (char *)stream->_buf, if (!do_write(fileno(stream), (char*)stream->_buf,
count)) { count))
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
} }
} else { }
else
{
int count = stream->_ptr - stream->_buf; int count = stream->_ptr - stream->_buf;
stream->_count = stream->_bufsiz - 1; stream->_count = stream->_bufsiz - 1;
stream->_ptr = stream->_buf + 1; stream->_ptr = stream->_buf + 1;
if (count > 0) { if (count > 0)
if (io_testflag(stream, _IOAPPEND)) { {
if (lseek(fileno(stream), 0L, SEEK_END) == -1) { if (io_testflag(stream, _IOAPPEND))
{
if (lseek(fileno(stream), 0L, SEEK_END) == -1)
{
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
} }
if (! do_write(fileno(stream), (char *)stream->_buf, count)) { if (!do_write(fileno(stream), (char*)stream->_buf, count))
{
*(stream->_buf) = c; *(stream->_buf) = c;
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
@ -119,5 +148,5 @@ __flushbuf(int c, FILE * stream)
} }
*(stream->_buf) = c; *(stream->_buf) = c;
} }
return (unsigned char) c; return (unsigned char)c;
} }

View file

@ -10,7 +10,7 @@
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
#define PMODE 0666 #define PMODE 0666
/* Since the O_CREAT flag is not available on all systems, we can't get it /* Since the O_CREAT flag is not available on all systems, we can't get it
* from the standard library. Furthermore, even if we know that <fcntl.h> * from the standard library. Furthermore, even if we know that <fcntl.h>
@ -31,48 +31,50 @@
* Remember to fix freopen.c if changing this. * Remember to fix freopen.c if changing this.
*/ */
FILE * FILE* fopen(const char* name, const char* mode)
fopen(const char *name, const char *mode)
{ {
register int i; register int i;
int rwmode = 0, rwflags = 0; int rwmode = 0, rwflags = 0;
FILE *stream; FILE* stream;
int fd, flags = 0; int fd, flags = 0;
for (i = 0; __iotab[i] != 0 ; i++) for (i = 0; __iotab[i] != 0; i++)
if ( i >= FOPEN_MAX-1 ) if (i >= FOPEN_MAX - 1)
return (FILE *)NULL; return (FILE*)NULL;
switch(*mode++) { switch (*mode++)
case 'r': {
flags |= _IOREAD | _IOREADING; case 'r':
rwmode = O_RDONLY; flags |= _IOREAD | _IOREADING;
break; rwmode = O_RDONLY;
case 'w': break;
flags |= _IOWRITE | _IOWRITING; case 'w':
rwmode = O_WRONLY; flags |= _IOWRITE | _IOWRITING;
rwflags = O_CREAT | O_TRUNC; rwmode = O_WRONLY;
break; rwflags = O_CREAT | O_TRUNC;
case 'a': break;
flags |= _IOWRITE | _IOWRITING | _IOAPPEND; case 'a':
rwmode = O_WRONLY; flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
rwflags |= O_APPEND | O_CREAT; rwmode = O_WRONLY;
break; rwflags |= O_APPEND | O_CREAT;
default: break;
return (FILE *)NULL; default:
return (FILE*)NULL;
} }
while (*mode) { while (*mode)
switch(*mode++) { {
case 'b': switch (*mode++)
continue; {
case '+': case 'b':
rwmode = O_RDWR; continue;
flags |= _IOREAD | _IOWRITE; case '+':
continue; rwmode = O_RDWR;
/* The sequence may be followed by additional characters */ flags |= _IOREAD | _IOWRITE;
default: continue;
break; /* The sequence may be followed by additional characters */
default:
break;
} }
break; break;
} }
@ -82,22 +84,25 @@ fopen(const char *name, const char *mode)
*/ */
if ((rwflags & O_TRUNC) if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0) || (((fd = open(name, rwmode)) < 0)
&& (rwflags & O_CREAT))) { && (rwflags & O_CREAT)))
if (((fd = creat(name, PMODE)) > 0) && flags | _IOREAD) { {
(void) close(fd); if (((fd = creat(name, PMODE)) > 0) && flags | _IOREAD)
{
(void)close(fd);
fd = open(name, rwmode); fd = open(name, rwmode);
} }
} }
if (fd < 0) return (FILE *)NULL; if (fd < 0)
return (FILE*)NULL;
if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) { if ((stream = (FILE*)malloc(sizeof(FILE))) == NULL)
{
close(fd); close(fd);
return (FILE *)NULL; return (FILE*)NULL;
} }
if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE)) if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE))
flags &= ~(_IOREADING | _IOWRITING); flags &= ~(_IOREADING | _IOWRITING);
stream->_count = 0; stream->_count = 0;

View file

@ -3,19 +3,18 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int fprintf(FILE* stream, const char* format, ...)
fprintf(FILE *stream, const char *format, ...)
{ {
va_list ap; va_list ap;
int retval; int retval;
va_start(ap, format); va_start(ap, format);
retval = _doprnt (format, ap, stream); retval = _doprnt(format, ap, stream);
va_end(ap); va_end(ap);

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int fputc(int c, FILE* stream)
fputc(int c, FILE *stream)
{ {
return putc(c, stream); return putc(c, stream);
} }

View file

@ -3,16 +3,17 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int fputs(register const char* s, register FILE* stream)
fputs(register const char *s, register FILE *stream)
{ {
register int i = 0; register int i = 0;
while (*s) while (*s)
if (putc(*s++, stream) == EOF) return EOF; if (putc(*s++, stream) == EOF)
else i++; return EOF;
else
i++;
return i; return i;
} }

View file

@ -3,20 +3,22 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
size_t size_t
fread(void *ptr, size_t size, size_t nmemb, register FILE *stream) fread(void* ptr, size_t size, size_t nmemb, register FILE* stream)
{ {
register char *cp = ptr; register char* cp = ptr;
register int c; register int c;
size_t ndone = 0; size_t ndone = 0;
register size_t s; register size_t s;
if (size) if (size)
while ( ndone < nmemb ) { while (ndone < nmemb)
{
s = size; s = size;
do { do
{
if ((c = getc(stream)) != EOF) if ((c = getc(stream)) != EOF)
*cp++ = c; *cp++ = c;
else else

View file

@ -9,75 +9,82 @@
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
#define PMODE 0666 #define PMODE 0666
/* Do not "optimize" this file to use the open with O_CREAT if the file /* Do not "optimize" this file to use the open with O_CREAT if the file
* does not exist. The reason is given in fopen.c. * does not exist. The reason is given in fopen.c.
*/ */
FILE * FILE* freopen(const char* name, const char* mode, FILE* stream)
freopen(const char *name, const char *mode, FILE *stream)
{ {
register int i; register int i;
int rwmode = 0, rwflags = 0; int rwmode = 0, rwflags = 0;
int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF); int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF);
(void) fflush(stream); /* ignore errors */ (void)fflush(stream); /* ignore errors */
(void) close(fileno(stream)); (void)close(fileno(stream));
switch(*mode++) { switch (*mode++)
case 'r': {
flags |= _IOREAD; case 'r':
rwmode = O_RDONLY; flags |= _IOREAD;
break; rwmode = O_RDONLY;
case 'w': break;
flags |= _IOWRITE; case 'w':
rwmode = O_WRONLY; flags |= _IOWRITE;
rwflags = O_CREAT | O_TRUNC; rwmode = O_WRONLY;
break; rwflags = O_CREAT | O_TRUNC;
case 'a': break;
flags |= _IOWRITE | _IOAPPEND; case 'a':
rwmode = O_WRONLY; flags |= _IOWRITE | _IOAPPEND;
rwflags |= O_APPEND | O_CREAT; rwmode = O_WRONLY;
break; rwflags |= O_APPEND | O_CREAT;
default: break;
return (FILE *)NULL; default:
return (FILE*)NULL;
} }
while (*mode) { while (*mode)
switch(*mode++) { {
case 'b': switch (*mode++)
continue; {
case '+': case 'b':
rwmode = O_RDWR; continue;
flags |= _IOREAD | _IOWRITE; case '+':
continue; rwmode = O_RDWR;
/* The sequence may be followed by aditional characters */ flags |= _IOREAD | _IOWRITE;
default: continue;
break; /* The sequence may be followed by aditional characters */
default:
break;
} }
break; break;
} }
if ((rwflags & O_TRUNC) if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0) || (((fd = open(name, rwmode)) < 0)
&& (rwflags & O_CREAT))) { && (rwflags & O_CREAT)))
if (((fd = creat(name, PMODE)) < 0) && flags | _IOREAD) { {
(void) close(fd); if (((fd = creat(name, PMODE)) < 0) && flags | _IOREAD)
{
(void)close(fd);
fd = open(name, rwmode); fd = open(name, rwmode);
} }
} }
if (fd < 0) { if (fd < 0)
for( i = 0; i < FOPEN_MAX; i++) { {
if (stream == __iotab[i]) { for (i = 0; i < FOPEN_MAX; i++)
{
if (stream == __iotab[i])
{
__iotab[i] = 0; __iotab[i] = 0;
break; break;
} }
} }
if (stream != stdin && stream != stdout && stream != stderr) if (stream != stdin && stream != stdout && stream != stderr)
free((void *)stream); free((void*)stream);
return (FILE *)NULL; return (FILE*)NULL;
} }
stream->_count = 0; stream->_count = 0;

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int fscanf(FILE* stream, const char* format, ...)
fscanf(FILE *stream, const char *format, ...)
{ {
va_list ap; va_list ap;
int retval; int retval;

View file

@ -8,8 +8,7 @@
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
int int fseek(FILE* stream, long int offset, int whence)
fseek(FILE *stream, long int offset, int whence)
{ {
int adjust = 0; int adjust = 0;
long pos; long pos;
@ -17,16 +16,20 @@ fseek(FILE *stream, long int offset, int whence)
stream->_flags &= ~(_IOEOF | _IOERR); stream->_flags &= ~(_IOEOF | _IOERR);
/* Clear both the end of file and error flags */ /* Clear both the end of file and error flags */
if (io_testflag(stream, _IOREADING)) { if (io_testflag(stream, _IOREADING))
{
if (whence == SEEK_CUR if (whence == SEEK_CUR
&& stream->_buf && stream->_buf
&& !io_testflag(stream,_IONBF)) && !io_testflag(stream, _IONBF))
adjust = stream->_count; adjust = stream->_count;
stream->_count = 0; stream->_count = 0;
} else if (io_testflag(stream,_IOWRITING)) { }
else if (io_testflag(stream, _IOWRITING))
{
fflush(stream); fflush(stream);
} else /* neither reading nor writing. The buffer must be empty */ }
/* EMPTY */ ; else /* neither reading nor writing. The buffer must be empty */
/* EMPTY */;
pos = lseek(fileno(stream), offset - adjust, whence); pos = lseek(fileno(stream), offset - adjust, whence);
if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE)) if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int fsetpos(FILE* stream, fpos_t* pos)
fsetpos(FILE *stream, fpos_t *pos)
{ {
return fseek(stream, *pos, SEEK_SET); return fseek(stream, *pos, SEEK_SET);
} }

View file

@ -6,26 +6,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
long ftell(FILE *stream) long ftell(FILE* stream)
{ {
long result; long result;
int adjust = 0; int adjust = 0;
if (io_testflag(stream,_IOREADING)) if (io_testflag(stream, _IOREADING))
adjust = -stream->_count; adjust = -stream->_count;
else if (io_testflag(stream,_IOWRITING) else if (io_testflag(stream, _IOWRITING)
&& stream->_buf && stream->_buf
&& !io_testflag(stream,_IONBF)) && !io_testflag(stream, _IONBF))
adjust = stream->_ptr - stream->_buf; adjust = stream->_ptr - stream->_buf;
else adjust = 0; else
adjust = 0;
result = lseek(fileno(stream), 0, SEEK_CUR); result = lseek(fileno(stream), 0, SEEK_CUR);
if ( result == -1 ) if (result == -1)
return result; return result;
result += (long) adjust; result += (long)adjust;
return result; return result;
} }

View file

@ -3,26 +3,27 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
size_t size_t
fwrite(const void *ptr, size_t size, size_t nmemb, fwrite(const void* ptr, size_t size, size_t nmemb,
register FILE *stream) register FILE* stream)
{ {
register const unsigned char *cp = ptr; register const unsigned char* cp = ptr;
register size_t s; register size_t s;
size_t ndone = 0; size_t ndone = 0;
if (size) if (size)
while ( ndone < nmemb ) { while (ndone < nmemb)
{
s = size; s = size;
do { do
{
if (putc((int)*cp, stream) if (putc((int)*cp, stream)
== EOF) == EOF)
return ndone; return ndone;
cp++; cp++;
} } while (--s);
while (--s);
ndone++; ndone++;
} }
return ndone; return ndone;

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int(getc)(FILE* stream)
(getc)(FILE *stream)
{ {
return getc(stream); return getc(stream);
} }

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int(getchar)(void)
(getchar)(void)
{ {
return getchar(); return getchar();
} }

View file

@ -3,23 +3,27 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
char * char* gets(char* s)
gets(char *s)
{ {
register FILE *stream = stdin; register FILE* stream = stdin;
register int ch; register int ch;
register char *ptr; register char* ptr;
ptr = s; ptr = s;
while ((ch = getc(stream)) != EOF && ch != '\n') while ((ch = getc(stream)) != EOF && ch != '\n')
*ptr++ = ch; *ptr++ = ch;
if (ch == EOF) { if (ch == EOF)
if (feof(stream)) { {
if (ptr == s) return NULL; if (feof(stream))
} else return NULL; {
if (ptr == s)
return NULL;
}
else
return NULL;
} }
*ptr = '\0'; *ptr = '\0';

View file

@ -3,19 +3,18 @@
*/ */
/* $Id$ */ /* $Id$ */
#include "loc_incl.h" #include "loc_incl.h"
/* This routine is used in doprnt.c as well as in tmpfile.c and tmpnam.c. */ /* This routine is used in doprnt.c as well as in tmpfile.c and tmpnam.c. */
char * char* _i_compute(unsigned long val, int base, char* s, int nrdigits)
_i_compute(unsigned long val, int base, char *s, int nrdigits)
{ {
int c; int c;
c= val % base ; c = val % base;
val /= base ; val /= base;
if (val || nrdigits > 1) if (val || nrdigits > 1)
s = _i_compute(val, base, s, nrdigits - 1); s = _i_compute(val, base, s, nrdigits - 1);
*s++ = (c>9 ? c-10+'a' : c+'0'); *s++ = (c > 9 ? c - 10 + 'a' : c + '0');
return s; return s;
} }

View file

@ -3,7 +3,7 @@
*/ */
/* $Id$ */ /* $Id$ */
int _gtty(int d, char *buf); int _gtty(int d, char* buf);
int _isatty(int d) int _isatty(int d)
{ {

View file

@ -3,17 +3,17 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
void void perror(const char* s)
perror(const char *s)
{ {
if (s && *s) { if (s && *s)
(void) fputs(s, stderr); {
(void) fputs(": ", stderr); (void)fputs(s, stderr);
(void)fputs(": ", stderr);
} }
(void) fputs(strerror(errno), stderr); (void)fputs(strerror(errno), stderr);
(void) fputs("\n", stderr); (void)fputs("\n", stderr);
} }

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int printf(const char* format, ...)
printf(const char *format, ...)
{ {
va_list ap; va_list ap;
int retval; int retval;

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int(putc)(int c, FILE* stream)
(putc)(int c, FILE *stream)
{ {
return putc(c, stream); return putc(c, stream);
} }

View file

@ -3,10 +3,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int(putchar)(int c)
(putchar)(int c)
{ {
return putchar(c); return putchar(c);
} }

View file

@ -3,18 +3,21 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
int int puts(register const char* s)
puts(register const char *s)
{ {
register FILE *file = stdout; register FILE* file = stdout;
register int i = 0; register int i = 0;
while (*s) { while (*s)
if (putc(*s++, file) == EOF) return EOF; {
else i++; if (putc(*s++, file) == EOF)
return EOF;
else
i++;
} }
if (putc('\n', file) == EOF) return EOF; if (putc('\n', file) == EOF)
return EOF;
return i + 1; return i + 1;
} }

View file

@ -7,7 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
int int remove(const char* filename)
remove(const char *filename) { {
return unlink(filename); return unlink(filename);
} }

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include "loc_incl.h" #include "loc_incl.h"
void void rewind(FILE* stream)
rewind(FILE *stream)
{ {
(void) fseek(stream, 0L, SEEK_SET); (void)fseek(stream, 0L, SEEK_SET);
clearerr(stream); clearerr(stream);
} }

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int scanf(const char* format, ...)
scanf(const char *format, ...)
{ {
va_list ap; va_list ap;
int retval; int retval;
@ -21,5 +20,3 @@ scanf(const char *format, ...)
return retval; return retval;
} }

View file

@ -3,11 +3,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include "loc_incl.h" #include "loc_incl.h"
void void setbuf(register FILE* stream, char* buf)
setbuf(register FILE *stream, char *buf)
{ {
(void) setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), (size_t) BUFSIZ); (void)setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), (size_t)BUFSIZ);
} }

View file

@ -3,14 +3,13 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "loc_incl.h" #include "loc_incl.h"
extern void (*_clean)(void); extern void (*_clean)(void);
int int setvbuf(register FILE* stream, char* buf, int mode, size_t size)
setvbuf(register FILE *stream, char *buf, int mode, size_t size)
{ {
int retval = 0; int retval = 0;
@ -18,29 +17,37 @@ setvbuf(register FILE *stream, char *buf, int mode, size_t size)
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF) if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
return EOF; return EOF;
if (stream->_buf && io_testflag(stream,_IOMYBUF) ) if (stream->_buf && io_testflag(stream, _IOMYBUF))
free((void *)stream->_buf); free((void*)stream->_buf);
stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF); stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
if (buf && size <= 0) retval = EOF; if (buf && size <= 0)
if (!buf && (mode != _IONBF)) { retval = EOF;
if (size <= 0 || (buf = (char *) malloc(size)) == NULL) { if (!buf && (mode != _IONBF))
{
if (size <= 0 || (buf = (char*)malloc(size)) == NULL)
{
retval = EOF; retval = EOF;
} else { }
else
{
stream->_flags |= _IOMYBUF; stream->_flags |= _IOMYBUF;
} }
} }
stream->_buf = (unsigned char *) buf; stream->_buf = (unsigned char*)buf;
stream->_count = 0; stream->_count = 0;
stream->_flags |= mode; stream->_flags |= mode;
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
if (!buf) { if (!buf)
{
stream->_bufsiz = 1; stream->_bufsiz = 1;
} else { }
else
{
stream->_bufsiz = size; stream->_bufsiz = size;
} }

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int snprintf(char* s, size_t len, const char* format, ...)
snprintf(char * s, size_t len, const char *format, ...)
{ {
va_list ap; va_list ap;
int retval; int retval;
@ -16,14 +15,14 @@ snprintf(char * s, size_t len, const char *format, ...)
va_start(ap, format); va_start(ap, format);
tmp_stream._fd = -1; tmp_stream._fd = -1;
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING; tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
tmp_stream._buf = (unsigned char *) s; tmp_stream._buf = (unsigned char*)s;
tmp_stream._ptr = (unsigned char *) s; tmp_stream._ptr = (unsigned char*)s;
tmp_stream._count = len; tmp_stream._count = len;
retval = _doprnt(format, ap, &tmp_stream); retval = _doprnt(format, ap, &tmp_stream);
putc('\0',&tmp_stream); putc('\0', &tmp_stream);
va_end(ap); va_end(ap);

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int sprintf(char* s, const char* format, ...)
sprintf(char * s, const char *format, ...)
{ {
va_list ap; va_list ap;
int retval; int retval;
@ -16,14 +15,14 @@ sprintf(char * s, const char *format, ...)
va_start(ap, format); va_start(ap, format);
tmp_stream._fd = -1; tmp_stream._fd = -1;
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING; tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
tmp_stream._buf = (unsigned char *) s; tmp_stream._buf = (unsigned char*)s;
tmp_stream._ptr = (unsigned char *) s; tmp_stream._ptr = (unsigned char*)s;
tmp_stream._count = 32767; tmp_stream._count = 32767;
retval = _doprnt(format, ap, &tmp_stream); retval = _doprnt(format, ap, &tmp_stream);
putc('\0',&tmp_stream); putc('\0', &tmp_stream);
va_end(ap); va_end(ap);

View file

@ -3,12 +3,12 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include "loc_incl.h" #include "loc_incl.h"
int sscanf(const char *s, const char *format, ...) int sscanf(const char* s, const char* format, ...)
{ {
va_list ap; va_list ap;
int retval; int retval;
@ -16,11 +16,11 @@ int sscanf(const char *s, const char *format, ...)
va_start(ap, format); va_start(ap, format);
tmp_stream._fd = -1; tmp_stream._fd = -1;
tmp_stream._flags = _IOREAD + _IONBF + _IOREADING; tmp_stream._flags = _IOREAD + _IONBF + _IOREADING;
tmp_stream._buf = (unsigned char *) s; tmp_stream._buf = (unsigned char*)s;
tmp_stream._ptr = (unsigned char *) s; tmp_stream._ptr = (unsigned char*)s;
tmp_stream._count = strlen(s); tmp_stream._count = strlen(s);
retval = _doscan(&tmp_stream, format, ap); retval = _doscan(&tmp_stream, format, ap);

View file

@ -9,20 +9,22 @@
#include <string.h> #include <string.h>
#include "loc_incl.h" #include "loc_incl.h"
FILE * FILE* tmpfile(void)
tmpfile(void) { {
static char name_buffer[L_tmpnam] = "/tmp/tmp." ; static char name_buffer[L_tmpnam] = "/tmp/tmp.";
static char *name = NULL; static char* name = NULL;
FILE *file; FILE* file;
if (!name) { if (!name)
{
name = name_buffer + strlen(name_buffer); name = name_buffer + strlen(name_buffer);
name = _i_compute(getpid(), 10, name, 5); name = _i_compute(getpid(), 10, name, 5);
*name = '\0'; *name = '\0';
} }
file = fopen(name_buffer,"wb+"); file = fopen(name_buffer, "wb+");
if (!file) return (FILE *)NULL; if (!file)
(void) remove(name_buffer); return (FILE*)NULL;
(void)remove(name_buffer);
return file; return file;
} }

View file

@ -9,20 +9,24 @@
#include <unistd.h> #include <unistd.h>
#include "loc_incl.h" #include "loc_incl.h"
char * char* tmpnam(char* s)
tmpnam(char *s) { {
static char name_buffer[L_tmpnam] = "/tmp/tmp."; static char name_buffer[L_tmpnam] = "/tmp/tmp.";
static unsigned long count = 0; static unsigned long count = 0;
static char *name = NULL; static char* name = NULL;
if (!name) { if (!name)
{
name = name_buffer + strlen(name_buffer); name = name_buffer + strlen(name_buffer);
name = _i_compute(getpid(), 10, name, 5); name = _i_compute(getpid(), 10, name, 5);
*name++ = '.'; *name++ = '.';
*name = '\0'; *name = '\0';
} }
if (++count > TMP_MAX) count = 1; /* wrap-around */ if (++count > TMP_MAX)
count = 1; /* wrap-around */
*_i_compute(count, 10, name, 3) = '\0'; *_i_compute(count, 10, name, 3) = '\0';
if (s) return strcpy(s, name_buffer); if (s)
else return name_buffer; return strcpy(s, name_buffer);
else
return name_buffer;
} }

View file

@ -3,24 +3,25 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include "loc_incl.h" #include "loc_incl.h"
int int ungetc(int ch, FILE* stream)
ungetc(int ch, FILE *stream)
{ {
unsigned char *p; unsigned char* p;
if (ch == EOF || !io_testflag(stream,_IOREADING)) if (ch == EOF || !io_testflag(stream, _IOREADING))
return EOF; return EOF;
if (stream->_ptr == stream->_buf) { if (stream->_ptr == stream->_buf)
if (stream->_count != 0) return EOF; {
if (stream->_count != 0)
return EOF;
stream->_ptr++; stream->_ptr++;
} }
stream->_count++; stream->_count++;
p = --(stream->_ptr); /* ??? Bloody vax assembler !!! */ p = --(stream->_ptr); /* ??? Bloody vax assembler !!! */
/* ungetc() in sscanf() shouldn't write in rom */ /* ungetc() in sscanf() shouldn't write in rom */
if (*p != (unsigned char) ch) if (*p != (unsigned char)ch)
*p = (unsigned char) ch; *p = (unsigned char)ch;
return ch; return ch;
} }

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int vfprintf(FILE* stream, const char* format, va_list arg)
vfprintf(FILE *stream, const char *format, va_list arg)
{ {
return _doprnt (format, arg, stream); return _doprnt(format, arg, stream);
} }

View file

@ -3,12 +3,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int vprintf(const char* format, va_list arg)
vprintf(const char *format, va_list arg)
{ {
return _doprnt(format, arg, stdout); return _doprnt(format, arg, stdout);
} }

View file

@ -3,24 +3,23 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int vsnprintf(char* s, size_t len, const char* format, va_list arg)
vsnprintf(char *s, size_t len, const char *format, va_list arg)
{ {
int retval; int retval;
FILE tmp_stream; FILE tmp_stream;
tmp_stream._fd = -1; tmp_stream._fd = -1;
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING; tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
tmp_stream._buf = (unsigned char *) s; tmp_stream._buf = (unsigned char*)s;
tmp_stream._ptr = (unsigned char *) s; tmp_stream._ptr = (unsigned char*)s;
tmp_stream._count = len; tmp_stream._count = len;
retval = _doprnt(format, arg, &tmp_stream); retval = _doprnt(format, arg, &tmp_stream);
putc('\0',&tmp_stream); putc('\0', &tmp_stream);
return retval; return retval;
} }

View file

@ -3,24 +3,23 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
int int vsprintf(char* s, const char* format, va_list arg)
vsprintf(char *s, const char *format, va_list arg)
{ {
int retval; int retval;
FILE tmp_stream; FILE tmp_stream;
tmp_stream._fd = -1; tmp_stream._fd = -1;
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING; tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
tmp_stream._buf = (unsigned char *) s; tmp_stream._buf = (unsigned char*)s;
tmp_stream._ptr = (unsigned char *) s; tmp_stream._ptr = (unsigned char*)s;
tmp_stream._count = 32767; tmp_stream._count = 32767;
retval = _doprnt(format, arg, &tmp_stream); retval = _doprnt(format, arg, &tmp_stream);
putc('\0',&tmp_stream); putc('\0', &tmp_stream);
return retval; return retval;
} }

View file

@ -4,18 +4,17 @@
*/ */
/* $Id$ */ /* $Id$ */
#if defined(_POSIX_SOURCE) #if defined(_POSIX_SOURCE)
#include <sys/types.h> #include <sys/types.h>
#endif #endif
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
extern void (*_clean)(void); extern void (*_clean)(void);
void void abort(void)
abort(void)
{ {
if (_clean) _clean(); /* flush all output files */ if (_clean)
_clean(); /* flush all output files */
raise(SIGABRT); raise(SIGABRT);
} }

View file

@ -4,10 +4,9 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdlib.h> #include <stdlib.h>
int int abs(register int i)
abs(register int i)
{ {
return i >= 0 ? i : -i; return i >= 0 ? i : -i;
} }

Some files were not shown because too many files have changed in this diff Show more