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 <stdlib.h>
void __bad_assertion(const char *mess) {
void __bad_assertion(const char* mess)
{
fputs(mess, stderr);
abort();

View file

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

View file

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

View file

@ -8,13 +8,15 @@
struct lconv _lc;
char *
setlocale(int category, const char *locale)
char* setlocale(int category, const char* locale)
{
if (!locale) return "C";
if (*locale && strcmp(locale, "C")) return (char *)NULL;
if (!locale)
return "C";
if (*locale && strcmp(locale, "C"))
return (char*)NULL;
switch(category) {
switch (category)
{
case LC_ALL:
case LC_CTYPE:
case LC_COLLATE:

View file

@ -148,4 +148,3 @@ void free(void *ptr)
/* ...and update the ring pointer. */
__mem_freelist = p;
}

View file

@ -32,17 +32,21 @@ asin_acos(double x, int cosfl)
1.0
};
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (negative) {
if (negative)
{
x = -x;
}
if (x > 0.5) {
if (x > 0.5)
{
i = 1;
if (x > 1) {
if (x > 1)
{
errno = EDOM;
return 0;
}
@ -50,22 +54,28 @@ asin_acos(double x, int cosfl)
x = -sqrt(g);
x += x;
}
else {
else
{
/* ??? avoid underflow ??? */
i = 0;
g = x * x;
}
x += x * g * POLYNOM4(g, p) / POLYNOM5(g, q);
if (cosfl) {
if (! negative) x = -x;
if (cosfl)
{
if (!negative)
x = -x;
}
if ((cosfl == 0) == (i == 1)) {
if ((cosfl == 0) == (i == 1))
{
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;
}
if (! cosfl && negative) x = -x;
if (!cosfl && negative)
x = -x;
return x;
}

View file

@ -43,30 +43,35 @@ atan(double x)
int n;
double g;
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (neg) {
if (neg)
{
x = -x;
}
if (x > 1.0) {
if (x > 1.0)
{
x = 1.0 / x;
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;
x = (((0.73205080756887729353*x-0.5)-0.5)+x)/
(1.73205080756887729353+x);
x = (((0.73205080756887729353 * x - 0.5) - 0.5) + x) / (1.73205080756887729353 + x);
}
/* ??? avoid underflow ??? */
g = x * x;
x += x * g * POLYNOM3(g, p) / POLYNOM4(g, q);
if (n > 1) x = -x;
if (n > 1)
x = -x;
x += a[n];
return neg ? -x : x;
}

View file

@ -15,26 +15,32 @@ atan2(double y, double x)
{
double absx, absy, val;
if (x == 0 && y == 0) {
if (x == 0 && y == 0)
{
errno = EDOM;
return 0;
}
absy = y < 0 ? -y : y;
absx = x < 0 ? -x : x;
if (absy - absx == absy) {
if (absy - absx == absy)
{
/* x negligible compared to y */
return y < 0 ? -M_PI_2 : M_PI_2;
}
if (absx - absy == absx) {
if (absx - absy == absx)
{
/* y negligible compared to x */
val = 0.0;
}
else val = atan(y/x);
if (x > 0) {
else
val = atan(y / x);
if (x > 0)
{
/* first or fourth quadrant; already correct */
return val;
}
if (y < 0) {
if (y < 0)
{
/* third quadrant */
return val - M_PI;
}

View file

@ -11,7 +11,6 @@
#include <errno.h>
#include "localmath.h"
double
exp(double x)
{
@ -36,20 +35,24 @@ exp(double x)
int n;
int negative = x < 0;
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (x < M_LN_MIN_D) {
if (x < M_LN_MIN_D)
{
errno = ERANGE;
return 0.0;
}
if (x > M_LN_MAX_D) {
if (x > M_LN_MAX_D)
{
errno = ERANGE;
return HUGE_VAL;
}
if (negative) x = -x;
if (negative)
x = -x;
/* ??? avoid underflow ??? */
@ -61,7 +64,8 @@ exp(double x)
g = ((x1 - xn * 0.693359375) + x2) - xn * (-2.1219444005469058277e-4);
}
if (negative) {
if (negative)
{
g = -g;
n = -n;
}

View file

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

View file

@ -14,20 +14,25 @@ hypot(double x,double y)
{
/* Computes sqrt(x*x+y*y), avoiding overflow */
if (x < 0) x = -x;
if (y < 0) y = -y;
if (x > y) {
if (x < 0)
x = -x;
if (y < 0)
y = -y;
if (x > y)
{
double t = y;
y = x;
x = t;
}
/* 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;
return y * sqrt(x * x + 1.0);
}
struct complex {
struct complex
{
double r, i;
};

View file

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

View file

@ -14,38 +14,48 @@ ldexp(double fl, int exp)
int sign = 1;
int currexp;
if (__IsNan(fl)) {
if (__IsNan(fl))
{
errno = EDOM;
return fl;
}
if (fl == 0.0) return 0.0;
if (fl<0) {
if (fl == 0.0)
return 0.0;
if (fl < 0)
{
fl = -fl;
sign = -1;
}
if (fl > DBL_MAX) { /* for infinity */
if (fl > DBL_MAX)
{ /* for infinity */
errno = ERANGE;
return sign * fl;
}
fl = frexp(fl, &currexp);
exp += currexp;
if (exp > 0) {
if (exp > DBL_MAX_EXP) {
if (exp > 0)
{
if (exp > DBL_MAX_EXP)
{
errno = ERANGE;
return sign * HUGE_VAL;
}
while (exp>30) {
while (exp > 30)
{
fl *= (double)(1L << 30);
exp -= 30;
}
fl *= (double)(1L << exp);
}
else {
else
{
/* number need not be normalized */
if (exp < DBL_MIN_EXP - DBL_MANT_DIG) {
if (exp < DBL_MIN_EXP - DBL_MANT_DIG)
{
return 0.0;
}
while (exp<-30) {
while (exp < -30)
{
fl /= (double)(1L << 30);
exp += 30;
}

View file

@ -33,33 +33,41 @@ log(double x)
double znum, zden, z, w;
int exponent;
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (x < 0) {
if (x < 0)
{
errno = EDOM;
return -HUGE_VAL;
}
else if (x == 0) {
else if (x == 0)
{
errno = ERANGE;
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);
if (x > M_1_SQRT2) {
if (x > M_1_SQRT2)
{
znum = (x - 0.5) - 0.5;
zden = x * 0.5 + 0.5;
}
else {
else
{
znum = x - 0.5;
zden = znum * 0.5 + 0.5;
exponent--;
}
z = znum/zden; w = z * z;
z = znum / zden;
w = z * z;
x = z + z * w * (POLYNOM2(w, a) / POLYNOM3(w, b));
z = exponent;
x += z * (-2.121944400546905827679e-4);

View file

@ -13,15 +13,18 @@
double
log10(double x)
{
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (x < 0) {
if (x < 0)
{
errno = EDOM;
return -HUGE_VAL;
}
else if (x == 0) {
else if (x == 0)
{
errno = ERANGE;
return -HUGE_VAL;
}

View file

@ -19,81 +19,103 @@ pow(double x, double y)
int ex, newexp;
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;
return 0;
}
if (y == 0) return 1.0;
if (y == 0)
return 1.0;
if (y < 0) {
if (y < 0)
{
y = -y;
negexp = 1;
}
else negexp = 0;
else
negexp = 0;
y_fractpart = modf(y, &y_intpart);
if (y_fractpart != 0) {
if (x < 0) {
if (y_fractpart != 0)
{
if (x < 0)
{
errno = EDOM;
return 0;
}
}
negx = 0;
if (x < 0) {
if (x < 0)
{
x = -x;
negx = 1;
}
if (y_intpart > ULONG_MAX) {
if (negx && modf(y_intpart/2.0, &y_fractpart) == 0) {
if (y_intpart > ULONG_MAX)
{
if (negx && modf(y_intpart / 2.0, &y_fractpart) == 0)
{
negx = 0;
}
x = log(x);
/* Beware of overflow in the multiplication */
if (x > 1.0 && y > DBL_MAX/x) {
if (x > 1.0 && y > DBL_MAX / x)
{
errno = ERANGE;
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);
}
if (y_fractpart != 0) {
if (y_fractpart != 0)
{
fp = exp(y_fractpart * log(x));
}
else fp = 1.0;
else
fp = 1.0;
yi = y_intpart;
if (! (yi & 1)) negx = 0;
if (!(yi & 1))
negx = 0;
x = frexp(x, &ex);
newexp = 0;
for (;;) {
if (yi & 1) {
for (;;)
{
if (yi & 1)
{
fp *= x;
newexp += ex;
}
yi >>= 1;
if (yi == 0) break;
if (yi == 0)
break;
x *= x;
ex <<= 1;
if (x < 0.5) {
if (x < 0.5)
{
x += x;
ex -= 1;
}
}
if (negexp) {
if (negexp)
{
fp = 1.0 / fp;
newexp = -newexp;
}
if (negx) {
if (negx)
{
return -ldexp(fp, newexp);
}
return ldexp(fp, newexp);

View file

@ -33,25 +33,30 @@ sinus(double x, int cos_flag)
double y;
int neg = 1;
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (x < 0) {
if (x < 0)
{
x = -x;
neg = -1;
}
if (cos_flag) {
if (cos_flag)
{
neg = 1;
y = M_PI_2 + x;
}
else y = x;
else
y = x;
/* ??? avoid loss of significance, if y is too large, error ??? */
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.
Here we used 12 bits of the mantissa for a1.
@ -63,8 +68,10 @@ sinus(double x, int cos_flag)
double x1, x2;
modf(y, &y);
if (modf(0.5*y, &x1)) neg = -neg;
if (cos_flag) y -= 0.5;
if (modf(0.5 * y, &x1))
neg = -neg;
if (cos_flag)
y -= 0.5;
x2 = modf(x, &x1);
x = x1 - y * A1;
x += x2;
@ -73,7 +80,8 @@ sinus(double x, int cos_flag)
#undef A2
}
if (x < 0) {
if (x < 0)
{
neg = -neg;
x = -x;
}
@ -94,6 +102,7 @@ sin(double x)
double
cos(double x)
{
if (x < 0) x = -x;
if (x < 0)
x = -x;
return sinus(x, 1);
}

View file

@ -34,32 +34,38 @@ sinh_cosh(double x, int cosh_flag)
int negative = x < 0;
double y = negative ? -x : x;
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (! cosh_flag && y <= 1.0) {
if (!cosh_flag && y <= 1.0)
{
/* ??? check for underflow ??? */
y = y * y;
return x + x * y * POLYNOM3(y, p) / POLYNOM3(y, q);
}
if (y >= M_LN_MAX_D) {
if (y >= M_LN_MAX_D)
{
/* exp(y) would cause overflow */
#define LNV 0.69316101074218750000e+0
#define VD2M1 0.52820835025874852469e-4
double w = y - LNV;
if (w < M_LN_MAX_D+M_LN2-LNV) {
if (w < M_LN_MAX_D + M_LN2 - LNV)
{
x = exp(w);
x += VD2M1 * x;
}
else {
else
{
errno = ERANGE;
x = HUGE_VAL;
}
}
else {
else
{
double z = exp(y);
x = 0.5 * (z + (cosh_flag ? 1.0 : -1.0) / z);
@ -76,6 +82,7 @@ sinh(double x)
double
cosh(double x)
{
if (x < 0) x = -x;
if (x < 0)
x = -x;
return sinh_cosh(x, 1);
}

View file

@ -18,25 +18,31 @@ sqrt(double x)
int exponent;
double val;
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (x <= 0) {
if (x < 0) errno = EDOM;
if (x <= 0)
{
if (x < 0)
errno = EDOM;
return 0;
}
if (x > DBL_MAX) return x; /* for infinity */
if (x > DBL_MAX)
return x; /* for infinity */
val = frexp(x, &exponent);
if (exponent & 1) {
if (exponent & 1)
{
exponent--;
val *= 2;
}
val = ldexp(val + 1.0, exponent / 2 - 1);
/* 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;
}
return val;

View file

@ -36,17 +36,20 @@ tan(double x)
0.49819433993786512270e-6
};
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
return x;
}
if (negative) x = -x;
if (negative)
x = -x;
/* ??? avoid loss of significance, error if x is too large ??? */
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.
Here we used 12 bits of the mantissa for a1.
@ -58,7 +61,8 @@ tan(double x)
double x1, x2;
modf(y, &y);
if (modf(0.5*y, &x1)) invert = 1;
if (modf(0.5 * y, &x1))
invert = 1;
x2 = modf(x, &x1);
x = x1 - y * A1;
x += x2;
@ -71,6 +75,7 @@ tan(double x)
y = x * x;
x += x * y * POLYNOM2(y, p + 1);
y = POLYNOM4(y, q);
if (negative) x = -x;
if (negative)
x = -x;
return invert ? -y / x : x / y;
}

View file

@ -32,21 +32,26 @@ tanh(double x)
};
int negative = x < 0;
if (__IsNan(x)) {
if (__IsNan(x))
{
errno = EDOM;
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;
}
#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 += x;
}
else {
else
{
/* ??? avoid underflow ??? */
double g = x * x;
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
closedir(register DIR *dirp) /* stream from opendir */
int closedir(register DIR* dirp) /* stream from opendir */
{
register int fd;

View file

@ -41,7 +41,8 @@
#define DIRSIZ 14 /* 14 char filename in Version 7 */
#endif
#define MAXNAMLEN 255
struct direct {
struct direct
{
off_t d_off; /* offset of next disk directory entry */
u_long d_fileno; /* file number of entry */
u_short d_reclen; /* length of this record */
@ -146,7 +147,10 @@ NameLen( char name[] ) /* return # chars in embedded name */
#endif
#ifdef UNK
static enum { maybe, no, yes } state = maybe;
static enum { maybe,
no,
yes } state
= maybe;
/* does _getdents() work? */
/*ARGSUSED*/
@ -157,8 +161,7 @@ sig_catch(int sig) /* sig must be SIGSYS */
}
#endif
int
getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
int getdents(int fildes, char* buf, unsigned nbyte) /* returns # bytes read;
0 on EOF, -1 on error */
/* fildes == directory file descriptor */
/* *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
|| (unsigned long)buf % sizeof(long) != 0 /* ugh */
#endif
) {
)
{
errno = EFAULT; /* invalid pointer */
return -1;
}
@ -235,7 +239,8 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
{ /* convert next directory block */
int size;
do size = GetBlock( fildes, u.dblk, DIRBLKSIZ );
do
size = GetBlock(fildes, u.dblk, DIRBLKSIZ);
while (size == -1 && errno == EINTR);
if (size <= 0)
@ -243,8 +248,8 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
for (dp = (struct direct*)u.dblk;
(char*)dp < &u.dblk[size];
dp = (struct direct *)((char *)dp + RecLen( dp ))
) {
dp = (struct direct*)((char*)dp + RecLen(dp)))
{
#ifndef UFS
if (dp->d_reclen <= 0)
{
@ -255,8 +260,7 @@ getdents(int fildes, char *buf, unsigned nbyte) /* returns # bytes read;
if (dp->d_fileno != 0)
{ /* non-empty; copy to user buffer */
register int reclen =
DIRENTSIZ( NameLen( dp->d_name ) );
register int reclen = DIRENTSIZ(NameLen(dp->d_name));
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_reclen = reclen;
(void)strncpy(bp->d_name, dp->d_name,
reclen - DIRENTBASESIZ
); /* adds NUL padding */
reclen - DIRENTBASESIZ); /* adds NUL padding */
bp = (struct dirent*)((char*)bp + reclen);
}

View file

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

View file

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

View file

@ -17,8 +17,7 @@ int _close(int d);
int _stty(int, struct sgttyb*);
int _gtty(int, struct sgttyb*);
char *
getpass(const char *prompt)
char* getpass(const char* prompt)
{
int i = 0;
struct sgttyb tty, ttysave;
@ -26,7 +25,8 @@ getpass(const char *prompt)
int fd;
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);
_write(2, prompt, strlen(prompt));
_gtty(fd, &tty);
@ -39,7 +39,8 @@ getpass(const char *prompt)
pwdbuf[i - 1] = '\0';
_stty(fd, &ttysave);
_write(2, "\n", 1);
if (fd != 0) _close(fd);
if (fd != 0)
_close(fd);
signal(SIGINT, savesig);
return (pwdbuf);
}

View file

@ -11,9 +11,11 @@ int getw(register FILE *stream)
int w;
register char* p = (char*)&w;
while (cnt--) {
while (cnt--)
{
*p++ = getc(stream);
}
if (feof(stream) || ferror(stream)) return EOF;
if (feof(stream) || ferror(stream))
return EOF;
return w;
}

View file

@ -12,17 +12,21 @@ char *mktemp(char *template)
pid = getpid(); /* get process id as semi-unique number */
p = template;
while (*p) p++; /* find end of string */
while (*p)
p++; /* find end of string */
/* Replace XXXXXX at end of template with pid. */
while (*--p == 'X') {
while (*--p == 'X')
{
*p = '0' + (pid % 10);
pid /= 10;
}
p++;
for (k = 'a'; k <= 'z'; k++) {
for (k = 'a'; k <= 'z'; k++)
{
*p = k;
if (access(template, 0) < 0) {
if (access(template, 0) < 0)
{
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)
#endif
DIR *
opendir(const char *dirname) /* name of directory */
DIR* opendir(const char* dirname) /* name of directory */
{
register DIR* dirp; /* -> malloc'ed storage */
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
|| (dirp->dd_buf = (char *)malloc( (unsigned)DIRBUF )) == NULL
) {
|| (dirp->dd_buf = (char*)malloc((unsigned)DIRBUF)) == NULL)
{
register int serrno = errno;
/* errno set to ENOMEM by sbrk() */

View file

@ -20,7 +20,8 @@ int _close(int d);
#if defined(__USG)
static
#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 _fork(void);
int _pipe(int fildes[2]);
@ -29,23 +30,24 @@ void _exit(int status);
static int pids[FOPEN_MAX];
FILE *
popen(const char *command, const char *type)
FILE* popen(const char* command, const char* type)
{
int piped[2];
int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2;
int pid;
if (Xtype == 2 ||
_pipe(piped) < 0 ||
(pid = _fork()) < 0) return 0;
if (Xtype == 2 || _pipe(piped) < 0 || (pid = _fork()) < 0)
return 0;
if (pid == 0) {
if (pid == 0)
{
/* child */
register int* p;
for (p = pids; p < &pids[ FOPEN_MAX]; p++) {
if (*p) _close(p - pids);
for (p = pids; p < &pids[FOPEN_MAX]; p++)
{
if (*p)
_close(p - pids);
}
_close(piped[Xtype]);
_dup2(piped[!Xtype], !Xtype);
@ -65,8 +67,7 @@ popen(const char *command, const char *type)
#define ret_val status
#endif
int
pclose(FILE *stream)
int pclose(FILE* stream)
{
int fd = fileno(stream);
wait_arg status;
@ -75,10 +76,13 @@ pclose(FILE *stream)
void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN);
fclose(stream);
while ((wret = _wait(&status)) != -1) {
if (wret == pids[fd]) break;
while ((wret = _wait(&status)) != -1)
{
if (wret == pids[fd])
break;
}
if (wret == -1) ret_val = -1;
if (wret == -1)
ret_val = -1;
signal(SIGINT, intsave);
signal(SIGQUIT, quitsave);
pids[fd] = 0;
@ -95,13 +99,18 @@ _dup2(int oldd, int newd)
int fdbuf[FOPEN_MAX];
/* ignore the error on the close() */
tmp = errno; (void) _close(newd); errno = tmp;
while ((fd = _dup(oldd)) != newd) {
if (fd == -1) break;
tmp = errno;
(void)_close(newd);
errno = tmp;
while ((fd = _dup(oldd)) != newd)
{
if (fd == -1)
break;
fdbuf[i++] = fd;
}
tmp = errno;
while (--i >= 0) {
while (--i >= 0)
{
_close(fdbuf[i]);
}
errno = tmp;

View file

@ -5,15 +5,16 @@
#include <stdio.h>
int
putw(int w, register FILE *stream)
int putw(int w, register FILE* stream)
{
register int cnt = sizeof(int);
register char* p = (char*)&w;
while (cnt--) {
while (cnt--)
{
putc(*p++, stream);
}
if (ferror(stream)) return EOF;
if (ferror(stream))
return EOF;
return w;
}

View file

@ -27,21 +27,18 @@ readdir(register DIR *dirp)
return NULL; /* invalid pointer */
}
do {
do
{
if (dirp->dd_loc >= dirp->dd_size) /* empty or obsolete */
dirp->dd_loc = dirp->dd_size = 0;
if (dirp->dd_size == 0 /* need to refill buffer */
&& (dirp->dd_size =
getdents( dirp->dd_fd, dirp->dd_buf, (unsigned)DIRBUF )
) <= 0
)
&& (dirp->dd_size = getdents(dirp->dd_fd, dirp->dd_buf, (unsigned)DIRBUF)) <= 0)
return NULL; /* EOF or error */
dp = (struct dirent*)&dirp->dd_buf[dirp->dd_loc];
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;
}

View file

@ -23,8 +23,7 @@ extern off_t _lseek(int d, int offset, int whence);
#define SEEK_SET 0
#endif
void
rewinddir(register DIR *dirp)
void rewinddir(register DIR* dirp)
{
if (dirp == NULL || dirp->dd_buf == NULL)
{

View file

@ -29,8 +29,7 @@ typedef int bool; /* Boolean data type */
#define false 0
#define true 1
void
seekdir(register DIR *dirp, register off_t loc)
void seekdir(register DIR* dirp, register off_t loc)
/* loc == position from telldir() */
{
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
<= loc /* match possible in this buffer */
) {
)
{
for (/* dp initialized above */;
(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)
{ /* found it! */
dirp->dd_loc =
(char *)dp - dirp->dd_buf;
dirp->dd_loc = (char*)dp - dirp->dd_buf;
return;
}
@ -92,14 +90,14 @@ seekdir(register DIR *dirp, register off_t loc)
errno = EINVAL;
return; /* no entry at specified loc */
}
else { /* rewind directory and start over */
else
{ /* rewind directory and start over */
rewind = false; /* but only once! */
dirp->dd_loc = dirp->dd_size = 0;
if (_lseek(dirp->dd_fd, (off_t)0, SEEK_SET)
!= 0
)
!= 0)
return; /* errno already set (EBADF) */
if (loc == 0)

View file

@ -17,15 +17,16 @@ alfun(int sig)
longjmp(setjmpbuf, 1);
} /* used with sleep() below */
void
sleep(int n)
void sleep(int n)
{
/* sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt. */
unsigned oldalarm = 0;
void (*oldsig)(int) = 0;
if (n <= 0) return;
if (setjmp(setjmpbuf)) {
if (n <= 0)
return;
if (setjmp(setjmpbuf))
{
signal(SIGALRM, oldsig);
_alarm(oldalarm);
return;
@ -33,14 +34,17 @@ sleep(int n)
oldalarm = _alarm(5000); /* Who cares how long, as long
* as it is long enough
*/
if (oldalarm > n) oldalarm -= n;
else if (oldalarm) {
if (oldalarm > n)
oldalarm -= n;
else if (oldalarm)
{
n = oldalarm;
oldalarm = 1;
}
oldsig = signal(SIGALRM, alfun);
_alarm(n);
for (;;) {
for (;;)
{
/* allow for other handlers ... */
_pause();
}

View file

@ -18,8 +18,7 @@ extern off_t _lseek(int d, int offset, int whence);
#define SEEK_CUR 1
#endif
off_t
telldir(register DIR *dirp) /* return offset of next entry */
off_t telldir(register DIR* dirp) /* return offset of next entry */
{
if (dirp == NULL || dirp->dd_buf == NULL)
{

View file

@ -16,23 +16,24 @@
int _sigprocmask(int, sigset_t*, sigset_t*);
static void
__testsigset(void) {
__testsigset(void)
{
/* This switch compiles when a sigset_t has the right size. */
switch(0) {
switch (0)
{
case 0:
case sizeof(sigset_t) <= sizeof(long): break;
case sizeof(sigset_t) <= sizeof(long):
break;
}
}
void
__newsigset(sigset_t *p)
void __newsigset(sigset_t* p)
{
/* The SIG_SETMASK is not significant */
_sigprocmask(SIG_SETMASK, NULL, p);
}
void
__oldsigset(sigset_t *p)
void __oldsigset(sigset_t* p)
{
_sigprocmask(SIG_SETMASK, p, NULL);
}

View file

@ -9,8 +9,7 @@
#endif
#include <signal.h>
int
raise(int sig)
int raise(int sig)
{
if (sig < 0 || sig > _NSIG)
return -1;

View file

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

View file

@ -15,12 +15,16 @@ gnum(register const char *f, int *ip, va_list *app)
{
register int i, c;
if (*f == '*') {
if (*f == '*')
{
*ip = va_arg((*app), int);
f++;
} else {
}
else
{
i = 0;
while ((c = *f - '0') >= 0 && c <= 9) {
while ((c = *f - '0') >= 0 && c <= 9)
{
i = i * 10 + c;
f++;
}
@ -39,7 +43,8 @@ gnum(register const char *f, int *ip, va_list *app)
#endif
#define PUTC(c) \
do { \
do \
{ \
int i = putc(c, stream); \
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;
int base;
switch (flags & (FL_SHORT | FL_LONG)) {
switch (flags & (FL_SHORT | FL_LONG))
{
case FL_SHORT:
if (is_signed) {
if (is_signed)
{
signed_val = (short)va_arg(*ap, int);
} else {
}
else
{
unsigned_val = (unsigned short)va_arg(*ap, unsigned);
}
break;
case FL_LONG:
if (is_signed) {
if (is_signed)
{
signed_val = va_arg(*ap, long);
} else {
}
else
{
unsigned_val = va_arg(*ap, unsigned long);
}
break;
default:
if (is_signed) {
if (is_signed)
{
signed_val = va_arg(*ap, int);
} else {
}
else
{
unsigned_val = va_arg(*ap, unsigned int);
}
break;
}
if (is_signed) {
if (signed_val < 0) {
if (is_signed)
{
if (signed_val < 0)
{
*s++ = '-';
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;
}
if ((flags & FL_ALT) && (c == 'o')) *s++ = '0';
if (!unsigned_val) {
if ((flags & FL_ALT) && (c == 'o'))
*s++ = '0';
if (!unsigned_val)
{
if (!precision)
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++ = (c == 'X' ? 'X' : 'x');
}
switch (c) {
case 'b': base = 2; break;
case 'o': base = 8; break;
switch (c)
{
case 'b':
base = 2;
break;
case 'o':
base = 8;
break;
case 'd':
case 'i':
case 'u': base = 10; break;
case 'u':
base = 10;
break;
case 'x':
case 'X':
case 'p': base = 16; break;
case 'p':
base = 16;
break;
}
s = _i_compute(unsigned_val, base, s, precision);
if (c == 'X')
while (old_s != s) {
while (old_s != s)
{
*old_s = toupper(*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;
}
int
_doprnt(register const char *fmt, va_list ap, FILE *stream)
int _doprnt(register const char* fmt, va_list ap, FILE* stream)
{
register char* s;
register int j;
@ -131,10 +164,13 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
const char* oldfmt;
char *s1, buf[1025];
while (c = *fmt++) {
if (c != '%') {
while (c = *fmt++)
{
if (c != '%')
{
#ifdef CPM
if (c == '\n') {
if (c == '\n')
{
PUTC('\r');
}
#endif
@ -143,51 +179,84 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
continue;
}
flags = 0;
do {
switch(*fmt) {
case '-': flags |= FL_LJUST; break;
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;
do
{
switch (*fmt)
{
case '-':
flags |= FL_LJUST;
break;
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++;
} while (!(flags & FL_NOMORE));
oldfmt = fmt;
fmt = gnum(fmt, &width, &ap);
if (fmt != oldfmt) flags |= FL_WIDTHSPEC;
if (fmt != oldfmt)
flags |= FL_WIDTHSPEC;
if (*fmt == '.') {
fmt++; oldfmt = fmt;
if (*fmt == '.')
{
fmt++;
oldfmt = fmt;
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;
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_LJUST) flags &= ~FL_ZEROFILL;
if (flags & FL_SIGN)
flags &= ~FL_SPACE;
if (flags & FL_LJUST)
flags &= ~FL_ZEROFILL;
s = s1 = buf;
switch (*fmt) {
case 'h': flags |= FL_SHORT; fmt++; break;
case 'l': flags |= FL_LONG; fmt++; break;
case 'L': flags |= FL_LONGDOUBLE; fmt++; break;
switch (*fmt)
{
case 'h':
flags |= FL_SHORT;
fmt++;
break;
case 'l':
flags |= FL_LONG;
fmt++;
break;
case 'L':
flags |= FL_LONGDOUBLE;
fmt++;
break;
}
switch (c = *fmt++) {
switch (c = *fmt++)
{
default:
#ifdef CPM
if (c == '\n') {
if (c == '\n')
{
PUTC('\r');
nrchars++;
}
@ -208,7 +277,8 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
if (s1 == NULL)
s1 = "(null)";
s = s1;
while (precision || !(flags & FL_PRECSPEC)) {
while (precision || !(flags & FL_PRECSPEC))
{
if (*s == '\0')
break;
s++;
@ -223,15 +293,19 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
case 'u':
case 'x':
case 'X':
if (!(flags & FL_PRECSPEC)) precision = 1;
else if (c != 'p') flags &= ~FL_ZEROFILL;
if (!(flags & FL_PRECSPEC))
precision = 1;
else if (c != 'p')
flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 0);
break;
case 'd':
case 'i':
flags |= FL_SIGNEDCONV;
if (!(flags & FL_PRECSPEC)) precision = 1;
else flags &= ~FL_ZEROFILL;
if (!(flags & FL_PRECSPEC))
precision = 1;
else
flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 1);
break;
case 'c':
@ -261,7 +335,8 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
continue;
}
zfill = ' ';
if (flags & FL_ZEROFILL) zfill = '0';
if (flags & FL_ZEROFILL)
zfill = '0';
j = s - s1;
/* between_fill is true under the following conditions:
@ -280,29 +355,39 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
between_fill++;
if ((i = width - j) > 0)
if (!(flags & FL_LJUST)) { /* right justify */
if (!(flags & FL_LJUST))
{ /* right justify */
nrchars += i;
if (between_fill) {
if (flags & FL_SIGNEDCONV) {
j--; nrchars++;
if (between_fill)
{
if (flags & FL_SIGNEDCONV)
{
j--;
nrchars++;
PUTC(*s1++);
} else {
j -= 2; nrchars += 2;
}
else
{
j -= 2;
nrchars += 2;
PUTC(*s1++);
PUTC(*s1++);
}
}
do {
do
{
PUTC(zfill);
} while (--i);
}
nrchars += j;
while (--j >= 0) {
while (--j >= 0)
{
PUTC(*s1++);
}
if (i > 0) nrchars += i;
if (i > 0)
nrchars += i;
while (--i >= 0)
PUTC(zfill);
}

View file

@ -37,52 +37,72 @@ o_collect(register int c, register FILE *stream, char type,
register char* bufp = inp_buf;
register int base;
switch (type) {
switch (type)
{
case 'i': /* i means octal, decimal or hexadecimal */
case 'p':
case 'x':
case 'X': base = 16; break;
case 'X':
base = 16;
break;
case 'd':
case 'u': base = 10; break;
case 'o': base = 8; break;
case 'b': base = 2; break;
case 'u':
base = 10;
break;
case 'o':
base = 8;
break;
case 'b':
base = 2;
break;
}
if (c == '-' || c == '+') {
if (c == '-' || c == '+')
{
*bufp++ = c;
if (--width)
c = getc(stream);
}
if (width && c == '0' && base == 16) {
if (width && c == '0' && base == 16)
{
*bufp++ = c;
if (--width)
c = getc(stream);
if (c != 'x' && c != 'X') {
if (type == 'i') base = 8;
if (c != 'x' && c != 'X')
{
if (type == 'i')
base = 8;
}
else if (width) {
else if (width)
{
*bufp++ = c;
if (--width)
c = getc(stream);
}
}
else if (type == 'i') base = 10;
else if (type == 'i')
base = 10;
while (width) {
while (width)
{
if (((base == 10) && isdigit(c))
|| ((base == 16) && isxdigit(c))
|| ((base == 8) && isdigit(c) && (c < '8'))
|| ((base == 2) && isdigit(c) && (c < '2'))) {
|| ((base == 2) && isdigit(c) && (c < '2')))
{
*bufp++ = c;
if (--width)
c = getc(stream);
}
else break;
else
break;
}
if (width && c != EOF) ungetc(c, stream);
if (type == 'i') base = 0;
if (width && c != EOF)
ungetc(c, stream);
if (type == 'i')
base = 0;
*basep = base;
*bufp = '\0';
return bufp - 1;
@ -103,23 +123,27 @@ f_collect(register int c, register FILE *stream, register int width)
register char* bufp = inp_buf;
int digit_seen = 0;
if (c == '-' || c == '+') {
if (c == '-' || c == '+')
{
*bufp++ = c;
if (--width)
c = getc(stream);
}
while (width && isdigit(c)) {
while (width && isdigit(c))
{
digit_seen++;
*bufp++ = c;
if (--width)
c = getc(stream);
}
if (width && c == '.') {
if (width && c == '.')
{
*bufp++ = c;
if (--width)
c = getc(stream);
while (width && isdigit(c)) {
while (width && isdigit(c))
{
digit_seen++;
*bufp++ = c;
if (--width)
@ -127,46 +151,53 @@ f_collect(register int c, register FILE *stream, register int width)
}
}
if (!digit_seen) {
if (width && c != EOF) ungetc(c, stream);
if (!digit_seen)
{
if (width && c != EOF)
ungetc(c, stream);
return inp_buf - 1;
}
else digit_seen = 0;
else
digit_seen = 0;
if (width && (c == 'e' || c == 'E')) {
if (width && (c == 'e' || c == 'E'))
{
*bufp++ = c;
if (--width)
c = getc(stream);
if (width && (c == '+' || c == '-')) {
if (width && (c == '+' || c == '-'))
{
*bufp++ = c;
if (--width)
c = getc(stream);
}
while (width && isdigit(c)) {
while (width && isdigit(c))
{
digit_seen++;
*bufp++ = c;
if (--width)
c = getc(stream);
}
if (!digit_seen) {
if (width && c != EOF) ungetc(c,stream);
if (!digit_seen)
{
if (width && c != EOF)
ungetc(c, stream);
return inp_buf - 1;
}
}
if (width && c != EOF) ungetc(c, stream);
if (width && c != EOF)
ungetc(c, stream);
*bufp = '\0';
return bufp - 1;
}
#endif /* ACKCONF_NO_STDIO_FLOAT */
/*
* the routine that does the scanning
*/
int
_doscan(register FILE *stream, const char *format, va_list ap)
int _doscan(register FILE* stream, const char* format, va_list ap)
{
int done = 0; /* number of items done */
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;
#endif
if (!*format) return 0;
if (!*format)
return 0;
while (1) {
if (isspace(*format)) {
while (1)
{
if (isspace(*format))
{
while (isspace(*format))
format++; /* skip whitespace */
ic = getc(stream);
nrchars++;
while (isspace (ic)) {
while (isspace(ic))
{
ic = getc(stream);
nrchars++;
}
if (ic != EOF) ungetc(ic,stream);
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
}
if (!*format) break; /* end of format */
if (!*format)
break; /* end of format */
if (*format != '%') {
if (*format != '%')
{
ic = getc(stream);
nrchars++;
if (ic != *format++) {
if (ic != EOF) ungetc(ic,stream);
if (ic != *format++)
{
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
break; /* error */
}
continue;
}
format++;
if (*format == '%') {
if (*format == '%')
{
ic = getc(stream);
nrchars++;
if (ic == '%') {
if (ic == '%')
{
format++;
continue;
}
else break;
else
break;
}
flags = 0;
if (*format == '*') {
if (*format == '*')
{
format++;
flags |= FL_NOASSIGN;
}
if (isdigit (*format)) {
if (isdigit(*format))
{
flags |= FL_WIDTHSPEC;
for (width = 0; isdigit(*format);)
width = width * 10 + *format++ - '0';
}
switch (*format) {
case 'h': flags |= FL_SHORT; format++; break;
case 'l': flags |= FL_LONG; format++; break;
case 'L': flags |= FL_LONGDOUBLE; format++; break;
switch (*format)
{
case 'h':
flags |= FL_SHORT;
format++;
break;
case 'l':
flags |= FL_LONG;
format++;
break;
case 'L':
flags |= FL_LONGDOUBLE;
format++;
break;
}
kind = *format;
if ((kind != 'c') && (kind != '[') && (kind != 'n')) {
do {
if ((kind != 'c') && (kind != '[') && (kind != 'n'))
{
do
{
ic = getc(stream);
nrchars++;
} while (isspace(ic));
if (ic == EOF) break; /* outer while */
} else if (kind != 'n') { /* %c or %[ */
if (ic == EOF)
break; /* outer while */
}
else if (kind != 'n')
{ /* %c or %[ */
ic = getc(stream);
if (ic == EOF) break; /* outer while */
if (ic == EOF)
break; /* outer while */
nrchars++;
}
switch (kind) {
switch (kind)
{
default:
/* not recognized, like %q */
return conv || (ic != EOF) ? done : EOF;
break;
case 'n':
if (!(flags & FL_NOASSIGN)) { /* silly, though */
if (!(flags & FL_NOASSIGN))
{ /* silly, though */
if (flags & FL_SHORT)
*va_arg(ap, short*) = (short)nrchars;
else if (flags & FL_LONG)
@ -276,13 +339,15 @@ _doscan(register FILE *stream, const char *format, va_list ap)
case 'X': /* ditto */
if (!(flags & FL_WIDTHSPEC) || width > NUMLEN)
width = NUMLEN;
if (!width) return done;
if (!width)
return done;
str = o_collect(ic, stream, kind, width, &base);
if (str < inp_buf
|| (str == inp_buf
&& (*str == '-'
|| *str == '+'))) return done;
|| *str == '+')))
return done;
/*
* 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;
if (!(flags & FL_NOASSIGN)) {
if (!(flags & FL_NOASSIGN))
{
if (kind == 'd' || kind == 'i')
val = strtol(inp_buf, &tmp_string, base);
else
@ -308,19 +374,24 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 1;
if (!(flags & FL_NOASSIGN))
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))
*str++ = (char)ic;
if (--width) {
if (--width)
{
ic = getc(stream);
nrchars++;
}
}
if (width) {
if (ic != EOF) ungetc(ic,stream);
if (width)
{
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
}
break;
@ -329,12 +400,15 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 0xffff;
if (!(flags & FL_NOASSIGN))
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))
*str++ = (char)ic;
if (--width) {
if (--width)
{
ic = getc(stream);
nrchars++;
}
@ -342,67 +416,82 @@ _doscan(register FILE *stream, const char *format, va_list ap)
/* terminate the string */
if (!(flags & FL_NOASSIGN))
*str = '\0';
if (width) {
if (ic != EOF) ungetc(ic,stream);
if (width)
{
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
}
break;
case '[':
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!width) return done;
if (!width)
return done;
if ( *++format == '^' ) {
if (*++format == '^')
{
reverse = 1;
format++;
} else
}
else
reverse = 0;
for (str = Xtable; str < &Xtable[NR_CHARS]
; str++)
for (str = Xtable; str < &Xtable[NR_CHARS]; str++)
*str = 0;
if (*format == ']') Xtable[*format++] = 1;
while (*format && *format != ']') {
if (*format == ']')
Xtable[*format++] = 1;
if (*format == '-') {
while (*format && *format != ']')
{
Xtable[*format++] = 1;
if (*format == '-')
{
format++;
if (*format
&& *format != ']'
&& *(format) >= *(format -2)) {
&& *(format) >= *(format - 2))
{
int c;
for( c = *(format -2) + 1
; c <= *format ; c++)
for (c = *(format - 2) + 1; c <= *format; c++)
Xtable[c] = 1;
format++;
}
else Xtable['-'] = 1;
else
Xtable['-'] = 1;
}
}
if (!*format || !(Xtable[ic] ^ reverse)) {
if (ic != EOF) ungetc(ic, stream);
if (!*format || !(Xtable[ic] ^ reverse))
{
if (ic != EOF)
ungetc(ic, stream);
return done;
}
if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char*);
do {
do
{
if (!(flags & FL_NOASSIGN))
*str++ = (char)ic;
if (--width) {
if (--width)
{
ic = getc(stream);
nrchars++;
}
} while (width && ic != EOF && (Xtable[ic] ^ reverse));
if (width) {
if (ic != EOF) ungetc(ic, stream);
if (width)
{
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
}
if (!(flags & FL_NOASSIGN)) { /* terminate string */
if (!(flags & FL_NOASSIGN))
{ /* terminate string */
*str = '\0';
}
break;
@ -415,13 +504,15 @@ _doscan(register FILE *stream, const char *format, va_list ap)
if (!(flags & FL_WIDTHSPEC) || width > NUMLEN)
width = NUMLEN;
if (!width) return done;
if (!width)
return done;
str = f_collect(ic, stream, width);
if (str < inp_buf
|| (str == inp_buf
&& (*str == '-'
|| *str == '+'))) return done;
|| *str == '+')))
return done;
/*
* 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;
if (!(flags & FL_NOASSIGN)) {
if (!(flags & FL_NOASSIGN))
{
ld_val = strtod(inp_buf, &tmp_string);
if (flags & FL_LONGDOUBLE)
*va_arg(ap, long double*) = (long double)ld_val;
else
if (flags & FL_LONG)
else if (flags & FL_LONG)
*va_arg(ap, double*) = (double)ld_val;
else
*va_arg(ap, float*) = (float)ld_val;
@ -443,7 +534,8 @@ _doscan(register FILE *stream, const char *format, va_list ap)
#endif
} /* end switch */
conv++;
if (!(flags & FL_NOASSIGN) && kind != 'n') done++;
if (!(flags & FL_NOASSIGN) && kind != 'n')
done++;
format++;
}
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);
}
char *
_ecvt(long double value, int ndigit, int *decpt, int *sign)
char* _ecvt(long double value, int ndigit, int* decpt, int* sign)
{
return cvt(value, ndigit, decpt, sign, 1);
}
char *
_fcvt(long double value, int ndigit, int *decpt, int *sign)
char* _fcvt(long double value, int ndigit, int* decpt, int* sign)
{
return cvt(value, ndigit, decpt, sign, 0);
}

View file

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

View file

@ -7,19 +7,20 @@
#include <stdlib.h>
#include "../stdio/loc_incl.h"
FILE *
fdopen(int fd, const char *mode)
FILE* fdopen(int fd, const char* mode)
{
register int i;
FILE* stream;
int flags = 0;
if (fd < 0) return (FILE *)NULL;
if (fd < 0)
return (FILE*)NULL;
for (i = 0; __iotab[i] != 0; i++)
if (i >= FOPEN_MAX - 1)
return (FILE*)NULL;
switch(*mode++) {
switch (*mode++)
{
case 'r':
flags |= _IOREAD | _IOREADING;
break;
@ -31,8 +32,10 @@ fdopen(int fd, const char *mode)
default:
return (FILE*)NULL;
}
while(*mode) {
switch(*mode++) {
while (*mode)
{
switch (*mode++)
{
case 'b':
continue;
case '+':
@ -45,7 +48,8 @@ fdopen(int fd, const char *mode)
break;
}
if ((stream = (FILE *) malloc(sizeof(FILE))) == NULL) {
if ((stream = (FILE*)malloc(sizeof(FILE))) == NULL)
{
return (FILE*)NULL;
}

View file

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

View file

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

View file

@ -8,12 +8,12 @@
#include <unistd.h>
#include "loc_incl.h"
int
fflush(FILE *stream)
int fflush(FILE* stream)
{
int count, c1, i, retval = 0;
if (!stream) {
if (!stream)
{
for (i = 0; i < FOPEN_MAX; i++)
if (__iotab[i] && fflush(__iotab[i]))
retval = EOF;
@ -24,7 +24,8 @@ fflush(FILE *stream)
|| (!io_testflag(stream, _IOREADING)
&& !io_testflag(stream, _IOWRITING)))
return 0;
if (io_testflag(stream, _IOREADING)) {
if (io_testflag(stream, _IOREADING))
{
/* (void) fseek(stream, 0L, SEEK_CUR); */
int adjust = 0;
if (stream->_buf && !io_testflag(stream, _IONBF))
@ -35,7 +36,9 @@ fflush(FILE *stream)
stream->_flags &= ~(_IOREADING | _IOWRITING);
stream->_ptr = stream->_buf;
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 */
stream->_flags &= ~_IOWRITING;
@ -46,8 +49,10 @@ fflush(FILE *stream)
if (count <= 0)
return 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;
return EOF;
}
@ -63,8 +68,7 @@ fflush(FILE *stream)
return EOF;
}
void
__cleanup(void)
void __cleanup(void)
{
register int i;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -27,18 +27,23 @@ _pfloat(long double r, register char *s, int n, int flags)
if (dp <= 0)
*s++ = '0';
for (i = dp; i > 0; i--)
if (*s1) *s++ = *s1++;
else *s++ = '0';
if (*s1)
*s++ = *s1++;
else
*s++ = '0';
if (((i = n) > 0) || (flags & FL_ALT))
*s++ = '.';
while (++dp <= 0) {
while (++dp <= 0)
{
if (--i < 0)
break;
*s++ = '0';
}
while (--i >= 0)
if (*s1) *s++ = *s1++;
else *s++ = '0';
if (*s1)
*s++ = *s1++;
else
*s++ = '0';
return s;
}
@ -60,16 +65,24 @@ _pscien(long double r, register char *s, int n, int flags)
if ((n > 0) || (flags & FL_ALT))
*s++ = '.';
while (--n >= 0)
if (*s1) *s++ = *s1++;
else *s++ = '0';
if (*s1)
*s++ = *s1++;
else
*s++ = '0';
*s++ = 'e';
if ( r != 0 ) --dp ;
if ( dp<0 ) {
*s++ = '-' ; dp= -dp ;
} else {
if (r != 0)
--dp;
if (dp < 0)
{
*s++ = '-';
dp = -dp;
}
else
{
*s++ = '+';
}
if (dp >= 100) {
if (dp >= 100)
{
*s++ = '0' + (dp / 100);
dp %= 100;
}
@ -92,7 +105,8 @@ _gcvt(long double value, int ndigit, char *s, int flags)
s1 = _ecvt(value, ndigit, &dp, &sign);
s2 = s;
if (sign) *s2++ = '-';
if (sign)
*s2++ = '-';
else if (flags & FL_SIGN)
*s2++ = '+';
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--)
nndigit--;
if (USE_EXP(dp,ndigit)) {
if (USE_EXP(dp, ndigit))
{
/* Use E format */
dp--;
*s2++ = *s1++;
if ((nndigit > 1) || (flags & FL_ALT)) *s2++ = '.';
while (--nndigit > 0) *s2++ = *s1++;
if ((nndigit > 1) || (flags & FL_ALT))
*s2++ = '.';
while (--nndigit > 0)
*s2++ = *s1++;
*s2++ = 'e';
if (dp < 0) {
if (dp < 0)
{
*s2++ = '-';
dp = -dp;
}
else *s2++ = '+';
else
*s2++ = '+';
s2 += NDIGINEXP(dp);
*s2 = 0;
for (i = NDIGINEXP(dp); i > 0; i--) {
for (i = NDIGINEXP(dp); i > 0; i--)
{
*--s2 = dp % 10 + '0';
dp /= 10;
}
return s;
}
/* Use f format */
if (dp <= 0) {
if (*s1 != '0') {
if (dp <= 0)
{
if (*s1 != '0')
{
/* otherwise the whole number is 0 */
*s2++ = '0';
*s2++ = '.';
}
while (dp < 0) {
while (dp < 0)
{
dp++;
*s2++ = '0';
}
}
for (i = 1; i <= nndigit; i++) {
for (i = 1; i <= nndigit; i++)
{
*s2++ = *s1++;
if (i == dp) *s2++ = '.';
}
if (i <= dp) {
while (i++ <= dp) *s2++ = '0';
if (i == dp)
*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';
return s;
}
char *
_f_print(va_list *ap, int flags, char *s, char c, int precision)
char* _f_print(va_list* ap, int flags, char* s, char c, int precision)
{
register char* old_s = s;
long double ld_val;
if (flags & FL_LONGDOUBLE) ld_val = va_arg(*ap, long double);
else ld_val = (long double) va_arg(*ap, double);
if (flags & FL_LONGDOUBLE)
ld_val = va_arg(*ap, long double);
else
ld_val = (long double)va_arg(*ap, double);
switch(c) {
switch (c)
{
case 'f':
s = _pfloat(ld_val, s, precision, flags);
break;
@ -170,9 +200,12 @@ _f_print(va_list *ap, int flags, char *s, char c, int precision)
s += strlen(s);
break;
}
if ( c == 'E' || c == 'G') {
while (*old_s && *old_s != 'e') old_s++;
if (*old_s == 'e') *old_s = 'E';
if (c == 'E' || c == 'G')
{
while (*old_s && *old_s != 'e')
old_s++;
if (*old_s == 'e')
*old_s = 'E';
}
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
than nbytes, so loop ...
*/
while ((c = write(d, buf, nbytes)) > 0 && c < nbytes) {
while ((c = write(d, buf, nbytes)) > 0 && c < nbytes)
{
nbytes -= c;
buf += c;
}
return c > 0;
}
int
__flushbuf(int c, FILE * stream)
int __flushbuf(int c, FILE* stream)
{
_clean = __cleanup;
if (fileno(stream) < 0) return EOF;
if (!io_testflag(stream, _IOWRITE)) return EOF;
if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
if (fileno(stream) < 0)
return EOF;
if (!io_testflag(stream, _IOWRITE))
return EOF;
if (io_testflag(stream, _IOREADING) && !feof(stream))
return EOF;
stream->_flags &= ~_IOREADING;
stream->_flags |= _IOWRITING;
if (!io_testflag(stream, _IONBF)) {
if (!stream->_buf) {
if (stream == stdout && isatty(fileno(stdout))) {
if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) {
if (!io_testflag(stream, _IONBF))
{
if (!stream->_buf)
{
if (stream == stdout && isatty(fileno(stdout)))
{
if (!(stream->_buf = (unsigned char*)malloc(BUFSIZ)))
{
stream->_flags |= _IONBF;
} else {
}
else
{
stream->_flags |= _IOLBF | _IOMYBUF;
stream->_bufsiz = BUFSIZ;
stream->_count = -1;
}
} else {
if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) {
}
else
{
if (!(stream->_buf = (unsigned char*)malloc(BUFSIZ)))
{
stream->_flags |= _IONBF;
} else {
}
else
{
stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ;
if (!io_testflag(stream, _IOLBF))
stream->_count = BUFSIZ - 1;
else stream->_count = -1;
else
stream->_count = -1;
}
}
stream->_ptr = stream->_buf;
}
}
if (io_testflag(stream, _IONBF)) {
if (io_testflag(stream, _IONBF))
{
char c1 = c;
stream->_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;
return EOF;
}
}
if (write(fileno(stream), &c1, 1) != 1) {
if (write(fileno(stream), &c1, 1) != 1)
{
stream->_flags |= _IOERR;
return EOF;
}
return (unsigned char)c;
} else if (io_testflag(stream, _IOLBF)) {
}
else if (io_testflag(stream, _IOLBF))
{
*stream->_ptr++ = c;
/* 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;
stream->_ptr = stream->_buf;
stream->_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;
return EOF;
}
}
if (!do_write(fileno(stream), (char*)stream->_buf,
count)) {
count))
{
stream->_flags |= _IOERR;
return EOF;
}
}
} else {
}
else
{
int count = stream->_ptr - stream->_buf;
stream->_count = stream->_bufsiz - 1;
stream->_ptr = stream->_buf + 1;
if (count > 0) {
if (io_testflag(stream, _IOAPPEND)) {
if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
if (count > 0)
{
if (io_testflag(stream, _IOAPPEND))
{
if (lseek(fileno(stream), 0L, SEEK_END) == -1)
{
stream->_flags |= _IOERR;
return EOF;
}
}
if (! do_write(fileno(stream), (char *)stream->_buf, count)) {
if (!do_write(fileno(stream), (char*)stream->_buf, count))
{
*(stream->_buf) = c;
stream->_flags |= _IOERR;
return EOF;

View file

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

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
fprintf(FILE *stream, const char *format, ...)
int fprintf(FILE* stream, const char* format, ...)
{
va_list ap;
int retval;

View file

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

View file

@ -5,14 +5,15 @@
#include <stdio.h>
int
fputs(register const char *s, register FILE *stream)
int fputs(register const char* s, register FILE* stream)
{
register int i = 0;
while (*s)
if (putc(*s++, stream) == EOF) return EOF;
else i++;
if (putc(*s++, stream) == EOF)
return EOF;
else
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;
if (size)
while ( ndone < nmemb ) {
while (ndone < nmemb)
{
s = size;
do {
do
{
if ((c = getc(stream)) != EOF)
*cp++ = c;
else

View file

@ -15,8 +15,7 @@
* does not exist. The reason is given in fopen.c.
*/
FILE *
freopen(const char *name, const char *mode, FILE *stream)
FILE* freopen(const char* name, const char* mode, FILE* stream)
{
register int i;
int rwmode = 0, rwflags = 0;
@ -25,7 +24,8 @@ freopen(const char *name, const char *mode, FILE *stream)
(void)fflush(stream); /* ignore errors */
(void)close(fileno(stream));
switch(*mode++) {
switch (*mode++)
{
case 'r':
flags |= _IOREAD;
rwmode = O_RDONLY;
@ -44,8 +44,10 @@ freopen(const char *name, const char *mode, FILE *stream)
return (FILE*)NULL;
}
while (*mode) {
switch(*mode++) {
while (*mode)
{
switch (*mode++)
{
case 'b':
continue;
case '+':
@ -61,16 +63,21 @@ freopen(const char *name, const char *mode, FILE *stream)
if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0)
&& (rwflags & O_CREAT))) {
if (((fd = creat(name, PMODE)) < 0) && flags | _IOREAD) {
&& (rwflags & O_CREAT)))
{
if (((fd = creat(name, PMODE)) < 0) && flags | _IOREAD)
{
(void)close(fd);
fd = open(name, rwmode);
}
}
if (fd < 0) {
for( i = 0; i < FOPEN_MAX; i++) {
if (stream == __iotab[i]) {
if (fd < 0)
{
for (i = 0; i < FOPEN_MAX; i++)
{
if (stream == __iotab[i])
{
__iotab[i] = 0;
break;
}

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
fscanf(FILE *stream, const char *format, ...)
int fscanf(FILE* stream, const char* format, ...)
{
va_list ap;
int retval;

View file

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

View file

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

View file

@ -19,7 +19,8 @@ long ftell(FILE *stream)
&& stream->_buf
&& !io_testflag(stream, _IONBF))
adjust = stream->_ptr - stream->_buf;
else adjust = 0;
else
adjust = 0;
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;
if (size)
while ( ndone < nmemb ) {
while (ndone < nmemb)
{
s = size;
do {
do
{
if (putc((int)*cp, stream)
== EOF)
return ndone;
cp++;
}
while (--s);
} while (--s);
ndone++;
}
return ndone;

View file

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

View file

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

View file

@ -5,8 +5,7 @@
#include <stdio.h>
char *
gets(char *s)
char* gets(char* s)
{
register FILE* stream = stdin;
register int ch;
@ -16,10 +15,15 @@ gets(char *s)
while ((ch = getc(stream)) != EOF && ch != '\n')
*ptr++ = ch;
if (ch == EOF) {
if (feof(stream)) {
if (ptr == s) return NULL;
} else return NULL;
if (ch == EOF)
{
if (feof(stream))
{
if (ptr == s)
return NULL;
}
else
return NULL;
}
*ptr = '\0';

View file

@ -7,8 +7,7 @@
/* This routine is used in doprnt.c as well as in tmpfile.c and tmpnam.c. */
char *
_i_compute(unsigned long val, int base, char *s, int nrdigits)
char* _i_compute(unsigned long val, int base, char* s, int nrdigits)
{
int c;

View file

@ -7,10 +7,10 @@
#include <stdio.h>
#include <string.h>
void
perror(const char *s)
void perror(const char* s)
{
if (s && *s)
{
if (s && *s) {
(void)fputs(s, stderr);
(void)fputs(": ", stderr);
}

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
printf(const char *format, ...)
int printf(const char* format, ...)
{
va_list ap;
int retval;

View file

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

View file

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

View file

@ -5,16 +5,19 @@
#include <stdio.h>
int
puts(register const char *s)
int puts(register const char* s)
{
register FILE* file = stdout;
register int i = 0;
while (*s) {
if (putc(*s++, file) == EOF) return EOF;
else i++;
while (*s)
{
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;
}

View file

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

View file

@ -6,8 +6,7 @@
#include <stdio.h>
#include "loc_incl.h"
void
rewind(FILE *stream)
void rewind(FILE* stream)
{
(void)fseek(stream, 0L, SEEK_SET);
clearerr(stream);

View file

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

View file

@ -6,8 +6,7 @@
#include <stdio.h>
#include "loc_incl.h"
void
setbuf(register FILE *stream, char *buf)
void setbuf(register FILE* stream, char* buf)
{
(void)setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), (size_t)BUFSIZ);
}

View file

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

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
snprintf(char * s, size_t len, const char *format, ...)
int snprintf(char* s, size_t len, const char* format, ...)
{
va_list ap;
int retval;

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
sprintf(char * s, const char *format, ...)
int sprintf(char* s, const char* format, ...)
{
va_list ap;
int retval;

View file

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

View file

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

View file

@ -6,15 +6,16 @@
#include <stdio.h>
#include "loc_incl.h"
int
ungetc(int ch, FILE *stream)
int ungetc(int ch, FILE* stream)
{
unsigned char* p;
if (ch == EOF || !io_testflag(stream, _IOREADING))
return EOF;
if (stream->_ptr == stream->_buf) {
if (stream->_count != 0) return EOF;
if (stream->_ptr == stream->_buf)
{
if (stream->_count != 0)
return EOF;
stream->_ptr++;
}
stream->_count++;

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
vfprintf(FILE *stream, const char *format, va_list arg)
int vfprintf(FILE* stream, const char* format, va_list arg)
{
return _doprnt(format, arg, stream);
}

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
vprintf(const char *format, va_list arg)
int vprintf(const char* format, va_list arg)
{
return _doprnt(format, arg, stdout);
}

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
vsnprintf(char *s, size_t len, const char *format, va_list arg)
int vsnprintf(char* s, size_t len, const char* format, va_list arg)
{
int retval;
FILE tmp_stream;

View file

@ -7,8 +7,7 @@
#include <stdarg.h>
#include "loc_incl.h"
int
vsprintf(char *s, const char *format, va_list arg)
int vsprintf(char* s, const char* format, va_list arg)
{
int retval;
FILE tmp_stream;

View file

@ -12,10 +12,9 @@
extern void (*_clean)(void);
void
abort(void)
void abort(void)
{
if (_clean) _clean(); /* flush all output files */
if (_clean)
_clean(); /* flush all output files */
raise(SIGABRT);
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -39,13 +39,14 @@ char* _findenv(register const char* name, int* offset)
if (!environ)
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)
if (!strncmp(*P, name, len))
if (*(C = *P + len) == '=') {
if (*(C = *P + len) == '=')
{
*offset = P - environ;
return (char*)(++C);
}
return (NULL);
}

View file

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

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