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

@ -7,7 +7,8 @@
#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

@ -8,13 +8,15 @@
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_ALL:
case LC_CTYPE: case LC_CTYPE:
case LC_COLLATE: case LC_COLLATE:

View file

@ -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

@ -32,17 +32,21 @@ asin_acos(double x, int cosfl)
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;
} }
@ -50,22 +54,28 @@ asin_acos(double x, int cosfl)
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

@ -43,30 +43,35 @@ atan(double x)
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

@ -15,26 +15,32 @@ 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

@ -11,7 +11,6 @@
#include <errno.h> #include <errno.h>
#include "localmath.h" #include "localmath.h"
double double
exp(double x) exp(double x)
{ {
@ -36,20 +35,24 @@ exp(double x)
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 ??? */
@ -61,7 +64,8 @@ exp(double x)
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;
} }

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 */ /* try to substract |y|*2^n */
t = ldexp(y, n); t = ldexp(y, n);
if (t <= x) x -= t; if (t <= x)
x -= t;
} }
return (neg ? -x : x); return (neg ? -x : x);
} }

View file

@ -14,20 +14,25 @@ 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;
}; };

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

@ -14,38 +14,48 @@ 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;
} }

View file

@ -33,33 +33,41 @@ log(double x)
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;
w = z * z;
x = z + z * w * (POLYNOM2(w, a) / POLYNOM3(w, b)); x = z + z * w * (POLYNOM2(w, a) / POLYNOM3(w, b));
z = exponent; z = exponent;
x += z * (-2.121944400546905827679e-4); x += z * (-2.121944400546905827679e-4);

View file

@ -13,15 +13,18 @@
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

@ -19,81 +19,103 @@ pow(double x, double y)
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

@ -33,25 +33,30 @@ sinus(double x, int cos_flag)
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.
@ -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;
} }
@ -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

@ -34,32 +34,38 @@ sinh_cosh(double x, int cosh_flag)
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 */ /* exp(y) would cause overflow */
#define LNV 0.69316101074218750000e+0 #define LNV 0.69316101074218750000e+0
#define VD2M1 0.52820835025874852469e-4 #define VD2M1 0.52820835025874852469e-4
double w = y - LNV; 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);
@ -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

@ -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

@ -36,17 +36,20 @@ tan(double x)
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.
@ -58,7 +61,8 @@ tan(double x)
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;
@ -71,6 +75,7 @@ tan(double x)
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)
x = -x;
return invert ? -y / x : x / y; return invert ? -y / x : x / y;
} }

View file

@ -32,21 +32,26 @@ tanh(double x)
}; };
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);

View file

@ -18,8 +18,7 @@ typedef void *pointer; /* (void *) if you have it */
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;

View file

@ -41,7 +41,8 @@
#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 */ off_t d_off; /* offset of next disk directory entry */
u_long d_fileno; /* file number of entry */ u_long d_fileno; /* file number of entry */
u_short d_reclen; /* length of this record */ u_short d_reclen; /* length of this record */
@ -146,7 +147,10 @@ NameLen( char name[] ) /* return # chars in embedded name */
#endif #endif
#ifdef UNK #ifdef UNK
static enum { maybe, no, yes } state = maybe; static enum { maybe,
no,
yes } state
= maybe;
/* does _getdents() work? */ /* does _getdents() work? */
/*ARGSUSED*/ /*ARGSUSED*/
@ -157,8 +161,7 @@ sig_catch(int sig) /* sig must be SIGSYS */
} }
#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 */
@ -204,7 +207,8 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
#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;
} }
@ -235,7 +239,8 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
{ /* convert next directory block */ { /* convert next directory block */
int size; int size;
do size = GetBlock( fildes, u.dblk, DIRBLKSIZ ); do
size = GetBlock(fildes, u.dblk, DIRBLKSIZ);
while (size == -1 && errno == EINTR); while (size == -1 && errno == EINTR);
if (size <= 0) if (size <= 0)
@ -243,8 +248,8 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
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)
{ {
@ -255,8 +260,7 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
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])
{ {
@ -268,8 +272,7 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
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);
} }

View file

@ -33,8 +33,7 @@ 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);
@ -45,8 +44,7 @@ setgrent(void)
return _gfd; return _gfd;
} }
void void endgrent(void)
endgrent(void)
{ {
if (_gfd >= 0) if (_gfd >= 0)
_close(_gfd); _close(_gfd);
@ -55,7 +53,6 @@ endgrent(void)
_bufcnt = 0; _bufcnt = 0;
} }
static int static int
getline(void) getline(void)
{ {
@ -63,8 +60,10 @@ getline(void)
return 0; return 0;
_buf = _grbuf; _buf = _grbuf;
do { do
if (--_bufcnt <= 0){ {
if (--_bufcnt <= 0)
{
if ((_bufcnt = _read(_gfd, _buffer, RBUFSIZE)) <= 0) if ((_bufcnt = _read(_gfd, _buffer, RBUFSIZE)) <= 0)
return 0; return 0;
else else

View file

@ -6,53 +6,63 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define ERR(s, c) if(opterr){\ #define ERR(s, c) \
if (opterr) \
{ \
fputs(argv[0], stderr); \ fputs(argv[0], stderr); \
fputs(s, stderr); \ fputs(s, stderr); \
fputc(c, stderr); \ fputc(c, stderr); \
fputc('\n', 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); ERR(": illegal option -- ", c);
if (argv[optind][++sp] == '\0') { if (argv[optind][++sp] == '\0')
{
optind++; optind++;
sp = 1; sp = 1;
} }
return '?'; return '?';
} }
if (*++cp == ':') { if (*++cp == ':')
{
if (argv[optind][sp + 1] != '\0') if (argv[optind][sp + 1] != '\0')
optarg = &argv[optind++][sp + 1]; optarg = &argv[optind++][sp + 1];
else if (++optind >= argc) { else if (++optind >= argc)
{
ERR(": option requires an argument -- ", c); 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

@ -17,8 +17,7 @@ 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

@ -11,9 +11,11 @@ int getw(register FILE *stream)
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

@ -12,17 +12,21 @@ char *mktemp(char *template)
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); *p = '0' + (pid % 10);
pid /= 10; pid /= 10;
} }
p++; p++;
for (k = 'a'; k <= 'z'; k++) { for (k = 'a'; k <= 'z'; k++)
{
*p = k; *p = k;
if (access(template, 0) < 0) { if (access(template, 0) < 0)
{
return template; return template;
} }
} }

View file

@ -29,8 +29,7 @@ extern int _fstat(int fd, struct stat *buf);
#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 */
@ -47,8 +46,8 @@ opendir(const char *dirname) /* name of 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() */

View file

@ -20,7 +20,8 @@ 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
_dup2(int oldd, int newd); /* not present in System 5 */
int _execl(const char* name, ...); int _execl(const char* name, ...);
int _fork(void); int _fork(void);
int _pipe(int fildes[2]); int _pipe(int fildes[2]);
@ -29,23 +30,24 @@ 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);
@ -65,8 +67,7 @@ popen(const char *command, const char *type)
#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,10 +76,13 @@ 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;
@ -95,13 +99,18 @@ _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;

View file

@ -5,15 +5,16 @@
#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

@ -27,21 +27,18 @@ readdir(register DIR *dirp)
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 )
) <= 0
)
return NULL; /* EOF or error */ 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

@ -23,8 +23,7 @@ extern off_t _lseek(int d, int offset, int whence);
#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)
{ {

View file

@ -29,8 +29,7 @@ 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 */
@ -71,15 +70,14 @@ seekdir(register DIR *dirp, register off_t loc)
) )
&& (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 */; for (/* dp initialized above */;
(char*)dp < &dirp->dd_buf[dirp->dd_size]; (char*)dp < &dirp->dd_buf[dirp->dd_size];
dp = (struct dirent *)((char *)dp + dp->d_reclen) 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;
} }
@ -92,14 +90,14 @@ seekdir(register DIR *dirp, register off_t loc)
errno = EINVAL; errno = EINVAL;
return; /* no entry at specified loc */ return; /* no entry at specified loc */
} }
else { /* rewind directory and start over */ else
{ /* rewind directory and start over */
rewind = false; /* but only once! */ rewind = false; /* but only once! */
dirp->dd_loc = dirp->dd_size = 0; dirp->dd_loc = dirp->dd_size = 0;
if (_lseek(dirp->dd_fd, (off_t)0, SEEK_SET) if (_lseek(dirp->dd_fd, (off_t)0, SEEK_SET)
!= 0 != 0)
)
return; /* errno already set (EBADF) */ return; /* errno already set (EBADF) */
if (loc == 0) if (loc == 0)

View file

@ -17,15 +17,16 @@ 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;
@ -33,14 +34,17 @@ sleep(int n)
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

@ -18,8 +18,7 @@ extern off_t _lseek(int d, int offset, int whence);
#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)
{ {

View file

@ -16,23 +16,24 @@
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 0:
case sizeof(sigset_t) <= sizeof(long): break; 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);
} }

View file

@ -9,8 +9,7 @@
#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

@ -5,8 +5,7 @@
#include <stdio.h> #include <stdio.h>
void void(clearerr)(FILE* stream)
(clearerr)(FILE *stream)
{ {
clearerr(stream); clearerr(stream);
} }

View file

@ -15,12 +15,16 @@ 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++;
} }
@ -39,7 +43,8 @@ gnum(register const char *f, int *ip, va_list *app)
#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) \
{ \ { \
@ -57,63 +62,92 @@ o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
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: case FL_SHORT:
if (is_signed) { if (is_signed)
{
signed_val = (short)va_arg(*ap, int); signed_val = (short)va_arg(*ap, int);
} else { }
else
{
unsigned_val = (unsigned short)va_arg(*ap, unsigned); unsigned_val = (unsigned short)va_arg(*ap, unsigned);
} }
break; break;
case FL_LONG: case FL_LONG:
if (is_signed) { if (is_signed)
{
signed_val = va_arg(*ap, long); signed_val = va_arg(*ap, long);
} else { }
else
{
unsigned_val = va_arg(*ap, unsigned long); unsigned_val = va_arg(*ap, unsigned long);
} }
break; break;
default: default:
if (is_signed) { if (is_signed)
{
signed_val = va_arg(*ap, int); signed_val = va_arg(*ap, int);
} else { }
else
{
unsigned_val = va_arg(*ap, unsigned int); unsigned_val = va_arg(*ap, unsigned int);
} }
break; 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 (!unsigned_val)
{
if (!precision) 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':
base = 2;
break;
case 'o':
base = 8;
break;
case 'd': case 'd':
case 'i': case 'i':
case 'u': base = 10; break; case 'u':
base = 10;
break;
case 'x': case 'x':
case 'X': case 'X':
case 'p': base = 16; break; 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,8 +155,7 @@ 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;
@ -131,10 +164,13 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
const char* oldfmt; const char* oldfmt;
char *s1, buf[1025]; char *s1, buf[1025];
while (c = *fmt++) { while (c = *fmt++)
if (c != '%') { {
if (c != '%')
{
#ifdef CPM #ifdef CPM
if (c == '\n') { if (c == '\n')
{
PUTC('\r'); PUTC('\r');
} }
#endif #endif
@ -143,51 +179,84 @@ _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: default:
#ifdef CPM #ifdef CPM
if (c == '\n') { if (c == '\n')
{
PUTC('\r'); PUTC('\r');
nrchars++; nrchars++;
} }
@ -208,7 +277,8 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
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') if (*s == '\0')
break; break;
s++; s++;
@ -223,15 +293,19 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
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;
else if (c != 'p')
flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 0); s = o_print(&ap, flags, s, c, precision, 0);
break; break;
case 'd': case 'd':
case 'i': case 'i':
flags |= FL_SIGNEDCONV; flags |= FL_SIGNEDCONV;
if (!(flags & FL_PRECSPEC)) precision = 1; if (!(flags & FL_PRECSPEC))
else flags &= ~FL_ZEROFILL; precision = 1;
else
flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 1); s = o_print(&ap, flags, s, c, precision, 1);
break; break;
case 'c': case 'c':
@ -261,7 +335,8 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
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:
@ -280,29 +355,39 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
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)
{
j--;
nrchars++;
PUTC(*s1++); PUTC(*s1++);
} else { }
j -= 2; nrchars += 2; else
{
j -= 2;
nrchars += 2;
PUTC(*s1++); PUTC(*s1++);
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

@ -37,52 +37,72 @@ o_collect(register int c, register FILE *stream, char type,
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 'i': /* i means octal, decimal or hexadecimal */
case 'p': case 'p':
case 'x': case 'x':
case 'X': base = 16; break; case 'X':
base = 16;
break;
case 'd': case 'd':
case 'u': base = 10; break; case 'u':
case 'o': base = 8; break; base = 10;
case 'b': base = 2; break; break;
case 'o':
base = 8;
break;
case 'b':
base = 2;
break;
} }
if (c == '-' || c == '+') { if (c == '-' || c == '+')
{
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
} }
if (width && c == '0' && base == 16) { 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 (c != 'x' && c != 'X')
if (type == 'i') base = 8; {
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;
@ -103,23 +123,27 @@ 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; *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)
@ -127,46 +151,53 @@ f_collect(register int c, register FILE *stream, register int width)
} }
} }
if (!digit_seen) { if (!digit_seen)
if (width && c != EOF) ungetc(c, stream); {
if (width && c != EOF)
ungetc(c, stream);
return inp_buf - 1; return inp_buf - 1;
} }
else digit_seen = 0; else
digit_seen = 0;
if (width && (c == 'e' || c == 'E')) { if (width && (c == 'e' || c == 'E'))
{
*bufp++ = c; *bufp++ = c;
if (--width) if (--width)
c = getc(stream); c = getc(stream);
if (width && (c == '+' || c == '-')) { if (width && (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 (!digit_seen) { if (!digit_seen)
if (width && c != EOF) ungetc(c,stream); {
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 */
@ -184,78 +215,110 @@ _doscan(register FILE *stream, const char *format, va_list ap)
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: default:
/* not recognized, like %q */ /* not recognized, like %q */
return conv || (ic != EOF) ? done : EOF; return conv || (ic != EOF) ? done : EOF;
break; break;
case 'n': case 'n':
if (!(flags & FL_NOASSIGN)) { /* silly, though */ if (!(flags & FL_NOASSIGN))
{ /* silly, though */
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)
@ -276,13 +339,15 @@ _doscan(register FILE *stream, const char *format, va_list ap)
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
@ -290,7 +355,8 @@ _doscan(register FILE *stream, const char *format, va_list ap)
*/ */
nrchars += str - inp_buf; nrchars += str - inp_buf;
if (!(flags & FL_NOASSIGN)) { if (!(flags & FL_NOASSIGN))
{
if (kind == 'd' || kind == 'i') if (kind == 'd' || kind == 'i')
val = strtol(inp_buf, &tmp_string, base); val = strtol(inp_buf, &tmp_string, base);
else else
@ -308,19 +374,24 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 1; width = 1;
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char*); str = va_arg(ap, char*);
if (!width) return done; if (!width)
return done;
while (width && ic != EOF) { while (width && ic != EOF)
{
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*str++ = (char)ic; *str++ = (char)ic;
if (--width) { if (--width)
{
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
} }
} }
if (width) { if (width)
if (ic != EOF) ungetc(ic,stream); {
if (ic != EOF)
ungetc(ic, stream);
nrchars--; nrchars--;
} }
break; break;
@ -329,12 +400,15 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 0xffff; width = 0xffff;
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char*); str = va_arg(ap, char*);
if (!width) return done; if (!width)
return done;
while (width && ic != EOF && !isspace(ic)) { while (width && ic != EOF && !isspace(ic))
{
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*str++ = (char)ic; *str++ = (char)ic;
if (--width) { if (--width)
{
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
} }
@ -342,67 +416,82 @@ _doscan(register FILE *stream, const char *format, va_list ap)
/* terminate the string */ /* terminate the string */
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*str = '\0'; *str = '\0';
if (width) { if (width)
if (ic != EOF) ungetc(ic,stream); {
if (ic != EOF)
ungetc(ic, stream);
nrchars--; nrchars--;
} }
break; break;
case '[': case '[':
if (!(flags & FL_WIDTHSPEC)) if (!(flags & FL_WIDTHSPEC))
width = 0xffff; width = 0xffff;
if (!width) return done; if (!width)
return done;
if ( *++format == '^' ) { if (*++format == '^')
{
reverse = 1; reverse = 1;
format++; format++;
} else }
else
reverse = 0; reverse = 0;
for (str = Xtable; str < &Xtable[NR_CHARS] for (str = Xtable; str < &Xtable[NR_CHARS]; str++)
; str++)
*str = 0; *str = 0;
if (*format == ']') Xtable[*format++] = 1; if (*format == ']')
while (*format && *format != ']') {
Xtable[*format++] = 1; Xtable[*format++] = 1;
if (*format == '-') {
while (*format && *format != ']')
{
Xtable[*format++] = 1;
if (*format == '-')
{
format++; format++;
if (*format if (*format
&& *format != ']' && *format != ']'
&& *(format) >= *(format -2)) { && *(format) >= *(format - 2))
{
int c; int c;
for( c = *(format -2) + 1 for (c = *(format - 2) + 1; c <= *format; c++)
; c <= *format ; c++)
Xtable[c] = 1; Xtable[c] = 1;
format++; format++;
} }
else Xtable['-'] = 1; else
Xtable['-'] = 1;
} }
} }
if (!*format || !(Xtable[ic] ^ reverse)) { if (!*format || !(Xtable[ic] ^ reverse))
if (ic != EOF) ungetc(ic, stream); {
if (ic != EOF)
ungetc(ic, stream);
return done; return done;
} }
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char*); str = va_arg(ap, char*);
do { do
{
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*str++ = (char)ic; *str++ = (char)ic;
if (--width) { if (--width)
{
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
} }
} while (width && ic != EOF && (Xtable[ic] ^ reverse)); } while (width && ic != EOF && (Xtable[ic] ^ reverse));
if (width) { if (width)
if (ic != EOF) ungetc(ic, stream); {
if (ic != EOF)
ungetc(ic, stream);
nrchars--; nrchars--;
} }
if (!(flags & FL_NOASSIGN)) { /* terminate string */ if (!(flags & FL_NOASSIGN))
{ /* terminate string */
*str = '\0'; *str = '\0';
} }
break; break;
@ -415,13 +504,15 @@ _doscan(register FILE *stream, const char *format, va_list ap)
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 = f_collect(ic, stream, width); str = f_collect(ic, stream, width);
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
@ -429,12 +520,12 @@ _doscan(register FILE *stream, const char *format, va_list ap)
*/ */
nrchars += str - inp_buf; nrchars += str - inp_buf;
if (!(flags & FL_NOASSIGN)) { if (!(flags & FL_NOASSIGN))
{
ld_val = strtod(inp_buf, &tmp_string); ld_val = strtod(inp_buf, &tmp_string);
if (flags & FL_LONGDOUBLE) if (flags & FL_LONGDOUBLE)
*va_arg(ap, long double*) = (long double)ld_val; *va_arg(ap, long double*) = (long double)ld_val;
else else if (flags & FL_LONG)
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;
@ -443,7 +534,8 @@ _doscan(register FILE *stream, const char *format, va_list ap)
#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

@ -17,15 +17,13 @@ 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);
} }

View file

@ -8,20 +8,22 @@
#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 (close(fileno(fp)))
retval = EOF;
if (io_testflag(fp, _IOMYBUF) && fp->_buf) if (io_testflag(fp, _IOMYBUF) && fp->_buf)
free((void*)fp->_buf); free((void*)fp->_buf);
if (fp != stdin && fp != stdout && fp != stderr) if (fp != stdin && fp != stdout && fp != stderr)

View file

@ -7,19 +7,20 @@
#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)
return (FILE*)NULL;
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': case 'r':
flags |= _IOREAD | _IOREADING; flags |= _IOREAD | _IOREADING;
break; break;
@ -31,8 +32,10 @@ fdopen(int fd, const char *mode)
default: default:
return (FILE*)NULL; return (FILE*)NULL;
} }
while(*mode) { while (*mode)
switch(*mode++) { {
switch (*mode++)
{
case 'b': case 'b':
continue; continue;
case '+': case '+':
@ -45,7 +48,8 @@ fdopen(int fd, const char *mode)
break; break;
} }
if ((stream = (FILE *) malloc(sizeof(FILE))) == NULL) { if ((stream = (FILE*)malloc(sizeof(FILE))) == NULL)
{
return (FILE*)NULL; return (FILE*)NULL;
} }

View file

@ -5,8 +5,7 @@
#include <stdio.h> #include <stdio.h>
int int(feof)(FILE* stream)
(feof)(FILE *stream)
{ {
return feof(stream); return feof(stream);
} }

View file

@ -5,8 +5,7 @@
#include <stdio.h> #include <stdio.h>
int int(ferror)(FILE* stream)
(ferror)(FILE *stream)
{ {
return ferror(stream); return ferror(stream);
} }

View file

@ -8,12 +8,12 @@
#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++) for (i = 0; i < FOPEN_MAX; i++)
if (__iotab[i] && fflush(__iotab[i])) if (__iotab[i] && fflush(__iotab[i]))
retval = EOF; retval = EOF;
@ -24,7 +24,8 @@ fflush(FILE *stream)
|| (!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))
@ -35,7 +36,9 @@ fflush(FILE *stream)
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;
@ -46,8 +49,10 @@ fflush(FILE *stream)
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;
} }
@ -63,8 +68,7 @@ fflush(FILE *stream)
return EOF; return EOF;
} }
void void __cleanup(void)
__cleanup(void)
{ {
register int i; register int i;

View file

@ -5,8 +5,7 @@
#include <stdio.h> #include <stdio.h>
int int fgetc(FILE* stream)
fgetc(FILE *stream)
{ {
return getc(stream); return getc(stream);
} }

View file

@ -5,10 +5,10 @@
#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

@ -5,22 +5,27 @@
#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

@ -5,8 +5,7 @@
#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); stream->_buf = (unsigned char*)malloc(BUFSIZ);
if (!stream->_buf) { 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

@ -27,18 +27,23 @@ _pfloat(long double r, register char *s, int n, int flags)
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++;
else
*s++ = '0';
if (((i = n) > 0) || (flags & FL_ALT)) 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;
} }
@ -60,16 +65,24 @@ _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++ = '-';
dp = -dp;
}
else
{
*s++ = '+'; *s++ = '+';
} }
if (dp >= 100) { if (dp >= 100)
{
*s++ = '0' + (dp / 100); *s++ = '0' + (dp / 100);
dp %= 100; dp %= 100;
} }
@ -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,61 +116,77 @@ _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)
}
if (i <= dp) {
while (i++ <= dp) *s2++ = '0';
*s2++ = '.'; *s2++ = '.';
} }
if ((s2[-1]=='.') && !(flags & FL_ALT)) s2--; if (i <= dp)
{
while (i++ <= dp)
*s2++ = '0';
*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': case 'f':
s = _pfloat(ld_val, s, precision, flags); s = _pfloat(ld_val, s, precision, flags);
break; break;
@ -170,9 +200,12 @@ _f_print(va_list *ap, int flags, char *s, char c, int precision)
s += strlen(s); s += strlen(s);
break; 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;
} }

View file

@ -18,100 +18,129 @@ do_write(int d, char *buf, int nbytes)
/* 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 { }
else
{
stream->_flags |= _IOLBF | _IOMYBUF; 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;

View file

@ -31,8 +31,7 @@
* 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;
@ -43,7 +42,8 @@ fopen(const char *name, const char *mode)
if (i >= FOPEN_MAX - 1) if (i >= FOPEN_MAX - 1)
return (FILE*)NULL; return (FILE*)NULL;
switch(*mode++) { switch (*mode++)
{
case 'r': case 'r':
flags |= _IOREAD | _IOREADING; flags |= _IOREAD | _IOREADING;
rwmode = O_RDONLY; rwmode = O_RDONLY;
@ -62,8 +62,10 @@ fopen(const char *name, const char *mode)
return (FILE*)NULL; return (FILE*)NULL;
} }
while (*mode) { while (*mode)
switch(*mode++) { {
switch (*mode++)
{
case 'b': case 'b':
continue; continue;
case '+': case '+':
@ -82,17 +84,20 @@ 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) { {
if (((fd = creat(name, PMODE)) > 0) && flags | _IOREAD)
{
(void)close(fd); (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;
} }

View file

@ -7,8 +7,7 @@
#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;

View file

@ -5,8 +5,7 @@
#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

@ -5,14 +5,15 @@
#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

@ -14,9 +14,11 @@ fread(void *ptr, size_t size, size_t nmemb, register FILE *stream)
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

@ -15,8 +15,7 @@
* 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;
@ -25,7 +24,8 @@ freopen(const char *name, const char *mode, FILE *stream)
(void)fflush(stream); /* ignore errors */ (void)fflush(stream); /* ignore errors */
(void)close(fileno(stream)); (void)close(fileno(stream));
switch(*mode++) { switch (*mode++)
{
case 'r': case 'r':
flags |= _IOREAD; flags |= _IOREAD;
rwmode = O_RDONLY; rwmode = O_RDONLY;
@ -44,8 +44,10 @@ freopen(const char *name, const char *mode, FILE *stream)
return (FILE*)NULL; return (FILE*)NULL;
} }
while (*mode) { while (*mode)
switch(*mode++) { {
switch (*mode++)
{
case 'b': case 'b':
continue; continue;
case '+': case '+':
@ -61,16 +63,21 @@ freopen(const char *name, const char *mode, FILE *stream)
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) { {
if (((fd = creat(name, PMODE)) < 0) && flags | _IOREAD)
{
(void)close(fd); (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;
} }

View file

@ -7,8 +7,7 @@
#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,15 +16,19 @@ 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 */ }
else /* neither reading nor writing. The buffer must be empty */
/* EMPTY */; /* EMPTY */;
pos = lseek(fileno(stream), offset - adjust, whence); pos = lseek(fileno(stream), offset - adjust, whence);

View file

@ -5,8 +5,7 @@
#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

@ -19,7 +19,8 @@ long ftell(FILE *stream)
&& 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);

View file

@ -14,15 +14,16 @@ fwrite(const void *ptr, size_t size, size_t nmemb,
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

@ -5,8 +5,7 @@
#include <stdio.h> #include <stdio.h>
int int(getc)(FILE* stream)
(getc)(FILE *stream)
{ {
return getc(stream); return getc(stream);
} }

View file

@ -5,8 +5,7 @@
#include <stdio.h> #include <stdio.h>
int int(getchar)(void)
(getchar)(void)
{ {
return getchar(); return getchar();
} }

View file

@ -5,8 +5,7 @@
#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;
@ -16,10 +15,15 @@ gets(char *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

@ -7,8 +7,7 @@
/* 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;

View file

@ -7,10 +7,10 @@
#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(s, stderr);
(void)fputs(": ", stderr); (void)fputs(": ", stderr);
} }

View file

@ -7,8 +7,7 @@
#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

@ -5,8 +5,7 @@
#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

@ -5,8 +5,7 @@
#include <stdio.h> #include <stdio.h>
int int(putchar)(int c)
(putchar)(int c)
{ {
return putchar(c); return putchar(c);
} }

View file

@ -5,16 +5,19 @@
#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

@ -6,8 +6,7 @@
#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

@ -7,8 +7,7 @@
#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

@ -6,8 +6,7 @@
#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

@ -9,8 +9,7 @@
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;
@ -23,11 +22,16 @@ setvbuf(register FILE *stream, char *buf, int mode, size_t size)
stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF); stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
if (buf && size <= 0) retval = EOF; if (buf && size <= 0)
if (!buf && (mode != _IONBF)) {
if (size <= 0 || (buf = (char *) malloc(size)) == NULL) {
retval = EOF; retval = EOF;
} else { if (!buf && (mode != _IONBF))
{
if (size <= 0 || (buf = (char*)malloc(size)) == NULL)
{
retval = EOF;
}
else
{
stream->_flags |= _IOMYBUF; stream->_flags |= _IOMYBUF;
} }
} }
@ -38,9 +42,12 @@ setvbuf(register FILE *stream, char *buf, int mode, size_t size)
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

@ -7,8 +7,7 @@
#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;

View file

@ -7,8 +7,7 @@
#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;

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)
return (FILE*)NULL;
(void)remove(name_buffer); (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

@ -6,15 +6,16 @@
#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++;

View file

@ -7,8 +7,7 @@
#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

@ -7,8 +7,7 @@
#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

@ -7,8 +7,7 @@
#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;

View file

@ -7,8 +7,7 @@
#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;

View file

@ -12,10 +12,9 @@
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

@ -6,8 +6,7 @@
#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;
} }

View file

@ -7,8 +7,7 @@
extern void (*__functab[NEXITS])(void); extern void (*__functab[NEXITS])(void);
extern int __funccnt; extern int __funccnt;
int int atexit(void (*func)(void))
atexit(void (*func)(void))
{ {
if (__funccnt >= NEXITS) if (__funccnt >= NEXITS)
return 1; return 1;

View file

@ -7,8 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
double double(atof)(const char* nptr)
(atof)(const char *nptr)
{ {
double d; double d;
int e = errno; int e = errno;

View file

@ -9,19 +9,22 @@
/* We do not use strtol here for backwards compatibility in behaviour on /* We do not use strtol here for backwards compatibility in behaviour on
overflow. overflow.
*/ */
int int atoi(register const char* nptr)
atoi(register const char *nptr)
{ {
int total = 0; int total = 0;
int minus = 0; int minus = 0;
while (isspace(*nptr)) nptr++; while (isspace(*nptr))
if (*nptr == '+') nptr++; nptr++;
else if (*nptr == '-') { if (*nptr == '+')
nptr++;
else if (*nptr == '-')
{
minus = 1; minus = 1;
nptr++; nptr++;
} }
while (isdigit(*nptr)) { while (isdigit(*nptr))
{
total *= 10; total *= 10;
total += (*nptr++ - '0'); total += (*nptr++ - '0');
} }

View file

@ -9,19 +9,22 @@
/* We do not use strtol here for backwards compatibility in behaviour on /* We do not use strtol here for backwards compatibility in behaviour on
overflow. overflow.
*/ */
long long atol(register const char* nptr)
atol(register const char *nptr)
{ {
long total = 0; long total = 0;
int minus = 0; int minus = 0;
while (isspace(*nptr)) nptr++; while (isspace(*nptr))
if (*nptr == '+') nptr++; nptr++;
else if (*nptr == '-') { if (*nptr == '+')
nptr++;
else if (*nptr == '-')
{
minus = 1; minus = 1;
nptr++; nptr++;
} }
while (isdigit(*nptr)) { while (isdigit(*nptr))
{
total *= 10; total *= 10;
total += (*nptr++ - '0'); total += (*nptr++ - '0');
} }

View file

@ -6,22 +6,24 @@
#include <stdlib.h> #include <stdlib.h>
void * void* bsearch(register const void* key, register const void* base,
bsearch(register const void *key, register const void *base,
register size_t nmemb, register size_t size, register size_t nmemb, register size_t size,
int (*compar)(const void*, const void*)) int (*compar)(const void*, const void*))
{ {
register const void* mid_point; register const void* mid_point;
register int cmp; register int cmp;
while (nmemb > 0) { while (nmemb > 0)
{
mid_point = (char*)base + size * (nmemb >> 1); mid_point = (char*)base + size * (nmemb >> 1);
if ((cmp = (*compar)(key, mid_point)) == 0) if ((cmp = (*compar)(key, mid_point)) == 0)
return (void*)mid_point; return (void*)mid_point;
if (cmp >= 0) { if (cmp >= 0)
{
base = (char*)mid_point + size; base = (char*)mid_point + size;
nmemb = (nmemb - 1) >> 1; nmemb = (nmemb - 1) >> 1;
} else }
else
nmemb >>= 1; nmemb >>= 1;
} }
return (void*)NULL; return (void*)NULL;

View file

@ -6,15 +6,15 @@
#include <stdlib.h> #include <stdlib.h>
div_t div_t div(register int numer, register int denom)
div(register int numer, register int denom)
{ {
div_t r; div_t r;
r.quot = numer / denom; /* might trap if denom == 0 */ r.quot = numer / denom; /* might trap if denom == 0 */
r.rem = numer % denom; r.rem = numer % denom;
if (r.rem != 0 && (numer > 0) != (r.rem > 0)) { if (r.rem != 0 && (numer > 0) != (r.rem > 0))
{
r.quot++; r.quot++;
r.rem -= denom; r.rem -= denom;
} }

View file

@ -26,10 +26,10 @@ _calls(void)
(*__functab[i])(); (*__functab[i])();
} }
void void exit(int status)
exit(int status)
{ {
_calls(); _calls();
if (_clean) _clean(); if (_clean)
_clean();
_exit(status); _exit(status);
} }

View file

@ -20,8 +20,7 @@
static int b64_add(struct mantissa* e1, struct mantissa* e2); static int b64_add(struct mantissa* e1, struct mantissa* e2);
static b64_sft(struct mantissa* e1, int n); static b64_sft(struct mantissa* e1, int n);
static static mul_ext(struct EXTEND* e1, struct EXTEND* e2, struct EXTEND* e3)
mul_ext(struct EXTEND *e1, struct EXTEND *e2, struct EXTEND *e3)
{ {
/* Multiply the extended numbers e1 and e2, and put the /* Multiply the extended numbers e1 and e2, and put the
result in e3. result in e3.
@ -53,29 +52,37 @@ mul_ext(struct EXTEND *e1, struct EXTEND *e2, struct EXTEND *e3)
mc[1] = (unsigned short)e2->m1; mc[1] = (unsigned short)e2->m1;
mc[2] = e2->m2 >> 16; mc[2] = e2->m2 >> 16;
mc[3] = (unsigned short)e2->m2; mc[3] = (unsigned short)e2->m2;
for (i = 8; i--;) { for (i = 8; i--;)
{
result[i] = 0; result[i] = 0;
} }
/* /*
* fill registers with their components * fill registers with their components
*/ */
for(i=4, pres = &result[4];i--;pres--) if (mp[i]) { for (i = 4, pres = &result[4]; i--; pres--)
if (mp[i])
{
unsigned short k = 0; unsigned short k = 0;
unsigned long mpi = mp[i]; unsigned long mpi = mp[i];
for(j=4;j--;) { for (j = 4; j--;)
{
unsigned long tmp = (unsigned long)pres[j] + k; unsigned long tmp = (unsigned long)pres[j] + k;
if (mc[j]) tmp += mpi * mc[j]; if (mc[j])
tmp += mpi * mc[j];
pres[j] = tmp; pres[j] = tmp;
k = tmp >> 16; k = tmp >> 16;
} }
pres[-1] = k; pres[-1] = k;
} }
if (! (result[0] & 0x8000)) { if (!(result[0] & 0x8000))
{
e3->exp--; e3->exp--;
for (i = 0; i <= 3; i++) { for (i = 0; i <= 3; i++)
{
result[i] <<= 1; result[i] <<= 1;
if (result[i+1]&0x8000) result[i] |= 1; if (result[i + 1] & 0x8000)
result[i] |= 1;
} }
result[4] <<= 1; result[4] <<= 1;
} }
@ -84,9 +91,12 @@ mul_ext(struct EXTEND *e1, struct EXTEND *e2, struct EXTEND *e3)
*/ */
e3->m1 = ((unsigned long)(result[0]) << 16) + result[1]; e3->m1 = ((unsigned long)(result[0]) << 16) + result[1];
e3->m2 = ((unsigned long)(result[2]) << 16) + result[3]; e3->m2 = ((unsigned long)(result[2]) << 16) + result[3];
if (result[4] & 0x8000) { if (result[4] & 0x8000)
if (++e3->m2 == 0) { {
if (++e3->m1 == 0) { if (++e3->m2 == 0)
{
if (++e3->m1 == 0)
{
e3->m1 = 0x80000000; e3->m1 = 0x80000000;
e3->exp++; e3->exp++;
} }
@ -94,8 +104,7 @@ mul_ext(struct EXTEND *e1, struct EXTEND *e2, struct EXTEND *e3)
} }
} }
static static add_ext(struct EXTEND* e1, struct EXTEND* e2, struct EXTEND* e3)
add_ext(struct EXTEND *e1, struct EXTEND *e2, struct EXTEND *e3)
{ {
/* Add two extended numbers e1 and e2, and put the result /* Add two extended numbers e1 and e2, and put the result
in e3 in e3
@ -103,11 +112,13 @@ add_ext(struct EXTEND *e1, struct EXTEND *e2, struct EXTEND *e3)
struct EXTEND ce2; struct EXTEND ce2;
int diff; int diff;
if ((e2->m1 | e2->m2) == 0L) { if ((e2->m1 | e2->m2) == 0L)
{
*e3 = *e1; *e3 = *e1;
return; return;
} }
if ((e1->m1 | e1->m2) == 0L) { if ((e1->m1 | e1->m2) == 0L)
{
*e3 = *e2; *e3 = *e2;
return; return;
} }
@ -117,52 +128,66 @@ add_ext(struct EXTEND *e1, struct EXTEND *e2, struct EXTEND *e3)
/* adjust mantissas to equal power */ /* adjust mantissas to equal power */
diff = e3->exp - e1->exp; diff = e3->exp - e1->exp;
if (diff < 0) { if (diff < 0)
{
diff = -diff; diff = -diff;
e3->exp += diff; e3->exp += diff;
b64_sft(&(e3->mantissa), diff); b64_sft(&(e3->mantissa), diff);
} }
else if (diff > 0) { else if (diff > 0)
{
e1->exp += diff; e1->exp += diff;
b64_sft(&(e1->mantissa), diff); b64_sft(&(e1->mantissa), diff);
} }
if (e1->sign != e3->sign) { if (e1->sign != e3->sign)
{
/* e3 + e1 = e3 - (-e1) */ /* e3 + e1 = e3 - (-e1) */
if (e1->m1 > e3->m1 || if (e1->m1 > e3->m1 || (e1->m1 == e3->m1 && e1->m2 > e3->m2))
(e1->m1 == e3->m1 && e1->m2 > e3->m2)) { {
/* abs(e1) > abs(e3) */ /* abs(e1) > abs(e3) */
if (e3->m2 > e1->m2) { if (e3->m2 > e1->m2)
{
e1->m1 -= 1; /* carry in */ e1->m1 -= 1; /* carry in */
} }
e1->m1 -= e3->m1; e1->m1 -= e3->m1;
e1->m2 -= e3->m2; e1->m2 -= e3->m2;
*e3 = *e1; *e3 = *e1;
} }
else { else
{
if (e1->m2 > e3->m2) if (e1->m2 > e3->m2)
e3->m1 -= 1; /* carry in */ e3->m1 -= 1; /* carry in */
e3->m1 -= e1->m1; e3->m1 -= e1->m1;
e3->m2 -= e1->m2; e3->m2 -= e1->m2;
} }
} }
else { else
if (b64_add(&e3->mantissa,&e1->mantissa)) {/* addition carry */ {
if (b64_add(&e3->mantissa, &e1->mantissa))
{ /* addition carry */
b64_sft(&e3->mantissa, 1); /* shift mantissa one bit RIGHT */ b64_sft(&e3->mantissa, 1); /* shift mantissa one bit RIGHT */
e3->m1 |= 0x80000000L; /* set max bit */ e3->m1 |= 0x80000000L; /* set max bit */
e3->exp++; /* increase the exponent */ e3->exp++; /* increase the exponent */
} }
} }
if ((e3->m2 | e3->m1) != 0L) { if ((e3->m2 | e3->m1) != 0L)
{
/* normalize */ /* normalize */
if (e3->m1 == 0L) { if (e3->m1 == 0L)
e3->m1 = e3->m2; e3->m2 = 0L; e3->exp -= 32; {
e3->m1 = e3->m2;
e3->m2 = 0L;
e3->exp -= 32;
} }
if (!(e3->m1 & 0x80000000)) { if (!(e3->m1 & 0x80000000))
{
unsigned long l = 0x40000000; unsigned long l = 0x40000000;
int cnt = -1; int cnt = -1;
while (! (l & e3->m1)) { while (!(l & e3->m1))
l >>= 1; cnt--; {
l >>= 1;
cnt--;
} }
e3->exp += cnt; e3->exp += cnt;
b64_sft(&(e3->mantissa), cnt); b64_sft(&(e3->mantissa), cnt);
@ -178,28 +203,34 @@ cmp_ext(struct EXTEND *e1, struct EXTEND *e2)
e2->sign = !e2->sign; e2->sign = !e2->sign;
add_ext(e1, e2, &tmp); add_ext(e1, e2, &tmp);
e2->sign = !e2->sign; e2->sign = !e2->sign;
if (tmp.m1 == 0 && tmp.m2 == 0) return 0; if (tmp.m1 == 0 && tmp.m2 == 0)
if (tmp.sign) return -1; return 0;
if (tmp.sign)
return -1;
return 1; return 1;
} }
static static b64_sft(struct mantissa* e1, int n)
b64_sft(struct mantissa *e1, int n) {
if (n > 0)
{
if (n > 63)
{ {
if (n > 0) {
if (n > 63) {
e1->l_32 = 0; e1->l_32 = 0;
e1->h_32 = 0; e1->h_32 = 0;
return; return;
} }
if (n >= 32) { if (n >= 32)
{
e1->l_32 = e1->h_32; e1->l_32 = e1->h_32;
e1->h_32 = 0; e1->h_32 = 0;
n -= 32; n -= 32;
} }
if (n > 0) { if (n > 0)
{
e1->l_32 >>= n; e1->l_32 >>= n;
if (e1->h_32 != 0) { if (e1->h_32 != 0)
{
e1->l_32 |= (e1->h_32 << (32 - n)); e1->l_32 |= (e1->h_32 << (32 - n));
e1->h_32 >>= n; e1->h_32 >>= n;
} }
@ -207,20 +238,25 @@ b64_sft(struct mantissa *e1, int n)
return; return;
} }
n = -n; n = -n;
if (n > 0) { if (n > 0)
if (n > 63) { {
if (n > 63)
{
e1->l_32 = 0; e1->l_32 = 0;
e1->h_32 = 0; e1->h_32 = 0;
return; return;
} }
if (n >= 32) { if (n >= 32)
{
e1->h_32 = e1->l_32; e1->h_32 = e1->l_32;
e1->l_32 = 0; e1->l_32 = 0;
n -= 32; n -= 32;
} }
if (n > 0) { if (n > 0)
{
e1->h_32 <<= n; e1->h_32 <<= n;
if (e1->l_32 != 0) { if (e1->l_32 != 0)
{
e1->h_32 |= (e1->l_32 >> (32 - n)); e1->h_32 |= (e1->l_32 >> (32 - n));
e1->l_32 <<= n; e1->l_32 <<= n;
} }
@ -436,21 +472,23 @@ static struct EXTEND r_big_ten_powers[] = { /* representation of 10 ** -(28*i) *
#define BTP (int)(sizeof(big_ten_powers) / sizeof(big_ten_powers[0])) #define BTP (int)(sizeof(big_ten_powers) / sizeof(big_ten_powers[0]))
#define MAX_EXP (TP * BTP - 1) #define MAX_EXP (TP * BTP - 1)
static static add_exponent(struct EXTEND* e, int exp)
add_exponent(struct EXTEND *e, int exp)
{ {
int neg = exp < 0; int neg = exp < 0;
int divsz, modsz; int divsz, modsz;
struct EXTEND x; struct EXTEND x;
if (neg) exp = -exp; if (neg)
exp = -exp;
divsz = exp / TP; divsz = exp / TP;
modsz = exp % TP; modsz = exp % TP;
if (neg) { if (neg)
{
mul_ext(e, &r_ten_powers[modsz], &x); mul_ext(e, &r_ten_powers[modsz], &x);
mul_ext(&x, &r_big_ten_powers[divsz], e); mul_ext(&x, &r_big_ten_powers[divsz], e);
} }
else { else
{
mul_ext(e, &ten_powers[modsz], &x); mul_ext(e, &ten_powers[modsz], &x);
mul_ext(&x, &big_ten_powers[divsz], e); mul_ext(&x, &big_ten_powers[divsz], e);
} }
@ -464,24 +502,30 @@ _str_ext_cvt(const char *s, char **ss, struct EXTEND *e)
int digitseen = 0; int digitseen = 0;
int exp = 0; int exp = 0;
if (ss) *ss = (char *)s; if (ss)
while (isspace(*s)) s++; *ss = (char*)s;
while (isspace(*s))
s++;
e->sign = 0; e->sign = 0;
e->exp = 0; e->exp = 0;
e->m1 = e->m2 = 0; e->m1 = e->m2 = 0;
c = *s; c = *s;
switch(c) { switch (c)
{
case '-': case '-':
e->sign = 1; e->sign = 1;
case '+': case '+':
s++; s++;
} }
while (c = *s++, isdigit(c) || (c == '.' && ! dotseen++)) { while (c = *s++, isdigit(c) || (c == '.' && !dotseen++))
if (c == '.') continue; {
if (c == '.')
continue;
digitseen = 1; digitseen = 1;
if (e->m1 <= (unsigned long)(0xFFFFFFFF)/10) { if (e->m1 <= (unsigned long)(0xFFFFFFFF) / 10)
{
struct mantissa a1; struct mantissa a1;
a1 = e->mantissa; a1 = e->mantissa;
@ -492,45 +536,58 @@ _str_ext_cvt(const char *s, char **ss, struct EXTEND *e)
a1.l_32 = c - '0'; a1.l_32 = c - '0';
b64_add(&(e->mantissa), &a1); b64_add(&(e->mantissa), &a1);
} }
else exp++; else
if (dotseen) exp--; exp++;
if (dotseen)
exp--;
} }
if (! digitseen) return; if (!digitseen)
return;
if (ss) *ss = (char *)s - 1; if (ss)
*ss = (char*)s - 1;
if (c == 'E' || c == 'e') { if (c == 'E' || c == 'e')
{
int exp1 = 0; int exp1 = 0;
int sign = 1; int sign = 1;
int exp_overflow = 0; int exp_overflow = 0;
switch(*s) { switch (*s)
{
case '-': case '-':
sign = -1; sign = -1;
case '+': case '+':
s++; s++;
} }
if (c = *s, isdigit(c)) { if (c = *s, isdigit(c))
do { {
do
{
int tmp; int tmp;
exp1 = 10 * exp1 + (c - '0'); exp1 = 10 * exp1 + (c - '0');
if ((tmp = sign * exp1 + exp) > MAX_EXP || if ((tmp = sign * exp1 + exp) > MAX_EXP || tmp < -MAX_EXP)
tmp < -MAX_EXP) { {
exp_overflow = 1; exp_overflow = 1;
} }
} while (c = *++s, isdigit(c)); } while (c = *++s, isdigit(c));
if (ss) *ss = (char *)s; if (ss)
*ss = (char*)s;
} }
exp += sign * exp1; exp += sign * exp1;
if (exp_overflow) { if (exp_overflow)
{
exp = sign * MAX_EXP; exp = sign * MAX_EXP;
if (e->m1 != 0 || e->m2 != 0) errno = ERANGE; if (e->m1 != 0 || e->m2 != 0)
errno = ERANGE;
} }
} }
if (e->m1 == 0 && e->m2 == 0) return; if (e->m1 == 0 && e->m2 == 0)
return;
e->exp = 63; e->exp = 63;
while (! (e->m1 & 0x80000000)) { while (!(e->m1 & 0x80000000))
{
b64_sft(&(e->mantissa), -1); b64_sft(&(e->mantissa), -1);
e->exp--; e->exp--;
} }
@ -539,8 +596,7 @@ _str_ext_cvt(const char *s, char **ss, struct EXTEND *e)
#include <math.h> #include <math.h>
static static ten_mult(struct EXTEND* e)
ten_mult(struct EXTEND *e)
{ {
struct EXTEND e1 = *e; struct EXTEND e1 = *e;
@ -552,8 +608,7 @@ ten_mult(struct EXTEND *e)
#define NDIGITS 128 #define NDIGITS 128
#define NSIGNIFICANT 19 #define NSIGNIFICANT 19
char * char* _ext_str_cvt(struct EXTEND* e, int ndigit, int* decpt, int* sign, int ecvtflag)
_ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int *sign, int ecvtflag)
{ {
/* Like cvt(), but for extended precision */ /* Like cvt(), but for extended precision */
@ -563,39 +618,48 @@ _ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int *sign, int ecvtflag)
register char* pe; register char* pe;
int findex = 0; int findex = 0;
if (ndigit < 0) ndigit = 0; if (ndigit < 0)
if (ndigit > NDIGITS) ndigit = NDIGITS; ndigit = 0;
if (ndigit > NDIGITS)
ndigit = NDIGITS;
pe = &buf[ndigit]; pe = &buf[ndigit];
buf[0] = '\0'; buf[0] = '\0';
*sign = 0; *sign = 0;
if (e->sign) { if (e->sign)
{
*sign = 1; *sign = 1;
e->sign = 0; e->sign = 0;
} }
*decpt = 0; *decpt = 0;
if (e->m1 != 0) { if (e->m1 != 0)
{
register struct EXTEND* pp = &big_ten_powers[1]; register struct EXTEND* pp = &big_ten_powers[1];
while(cmp_ext(e,pp) >= 0) { while (cmp_ext(e, pp) >= 0)
{
pp++; pp++;
findex = pp - big_ten_powers; findex = pp - big_ten_powers;
if (findex >= BTP) break; if (findex >= BTP)
break;
} }
pp--; pp--;
findex = pp - big_ten_powers; findex = pp - big_ten_powers;
mul_ext(e, &r_big_ten_powers[findex], e); mul_ext(e, &r_big_ten_powers[findex], e);
*decpt += findex * TP; *decpt += findex * TP;
pp = &ten_powers[1]; pp = &ten_powers[1];
while(pp < &ten_powers[TP] && cmp_ext(e, pp) >= 0) pp++; while (pp < &ten_powers[TP] && cmp_ext(e, pp) >= 0)
pp++;
pp--; pp--;
findex = pp - ten_powers; findex = pp - ten_powers;
*decpt += findex; *decpt += findex;
if (cmp_ext(e, &ten_powers[0]) < 0) { if (cmp_ext(e, &ten_powers[0]) < 0)
{
pp = &r_big_ten_powers[1]; pp = &r_big_ten_powers[1];
while(cmp_ext(e,pp) < 0) pp++; while (cmp_ext(e, pp) < 0)
pp++;
pp--; pp--;
findex = pp - r_big_ten_powers; findex = pp - r_big_ten_powers;
mul_ext(e, &big_ten_powers[findex], e); mul_ext(e, &big_ten_powers[findex], e);
@ -604,7 +668,8 @@ _ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int *sign, int ecvtflag)
ten_mult(e); ten_mult(e);
(*decpt)--; (*decpt)--;
pp = &r_ten_powers[0]; pp = &r_ten_powers[0];
while(cmp_ext(e, pp) < 0) pp++; while (cmp_ext(e, pp) < 0)
pp++;
findex = pp - r_ten_powers; findex = pp - r_ten_powers;
mul_ext(e, &ten_powers[findex], e); mul_ext(e, &ten_powers[findex], e);
*decpt -= findex; *decpt -= findex;
@ -612,23 +677,28 @@ _ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int *sign, int ecvtflag)
} }
(*decpt)++; /* because now value in [1.0, 10.0) */ (*decpt)++; /* because now value in [1.0, 10.0) */
} }
if (! ecvtflag) { if (!ecvtflag)
{
/* for fcvt() we need ndigit digits behind the dot */ /* for fcvt() we need ndigit digits behind the dot */
pe += *decpt; pe += *decpt;
if (pe > &buf[NDIGITS]) pe = &buf[NDIGITS]; if (pe > &buf[NDIGITS])
pe = &buf[NDIGITS];
} }
m.exp = -62; m.exp = -62;
m.sign = 0; m.sign = 0;
m.m1 = 0xA0000000; m.m1 = 0xA0000000;
m.m2 = 0; m.m2 = 0;
while (p <= pe) { while (p <= pe)
{
struct EXTEND oneminm; struct EXTEND oneminm;
if (p - pe > NSIGNIFICANT) { if (p - pe > NSIGNIFICANT)
{
findex = 0; findex = 0;
e->m1 = 0; e->m1 = 0;
} }
if (findex) { if (findex)
{
struct EXTEND tc, oldtc; struct EXTEND tc, oldtc;
int count = 0; int count = 0;
@ -637,7 +707,8 @@ _ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int *sign, int ecvtflag)
oldtc.m1 = 0; oldtc.m1 = 0;
oldtc.m2 = 0; oldtc.m2 = 0;
tc = ten_powers[findex]; tc = ten_powers[findex];
while (cmp_ext(e, &tc) >= 0) { while (cmp_ext(e, &tc) >= 0)
{
oldtc = tc; oldtc = tc;
add_ext(&tc, &ten_powers[findex], &tc); add_ext(&tc, &ten_powers[findex], &tc);
count++; count++;
@ -648,46 +719,59 @@ _ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int *sign, int ecvtflag)
findex--; findex--;
continue; continue;
} }
if (e->m1) { if (e->m1)
{
m.sign = 1; m.sign = 1;
add_ext(&ten_powers[0], &m, &oneminm); add_ext(&ten_powers[0], &m, &oneminm);
m.sign = 0; m.sign = 0;
if (e->exp >= 0) { if (e->exp >= 0)
{
struct EXTEND x; struct EXTEND x;
x.m2 = 0; x.exp = e->exp; x.m2 = 0;
x.exp = e->exp;
x.sign = 1; x.sign = 1;
x.m1 = e->m1 >> (31 - e->exp); x.m1 = e->m1 >> (31 - e->exp);
*p++ = (x.m1) + '0'; *p++ = (x.m1) + '0';
x.m1 = x.m1 << (31 - e->exp); x.m1 = x.m1 << (31 - e->exp);
add_ext(e, &x, e); add_ext(e, &x, e);
} }
else *p++ = '0'; else
*p++ = '0';
/* Check that remainder is still significant */ /* Check that remainder is still significant */
if (cmp_ext(&m, e) > 0 || cmp_ext(e, &oneminm) > 0) { if (cmp_ext(&m, e) > 0 || cmp_ext(e, &oneminm) > 0)
if (e->m1 && e->exp >= -1) *(p-1) += 1; {
if (e->m1 && e->exp >= -1)
*(p - 1) += 1;
e->m1 = 0; e->m1 = 0;
continue; continue;
} }
ten_mult(&m); ten_mult(&m);
ten_mult(e); ten_mult(e);
} }
else *p++ = '0'; else
*p++ = '0';
} }
if (pe >= buf) { if (pe >= buf)
{
p = pe; p = pe;
*p += 5; /* round of at the end */ *p += 5; /* round of at the end */
while (*p > '9') { while (*p > '9')
{
*p = '0'; *p = '0';
if (p > buf) ++*--p; if (p > buf)
else { ++*--p;
else
{
*p = '1'; *p = '1';
++*decpt; ++*decpt;
if (! ecvtflag) { if (!ecvtflag)
{
/* maybe add another digit at the end, /* maybe add another digit at the end,
because the point was shifted right because the point was shifted right
*/ */
if (pe > buf) *pe = '0'; if (pe > buf)
*pe = '0';
pe++; pe++;
} }
} }
@ -705,7 +789,8 @@ _dbl_ext_cvt(double value, struct EXTEND *e)
value = frexp(value, &exponent); value = frexp(value, &exponent);
e->sign = value < 0.0; e->sign = value < 0.0;
if (e->sign) value = -value; if (e->sign)
value = -value;
e->exp = exponent - 1; e->exp = exponent - 1;
value *= 4294967296.0; value *= 4294967296.0;
e->m1 = value; e->m1 = value;
@ -725,19 +810,25 @@ _ext_dbl_cvt(struct EXTEND *e)
int sign = e->sign; int sign = e->sign;
e->sign = 0; e->sign = 0;
if (e->m1 == 0 && e->m2 == 0) { if (e->m1 == 0 && e->m2 == 0)
{
return 0.0; return 0.0;
} }
if (max_d.exp == 0) { if (max_d.exp == 0)
{
_dbl_ext_cvt(DBL_MAX, &max_d); _dbl_ext_cvt(DBL_MAX, &max_d);
} }
if (cmp_ext(&max_d, e) < 0) { if (cmp_ext(&max_d, e) < 0)
{
f = HUGE_VAL; f = HUGE_VAL;
errno = ERANGE; errno = ERANGE;
} }
else f = ldexp((double)e->m1*4294967296.0 + (double)e->m2, e->exp-63); else
if (sign) f = -f; f = ldexp((double)e->m1 * 4294967296.0 + (double)e->m2, e->exp - 63);
if (f == 0.0 && (e->m1 != 0 || e->m2 != 0)) { if (sign)
f = -f;
if (f == 0.0 && (e->m1 != 0 || e->m2 != 0))
{
errno = ERANGE; errno = ERANGE;
} }
return f; return f;

View file

@ -39,13 +39,14 @@ char* _findenv(register const char* name, int* offset)
if (!environ) if (!environ)
return NULL; return NULL;
for (C = name,len = 0;*C && *C != '=';++C,++len); for (C = name, len = 0; *C && *C != '='; ++C, ++len)
;
for (P = environ; *P; ++P) for (P = environ; *P; ++P)
if (!strncmp(*P, name, len)) if (!strncmp(*P, name, len))
if (*(C = *P + len) == '=') { if (*(C = *P + len) == '=')
{
*offset = P - environ; *offset = P - environ;
return (char*)(++C); return (char*)(++C);
} }
return (NULL); return (NULL);
} }

View file

@ -6,8 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
long long labs(register long l)
labs(register long l)
{ {
return l >= 0 ? l : -l; return l >= 0 ? l : -l;
} }

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