├── ERRNO └── ERRNO.C ├── MATH ├── COS.C ├── SIN.C ├── LOG.C ├── ACOS.C ├── ASIN.C ├── LOG10.C ├── CEIL.C ├── FLOOR.C ├── XPOLY.C ├── MODF.C ├── FABS.C ├── LDEXP.C ├── EXP.C ├── FREXP.C ├── XVALUES.C ├── XDTEST.C ├── COSH.C ├── ATAN.C ├── XDUNSCAL.C ├── ATAN2.C ├── SQRT.C ├── XDNORM.C ├── FMOD.C ├── TANH.C ├── SINH.C ├── XATAN.C ├── XEXP.C ├── XDINT.C ├── TAN.C ├── XDTENTO.C ├── XDSCALE.C ├── XSIN.C ├── XLOG.C └── XASIN.C ├── CTYPE ├── ISDIGIT.C ├── ISLOWER.C ├── ISUPPER.C ├── ISXDIGIT.C ├── TOLOWER.C ├── TOUPPER.C ├── ISCNTRL.C ├── ISPUNCT.C ├── ISALPHA.C ├── ISSPACE.C ├── ISGRAPH.C ├── ISALNUM.C ├── ISPRINT.C ├── XCTYPE.C ├── XTOUPPER.C └── XTOLOWER.C ├── STDIO ├── GETC.C ├── GETCHAR.C ├── PUTC.C ├── FTELL.C ├── PUTCHAR.C ├── FEOF.C ├── FERROR.C ├── RENAME.C ├── FGETPOS.C ├── REWIND.C ├── FSEEK.C ├── SETBUF.C ├── FSETPOS.C ├── PUTS.C ├── CLEARERR.C ├── REMOVE.C ├── FREOPEN.C ├── VPRINTF.C ├── VFPRINTF.C ├── PERROR.C ├── FGETC.C ├── PRINTF.C ├── TMPFILE.C ├── VSPRINTF.C ├── FPRINTF.C ├── FPUTC.C ├── SPRINTF.C ├── SCANF.C ├── UNGETC.C ├── FSCANF.C ├── XFILES.C ├── SSCANF.C ├── FOPEN.C ├── XFOPEN.C ├── FFLUSH.C ├── XFWPREP.C ├── FREAD.C ├── TMPNAM.C ├── FCLOSE.C ├── FPUTS.C ├── SETVBUF.C ├── FWRITE.C ├── XFOPREP.C ├── XLITOB.C ├── XFGPOS.C ├── GETS.C ├── XFRPREP.C ├── FGETS.C ├── XFSPOS.C ├── XGETFLOA.C ├── XSCANF.C └── XGETINT.C ├── STDLIB ├── SRAND.C ├── ABS.C ├── ATOF.C ├── ATOI.C ├── ATOL.C ├── LABS.C ├── ABORT.C ├── STRTOD.C ├── STRTOUL.C ├── MBLEN.C ├── WCTOMB.C ├── MBTOWC.C ├── RAND.C ├── CALLOC.C ├── XGETMEM.C ├── ATEXIT.C ├── DIV.C ├── LDIV.C ├── GETENV.C ├── MBSTOWCS.C ├── EXIT.C ├── SYSTEM.C ├── BSEARCH.C ├── STRTOL.C ├── WCSTOMBS.C ├── REALLOC.C ├── FREE.C ├── XWCTOMB.C ├── XMBTOWC.C ├── QSORT.C └── MALLOC.C ├── _DUMMY ├── TIME.C ├── REMOVE.C ├── XFGPOS.C ├── XGETMEM.C ├── RENAME.C ├── SYSTEM.C ├── XFSPOS.C ├── XFOPEN.C └── TMPNAM.C ├── TIME ├── CLOCK.C ├── CTIME.C ├── GMTIME.C ├── STRFTIME.C ├── DIFFTIME.C ├── TIME.C ├── XGETTIME.C ├── LOCALTIM.C ├── MKTIME.C ├── ASCTIME.C ├── XGETZONE.C ├── XSTRFTIM.C ├── XISDST.C ├── XTTOTM.C └── XGETDST.C ├── _TC86 ├── RENAME.C ├── REMOVE.C ├── XGETMEM.C ├── YFUNS.H ├── XFOPEN.C ├── TMPNAM.C ├── XFGPOS.C ├── YVALS.H ├── XFSPOS.C └── YTCPP.CPP ├── STRING ├── STRLEN.C ├── STRCPY.C ├── STRCHR.C ├── MEMSET.C ├── STRCMP.C ├── MEMCPY.C ├── STRCAT.C ├── STRNCPY.C ├── MEMCHR.C ├── STRRCHR.C ├── STRNCAT.C ├── STRNCMP.C ├── STRCSPN.C ├── STRPBRK.C ├── MEMCMP.C ├── STRSPN.C ├── MEMMOVE.C ├── STRSTR.C ├── STRTOK.C ├── STRXFRM.C ├── STRERROR.C ├── XSTRXFRM.C └── STRCOLL.C ├── _UNIX68K ├── REMOVE.C ├── RAISE.C ├── TIME.C ├── RENAME.C ├── XGETMEM.C ├── XFOPEN.C ├── YFUNS.H ├── TMPNAM.C ├── SYSTEM.C ├── XFGPOS.C ├── XFSPOS.C └── YVALS.H ├── _UNIXVAX ├── REMOVE.C ├── RAISE.C ├── TIME.C ├── RENAME.C ├── XGETMEM.C ├── YFUNS.H ├── XFOPEN.C ├── TMPNAM.C ├── SYSTEM.C ├── XFGPOS.C ├── XFSPOS.C └── YVALS.H ├── ASSERT └── XASSERT.C ├── _HEADERS ├── XSTRXFRM.H ├── XTINFO.H ├── SETJMP.H ├── ERRNO.H ├── ASSERT.H ├── STDDEF.H ├── STDARG.H ├── XSTATE.H ├── YFUNS.H ├── XALLOC.H ├── SIGNAL.H ├── XTIME.H ├── TIME.H ├── LIMITS.H ├── MATH.H ├── LOCALE.H ├── YVALS.H ├── XMATH.H ├── STRING.H ├── CTYPE.H ├── XLOCALE.H ├── FLOAT.H └── XSTDIO.H ├── LOCALE ├── XDEFLOC.C ├── XFREELOC.C ├── LOCALECO.C ├── XREADLOC.C ├── XSETLOC.C ├── XLOCTERM.C └── XLOCTAB.C ├── SIGNAL ├── SIGNAL.C └── RAISE.C ├── _TEST ├── TERRNO.C ├── TSIGNAL.C ├── TTIME.C ├── TASSERT.C ├── TSTDARG.C ├── TSTDDEF.C ├── TSETJMP.C ├── TMATH3.C ├── TSTDIO2.C ├── TLOCALE.C └── TLIMITS.C └── SETJMP ├── SETJMP.C └── LONGJMP.C /ERRNO/ERRNO.C: -------------------------------------------------------------------------------- 1 | /* errno storage */ 2 | #include 3 | #undef errno 4 | 5 | int errno = 0; 6 | -------------------------------------------------------------------------------- /MATH/COS.C: -------------------------------------------------------------------------------- 1 | /* cos function */ 2 | #include 3 | 4 | double (cos)(double x) 5 | { /* compute cos */ 6 | return (_Sin(x, 1)); 7 | } 8 | -------------------------------------------------------------------------------- /MATH/SIN.C: -------------------------------------------------------------------------------- 1 | /* sin function */ 2 | #include 3 | 4 | double (sin)(double x) 5 | { /* compute sin */ 6 | return (_Sin(x, 0)); 7 | } 8 | -------------------------------------------------------------------------------- /MATH/LOG.C: -------------------------------------------------------------------------------- 1 | /* log function */ 2 | #include 3 | 4 | double (log)(double x) 5 | { /* compute ln(x) */ 6 | return (_Log(x, 0)); 7 | } 8 | -------------------------------------------------------------------------------- /MATH/ACOS.C: -------------------------------------------------------------------------------- 1 | /* acos function */ 2 | #include 3 | 4 | double (acos)(double x) 5 | { /* compute acos(x) */ 6 | return (_Asin(x, 1)); 7 | } 8 | -------------------------------------------------------------------------------- /MATH/ASIN.C: -------------------------------------------------------------------------------- 1 | /* asin function */ 2 | #include 3 | 4 | double (asin)(double x) 5 | { /* compute asin(x) */ 6 | return (_Asin(x, 0)); 7 | } 8 | -------------------------------------------------------------------------------- /MATH/LOG10.C: -------------------------------------------------------------------------------- 1 | /* log10 function */ 2 | #include 3 | 4 | double (log10)(double x) 5 | { /* compute log10(x) */ 6 | return (_Log(x, 1)); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISDIGIT.C: -------------------------------------------------------------------------------- 1 | /* isdigit function */ 2 | #include 3 | 4 | int (isdigit)(int c) 5 | { /* test for digit */ 6 | return (_Ctype[c] & _DI); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/GETC.C: -------------------------------------------------------------------------------- 1 | /* getc function */ 2 | #include "xstdio.h" 3 | 4 | int (getc)(FILE *str) 5 | { /* get a character from stream */ 6 | return (fgetc(str)); 7 | } 8 | -------------------------------------------------------------------------------- /STDLIB/SRAND.C: -------------------------------------------------------------------------------- 1 | /* srand function */ 2 | #include 3 | 4 | void (srand)(unsigned int seed) 5 | { /* alter the seed */ 6 | _Randseed = seed; 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/GETCHAR.C: -------------------------------------------------------------------------------- 1 | /* getchar function */ 2 | #include "xstdio.h" 3 | 4 | int (getchar)(void) 5 | { /* get a character from stdin */ 6 | return (fgetc(stdin)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/PUTC.C: -------------------------------------------------------------------------------- 1 | /* putc function */ 2 | #include "xstdio.h" 3 | 4 | int (putc)(int c, FILE *str) 5 | { /* put character to stream */ 6 | return (fputc(c, str)); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISLOWER.C: -------------------------------------------------------------------------------- 1 | /* islower function */ 2 | #include 3 | 4 | int (islower)(int c) 5 | { /* test for lowercase character */ 6 | return (_Ctype[c] & _LO); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISUPPER.C: -------------------------------------------------------------------------------- 1 | /* isupper function */ 2 | #include 3 | 4 | int (isupper)(int c) 5 | { /* test for uppercase character */ 6 | return (_Ctype[c] & _UP); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISXDIGIT.C: -------------------------------------------------------------------------------- 1 | /* isxdigit function */ 2 | #include 3 | 4 | int (isxdigit)(int c) 5 | { /* test for hexadecimal digit */ 6 | return (_Ctype[c] & _XD); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/TOLOWER.C: -------------------------------------------------------------------------------- 1 | /* tolower function */ 2 | #include 3 | 4 | int (tolower)(int c) 5 | { /* convert to lowercase character */ 6 | return (_Tolower[c]); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/TOUPPER.C: -------------------------------------------------------------------------------- 1 | /* toupper function */ 2 | #include 3 | 4 | int (toupper)(int c) 5 | { /* convert to uppercase character */ 6 | return (_Toupper[c]); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/FTELL.C: -------------------------------------------------------------------------------- 1 | /* ftell function */ 2 | #include "xstdio.h" 3 | 4 | long (ftell)(FILE *str) 5 | { /* get seek offset for stream */ 6 | return (_Fgpos(str, NULL)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/PUTCHAR.C: -------------------------------------------------------------------------------- 1 | /* putchar function */ 2 | #include "xstdio.h" 3 | 4 | int (putchar)(int c) 5 | { /* put character to stdout */ 6 | return (fputc(c, stdout)); 7 | } 8 | -------------------------------------------------------------------------------- /STDLIB/ABS.C: -------------------------------------------------------------------------------- 1 | /* abs function */ 2 | #include 3 | 4 | int (abs)(int i) 5 | { /* compute absolute value of int argument */ 6 | return ((i < 0) ? -i : i); 7 | } 8 | -------------------------------------------------------------------------------- /STDLIB/ATOF.C: -------------------------------------------------------------------------------- 1 | /* atof function */ 2 | #include 3 | 4 | double (atof)(const char *s) 5 | { /* convert string to double */ 6 | return (_Stod(s, NULL)); 7 | } 8 | -------------------------------------------------------------------------------- /_DUMMY/TIME.C: -------------------------------------------------------------------------------- 1 | /* time function -- dummy version */ 2 | #include 3 | 4 | time_t (time)(time_t *tod) 5 | { /* return calendar time */ 6 | return (-1); 7 | } 8 |  -------------------------------------------------------------------------------- /CTYPE/ISCNTRL.C: -------------------------------------------------------------------------------- 1 | /* iscntrl function */ 2 | #include 3 | 4 | int (iscntrl)(int c) 5 | { /* test for control character */ 6 | return (_Ctype[c] & (_BB|_CN)); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISPUNCT.C: -------------------------------------------------------------------------------- 1 | /* ispunct function */ 2 | #include 3 | 4 | int (ispunct)(int c) 5 | { /* test for punctuation character */ 6 | return (_Ctype[c] & _PU); 7 | } 8 | -------------------------------------------------------------------------------- /STDLIB/ATOI.C: -------------------------------------------------------------------------------- 1 | /* atoi function */ 2 | #include 3 | 4 | int (atoi)(const char *s) 5 | { /* convert string to int */ 6 | return ((int)_Stoul(s, NULL, 10)); 7 | } 8 | -------------------------------------------------------------------------------- /STDLIB/ATOL.C: -------------------------------------------------------------------------------- 1 | /* atol function */ 2 | #include 3 | 4 | long (atol)(const char *s) 5 | { /* convert string to long */ 6 | return ((long)_Stoul(s, NULL, 10)); 7 | } 8 | -------------------------------------------------------------------------------- /TIME/CLOCK.C: -------------------------------------------------------------------------------- 1 | /* clock function -- simple version */ 2 | #include 3 | 4 | clock_t (clock)(void) 5 | { /* return CPU time */ 6 | return ((clock_t)time(NULL)); 7 | } 8 | -------------------------------------------------------------------------------- /_DUMMY/REMOVE.C: -------------------------------------------------------------------------------- 1 | /* remove function -- dummy version */ 2 | #include "xstdio.h" 3 | 4 | int (remove)(const char *filename) 5 | { /* remove a file */ 6 | return (-1); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISALPHA.C: -------------------------------------------------------------------------------- 1 | /* isalpha function */ 2 | #include 3 | 4 | int (isalpha)(int c) 5 | { /* test for alphabetic character */ 6 | return (_Ctype[c] & (_LO|_UP|_XA)); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISSPACE.C: -------------------------------------------------------------------------------- 1 | /* isspace function */ 2 | #include 3 | 4 | int (isspace)(int c) 5 | { /* test for spacing character */ 6 | return (_Ctype[c] & (_CN|_SP|_XS)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/FEOF.C: -------------------------------------------------------------------------------- 1 | /* feof function */ 2 | #include "xstdio.h" 3 | 4 | int (feof)(FILE *str) 5 | { /* test end-of-file indicator for a stream */ 6 | return (str->_Mode & _MEOF); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/FERROR.C: -------------------------------------------------------------------------------- 1 | /* ferror function */ 2 | #include "xstdio.h" 3 | 4 | int (ferror)(FILE *str) 5 | { /* test error indicator for a stream */ 6 | return (str->_Mode & _MERR); 7 | } 8 | -------------------------------------------------------------------------------- /STDLIB/LABS.C: -------------------------------------------------------------------------------- 1 | /* labs function */ 2 | #include 3 | 4 | long (labs)(long i) 5 | { /* compute absolute value of long argument */ 6 | return ((i < 0) ? -i : i); 7 | } 8 | -------------------------------------------------------------------------------- /_DUMMY/XFGPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fgpos function -- dummy version */ 2 | #include "xstdio.h" 3 | 4 | long _Fgpos(FILE *str, fpos_t *ptr) 5 | { /* position a file */ 6 | return (EOF); 7 | } 8 |  -------------------------------------------------------------------------------- /_DUMMY/XGETMEM.C: -------------------------------------------------------------------------------- 1 | /* _Getmem function -- dummy version */ 2 | #include "xalloc.h" 3 | 4 | void *_Getmem(size_t size) 5 | { /* allocate raw storage */ 6 | return (NULL); 7 | } 8 |  -------------------------------------------------------------------------------- /CTYPE/ISGRAPH.C: -------------------------------------------------------------------------------- 1 | /* isgraph function */ 2 | #include 3 | 4 | int (isgraph)(int c) 5 | { /* test for graphic character */ 6 | return (_Ctype[c] & (_DI|_LO|_PU|_UP|_XA)); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISALNUM.C: -------------------------------------------------------------------------------- 1 | /* isalnum function */ 2 | #include 3 | 4 | int (isalnum)(int c) 5 | { /* test for alphanumeric character */ 6 | return (_Ctype[c] & (_DI|_LO|_UP|_XA)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/RENAME.C: -------------------------------------------------------------------------------- 1 | /* rename function -- dummy version */ 2 | #include "xstdio.h" 3 | 4 | int (rename)(const char *oldnm, const char *newnm) 5 | { /* rename a file */ 6 | return (-1); 7 | } 8 | -------------------------------------------------------------------------------- /TIME/CTIME.C: -------------------------------------------------------------------------------- 1 | /* ctime function */ 2 | #include 3 | 4 | char *(ctime)(const time_t *tod) 5 | { /* convert calendar time to local text */ 6 | return (asctime(localtime(tod))); 7 | } 8 | -------------------------------------------------------------------------------- /_DUMMY/RENAME.C: -------------------------------------------------------------------------------- 1 | /* rename function -- dummy version */ 2 | #include "xstdio.h" 3 | 4 | int (rename)(const char *oldnm, const char *newnm) 5 | { /* rename a file */ 6 | return (-1); 7 | } 8 | -------------------------------------------------------------------------------- /_TC86/RENAME.C: -------------------------------------------------------------------------------- 1 | /* rename function -- dummy version */ 2 | #include "xstdio.h" 3 | 4 | int (rename)(const char *oldnm, const char *newnm) 5 | { /* rename a file */ 6 | return (-1); 7 | } 8 | -------------------------------------------------------------------------------- /CTYPE/ISPRINT.C: -------------------------------------------------------------------------------- 1 | /* isprint function */ 2 | #include 3 | 4 | int (isprint)(int c) 5 | { /* test for printable character */ 6 | return (_Ctype[c] & (_DI|_LO|_PU|_SP|_UP|_XA)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/FGETPOS.C: -------------------------------------------------------------------------------- 1 | /* fgetpos function */ 2 | #include "xstdio.h" 3 | 4 | int (fgetpos)(FILE *str, fpos_t *p) 5 | { /* get file position indicator for stream */ 6 | return (_Fgpos(str, p)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/REWIND.C: -------------------------------------------------------------------------------- 1 | /* rewind function */ 2 | #include "xstdio.h" 3 | 4 | void (rewind)(FILE *str) 5 | { /* rewind stream */ 6 | _Fspos(str, NULL, 0L, SEEK_SET); 7 | str->_Mode &= ~_MERR; 8 | } 9 | -------------------------------------------------------------------------------- /_DUMMY/SYSTEM.C: -------------------------------------------------------------------------------- 1 | /* system function -- dummy version */ 2 | #include 3 | 4 | int (system)(const char *s) 5 | { /* send text to system command line processor */ 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /TIME/GMTIME.C: -------------------------------------------------------------------------------- 1 | /* gmtime function */ 2 | #include "xtime.h" 3 | 4 | struct tm *(gmtime)(const time_t *tod) 5 | { /* convert to Greenwich Mean Time (UTC) */ 6 | return (_Ttotm(NULL, *tod, 0)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/FSEEK.C: -------------------------------------------------------------------------------- 1 | /* fseek function */ 2 | #include "xstdio.h" 3 | 4 | int (fseek)(FILE *str, long off, int smode) 5 | { /* set seek offset for stream */ 6 | return (_Fspos(str, NULL, off, smode)); 7 | } 8 | -------------------------------------------------------------------------------- /STDIO/SETBUF.C: -------------------------------------------------------------------------------- 1 | /* setbuf function */ 2 | #include "xstdio.h" 3 | 4 | void (setbuf)(FILE *str, char *buf) 5 | { /* set up buffer for a stream */ 6 | setvbuf(str, buf, buf ? _IOFBF : _IONBF, BUFSIZ); 7 | } 8 | -------------------------------------------------------------------------------- /_DUMMY/XFSPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fspos function -- dummy version */ 2 | #include "xstdio.h" 3 | 4 | int _Fspos(FILE *str, const fpos_t *ptr, long *off, int way) 5 | { /* position a file */ 6 | return (EOF); 7 | } 8 |  -------------------------------------------------------------------------------- /STDLIB/ABORT.C: -------------------------------------------------------------------------------- 1 | /* abort function */ 2 | #include 3 | #include 4 | 5 | void (abort)(void) 6 | { /* terminate abruptly */ 7 | raise(SIGABRT); 8 | exit(EXIT_FAILURE); 9 | } 10 | -------------------------------------------------------------------------------- /STDLIB/STRTOD.C: -------------------------------------------------------------------------------- 1 | /* strtod function */ 2 | #include 3 | 4 | double (strtod)(const char *s, char **endptr) 5 | { /* convert string to double, with checking */ 6 | return (_Stod(s, endptr)); 7 | } 8 | -------------------------------------------------------------------------------- /_DUMMY/XFOPEN.C: -------------------------------------------------------------------------------- 1 | /* _Fopen function -- dummy version */ 2 | #include "xstdio.h" 3 | 4 | int _Fopen(const char *path, unsigned int smode, 5 | const char *mods) 6 | { /* open a file */ 7 | return (-1); 8 | } 9 | -------------------------------------------------------------------------------- /MATH/CEIL.C: -------------------------------------------------------------------------------- 1 | /* ceil function */ 2 | #include "xmath.h" 3 | 4 | double (ceil)(double x) 5 | { /* compute ceil(x) */ 6 | double y = x; 7 | 8 | return (_Dint(&y, 0) < 0 && 0.0 < x ? y + 1.0 : y); 9 | } 10 | -------------------------------------------------------------------------------- /STDIO/FSETPOS.C: -------------------------------------------------------------------------------- 1 | /* fsetpos function */ 2 | #include "xstdio.h" 3 | 4 | int (fsetpos)(FILE *str, const fpos_t *p) 5 | { /* set file position indicator for stream */ 6 | return (_Fspos(str, p, 0L, SEEK_SET)); 7 | } 8 | -------------------------------------------------------------------------------- /MATH/FLOOR.C: -------------------------------------------------------------------------------- 1 | /* floor function */ 2 | #include "xmath.h" 3 | 4 | double (floor)(double x) 5 | { /* compute floor(x) */ 6 | double y = x; 7 | 8 | return (_Dint(&y, 0) < 0 && x < 0.0 ? y - 1.0 : y); 9 | } 10 | -------------------------------------------------------------------------------- /STDIO/PUTS.C: -------------------------------------------------------------------------------- 1 | /* puts function */ 2 | #include "xstdio.h" 3 | 4 | int (puts)(const char *s) 5 | { /* put string + newline to stdout */ 6 | return (fputs(s, stdout) < 0 7 | || fputc('\n', stdout) < 0 ? EOF : 0); 8 | } 9 | -------------------------------------------------------------------------------- /STDIO/CLEARERR.C: -------------------------------------------------------------------------------- 1 | /* clearerr function */ 2 | #include "xstdio.h" 3 | 4 | void (clearerr)(FILE *str) 5 | { /* clear EOF and error indicators for a stream */ 6 | if (str->_Mode & (_MOPENR|_MOPENW)) 7 | str->_Mode &= ~(_MEOF|_MERR); 8 | } 9 | -------------------------------------------------------------------------------- /STDLIB/STRTOUL.C: -------------------------------------------------------------------------------- 1 | /* strtoul function */ 2 | #include 3 | 4 | unsigned long (strtoul)(const char *s, char **endptr, int base) 5 | { /* convert string to unsigned long, with checking */ 6 | return (_Stoul(s, endptr, base)); 7 | } 8 | -------------------------------------------------------------------------------- /TIME/STRFTIME.C: -------------------------------------------------------------------------------- 1 | /* strftime function */ 2 | #include "xtime.h" 3 | 4 | size_t (strftime)(char *s, size_t n, const char *fmt, 5 | const struct tm *t) 6 | { /* format time to string */ 7 | return (_Strftime(s, n, fmt, t, &_Times)); 8 | } 9 | -------------------------------------------------------------------------------- /STRING/STRLEN.C: -------------------------------------------------------------------------------- 1 | /* strlen function */ 2 | #include 3 | 4 | size_t (strlen)(const char *s) 5 | { /* find length of s[] */ 6 | const char *sc; 7 | 8 | for (sc = s; *sc != '\0'; ++sc) 9 | ; 10 | return (sc - s); 11 | } 12 | -------------------------------------------------------------------------------- /TIME/DIFFTIME.C: -------------------------------------------------------------------------------- 1 | /* difftime function */ 2 | #include 3 | 4 | double (difftime)(time_t t1, time_t t0) 5 | { /* compute difference in times */ 6 | t0 -= _TBIAS, t1 -= _TBIAS; 7 | return (t0 <= t1 ? (double)(t1 - t0) : -(double)(t0 - t1)); 8 | } 9 | -------------------------------------------------------------------------------- /_UNIX68K/REMOVE.C: -------------------------------------------------------------------------------- 1 | /* remove function -- UNIX version */ 2 | #include "xstdio.h" 3 | 4 | /* UNIX system call */ 5 | int unlink(const char *); 6 | 7 | int (remove)(const char *fname) 8 | { /* remove a file */ 9 | return (unlink(fname)); 10 | } 11 | -------------------------------------------------------------------------------- /MATH/XPOLY.C: -------------------------------------------------------------------------------- 1 | /* _Poly function */ 2 | #include "xmath.h" 3 | 4 | double _Poly(double x, const double *tab, int n) 5 | { /* compute polynomial */ 6 | double y; 7 | 8 | for (y = *tab; 0 <= --n; ) 9 | y = y * x + *++tab; 10 | return (y); 11 | } 12 | -------------------------------------------------------------------------------- /STRING/STRCPY.C: -------------------------------------------------------------------------------- 1 | /* strcpy function */ 2 | #include 3 | 4 | char *(strcpy)(char *s1, const char *s2) 5 | { /* copy char s2[] to s1[] */ 6 | char *s = s1; 7 | 8 | for (s = s1; (*s++ = *s2++) != '\0'; ) 9 | ; 10 | return (s1); 11 | } 12 | -------------------------------------------------------------------------------- /_UNIXVAX/REMOVE.C: -------------------------------------------------------------------------------- 1 | /* remove function -- UNIX version */ 2 | #include "xstdio.h" 3 | 4 | /* UNIX system call */ 5 | int _Unlink(const char *); 6 | 7 | int (remove)(const char *fname) 8 | { /* remove a file */ 9 | return (_Unlink(fname)); 10 | } 11 | -------------------------------------------------------------------------------- /STDIO/REMOVE.C: -------------------------------------------------------------------------------- 1 | /* remove function -- Turbo C++ version */ 2 | #include "xstdio.h" 3 | 4 | /* Turbo C++ system call */ 5 | int unlink(const char *); 6 | 7 | int (remove)(const char *fname) 8 | { /* remove a file */ 9 | return (unlink(fname)); 10 | } 11 | -------------------------------------------------------------------------------- /_TC86/REMOVE.C: -------------------------------------------------------------------------------- 1 | /* remove function -- Turbo C++ version */ 2 | #include "xstdio.h" 3 | 4 | /* Turbo C++ system call */ 5 | int unlink(const char *); 6 | 7 | int (remove)(const char *fname) 8 | { /* remove a file */ 9 | return (unlink(fname)); 10 | } 11 | -------------------------------------------------------------------------------- /_UNIX68K/RAISE.C: -------------------------------------------------------------------------------- 1 | /* raise function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system calls */ 5 | int getpid(void); 6 | int kill(int, int); 7 | 8 | int (raise)(int sig) 9 | { /* raise a signal */ 10 | return (kill(getpid(), sig)); 11 | } 12 |  -------------------------------------------------------------------------------- /STDLIB/MBLEN.C: -------------------------------------------------------------------------------- 1 | /* mblen function */ 2 | #include 3 | 4 | /* static data */ 5 | _Mbsave _Mbxlen = {0}; 6 | 7 | int (mblen)(const char *s, size_t n) 8 | { /* determine length of next multibyte code */ 9 | return (_Mbtowc(NULL, s, n, &_Mbxlen)); 10 | } 11 | -------------------------------------------------------------------------------- /_UNIXVAX/RAISE.C: -------------------------------------------------------------------------------- 1 | /* raise function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system calls */ 5 | int _Getpid(void); 6 | int _Kill(int, int); 7 | 8 | int (raise)(int sig) 9 | { /* raise a signal */ 10 | return (_Kill(_Getpid(), sig)); 11 | } 12 |  -------------------------------------------------------------------------------- /STDLIB/WCTOMB.C: -------------------------------------------------------------------------------- 1 | /* wctomb function */ 2 | #include 3 | 4 | /* static data */ 5 | char _Wcxtomb = {0}; 6 | 7 | int (wctomb)(char *s, wchar_t wchar) 8 | { /* translate wide character to multibyte string */ 9 | return (_Wctomb(s, wchar, &_Wcxtomb)); 10 | } 11 | -------------------------------------------------------------------------------- /STDLIB/MBTOWC.C: -------------------------------------------------------------------------------- 1 | /* mbtowc function */ 2 | #include 3 | 4 | /* static data */ 5 | _Mbsave _Mbxtowc = {0}; 6 | 7 | int (mbtowc)(wchar_t *pwc, const char *s, size_t n) 8 | { /* determine next multibyte code */ 9 | return (_Mbtowc(pwc, s, n, &_Mbxtowc)); 10 | } 11 | -------------------------------------------------------------------------------- /ASSERT/XASSERT.C: -------------------------------------------------------------------------------- 1 | /* _Assert function */ 2 | #include 3 | #include 4 | #include 5 | 6 | void _Assert(char *mesg) 7 | { /* print assertion message and abort */ 8 | fputs(mesg, stderr); 9 | fputs(" -- assertion failed\n", stderr); 10 | abort(); 11 | } 12 | -------------------------------------------------------------------------------- /STDLIB/RAND.C: -------------------------------------------------------------------------------- 1 | /* rand function */ 2 | #include 3 | 4 | /* the seed */ 5 | unsigned long _Randseed = 1; 6 | 7 | int (rand)(void) 8 | { /* compute pseudo-random value */ 9 | _Randseed = _Randseed * 1103515245 + 12345; 10 | return ((unsigned int)(_Randseed >> 16) & RAND_MAX); 11 | } 12 | -------------------------------------------------------------------------------- /STRING/STRCHR.C: -------------------------------------------------------------------------------- 1 | /* strchr function */ 2 | #include 3 | 4 | char *(strchr)(const char *s, int c) 5 | { /* find first occurrence of c in char s[] */ 6 | const char ch = c; 7 | 8 | for (; *s != ch; ++s) 9 | if (*s == '\0') 10 | return (NULL); 11 | return ((char *)s); 12 | } 13 | -------------------------------------------------------------------------------- /STRING/MEMSET.C: -------------------------------------------------------------------------------- 1 | /* memset function */ 2 | #include 3 | 4 | void *(memset)(void *s, int c, size_t n) 5 | { /* store c throughout unsigned char s[n] */ 6 | const unsigned char uc = c; 7 | unsigned char *su = (unsigned char *)s; 8 | 9 | for (; 0 < n; ++su, --n) 10 | *su = uc; 11 | return (s); 12 | } 13 | -------------------------------------------------------------------------------- /TIME/TIME.C: -------------------------------------------------------------------------------- 1 | /* time function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system call */ 5 | time_t _Time(time_t *); 6 | 7 | time_t (time)(time_t *tod) 8 | { /* return calendar time */ 9 | time_t t = _Time(NULL) + (70*365LU+17)*86400; 10 | 11 | if (tod) 12 | *tod = t; 13 | return (t); 14 | } 15 | -------------------------------------------------------------------------------- /_UNIX68K/TIME.C: -------------------------------------------------------------------------------- 1 | /* time function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system call */ 5 | time_t time(time_t *); 6 | 7 | time_t (time)(time_t *tod) 8 | { /* return calendar time */ 9 | time_t t = time(NULL) + (70*365LU+17)*86400; 10 | 11 | if (tod) 12 | *tod = t; 13 | return (t); 14 | } 15 | -------------------------------------------------------------------------------- /_UNIXVAX/TIME.C: -------------------------------------------------------------------------------- 1 | /* time function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system call */ 5 | time_t _Time(time_t *); 6 | 7 | time_t (time)(time_t *tod) 8 | { /* return calendar time */ 9 | time_t t = _Time(NULL) + (70*365LU+17)*86400; 10 | 11 | if (tod) 12 | *tod = t; 13 | return (t); 14 | } 15 | -------------------------------------------------------------------------------- /STRING/STRCMP.C: -------------------------------------------------------------------------------- 1 | /* strcmp function */ 2 | #include 3 | 4 | int (strcmp)(const char *s1, const char *s2) 5 | { /* compare unsigned char s1[], s2[] */ 6 | for (; *s1 == *s2; ++s1, ++s2) 7 | if (*s1 == '\0') 8 | return (0); 9 | return (*(unsigned char *)s1 < *(unsigned char *)s2 10 | ? -1 : +1); 11 | } 12 |  -------------------------------------------------------------------------------- /_HEADERS/XSTRXFRM.H: -------------------------------------------------------------------------------- 1 | /* xstrxfrm.h internal header */ 2 | #include 3 | #include "xstate.h" 4 | /* type definitions */ 5 | typedef struct { 6 | unsigned char _State; 7 | unsigned short _Wchar; 8 | } _Cosave; 9 | /* declarations */ 10 | size_t _Strxfrm(char *, const unsigned char **, size_t, 11 | _Cosave *); 12 | -------------------------------------------------------------------------------- /_HEADERS/XTINFO.H: -------------------------------------------------------------------------------- 1 | /* xtinfo.h internal header */ 2 | 3 | /* type definitions */ 4 | typedef struct { 5 | const char *_Ampm; 6 | const char *_Days; 7 | const char *_Formats; 8 | const char *_Isdst; 9 | const char *_Months; 10 | const char *_Tzone; 11 | } _Tinfo; 12 | /* declarations */ 13 | extern _Tinfo _Times; 14 | -------------------------------------------------------------------------------- /STRING/MEMCPY.C: -------------------------------------------------------------------------------- 1 | /* memcpy function */ 2 | #include 3 | 4 | void *(memcpy)(void *s1, const void *s2, size_t n) 5 | { /* copy char s2[n] to s1[n] in any order */ 6 | char *su1 = (char *)s1; 7 | const char *su2 = (const char *)s2; 8 | 9 | for (; 0 < n; ++su1, ++su2, --n) 10 | *su1 = *su2; 11 | return (s1); 12 | } 13 | -------------------------------------------------------------------------------- /_HEADERS/SETJMP.H: -------------------------------------------------------------------------------- 1 | /* setjmp.h standard header */ 2 | #ifndef _SETJMP 3 | #define _SETJMP 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* macros */ 8 | #define setjmp(env) _Setjmp(env) 9 | /* type definitions */ 10 | typedef int jmp_buf[_NSETJMP]; 11 | /* declarations */ 12 | void longjmp(jmp_buf, int); 13 | #endif 14 | -------------------------------------------------------------------------------- /_UNIX68K/RENAME.C: -------------------------------------------------------------------------------- 1 | /* rename function -- UNIX version */ 2 | #include "xstdio.h" 3 | 4 | /* UNIX system calls */ 5 | int link(const char *, const char *); 6 | int unlink(const char *); 7 | 8 | int (rename)(const char *oldnm, const char *newnm) 9 | { /* rename a file */ 10 | return (link(oldnm, newnm) ? -1 : unlink(oldnm)); 11 | } 12 | -------------------------------------------------------------------------------- /STDLIB/CALLOC.C: -------------------------------------------------------------------------------- 1 | /* calloc function */ 2 | #include 3 | #include 4 | 5 | void *(calloc)(size_t nelem, size_t size) 6 | { /* allocate a data object on the heap and clear it */ 7 | const size_t n = nelem * size; 8 | char *p = (char *)malloc(n); 9 | 10 | if (p) 11 | memset(p, '\0', n); 12 | return (p); 13 | } 14 | -------------------------------------------------------------------------------- /_UNIX68K/XGETMEM.C: -------------------------------------------------------------------------------- 1 | /* _Getmem function -- UNIX version */ 2 | #include "xalloc.h" 3 | 4 | /* UNIX system call */ 5 | void *sbrk(int); 6 | 7 | void *_Getmem(size_t size) 8 | { /* allocate raw storage */ 9 | void *p; 10 | int isize = size; 11 | 12 | return (isize <= 0 || (p = sbrk(isize)) == (void *)-1 13 | ? NULL : p); 14 | } 15 | -------------------------------------------------------------------------------- /_UNIXVAX/RENAME.C: -------------------------------------------------------------------------------- 1 | /* rename function -- UNIX version */ 2 | #include "xstdio.h" 3 | 4 | /* UNIX system calls */ 5 | int _Link(const char *, const char *); 6 | int _Unlink(const char *); 7 | 8 | int (rename)(const char *oldnm, const char *newnm) 9 | { /* rename a file */ 10 | return (_Link(oldnm, newnm) ? -1 : _Unlink(oldnm)); 11 | } 12 | -------------------------------------------------------------------------------- /_UNIXVAX/XGETMEM.C: -------------------------------------------------------------------------------- 1 | /* _Getmem function -- UNIX version */ 2 | #include "xalloc.h" 3 | 4 | /* UNIX system call */ 5 | void *_Sbrk(int); 6 | 7 | void *_Getmem(size_t size) 8 | { /* allocate raw storage */ 9 | void *p; 10 | int isize = size; 11 | 12 | return (isize <= 0 || (p = _Sbrk(isize)) == (void *)-1 13 | ? NULL : p); 14 | } 15 | -------------------------------------------------------------------------------- /STRING/STRCAT.C: -------------------------------------------------------------------------------- 1 | /* strcat function */ 2 | #include 3 | 4 | char *(strcat)(char *s1, const char *s2) 5 | { /* copy char s2[] to end of s1[] */ 6 | char *s; 7 | 8 | for (s = s1; *s != '\0'; ++s) 9 | ; /* find end of s1[] */ 10 | for (; (*s = *s2) != '\0'; ++s, ++s2) 11 | ; /* copy s2[] to end */ 12 | return (s1); 13 | } 14 | -------------------------------------------------------------------------------- /_TC86/XGETMEM.C: -------------------------------------------------------------------------------- 1 | /* _Getmem function -- Turbo C++ version */ 2 | #include "xalloc.h" 3 | 4 | /* Turbo C++ system call */ 5 | void *sbrk(int); 6 | 7 | void *_Getmem(size_t size) 8 | { /* allocate raw storage */ 9 | void *p; 10 | int isize = size; 11 | 12 | return (isize <= 0 || (p = sbrk(isize)) == (void *)-1 13 | ? NULL : p); 14 | } 15 | -------------------------------------------------------------------------------- /STDLIB/XGETMEM.C: -------------------------------------------------------------------------------- 1 | /* _Getmem function -- Turbo C++ version */ 2 | #include "xalloc.h" 3 | 4 | /* Turbo C++ system call */ 5 | void *sbrk(int); 6 | 7 | void *_Getmem(size_t size) 8 | { /* allocate raw storage */ 9 | void *p; 10 | int isize = size; 11 | 12 | return (isize <= 0 || (p = sbrk(isize)) == (void *)-1 13 | ? NULL : p); 14 | } 15 | -------------------------------------------------------------------------------- /STDIO/FREOPEN.C: -------------------------------------------------------------------------------- 1 | /* freopen function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | FILE *(freopen)(const char *name, const char *mods, FILE *str) 6 | { /* reopen a file */ 7 | unsigned short mode = str->_Mode & _MALFIL; 8 | 9 | str->_Mode &= ~_MALFIL; 10 | fclose(str); 11 | str->_Mode = mode; 12 | return (_Foprep(name, mods, str)); 13 | } 14 | -------------------------------------------------------------------------------- /STRING/STRNCPY.C: -------------------------------------------------------------------------------- 1 | /* strncpy function */ 2 | #include 3 | 4 | char *(strncpy)(char *s1, const char *s2, size_t n) 5 | { /* copy char s2[max n] to s1[n] */ 6 | char *s; 7 | 8 | for (s = s1; 0 < n && *s2 != '\0'; --n) 9 | *s++ = *s2++; /* copy at most n chars from s2[] */ 10 | for (; 0 < n; --n) 11 | *s++ = '\0'; 12 | return (s1); 13 | } 14 | -------------------------------------------------------------------------------- /STDLIB/ATEXIT.C: -------------------------------------------------------------------------------- 1 | /* atexit function */ 2 | #include 3 | 4 | /* external declarations */ 5 | extern void (*_Atfuns[])(void); 6 | extern size_t _Atcount; 7 | 8 | int (atexit)(void (*func)(void)) 9 | { /* function to call at exit */ 10 | if (_Atcount == 0) 11 | return (-1); /* list is full */ 12 | _Atfuns[--_Atcount] = func; 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /STRING/MEMCHR.C: -------------------------------------------------------------------------------- 1 | /* memchr function */ 2 | #include 3 | 4 | void *(memchr)(const void *s, int c, size_t n) 5 | { /* find first occurrence of c in s[n] */ 6 | const unsigned char uc = c; 7 | const unsigned char *su = (const unsigned char *)s; 8 | 9 | for (; 0 < n; ++su, --n) 10 | if (*su == uc) 11 | return ((void *)su); 12 | return (NULL); 13 | } 14 | -------------------------------------------------------------------------------- /MATH/MODF.C: -------------------------------------------------------------------------------- 1 | /* modf function */ 2 | #include "xmath.h" 3 | 4 | double (modf)(double x, double *pint) 5 | { /* compute modf(x, &intpart) */ 6 | *pint = x; 7 | switch (_Dint(pint, 0)) 8 | { /* test for special codes */ 9 | case NAN: 10 | return (x); 11 | case INF: 12 | case 0: 13 | return (0.0); 14 | default: /* finite */ 15 | return (x - *pint); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /STDIO/VPRINTF.C: -------------------------------------------------------------------------------- 1 | /* vprintf function */ 2 | #include "xstdio.h" 3 | 4 | static void *prout(void *str, const char *buf, size_t n) 5 | { /* write to file */ 6 | return (fwrite(buf, 1, n, (FILE *)str) == n ? str : NULL); 7 | } 8 | 9 | int (vprintf)(const char *fmt, char *ap) 10 | { /* print formatted to stdout from arg list */ 11 | return (_Printf(&prout, stdout, fmt, ap)); 12 | } 13 | -------------------------------------------------------------------------------- /STRING/STRRCHR.C: -------------------------------------------------------------------------------- 1 | /* strrchr function */ 2 | #include 3 | 4 | char *(strrchr)(const char *s, int c) 5 | { /* find last occurrence of c in char s[] */ 6 | const char ch = c; 7 | const char *sc; 8 | 9 | for (sc = NULL; ; ++s) 10 | { /* check another char */ 11 | if (*s == ch) 12 | sc = s; 13 | if (*s == '\0') 14 | return ((char *)sc); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /_HEADERS/ERRNO.H: -------------------------------------------------------------------------------- 1 | /* errno.h standard header */ 2 | #ifndef _ERRNO 3 | #define _ERRNO 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* error codes */ 8 | #define EDOM _EDOM 9 | #define ERANGE _ERANGE 10 | #define EFPOS _EFPOS 11 | /* ADD YOURS HERE */ 12 | #define _NERR _ERRMAX /* one more than last code */ 13 | /* declarations */ 14 | extern int errno; 15 | #endif 16 | -------------------------------------------------------------------------------- /STDIO/VFPRINTF.C: -------------------------------------------------------------------------------- 1 | /* vfprintf function */ 2 | #include "xstdio.h" 3 | 4 | static void *prout(void *str, const char *buf, size_t n) 5 | { /* write to file */ 6 | return (fwrite(buf, 1, n, (FILE *)str) == n ? str : NULL); 7 | } 8 | 9 | int (vfprintf)(FILE *str, const char *fmt, char *ap) 10 | { /* print formatted to stream from arg list */ 11 | return (_Printf(&prout, str, fmt, ap)); 12 | } 13 | -------------------------------------------------------------------------------- /STRING/STRNCAT.C: -------------------------------------------------------------------------------- 1 | /* strncat function */ 2 | #include 3 | 4 | char *(strncat)(char *s1, const char *s2, size_t n) 5 | { /* copy char s2[max n] to end of s1[] */ 6 | char *s; 7 | 8 | for (s = s1; *s != '\0'; ++s) 9 | ; /* find end of s1[] */ 10 | for (; 0 < n && *s2 != '\0'; --n) 11 | *s++ = *s2++; /* copy at most n chars from s2[] */ 12 | *s = '\0'; 13 | return (s1); 14 | } 15 |  -------------------------------------------------------------------------------- /STRING/STRNCMP.C: -------------------------------------------------------------------------------- 1 | /* strncmp function */ 2 | #include 3 | 4 | int (strncmp)(const char *s1, const char *s2, size_t n) 5 | { /* compare unsigned char s1[max n], s2[max n] */ 6 | for (; 0 < n; ++s1, ++s2, --n) 7 | if (*s1 != *s2) 8 | return (*(unsigned char *)s1 9 | < *(unsigned char *)s2 ? -1 : +1); 10 | else if (*s1 == '\0') 11 | return (0); 12 | return (0); 13 | } 14 |  -------------------------------------------------------------------------------- /STDLIB/DIV.C: -------------------------------------------------------------------------------- 1 | /* div function */ 2 | #include 3 | 4 | div_t (div)(int numer, int denom) 5 | { /* compute int quotient and remainder */ 6 | div_t val; 7 | 8 | val.quot = numer / denom; 9 | val.rem = numer - denom * val.quot; 10 | if (val.quot < 0 && 0 < val.rem) 11 | { /* fix remainder with wrong sign */ 12 | val.quot += 1; 13 | val.rem -= denom; 14 | } 15 | return (val); 16 | } 17 | -------------------------------------------------------------------------------- /_HEADERS/ASSERT.H: -------------------------------------------------------------------------------- 1 | /* assert.h standard header */ 2 | #undef assert /* remove existing definition */ 3 | 4 | #ifdef NDEBUG 5 | #define assert(test) ((void)0) 6 | #else /* NDEBUG not defined */ 7 | void _Assert(char *); 8 | /* macros */ 9 | #define _STR(x) _VAL(x) 10 | #define _VAL(x) #x 11 | #define assert(test) ((test) ? (void)0 \ 12 | : _Assert(__FILE__ ":" _STR(__LINE__) " " #test)) 13 | #endif 14 | -------------------------------------------------------------------------------- /MATH/FABS.C: -------------------------------------------------------------------------------- 1 | /* fabs function */ 2 | #include "xmath.h" 3 | 4 | double (fabs)(double x) 5 | { /* compute fabs */ 6 | switch (_Dtest(&x)) 7 | { /* test for special codes */ 8 | case NAN: 9 | errno = EDOM; 10 | return (x); 11 | case INF: 12 | errno = ERANGE; 13 | return (_Inf._D); 14 | case 0: 15 | return (0.0); 16 | default: /* finite */ 17 | return (x < 0.0 ? -x : x); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /STRING/STRCSPN.C: -------------------------------------------------------------------------------- 1 | /* strcspn function */ 2 | #include 3 | 4 | size_t (strcspn)(const char *s1, const char *s2) 5 | { /* find index of first s1[i] that matches any s2[] */ 6 | const char *sc1, *sc2; 7 | 8 | for (sc1 = s1; *sc1 != '\0'; ++sc1) 9 | for (sc2 = s2; *sc2 != '\0'; ++sc2) 10 | if (*sc1 == *sc2) 11 | return (sc1 - s1); 12 | return (sc1 - s1); /* terminating nulls match */ 13 | } 14 | -------------------------------------------------------------------------------- /STRING/STRPBRK.C: -------------------------------------------------------------------------------- 1 | /* strpbrk function */ 2 | #include 3 | 4 | char *(strpbrk)(const char *s1, const char *s2) 5 | { /* find index of first s1[i] that matches any s2[] */ 6 | const char *sc1, *sc2; 7 | 8 | for (sc1 = s1; *sc1 != '\0'; ++sc1) 9 | for (sc2 = s2; *sc2 != '\0'; ++sc2) 10 | if (*sc1 == *sc2) 11 | return ((char *)sc1); 12 | return (NULL); /* terminating nulls match */ 13 | } 14 | -------------------------------------------------------------------------------- /STDLIB/LDIV.C: -------------------------------------------------------------------------------- 1 | /* ldiv function */ 2 | #include 3 | 4 | ldiv_t (ldiv)(long numer, long denom) 5 | { /* compute long quotient and remainder */ 6 | ldiv_t val; 7 | 8 | val.quot = numer / denom; 9 | val.rem = numer - denom * val.quot; 10 | if (val.quot < 0 && 0 < val.rem) 11 | { /* fix remainder with wrong sign */ 12 | val.quot += 1; 13 | val.rem -= denom; 14 | } 15 | return (val); 16 | } 17 | -------------------------------------------------------------------------------- /STRING/MEMCMP.C: -------------------------------------------------------------------------------- 1 | /* memcmp function */ 2 | #include 3 | 4 | int (memcmp)(const void *s1, const void *s2, size_t n) 5 | { /* compare unsigned char s1[n], s2[n] */ 6 | const unsigned char *su1 = (const unsigned char *)s1; 7 | const unsigned char *su2 = (const unsigned char *)s2; 8 | 9 | for (; 0 < n; ++su1, ++su2, --n) 10 | if (*su1 != *su2) 11 | return (*su1 < *su2 ? -1 : +1); 12 | return (0); 13 | } 14 |  -------------------------------------------------------------------------------- /STDIO/PERROR.C: -------------------------------------------------------------------------------- 1 | /* perror function */ 2 | #include 3 | #include 4 | #include "xstdio.h" 5 | 6 | void (perror)(const char *s) 7 | { /* put error string to stderr */ 8 | static char buf[] = {"error #xxx"}; 9 | 10 | if (s) 11 | { /* put user-supplied prefix */ 12 | fputs(s, stderr); 13 | fputs(": ", stderr); 14 | } 15 | fputs(_Strerror(errno, buf), stderr); 16 | fputc('\n', stderr); 17 | } 18 | -------------------------------------------------------------------------------- /STRING/STRSPN.C: -------------------------------------------------------------------------------- 1 | /* strspn function */ 2 | #include 3 | 4 | size_t (strspn)(const char *s1, const char *s2) 5 | { /* find index of first s1[i] that matches no s2[] */ 6 | const char *sc1, *sc2; 7 | 8 | for (sc1 = s1; *sc1 != '\0'; ++sc1) 9 | for (sc2 = s2; ; ++sc2) 10 | if (*sc2 == '\0') 11 | return (sc1 - s1); 12 | else if (*sc1 == *sc2) 13 | break; 14 | return (sc1 - s1); /* null doesn't match */ 15 | } 16 | -------------------------------------------------------------------------------- /LOCALE/XDEFLOC.C: -------------------------------------------------------------------------------- 1 | /* _Defloc function */ 2 | #include 3 | #include "xlocale.h" 4 | 5 | const char *_Defloc(void) 6 | { /* find name of default locale */ 7 | char *s; 8 | static char *defname = NULL; 9 | 10 | if (defname) 11 | ; 12 | else if ((s = getenv("LOCALE")) != NULL 13 | && (defname = (char *)malloc(strlen(s) + 1)) != NULL) 14 | strcpy(defname, s); 15 | else 16 | defname = "C"; 17 | return (defname); 18 | } 19 |  -------------------------------------------------------------------------------- /STDIO/FGETC.C: -------------------------------------------------------------------------------- 1 | /* fgetc function */ 2 | #include "xstdio.h" 3 | 4 | int (fgetc)(FILE *str) 5 | { /* get a character from stream */ 6 | if (0 < str->_Nback) 7 | { /* deliver pushed back char */ 8 | if (--str->_Nback == 0) 9 | str->_Rend = str->_Rsave; 10 | return (str->_Back[str->_Nback]); 11 | } 12 | if (str->_Next < str->_Rend) 13 | ; 14 | else if (_Frprep(str) <= 0) 15 | return (EOF); 16 | return (*str->_Next++); 17 | } 18 | -------------------------------------------------------------------------------- /MATH/LDEXP.C: -------------------------------------------------------------------------------- 1 | /* ldexp function */ 2 | #include "xmath.h" 3 | 4 | double (ldexp)(double x, int xexp) 5 | { /* compute ldexp(x, xexp) */ 6 | switch (_Dtest(&x)) 7 | { /* test for special codes */ 8 | case NAN: 9 | errno = EDOM; 10 | break; 11 | case INF: 12 | errno = ERANGE; 13 | break; 14 | case 0: 15 | break; 16 | default: /* finite */ 17 | if (0 <= _Dscale(&x, xexp)) 18 | errno = ERANGE; 19 | } 20 | return (x); 21 | } 22 | -------------------------------------------------------------------------------- /MATH/EXP.C: -------------------------------------------------------------------------------- 1 | /* exp function */ 2 | #include "xmath.h" 3 | 4 | double (exp)(double x) 5 | { /* compute exp(x) */ 6 | switch (_Dtest(&x)) 7 | { /* test for special codes */ 8 | case NAN: 9 | errno = EDOM; 10 | return (x); 11 | case INF: 12 | errno = ERANGE; 13 | return (DSIGN(x) ? 0.0 : _Inf._D); 14 | case 0: 15 | return (1.0); 16 | default: /* finite */ 17 | if (0 <= _Exp(&x, 0)) 18 | errno = ERANGE; 19 | return (x); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /STDIO/PRINTF.C: -------------------------------------------------------------------------------- 1 | /* printf function */ 2 | #include "xstdio.h" 3 | 4 | static void *prout(void *str, const char *buf, size_t n) 5 | { /* write to file */ 6 | return (fwrite(buf, 1, n, (FILE *)str) == n ? str : NULL); 7 | } 8 | 9 | int (printf)(const char *fmt, ...) 10 | { /* print formatted to stdout */ 11 | int ans; 12 | va_list ap; 13 | 14 | va_start(ap, fmt); 15 | ans = _Printf(&prout, stdout, fmt, ap); 16 | va_end(ap); 17 | return (ans); 18 | } 19 | -------------------------------------------------------------------------------- /MATH/FREXP.C: -------------------------------------------------------------------------------- 1 | /* frexp function */ 2 | #include "xmath.h" 3 | 4 | double (frexp)(double x, int *pexp) 5 | { /* compute frexp(x, &i) */ 6 | short binexp; 7 | 8 | switch (_Dunscale(&binexp, &x)) 9 | { /* test for special codes */ 10 | case NAN: 11 | case INF: 12 | errno = EDOM; 13 | *pexp = 0; 14 | return (x); 15 | case 0: 16 | *pexp = 0; 17 | return (0.0); 18 | default: /* finite */ 19 | *pexp = binexp; 20 | return (x); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /STDIO/TMPFILE.C: -------------------------------------------------------------------------------- 1 | /* tmpfile function */ 2 | #include 3 | #include 4 | #include "xstdio.h" 5 | 6 | FILE *(tmpfile)(void) 7 | { /* open a temporary file */ 8 | FILE *str; 9 | char fn[L_tmpnam], *s; 10 | 11 | if ((str = fopen((const char *)tmpnam(fn), "wb+")) == NULL) 12 | ; 13 | else if ((s = (char *)malloc(sizeof (fn) + 1)) == NULL) 14 | fclose(str), str = NULL; 15 | else 16 | str->_Tmpnam = strcpy(s, fn); 17 | return (str); 18 | } 19 | -------------------------------------------------------------------------------- /STDIO/VSPRINTF.C: -------------------------------------------------------------------------------- 1 | /* vsprintf function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | static void *prout(void *s, const char *buf, size_t n) 6 | { /* write to string */ 7 | return ((char *)memcpy(s, buf, n) + n); 8 | } 9 | 10 | int (vsprintf)(char *s, const char *fmt, char *ap) 11 | { /* print formatted to string from arg list */ 12 | int ans = _Printf(&prout, s, fmt, ap); 13 | 14 | if (0 <= ans) 15 | s[ans] = '\0'; 16 | return (ans); 17 | } 18 | -------------------------------------------------------------------------------- /STDIO/FPRINTF.C: -------------------------------------------------------------------------------- 1 | /* fprintf function */ 2 | #include "xstdio.h" 3 | 4 | static void *prout(void *str, const char *buf, size_t n) 5 | { /* write to file */ 6 | return (fwrite(buf, 1, n, (FILE *)str) == n ? str : NULL); 7 | } 8 | 9 | int (fprintf)(FILE *str, const char *fmt, ...) 10 | { /* print formatted to stream */ 11 | int ans; 12 | va_list ap; 13 | 14 | va_start(ap, fmt); 15 | ans = _Printf(&prout, str, fmt, ap); 16 | va_end(ap); 17 | return (ans); 18 | } 19 | -------------------------------------------------------------------------------- /STRING/MEMMOVE.C: -------------------------------------------------------------------------------- 1 | /* memmove function */ 2 | #include 3 | 4 | void *(memmove)(void *s1, const void *s2, size_t n) 5 | { /* copy char s2[n] to s1[n] safely */ 6 | char *sc1 = (char *)s1; 7 | const char *sc2 = (const char *)s2; 8 | 9 | if (sc2 < sc1 && sc1 < sc2 + n) 10 | for (sc1 += n, sc2 += n; 0 < n; --n) 11 | *--sc1 = *--sc2; /*copy backwards */ 12 | else 13 | for (; 0 < n; --n) 14 | *sc1++ = *sc2++; /* copy forwards */ 15 | return (s1); 16 | } 17 | -------------------------------------------------------------------------------- /STDLIB/GETENV.C: -------------------------------------------------------------------------------- 1 | /* getenv function -- in-memory version */ 2 | #include 3 | #include 4 | #include "yfuns.h" 5 | 6 | char *(getenv)(const char *name) 7 | { /* search environment list for named entry */ 8 | const char *s; 9 | size_t n = strlen(name); 10 | 11 | for (s = _Envp; *s; s += strlen(s) + 1) 12 | { /* look for name match */ 13 | if (!strncmp(s, name, n) && s[n] == '=') 14 | return ((char *)&s[n + 1]); 15 | } 16 | return (NULL); 17 | } 18 | -------------------------------------------------------------------------------- /_HEADERS/STDDEF.H: -------------------------------------------------------------------------------- 1 | /* stddef.h standard header */ 2 | #ifndef _STDDEF 3 | #define _STDDEF 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* macros */ 8 | #define NULL _NULL 9 | #define offsetof(T, member) ((_Sizet)&((T *)0)->member) 10 | /* type definitions */ 11 | #ifndef _SIZET 12 | #define _SIZET 13 | typedef _Sizet size_t; 14 | #endif 15 | #ifndef _WCHART 16 | #define _WCHART 17 | typedef _Wchart wchar_t; 18 | #endif 19 | typedef _Ptrdifft ptrdiff_t; 20 | #endif 21 | -------------------------------------------------------------------------------- /SIGNAL/SIGNAL.C: -------------------------------------------------------------------------------- 1 | /* signal function -- simple version */ 2 | #include 3 | 4 | /* external declarations */ 5 | extern _Sigfun *_Sigtable[_NSIG]; 6 | 7 | _Sigfun *(signal)(int sig, _Sigfun *fun) 8 | { /* specify handling for a signal */ 9 | _Sigfun *s; 10 | 11 | if (sig <= 0 || _NSIG <= sig || fun == SIG_ERR) 12 | return (SIG_ERR); /* bad signal */ 13 | /* add machine-dependent handling here */ 14 | s = _Sigtable[sig], _Sigtable[sig] = fun; 15 | return (s); 16 | } 17 | -------------------------------------------------------------------------------- /_HEADERS/STDARG.H: -------------------------------------------------------------------------------- 1 | /* stdarg.h standard header */ 2 | #ifndef _STDARG 3 | #define _STDARG 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* type definitions */ 8 | typedef char *va_list; 9 | /* macros */ 10 | #define va_arg(ap, T) \ 11 | (*(T *)(((ap) += _Bnd(T, _AUPBND)) - _Bnd(T, _ADNBND))) 12 | #define va_end(ap) (void)0 13 | #define va_start(ap, A) \ 14 | (void)((ap) = (char *)&(A) + _Bnd(A, _AUPBND)) 15 | #define _Bnd(X, bnd) (sizeof (X) + (bnd) & ~(bnd)) 16 | #endif 17 | -------------------------------------------------------------------------------- /_HEADERS/XSTATE.H: -------------------------------------------------------------------------------- 1 | /* xstate.h internal header */ 2 | /* macros for finite state machines */ 3 | #define ST_CH 0x00ff 4 | #define ST_STATE 0x0f00 5 | #define ST_STOFF 8 6 | #define ST_FOLD 0x8000 7 | #define ST_INPUT 0x4000 8 | #define ST_OUTPUT 0x2000 9 | #define ST_ROTATE 0x1000 10 | #define _NSTATE 16 11 | /* type definitions */ 12 | typedef struct { 13 | const unsigned short *_Tab[_NSTATE]; 14 | } _Statab; 15 | /* declarations */ 16 | extern _Statab _Costate, _Mbstate, _Wcstate; 17 | -------------------------------------------------------------------------------- /MATH/XVALUES.C: -------------------------------------------------------------------------------- 1 | /* values used by math functions -- IEEE 754 version */ 2 | #include "xmath.h" 3 | 4 | /* macros */ 5 | #define NBITS (48+_DOFF) 6 | #if _D0 7 | #define INIT(w0) 0, 0, 0, w0 8 | #else 9 | #define INIT(w0) w0, 0, 0, 0 10 | #endif 11 | /* static data */ 12 | _Dconst _Hugeval = {INIT(_DMAX<<_DOFF)}; 13 | _Dconst _Inf = {INIT(_DMAX<<_DOFF)}; 14 | _Dconst _Nan = {INIT(_DNAN)}; 15 | _Dconst _Rteps = {INIT((_DBIAS-NBITS/2)<<_DOFF)}; 16 | _Dconst _Xbig = {INIT((_DBIAS+NBITS/2)<<_DOFF)}; 17 | -------------------------------------------------------------------------------- /STDIO/FPUTC.C: -------------------------------------------------------------------------------- 1 | /* fputc function */ 2 | #include "xstdio.h" 3 | 4 | int (fputc)(int ci, FILE *str) 5 | { /* put a character to stream */ 6 | unsigned char c = ci; 7 | 8 | if (str->_Next < str->_Wend) 9 | ; 10 | else if (_Fwprep(str) < 0) 11 | return (EOF); 12 | *str->_Next++ = c; 13 | if (str->_Mode & (_MLBF|_MNBF)) 14 | { /* disable macros and drain */ 15 | str->_Wend = str->_Buf; 16 | if ((str->_Mode & _MNBF || c == '\n') && fflush(str)) 17 | return (EOF); 18 | } 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /STRING/STRSTR.C: -------------------------------------------------------------------------------- 1 | /* strstr function */ 2 | #include 3 | 4 | char *(strstr)(const char *s1, const char *s2) 5 | { /* find first occurrence of s2[] in s1[] */ 6 | if (*s2 == '\0') 7 | return ((char *)s1); 8 | for (; (s1 = strchr(s1, *s2)) != NULL; ++s1) 9 | { /* match rest of prefix */ 10 | const char *sc1, *sc2; 11 | 12 | for (sc1 = s1, sc2 = s2; ; ) 13 | if (*++sc2 == '\0') 14 | return ((char *)s1); 15 | else if (*++sc1 != *sc2) 16 | break; 17 | } 18 | return (NULL); 19 | } 20 | -------------------------------------------------------------------------------- /STDIO/SPRINTF.C: -------------------------------------------------------------------------------- 1 | /* sprintf function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | static void *prout(void *s, const char *buf, size_t n) 6 | { /* write to string */ 7 | return ((char *)memcpy(s, buf, n) + n); 8 | } 9 | 10 | int (sprintf)(char *s, const char *fmt, ...) 11 | { /* print formatted to string */ 12 | int ans; 13 | va_list ap; 14 | 15 | va_start(ap, fmt); 16 | ans = _Printf(&prout, s, fmt, ap); 17 | if (0 <= ans) 18 | s[ans] = '\0'; 19 | va_end(ap); 20 | return (ans); 21 | } 22 | -------------------------------------------------------------------------------- /MATH/XDTEST.C: -------------------------------------------------------------------------------- 1 | /* _Dtest function -- IEEE 754 version */ 2 | #include "xmath.h" 3 | 4 | short _Dtest(double *px) 5 | { /* categorize *px */ 6 | unsigned short *ps = (unsigned short *)px; 7 | short xchar = (ps[_D0] & _DMASK) >> _DOFF; 8 | 9 | if (xchar == _DMAX) /* NaN or INF */ 10 | return (ps[_D0] & _DFRAC || ps[_D1] 11 | || ps[_D2] || ps[_D3] ? NAN : INF); 12 | else if (0 < xchar || ps[_D0] & _DFRAC 13 | || ps[_D1] || ps[_D2] || ps[_D3]) 14 | return (FINITE); /* finite */ 15 | else 16 | return (0); /* zero */ 17 | } 18 | -------------------------------------------------------------------------------- /STDIO/SCANF.C: -------------------------------------------------------------------------------- 1 | /* scanf function */ 2 | #include "xstdio.h" 3 | 4 | static int scin(void *str, int ch) 5 | { /* get or put a character */ 6 | if (ch == _WANT) 7 | return (fgetc((FILE *)str)); 8 | else if (0 <= ch) 9 | return (ungetc(ch, (FILE *)str)); 10 | else 11 | return (ch); 12 | } 13 | 14 | int (scanf)(const char *fmt, ...) 15 | { /* read formatted from stdin */ 16 | int ans; 17 | va_list ap; 18 | 19 | va_start(ap, fmt); 20 | ans = _Scanf(&scin, stdin, fmt, ap); 21 | va_end(ap); 22 | return (ans); 23 | } 24 | -------------------------------------------------------------------------------- /STDIO/UNGETC.C: -------------------------------------------------------------------------------- 1 | /* ungetc function */ 2 | #include "xstdio.h" 3 | 4 | int (ungetc)(int c, FILE *str) 5 | { /* push character back on stream */ 6 | if (c == EOF 7 | || sizeof (str->_Back) <= str->_Nback 8 | || (str->_Mode & (_MOPENR|_MWRITE)) != _MOPENR) 9 | return (EOF); 10 | str->_Mode = str->_Mode & ~_MEOF | _MREAD; 11 | if (str->_Nback == 0) 12 | { /* disable buffering */ 13 | str->_Rsave = str->_Rend; 14 | str->_Rend = str->_Buf; 15 | } 16 | str->_Back[str->_Nback++] = c; 17 | return ((unsigned char)c); 18 | } 19 | -------------------------------------------------------------------------------- /_DUMMY/TMPNAM.C: -------------------------------------------------------------------------------- 1 | /* tmpnam function -- dummy version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | char *(tmpnam)(char *s) 6 | { /* create a temporary file name */ 7 | int i; 8 | char *p; 9 | unsigned short t; 10 | static char buf[L_tmpnam]; 11 | static unsigned short seed = 0; 12 | 13 | if (s == NULL) 14 | s = buf; 15 | ++seed; 16 | strcpy(s, "ctm"); 17 | i = 5; 18 | p = s + strlen(s) + i; 19 | strcpy(p, ".tmp"); 20 | for (t = seed; 0 <= --i; t >>= 3) 21 | *--p = '0' + (t & 07); 22 | return (s); 23 | } 24 |  -------------------------------------------------------------------------------- /_TEST/TERRNO.C: -------------------------------------------------------------------------------- 1 | /* test errno macro */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { /* test basic workings of errno */ 9 | assert(errno == 0); 10 | perror("No error reported as"); 11 | errno = ERANGE; 12 | assert(errno == ERANGE); 13 | perror("Range error reported as"); 14 | errno = 0; 15 | assert(errno == 0); 16 | sqrt(-1.0); 17 | assert(errno == EDOM); 18 | perror("Domain error reported as"); 19 | puts("SUCCESS testing "); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /_UNIXVAX/YFUNS.H: -------------------------------------------------------------------------------- 1 | /* yfuns.h functions header -- UNIX version */ 2 | #ifndef _YFUNS 3 | #define _YFUNS 4 | /* macros */ 5 | #define _Envp (*_Environ) 6 | #define _Fclose(str) _Close((str)->_Handle) 7 | #define _Fread(str, buf, cnt) _Read((str)->_Handle, buf, cnt) 8 | #define _Fwrite(str, buf, cnt) _Write((str)->_Handle, buf, cnt) 9 | /* interface declarations */ 10 | extern const char **_Environ; 11 | int _Close(int); 12 | void _Exit(int); 13 | int _Read(int, unsigned char *, int); 14 | int _Write(int, const unsigned char *, int); 15 | #endif 16 | -------------------------------------------------------------------------------- /STDIO/FSCANF.C: -------------------------------------------------------------------------------- 1 | /* fscanf function */ 2 | #include "xstdio.h" 3 | 4 | static int scin(void *str, int ch) 5 | { /* get or put a character */ 6 | if (ch == _WANT) 7 | return (fgetc((FILE *)str)); 8 | else if (0 <= ch) 9 | return (ungetc(ch, (FILE *)str)); 10 | else 11 | return (ch); 12 | } 13 | 14 | int (fscanf)(FILE *str, const char *fmt, ...) 15 | { /* read formatted from stream */ 16 | int ans; 17 | va_list ap; 18 | 19 | va_start(ap, fmt); 20 | ans = _Scanf(&scin, str, fmt, ap); 21 | va_end(ap); 22 | return (ans); 23 | } 24 | -------------------------------------------------------------------------------- /STDLIB/MBSTOWCS.C: -------------------------------------------------------------------------------- 1 | /* mbstowcs function */ 2 | #include 3 | 4 | size_t (mbstowcs)(wchar_t *wcs, const char *s, size_t n) 5 | { /* translate multibyte string to wide char string */ 6 | int i; 7 | wchar_t *pwc; 8 | _Mbsave state = {0}; 9 | 10 | for (pwc = wcs; 0 < n; ++pwc, --n) 11 | { /* make another wide character */ 12 | i = _Mbtowc(pwc, s, MB_CUR_MAX, &state); 13 | if (i == -1) 14 | return (-1); 15 | else if (i == 0 || *pwc == 0) 16 | return (pwc - wcs); 17 | s += i; 18 | } 19 | return (pwc - wcs); 20 | } 21 |  -------------------------------------------------------------------------------- /MATH/COSH.C: -------------------------------------------------------------------------------- 1 | /* cosh function */ 2 | #include "xmath.h" 3 | 4 | double (cosh)(double x) 5 | { /* compute cosh(x) */ 6 | switch (_Dtest(&x)) 7 | { /* test for special codes */ 8 | case NAN: 9 | errno = EDOM; 10 | return (x); 11 | case INF: 12 | errno = ERANGE; 13 | return (_Inf._D); 14 | case 0: 15 | return (1.0); 16 | default: /* finite */ 17 | if (x < 0.0) 18 | x = -x; 19 | if (0 <= _Exp(&x, -1)) 20 | errno = ERANGE; /* x large */ 21 | else if (x < _Xbig._D) 22 | x += 0.25 / x; 23 | return (x); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /STDLIB/EXIT.C: -------------------------------------------------------------------------------- 1 | /* exit function */ 2 | #include 3 | #include 4 | #include "yfuns.h" 5 | 6 | /* macros */ 7 | #define NATS 32 8 | /* static data */ 9 | void (*_Atfuns[NATS])(void) = {0}; 10 | size_t _Atcount = {NATS}; 11 | 12 | void (exit)(int status) 13 | { /* tidy up and exit to system */ 14 | while (_Atcount < NATS) 15 | (*_Atfuns[_Atcount++])(); 16 | { /* close all files */ 17 | size_t i; 18 | 19 | for (i = 0; i < FOPEN_MAX; ++i) 20 | if (_Files[i]) 21 | fclose(_Files[i]); 22 | } 23 | _Exit(status); 24 | } 25 | -------------------------------------------------------------------------------- /STRING/STRTOK.C: -------------------------------------------------------------------------------- 1 | /* strtok function */ 2 | #include 3 | 4 | char *(strtok)(char *s1, const char *s2) 5 | { /* find next token in s1[] delimited by s2[] */ 6 | char *sbegin, *send; 7 | static char *ssave = ""; /* for safety */ 8 | 9 | sbegin = s1 ? s1 : ssave; 10 | sbegin += strspn(sbegin, s2); 11 | if (*sbegin == '\0') 12 | { /* end of scan */ 13 | ssave = ""; /* for safety */ 14 | return (NULL); 15 | } 16 | send = sbegin + strcspn(sbegin, s2); 17 | if (*send != '\0') 18 | *send++ = '\0'; 19 | ssave = send; 20 | return (sbegin); 21 | } 22 |  -------------------------------------------------------------------------------- /_UNIX68K/XFOPEN.C: -------------------------------------------------------------------------------- 1 | /* _Fopen function -- UNIX version */ 2 | #include "xstdio.h" 3 | 4 | /* UNIX system call */ 5 | int open(const char *, int, int); 6 | 7 | int _Fopen(const char *path, unsigned int smode, 8 | const char *mods) 9 | { /* open from a file */ 10 | unsigned int acc; 11 | 12 | acc = (smode & (_MOPENR|_MOPENW)) == (_MOPENR|_MOPENW) ? 2 13 | : smode & _MOPENW ? 1 : 0; 14 | if (smode & _MOPENA) 15 | acc |= 010; /* O_APPEND */ 16 | if (smode & _MTRUNC) 17 | acc |= 02000; /* O_TRUNC */ 18 | if (smode & _MCREAT) 19 | acc |= 01000; /* O_CREAT */ 20 | return (open(path, acc, 0666)); 21 | } 22 | -------------------------------------------------------------------------------- /_UNIX68K/YFUNS.H: -------------------------------------------------------------------------------- 1 | /* yfuns.h functions header -- UNIX version */ 2 | #ifndef _YFUNS 3 | #define _YFUNS 4 | /* process control functions */ 5 | #define _Envp (*environ) 6 | #define _Exit(status) _exit(status) 7 | /* stdio functions */ 8 | #define _Fclose(str) close((str)->_Handle) 9 | #define _Fread(str, buf, cnt) read((str)->_Handle, buf, cnt) 10 | #define _Fwrite(str, buf, cnt) write((str)->_Handle, buf, cnt) 11 | /* interface declarations */ 12 | extern const char **environ; 13 | int close(int); 14 | void _exit(int); 15 | int read(int, unsigned char *, int); 16 | int write(int, const unsigned char *, int); 17 | #endif 18 | -------------------------------------------------------------------------------- /_UNIXVAX/XFOPEN.C: -------------------------------------------------------------------------------- 1 | /* _Fopen function -- UNIX version */ 2 | #include "xstdio.h" 3 | 4 | /* UNIX system call */ 5 | int _Open(const char *, int, int); 6 | 7 | int _Fopen(const char *path, unsigned int smode, 8 | const char *mods) 9 | { /* open from a file */ 10 | unsigned int acc; 11 | 12 | acc = (smode & (_MOPENR|_MOPENW)) == (_MOPENR|_MOPENW) ? 2 13 | : smode & _MOPENW ? 1 : 0; 14 | if (smode & _MOPENA) 15 | acc |= 010; /* O_APPEND */ 16 | if (smode & _MTRUNC) 17 | acc |= 02000; /* O_TRUNC */ 18 | if (smode & _MCREAT) 19 | acc |= 01000; /* O_CREAT */ 20 | return (_Open(path, acc, 0666)); 21 | } 22 | -------------------------------------------------------------------------------- /MATH/ATAN.C: -------------------------------------------------------------------------------- 1 | /* atan function */ 2 | #include "xmath.h" 3 | 4 | double (atan)(double x) 5 | { /* compute atan(x) */ 6 | unsigned short hex; 7 | static const double piby2 = 1.57079632679489661923; 8 | 9 | switch (_Dtest(&x)) 10 | { /* test for special codes */ 11 | case NAN: 12 | errno = EDOM; 13 | return (x); 14 | case INF: 15 | return (DSIGN(x) ? -piby2 : piby2); 16 | case 0: 17 | return (0.0); 18 | default: /* finite */ 19 | if (x < 0.0) 20 | x = -x, hex = 0x8; 21 | else 22 | hex = 0x0; 23 | if (1.0 < x) 24 | x = 1.0 / x, hex ^= 0x2; 25 | return (_Atan(x, hex)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /_TC86/YFUNS.H: -------------------------------------------------------------------------------- 1 | /* yfuns.h functions header -- Turbo C++ version */ 2 | #ifndef _YFUNS 3 | #define _YFUNS 4 | /* process control functions */ 5 | #define _Envp (*_C0environ) 6 | #define _Exit(status) _exit(status) 7 | /* stdio functions */ 8 | #define _Fclose(str) _close((str)->_Handle) 9 | #define _Fread(str, buf, cnt) read((str)->_Handle, buf, cnt) 10 | #define _Fwrite(str, buf, cnt) write((str)->_Handle, buf, cnt) 11 | /* interface declarations */ 12 | extern const char **_C0environ; 13 | int _close(int); 14 | void _exit(int); 15 | int read(int, unsigned char *, int); 16 | int write(int, const unsigned char *, int); 17 | #endif 18 | -------------------------------------------------------------------------------- /_UNIX68K/TMPNAM.C: -------------------------------------------------------------------------------- 1 | /* tmpnam function -- UNIX version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* UNIX system call */ 6 | int getpid(void); 7 | 8 | char *(tmpnam)(char *s) 9 | { /* create a temporary file name */ 10 | int i; 11 | char *p; 12 | unsigned short t; 13 | static char buf[L_tmpnam]; 14 | static unsigned long seed = 0; 15 | 16 | if (s == NULL) 17 | s = buf; 18 | seed = seed == 0 ? getpid() << 6 : seed + 1; 19 | strcpy(s, "/tmp/t"); 20 | i = 7; 21 | p = s + strlen(s) + i; 22 | *p = '\0'; 23 | for (t = seed; 0 <= --i; t >>= 3) 24 | *--p = '0' + (t & 07); 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /_HEADERS/YFUNS.H: -------------------------------------------------------------------------------- 1 | /* yfuns.h functions header -- Turbo C++ version */ 2 | #ifndef _YFUNS 3 | #define _YFUNS 4 | /* process control functions */ 5 | #define _Envp (*_C0environ) 6 | #define _Exit(status) _exit(status) 7 | /* stdio functions */ 8 | #define _Fclose(str) _close((str)->_Handle) 9 | #define _Fread(str, buf, cnt) read((str)->_Handle, buf, cnt) 10 | #define _Fwrite(str, buf, cnt) write((str)->_Handle, buf, cnt) 11 | /* interface declarations */ 12 | extern const char **_C0environ; 13 | int _close(int); 14 | void _exit(int); 15 | int read(int, unsigned char *, int); 16 | int write(int, const unsigned char *, int); 17 | #endif 18 | -------------------------------------------------------------------------------- /_UNIXVAX/TMPNAM.C: -------------------------------------------------------------------------------- 1 | /* tmpnam function -- UNIX version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* UNIX system call */ 6 | int _Getpid(void); 7 | 8 | char *(tmpnam)(char *s) 9 | { /* create a temporary file name */ 10 | int i; 11 | char *p; 12 | unsigned short t; 13 | static char buf[L_tmpnam]; 14 | static unsigned long seed = 0; 15 | 16 | if (s == NULL) 17 | s = buf; 18 | seed = seed == 0 ? _Getpid() << 6 : seed + 1; 19 | strcpy(s, "/tmp/t"); 20 | i = 7; 21 | p = s + strlen(s) + i; 22 | *p = '\0'; 23 | for (t = seed; 0 <= --i; t >>= 3) 24 | *--p = '0' + (t & 07); 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /TIME/XGETTIME.C: -------------------------------------------------------------------------------- 1 | /* _Gettime function */ 2 | #include 3 | #include "xtime.h" 4 | 5 | const char *_Gettime(const char *s, int n, int *len) 6 | { /* get time info from environment */ 7 | const char delim = *s ? *s++ : '\0'; 8 | const char *s1; 9 | 10 | for (; ; --n, s = s1 + 1) 11 | { /* find end of current field */ 12 | if ((s1 = strchr(s, delim)) == NULL) 13 | s1 = s + strlen(s); 14 | if (n <= 0) 15 | { /* found proper field */ 16 | *len = s1 - s; 17 | return (s); 18 | } 19 | else if (*s1 == '\0') 20 | { /* not enough fields */ 21 | *len = 1; 22 | return (s1); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /_HEADERS/XALLOC.H: -------------------------------------------------------------------------------- 1 | /* xalloc.h internal header */ 2 | #include 3 | #include 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* macros */ 8 | #define CELL_OFF (sizeof (size_t) + _MEMBND & ~_MEMBND) 9 | #define SIZE_BLOCK 512 /* minimum block size */ 10 | #define SIZE_CELL \ 11 | ((sizeof (_Cell) + _MEMBND & ~_MEMBND) - CELL_OFF) 12 | /* type definitions */ 13 | typedef struct _Cell { 14 | size_t _Size; 15 | struct _Cell *_Next; 16 | } _Cell; 17 | typedef struct { 18 | _Cell **_Plast; 19 | _Cell *_Head; 20 | } _Altab; 21 | /* declarations */ 22 | void *_Getmem(size_t); 23 | extern _Altab _Aldata; 24 | -------------------------------------------------------------------------------- /STDIO/XFILES.C: -------------------------------------------------------------------------------- 1 | /* _Files data object */ 2 | #include "xstdio.h" 3 | 4 | /* standard error buffer */ 5 | static unsigned char ebuf[80]; 6 | 7 | /* the standard streams */ 8 | static FILE sin = { /* standard input */ 9 | _MOPENR, 0, 10 | NULL, NULL, &sin._Cbuf, 11 | &sin._Cbuf, NULL, &sin._Cbuf, }; 12 | static FILE sout = { /* standard output */ 13 | _MOPENW, 1, 14 | NULL, NULL, &sout._Cbuf, 15 | &sout._Cbuf, NULL, &sout._Cbuf, }; 16 | static FILE serr = { /* standard error */ 17 | _MOPENW|_MNBF, 2, 18 | ebuf, ebuf + sizeof (ebuf), ebuf, 19 | ebuf, NULL, ebuf, }; 20 | 21 | /* the array of stream pointers */ 22 | FILE *_Files[FOPEN_MAX] = {&sin, &sout, &serr}; 23 | -------------------------------------------------------------------------------- /_HEADERS/SIGNAL.H: -------------------------------------------------------------------------------- 1 | /* signal.h standard header */ 2 | #ifndef _SIGNAL 3 | #define _SIGNAL 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* type definitions */ 8 | typedef int sig_atomic_t; 9 | typedef void _Sigfun(int); 10 | /* signal codes */ 11 | #define SIGABRT _SIGABRT 12 | #define SIGINT 2 13 | #define SIGILL 4 14 | #define SIGFPE 8 15 | #define SIGSEGV 11 16 | #define SIGTERM 15 17 | #define _NSIG _SIGMAX /* one more than last code */ 18 | /* signal return values */ 19 | #define SIG_DFL (_Sigfun *)0 20 | #define SIG_ERR (_Sigfun *)-1 21 | #define SIG_IGN (_Sigfun *)1 22 | /* declarations */ 23 | int raise(int); 24 | _Sigfun *signal(int, _Sigfun *); 25 | #endif 26 | -------------------------------------------------------------------------------- /STDIO/SSCANF.C: -------------------------------------------------------------------------------- 1 | /* sscanf function */ 2 | #include "xstdio.h" 3 | 4 | static int scin(void *str, int ch) 5 | { /* get or put a character */ 6 | char *s = *(char **)str; 7 | 8 | if (ch == _WANT) 9 | if (*s == '\0') 10 | return (EOF); 11 | else 12 | { /* deliver a character */ 13 | *(char **)str = s + 1; 14 | return (*s); 15 | } 16 | else if (0 <= ch) 17 | *(char **)str = s - 1; 18 | return (ch); 19 | } 20 | 21 | int (sscanf)(const char *buf, const char *fmt, ...) 22 | { /* read formatted from string */ 23 | int ans; 24 | va_list ap; 25 | 26 | va_start(ap, fmt); 27 | ans = _Scanf(&scin, (void **)&buf, fmt, ap); 28 | va_end(ap); 29 | return (ans); 30 | } 31 | -------------------------------------------------------------------------------- /STDIO/FOPEN.C: -------------------------------------------------------------------------------- 1 | /* fopen function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | FILE *(fopen)(const char *name, const char *mods) 6 | { /* open a file */ 7 | FILE *str; 8 | size_t i; 9 | 10 | for (i = 0; i < FOPEN_MAX; ++i) 11 | if (_Files[i] == NULL) 12 | { /* setup empty _Files[i] */ 13 | str = (FILE *)malloc(sizeof (FILE)); 14 | if (str == NULL) 15 | return (NULL); 16 | _Files[i] = str; 17 | str->_Mode = _MALFIL; 18 | break; 19 | } 20 | else if (_Files[i]->_Mode == 0) 21 | { /* setup preallocated _Files[i] */ 22 | str = _Files[i]; 23 | break; 24 | } 25 | if (FOPEN_MAX <= i) 26 | return (NULL); 27 | return (_Foprep(name, mods, str)); 28 | } 29 | -------------------------------------------------------------------------------- /_UNIX68K/SYSTEM.C: -------------------------------------------------------------------------------- 1 | /* system function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system calls */ 5 | int execl(const char *, const char *, ...); 6 | int fork(void); 7 | int wait (int *); 8 | 9 | int (system)(const char *s) 10 | { /* send text to system command line processor */ 11 | if (s) 12 | { /* not just a test */ 13 | int pid = fork(); 14 | 15 | if (pid < 0) 16 | ; /* fork failed */ 17 | else if (pid == 0) 18 | { /* continue here as child */ 19 | execl("/bin/sh", "sh", "-c", s, NULL); 20 | exit(EXIT_FAILURE); 21 | } 22 | else /* continue here as parent */ 23 | while (wait(NULL) != pid) 24 | ; /* wait for child */ 25 | } 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /STDLIB/SYSTEM.C: -------------------------------------------------------------------------------- 1 | /* system function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system calls */ 5 | int _Execl(const char *, const char *, ...); 6 | int _Fork(void); 7 | int _Wait (int *); 8 | 9 | int (system)(const char *s) 10 | { /* send text to system command line processor */ 11 | if (s) 12 | { /* not just a test */ 13 | int pid = _Fork(); 14 | 15 | if (pid < 0) 16 | ; /* fork failed */ 17 | else if (pid == 0) 18 | { /* continue here as child */ 19 | _Execl("/bin/sh", "sh", "-c", s, NULL); 20 | exit(EXIT_FAILURE); 21 | } 22 | else /* continue here as parent */ 23 | while (_Wait(NULL) != pid) 24 | ; /* wait for child */ 25 | } 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /_UNIXVAX/SYSTEM.C: -------------------------------------------------------------------------------- 1 | /* system function -- UNIX version */ 2 | #include 3 | 4 | /* UNIX system calls */ 5 | int _Execl(const char *, const char *, ...); 6 | int _Fork(void); 7 | int _Wait (int *); 8 | 9 | int (system)(const char *s) 10 | { /* send text to system command line processor */ 11 | if (s) 12 | { /* not just a test */ 13 | int pid = _Fork(); 14 | 15 | if (pid < 0) 16 | ; /* fork failed */ 17 | else if (pid == 0) 18 | { /* continue here as child */ 19 | _Execl("/bin/sh", "sh", "-c", s, NULL); 20 | exit(EXIT_FAILURE); 21 | } 22 | else /* continue here as parent */ 23 | while (_Wait(NULL) != pid) 24 | ; /* wait for child */ 25 | } 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /STDIO/XFOPEN.C: -------------------------------------------------------------------------------- 1 | /* _Fopen function -- Turbo C++ version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* Turbo C++ system call */ 6 | int open(const char *, unsigned int, unsigned int); 7 | 8 | int _Fopen(const char *path, unsigned int smode, 9 | const char *mods) 10 | { /* open a file */ 11 | unsigned int acc; 12 | static const unsigned int rwacc[] = { 0, 1, 2, 4}; 13 | 14 | assert((_MOPENR|_MOPENW) == 0x03); 15 | acc = rwacc[smode & 0x03]; 16 | if (smode & _MOPENA) 17 | acc |= 0x0800; 18 | if (smode & _MTRUNC) 19 | acc |= 0x0200; 20 | if (smode & _MCREAT) 21 | acc |= 0x0100; 22 | if (smode & _MBIN) 23 | acc |= 0x8000; 24 | else 25 | acc |= 0x4000; 26 | return (open(path, acc, 0x180)); 27 | } 28 | -------------------------------------------------------------------------------- /_HEADERS/XTIME.H: -------------------------------------------------------------------------------- 1 | /* xtime.h internal header */ 2 | #include 3 | #include "xtinfo.h" 4 | /* macros */ 5 | #define WDAY 1 /* to get day of week right */ 6 | /* type definitions */ 7 | typedef struct { 8 | unsigned char wday, hour, day, mon, year; 9 | } Dstrule; 10 | /* internal declarations */ 11 | int _Daysto(int, int); 12 | const char *_Gentime(const struct tm *, _Tinfo *, char, 13 | int *, char *); 14 | Dstrule *_Getdst(const char *); 15 | const char *_Gettime(const char *, int, int *); 16 | int _Isdst(const struct tm *); 17 | const char *_Getzone(void); 18 | size_t _Strftime(char *, size_t, const char *, 19 | const struct tm *, _Tinfo *); 20 | struct tm *_Ttotm(struct tm *, time_t, int); 21 | time_t _Tzoff(void); 22 |  -------------------------------------------------------------------------------- /_TC86/XFOPEN.C: -------------------------------------------------------------------------------- 1 | /* _Fopen function -- Turbo C++ version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* Turbo C++ system call */ 6 | int open(const char *, unsigned int, unsigned int); 7 | 8 | int _Fopen(const char *path, unsigned int smode, 9 | const char *mods) 10 | { /* open a file */ 11 | unsigned int acc; 12 | static const unsigned int rwacc[] = { 0, 1, 2, 4}; 13 | 14 | assert((_MOPENR|_MOPENW) == 0x03); 15 | acc = rwacc[smode & 0x03]; 16 | if (smode & _MOPENA) 17 | acc |= 0x0800; 18 | if (smode & _MTRUNC) 19 | acc |= 0x0200; 20 | if (smode & _MCREAT) 21 | acc |= 0x0100; 22 | if (smode & _MBIN) 23 | acc |= 0x8000; 24 | else 25 | acc |= 0x4000; 26 | return (open(path, acc, 0x180)); 27 | } 28 | -------------------------------------------------------------------------------- /MATH/XDUNSCAL.C: -------------------------------------------------------------------------------- 1 | /* _Dunscale function -- IEEE 754 version */ 2 | #include "xmath.h" 3 | 4 | short _Dunscale(short *pex, double *px) 5 | { /* separate *px to 1/2 <= |frac| < 1 and 2^*pex */ 6 | unsigned short *ps = (unsigned short *)px; 7 | short xchar = (ps[_D0] & _DMASK) >> _DOFF; 8 | 9 | if (xchar == _DMAX) 10 | { /* NaN or INF */ 11 | *pex = 0; 12 | return (ps[_D0] & _DFRAC || ps[_D1] 13 | || ps[_D2] || ps[_D3] ? NAN : INF); 14 | } 15 | else if (0 < xchar || (xchar = _Dnorm(ps)) <= 0) 16 | { /* finite, reduce to [1/2, 1) */ 17 | ps[_D0] = ps[_D0] & ~_DMASK | _DBIAS << _DOFF; 18 | *pex = xchar - _DBIAS; 19 | return (FINITE); 20 | } 21 | else 22 | { /* zero */ 23 | *pex = 0; 24 | return (0); 25 | } 26 | } 27 |  -------------------------------------------------------------------------------- /_UNIX68K/XFGPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fgpos function -- UNIX version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* UNIX system call */ 6 | long lseek(int, long, int); 7 | 8 | long _Fgpos(FILE *str, fpos_t *ptr) 9 | { /* get file position */ 10 | long loff = lseek(str->_Handle, 0L, 1); 11 | 12 | if (loff == -1) 13 | { /* query failed */ 14 | errno = EFPOS; 15 | return (EOF); 16 | } 17 | if (str->_Mode & _MWRITE) 18 | loff += str->_Next - str->_Buf; 19 | else if (str->_Mode & _MREAD) 20 | loff -= str->_Nback 21 | ? str->_Rsave - str->_Next + str->_Nback 22 | : str->_Rend - str->_Next; 23 | if (ptr == NULL) 24 | return (loff); /* ftell */ 25 | else 26 | { /* fgetpos */ 27 | ptr->_Off = loff; 28 | return (0); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /SETJMP/SETJMP.C: -------------------------------------------------------------------------------- 1 | /* setjmp function */ 2 | #include 3 | #include 4 | 5 | static void dummy(int a, int b, int c, int d, int e, 6 | int f, int g, int h, int i, int j) 7 | { /* threaten to use arguments */ 8 | } 9 | 10 | static int getfp(void) 11 | { /* return frame pointer of caller */ 12 | int arg; 13 | 14 | return ((int)(&arg + _JBFP)); 15 | } 16 | 17 | int setjmp(jmp_buf env) 18 | { /* save environment for re-return */ 19 | register int a = 0, b = 0, c = 0, d = 0, e = 0; 20 | register int f = 0, g = 0, h = 0, i = 0, j = 0; 21 | 22 | if (a) /* try to outsmart optimizer */ 23 | dummy(a, b, c, d, e, f, g, h, i, j); 24 | env[1] = getfp(); 25 | memcpy((char *)&env[2], (char *)env[1] + _JBOFF, _JBMOV); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /STDLIB/BSEARCH.C: -------------------------------------------------------------------------------- 1 | /* bsearch function */ 2 | #include 3 | 4 | void *(bsearch)(const void *key, const void *base, 5 | size_t nelem, size_t size, _Cmpfun *cmp) 6 | { /* search sorted table by binary chop */ 7 | const char *p; 8 | size_t n; 9 | 10 | for (p = (const char *)base, n = nelem; 0 < n; ) 11 | { /* check midpoint of whatever is left */ 12 | const size_t pivot = n >> 1; 13 | const char *const q = p + size * pivot; 14 | const int val = (*cmp)(key, q); 15 | 16 | if (val < 0) 17 | n = pivot; /* search below pivot */ 18 | else if (val == 0) 19 | return ((void *)q); /* found */ 20 | else 21 | { /* search above pivot */ 22 | p = q + size; 23 | n -= pivot + 1; 24 | } 25 | } 26 | return (NULL); /* no match */ 27 | } 28 | -------------------------------------------------------------------------------- /_UNIXVAX/XFGPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fgpos function -- UNIX version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* UNIX system call */ 6 | long _Lseek(int, long, int); 7 | 8 | long _Fgpos(FILE *str, fpos_t *ptr) 9 | { /* get file position */ 10 | long loff = _Lseek(str->_Handle, 0L, 1); 11 | 12 | if (loff == -1) 13 | { /* query failed */ 14 | errno = EFPOS; 15 | return (EOF); 16 | } 17 | if (str->_Mode & _MWRITE) 18 | loff += str->_Next - str->_Buf; 19 | else if (str->_Mode & _MREAD) 20 | loff -= str->_Nback 21 | ? str->_Rsave - str->_Next + str->_Nback 22 | : str->_Rend - str->_Next; 23 | if (ptr == NULL) 24 | return (loff); /* ftell */ 25 | else 26 | { /* fgetpos */ 27 | ptr->_Off = loff; 28 | return (0); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LOCALE/XFREELOC.C: -------------------------------------------------------------------------------- 1 | /* _Freeloc function */ 2 | #include "xlocale.h" 3 | 4 | void _Freeloc(_Linfo *p) 5 | { /* free all storage */ 6 | _Locitem *q; 7 | 8 | for (q = _Loctab; q->_Name; ++q) 9 | switch (q->_Code) 10 | { /* free all pointers */ 11 | case L_STATE: 12 | { /* free all state entries */ 13 | int i; 14 | unsigned short **pt 15 | = &ADDR(p, q, unsigned short *); 16 | 17 | for (i = _NSTATE; 0 <= --i; ++pt) 18 | if (*pt && (*pt)[-1] != 0) 19 | free(&(*pt)[-1]); 20 | } 21 | break; 22 | case L_TABLE: 23 | if (NEWADDR(p, q, short *)) 24 | free(ADDR(p, q, short *) - 1); 25 | break; 26 | case L_GSTRING: 27 | case L_NAME: 28 | case L_STRING: 29 | if (NEWADDR(p, q, char *)) 30 | free(ADDR(p, q, char *)); 31 | } 32 | } 33 |  -------------------------------------------------------------------------------- /STDLIB/STRTOL.C: -------------------------------------------------------------------------------- 1 | /* strtol function */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | long (strtol)(const char *s, char **endptr, int base) 8 | { /* convert string to long, with checking */ 9 | const char *sc; 10 | char *se, sign; 11 | unsigned long x; 12 | 13 | if (endptr == NULL) 14 | endptr = &se; 15 | for (sc = s; isspace(*sc); ++sc) 16 | ; 17 | sign = *sc == '-' || *sc == '+' ? *sc++ : '+'; 18 | x = _Stoul(sc, endptr, base); 19 | if (sc == *endptr) 20 | *endptr = (char *)s; 21 | if (s == *endptr && x != 0 || sign == '+' && LONG_MAX < x 22 | || sign == '-' && -(unsigned long)LONG_MIN < x) 23 | { /* overflow */ 24 | errno = ERANGE; 25 | return (sign == '-' ? LONG_MIN : LONG_MAX); 26 | } 27 | else 28 | return ((long)(sign == '-' ? -x : x)); 29 | } 30 |  -------------------------------------------------------------------------------- /_TEST/TSIGNAL.C: -------------------------------------------------------------------------------- 1 | /* test signal functions */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /* static data */ 8 | static int sigs[] = { 9 | SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM}; 10 | static void (*rets[])(int) = {SIG_DFL, SIG_ERR, SIG_IGN}; 11 | static sig_atomic_t atomic; 12 | 13 | static void field_fpe(int sig) 14 | { /* handle SIGFPE */ 15 | assert(sig == SIGFPE); 16 | puts("SUCCESS testing "); 17 | exit(EXIT_SUCCESS); 18 | } 19 | 20 | int main() 21 | { /* test basic workings of signal functions */ 22 | printf("sizeof (sig_atomic_t) = %u\n", 23 | sizeof (sig_atomic_t)); 24 | assert(signal(SIGFPE, &field_fpe) == SIG_DFL); 25 | assert(signal(SIGFPE, &field_fpe) == &field_fpe); 26 | raise(SIGFPE); 27 | puts("FAILURE testing "); 28 | return (EXIT_FAILURE); 29 | } 30 | -------------------------------------------------------------------------------- /TIME/LOCALTIM.C: -------------------------------------------------------------------------------- 1 | /* localtime function */ 2 | #include 3 | #include "xtime.h" 4 | 5 | time_t _Tzoff(void) 6 | { /* determine local time offset */ 7 | static const char *oldzone = NULL; 8 | static long tzoff = 0; 9 | static const long maxtz = 60*13; 10 | 11 | if (oldzone != _Times._Tzone) 12 | { /* determine time zone offset (East is +) */ 13 | const char *p, *pe; 14 | int n; 15 | 16 | if (_Times._Tzone[0] == '\0') 17 | _Times._Tzone = _Getzone(); 18 | p = _Gettime(_Times._Tzone, 2, &n); 19 | tzoff = strtol(p, (char **)&pe, 10); 20 | if (pe - p != n 21 | || tzoff <= -maxtz || maxtz <= tzoff) 22 | tzoff = 0; 23 | oldzone = _Times._Tzone; 24 | } 25 | return (-tzoff * 60); 26 | } 27 | 28 | struct tm *(localtime)(const time_t *tod) 29 | { /* convert to local time structure */ 30 | return (_Ttotm(NULL, *tod + _Tzoff(), -1)); 31 | } 32 | -------------------------------------------------------------------------------- /STDIO/FFLUSH.C: -------------------------------------------------------------------------------- 1 | /* fflush function */ 2 | #include "xstdio.h" 3 | #include "yfuns.h" 4 | 5 | int (fflush)(FILE *str) 6 | { /* flush an output stream */ 7 | int n; 8 | unsigned char *s; 9 | 10 | if (str == NULL) 11 | { /* recurse on all streams */ 12 | int nf, stat; 13 | 14 | for (stat = 0, nf = 0; nf < FOPEN_MAX; ++nf) 15 | if (_Files[nf] && fflush(_Files[nf]) < 0) 16 | stat = EOF; 17 | return (stat); 18 | } 19 | if (!(str->_Mode & _MWRITE)) 20 | return (0); 21 | for (s = str->_Buf; s < str->_Next; s += n) 22 | { /* try to write buffer */ 23 | n = _Fwrite(str, s, str->_Next - s); 24 | if (n <= 0) 25 | { /* report error and fail */ 26 | str->_Next = str->_Buf; 27 | str->_Wend = str->_Buf; 28 | str->_Mode |= _MERR; 29 | return (EOF); 30 | } 31 | } 32 | str->_Next = str->_Buf; 33 | str->_Wend = str->_Bend; 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /STRING/STRXFRM.C: -------------------------------------------------------------------------------- 1 | /* strxfrm function */ 2 | #include "xstrxfrm.h" 3 | 4 | size_t (strxfrm)(char *s1, const char *s2, size_t n) 5 | { /* transform s2[] to s1[] by locale-dependent rule */ 6 | size_t nx = 0; 7 | const unsigned char *s = (const unsigned char *)s2; 8 | _Cosave state = {0}; 9 | 10 | while (nx < n) 11 | { /* translate and deliver */ 12 | size_t i = _Strxfrm(s1, &s, n - nx, &state); 13 | 14 | s1 += i, nx += i; 15 | if (0 < i && s1[-1] == '\0') 16 | return (nx - 1); 17 | else if (*s == '\0') 18 | s = (const unsigned char *)s2; /* rescan */ 19 | } 20 | for (; ; ) 21 | { /* translate and count */ 22 | char buf[32]; 23 | size_t i = _Strxfrm(buf, &s, sizeof (buf), &state); 24 | 25 | nx += i; 26 | if (0 < i && buf[i - 1] == '\0') 27 | return (nx - 1); 28 | else if (*s == '\0') 29 | s = (const unsigned char *)s2; /* rescan */ 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /SETJMP/LONGJMP.C: -------------------------------------------------------------------------------- 1 | /* longjmp function */ 2 | #include 3 | #include 4 | 5 | static void dummy(int a, int b, int c, int d, int e, 6 | int f, int g, int h, int i, int j) 7 | { /* threaten to use arguments */ 8 | } 9 | 10 | static void setfp(int fp) 11 | { /* set frame pointer of caller */ 12 | int arg; 13 | 14 | (&arg)[_JBFP] = fp; 15 | } 16 | 17 | static int dojmp(jmp_buf env) 18 | { /* do the actual dirty business */ 19 | memcpy((char *)env[1] + _JBOFF, (char *)&env[2], _JBMOV); 20 | setfp(env[1]); 21 | return (env[0]); 22 | } 23 | 24 | void longjmp(jmp_buf env, int val) 25 | { /* re-return from setjmp */ 26 | register int a = 0, b = 0, c = 0, d = 0, e = 0; 27 | register int f = 0, g = 0, h = 0, i = 0, j = 0; 28 | 29 | if (a) /* try to outsmart optimizer */ 30 | dummy(a, b, c, d, e, f, g, h, i, j); 31 | env[0] = val ? val : 1; 32 | dojmp(env); 33 | } 34 | -------------------------------------------------------------------------------- /STDIO/XFWPREP.C: -------------------------------------------------------------------------------- 1 | /* _Fwprep function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | int _Fwprep(FILE *str) 6 | { /* prepare stream for writing */ 7 | if (str->_Next < str->_Wend) 8 | return (0); 9 | else if (str->_Mode & _MWRITE) 10 | return (fflush(str)); 11 | else if ((str->_Mode & (_MOPENW|_MREAD)) != _MOPENW) 12 | { /* can't write after read */ 13 | str->_Mode |= _MERR; 14 | return (-1); 15 | } 16 | if (str->_Buf) 17 | ; 18 | else if ((str->_Buf = (unsigned char *)malloc(BUFSIZ)) 19 | == NULL) 20 | { /* use 1-char _Cbuf */ 21 | str->_Buf = &str->_Cbuf; 22 | str->_Bend = str->_Buf + 1; 23 | } 24 | else 25 | { /* use allocated buffer */ 26 | str->_Mode |= _MALBUF; 27 | str->_Bend = str->_Buf + BUFSIZ; 28 | } 29 | str->_Next = str->_Buf; 30 | str->_Rend = str->_Buf; 31 | str->_Wend = str->_Bend; 32 | str->_Mode |= _MWRITE; 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /TIME/MKTIME.C: -------------------------------------------------------------------------------- 1 | /* mktime function */ 2 | #include 3 | #include "xtime.h" 4 | 5 | time_t (mktime)(struct tm *t) 6 | { /* convert local time structure to scalar time */ 7 | double dsecs; 8 | int mon, year, ymon; 9 | time_t secs; 10 | 11 | ymon = t->tm_mon / 12; 12 | mon = t->tm_mon - ymon * 12; 13 | if (mon < 0) 14 | mon += 12, --ymon; 15 | if (ymon < 0 && t->tm_year < INT_MIN - ymon 16 | || 0 < ymon && INT_MAX - ymon < t->tm_year) 17 | return ((time_t)(-1)); 18 | year = t->tm_year + ymon; 19 | dsecs = 86400.0 * (_Daysto(year, mon) - 1) 20 | + 31536000.0 * year + 86400.0 * t->tm_mday; 21 | dsecs += 3600.0 * t->tm_hour + 60.0 * t->tm_min 22 | + (double)t->tm_sec; 23 | if (dsecs < 0.0 || (double)(time_t)(-1) <= dsecs) 24 | return ((time_t)(-1)); 25 | secs = (time_t)(dsecs - _TBIAS); 26 | _Ttotm(t, secs, t->tm_isdst); 27 | if (0 < t->tm_isdst) 28 | secs -= 3600; 29 | return (secs - _Tzoff()); 30 | } 31 |  -------------------------------------------------------------------------------- /STDIO/FREAD.C: -------------------------------------------------------------------------------- 1 | /* fread function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | size_t (fread)(void *ptr, size_t size, size_t nelem, FILE *str) 6 | { /* read into array from stream */ 7 | size_t ns = size * nelem; 8 | unsigned char *s = (unsigned char *)ptr; 9 | 10 | if (ns == 0) 11 | return (0); 12 | if (0 < str->_Nback) 13 | { /* deliver pushed back chars */ 14 | for (; 0 < ns && 0 < str->_Nback; --ns) 15 | *s++ = str->_Back[--str->_Nback]; 16 | if (str->_Nback == 0) 17 | str->_Rend = str->_Rsave; 18 | } 19 | while (0 < ns) 20 | { /* ensure chars in buffer */ 21 | if (str->_Next < str->_Rend) 22 | ; 23 | else if (_Frprep(str) <= 0) 24 | break; 25 | { /* deliver as many as possible */ 26 | size_t m = str->_Rend - str->_Next; 27 | 28 | if (ns < m) 29 | m = ns; 30 | memcpy(s, str->_Next, m); 31 | s += m, ns -= m; 32 | str->_Next += m; 33 | } 34 | } 35 | return ((size * nelem - ns) / size); 36 | } 37 | -------------------------------------------------------------------------------- /STDIO/TMPNAM.C: -------------------------------------------------------------------------------- 1 | /* tmpnam function -- Turbo C++ version */ 2 | #include 3 | #include 4 | #include "xstdio.h" 5 | 6 | #if L_tmpnam < 13 7 | #error BAD ASSUMPTION ABOUT L_tmpnam 8 | #endif 9 | 10 | char *(tmpnam)(char *s) 11 | { /* create a temporary file name */ 12 | char *p; 13 | int i; 14 | unsigned short t; 15 | static char buf[L_tmpnam]; 16 | static char *root = NULL; 17 | static unsigned short seed = 0; 18 | 19 | if (s == NULL) 20 | s = buf; 21 | if (root) 22 | ; 23 | else if ((p = getenv("TEMP")) == NULL 24 | || L_tmpnam < strlen(p) + 14 25 | || (root = (char *)malloc(strlen(p) + 5)) == NULL) 26 | root = "ctm"; 27 | else 28 | { /* setup root directory */ 29 | strcpy(root, p); 30 | strcat(root, "/ctm"); 31 | } 32 | ++seed; 33 | strcpy(s, root); 34 | i = 5; 35 | p = s + strlen(s) + i; 36 | strcpy(p, ".tmp"); 37 | for (t = seed; 0 <= --i; t >>= 3) 38 | *--p = '0' + (t & 07); 39 | return (s); 40 | } 41 | -------------------------------------------------------------------------------- /STDLIB/WCSTOMBS.C: -------------------------------------------------------------------------------- 1 | /* wcstombs function */ 2 | #include 3 | #include 4 | #include 5 | 6 | size_t (wcstombs)(char *s, const wchar_t *wcs, size_t n) 7 | { /* translate wide char string to multibyte string */ 8 | char *sc; 9 | char state = {0}; 10 | size_t i; 11 | 12 | for (sc = s; 0 < n; n -= i, ++wcs) 13 | { /* translate another wide character */ 14 | if (MB_CUR_MAX <= n) 15 | { /* copy directly */ 16 | if ((i = _Wctomb(sc, *wcs, &state)) <= 0) 17 | return (-1); 18 | } 19 | else 20 | { /* copy into local buffer */ 21 | char buf[MB_LEN_MAX]; 22 | 23 | if ((i = _Wctomb(buf, *wcs, &state)) <= 0) 24 | return (-1); 25 | else if (i <= n) 26 | memcpy(sc, buf, i); 27 | else 28 | { /* won't all fit */ 29 | memcpy(sc, buf, n); 30 | return (sc - s + n); 31 | } 32 | } 33 | sc += i; 34 | if (sc[-1] == '\0') 35 | return (sc - s - 1); 36 | } 37 | return (sc - s); 38 | } 39 | -------------------------------------------------------------------------------- /_HEADERS/TIME.H: -------------------------------------------------------------------------------- 1 | /* time.h standard header */ 2 | #ifndef _TIME 3 | #define _TIME 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* macros */ 8 | #define NULL _NULL 9 | #define CLOCKS_PER_SEC _CPS 10 | /* type definitions */ 11 | #ifndef _SIZET 12 | #define _SIZET 13 | typedef _Sizet size_t; 14 | #endif 15 | typedef unsigned int clock_t; 16 | typedef unsigned long time_t; 17 | struct tm { 18 | int tm_sec; 19 | int tm_min; 20 | int tm_hour; 21 | int tm_mday; 22 | int tm_mon; 23 | int tm_year; 24 | int tm_wday; 25 | int tm_yday; 26 | int tm_isdst; 27 | }; 28 | /* declarations */ 29 | char *asctime(const struct tm *); 30 | clock_t clock(void); 31 | char *ctime(const time_t *); 32 | double difftime(time_t, time_t); 33 | struct tm *gmtime(const time_t *); 34 | struct tm *localtime(const time_t *); 35 | time_t mktime(struct tm *); 36 | size_t strftime(char *, size_t, const char *, 37 | const struct tm *); 38 | time_t time(time_t *); 39 | #endif 40 | -------------------------------------------------------------------------------- /_TC86/TMPNAM.C: -------------------------------------------------------------------------------- 1 | /* tmpnam function -- Turbo C++ version */ 2 | #include 3 | #include 4 | #include "xstdio.h" 5 | 6 | #if L_tmpnam < 13 7 | #error BAD ASSUMPTION ABOUT L_tmpnam 8 | #endif 9 | 10 | char *(tmpnam)(char *s) 11 | { /* create a temporary file name */ 12 | char *p; 13 | int i; 14 | unsigned short t; 15 | static char buf[L_tmpnam]; 16 | static char *root = NULL; 17 | static unsigned short seed = 0; 18 | 19 | if (s == NULL) 20 | s = buf; 21 | if (root) 22 | ; 23 | else if ((p = getenv("TEMP")) == NULL 24 | || L_tmpnam < strlen(p) + 14 25 | || (root = (char *)malloc(strlen(p) + 5)) == NULL) 26 | root = "ctm"; 27 | else 28 | { /* setup root directory */ 29 | strcpy(root, p); 30 | strcat(root, "/ctm"); 31 | } 32 | ++seed; 33 | strcpy(s, root); 34 | i = 5; 35 | p = s + strlen(s) + i; 36 | strcpy(p, ".tmp"); 37 | for (t = seed; 0 <= --i; t >>= 3) 38 | *--p = '0' + (t & 07); 39 | return (s); 40 | } 41 | -------------------------------------------------------------------------------- /STDIO/FCLOSE.C: -------------------------------------------------------------------------------- 1 | /* fclose function */ 2 | #include 3 | #include "xstdio.h" 4 | #include "yfuns.h" 5 | 6 | int (fclose)(FILE *str) 7 | { /* close a stream */ 8 | int alfil = str->_Mode & _MALFIL; 9 | int stat = fflush(str); 10 | 11 | if (str->_Mode & _MALBUF) 12 | free(str->_Buf); 13 | str->_Buf = NULL; 14 | if (0 <= str->_Handle && _Fclose(str)) 15 | stat = EOF; 16 | if (str->_Tmpnam) 17 | { /* remove temp file */ 18 | if (remove(str->_Tmpnam)) 19 | stat = EOF; 20 | free(str->_Tmpnam), str->_Tmpnam = NULL; 21 | } 22 | str->_Mode = 0; 23 | str->_Next = &str->_Cbuf; 24 | str->_Rend = &str->_Cbuf; 25 | str->_Wend = &str->_Cbuf; 26 | str->_Nback = 0; 27 | if (alfil) 28 | { /* find _Files[i] entry and free */ 29 | size_t i; 30 | 31 | for (i = 0; i < FOPEN_MAX; ++i) 32 | if (_Files[i] == str) 33 | { /* found entry */ 34 | _Files[i] = NULL; 35 | break; 36 | } 37 | free(str); 38 | } 39 | return (stat); 40 | } 41 |  -------------------------------------------------------------------------------- /STDIO/FPUTS.C: -------------------------------------------------------------------------------- 1 | /* fputs function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | int (fputs)(const char *s, FILE *str) 6 | { /* put a string to stream */ 7 | while (*s) 8 | { /* ensure room in buffer */ 9 | if (str->_Next < str->_Wend) 10 | ; 11 | else if (_Fwprep(str) < 0) 12 | return (EOF); 13 | { /* copy in as many as possible */ 14 | const char *s1 = str->_Mode & _MLBF 15 | ? strchr(s, '\n') : NULL; 16 | size_t m = s1 ? s1 - s + 1 : strlen(s); 17 | size_t n; 18 | 19 | n = str->_Wend - str->_Next; 20 | if (n < m) 21 | s1 = NULL, m = n; 22 | memcpy(str->_Next, s, m); 23 | s += m; 24 | str->_Next += m; 25 | if (s1 && fflush(str)) 26 | { /* fail on error */ 27 | str->_Wend = str->_Buf; 28 | return (EOF); 29 | } 30 | } 31 | } 32 | if (str->_Mode & _MNBF) 33 | { /* disable macros and drain */ 34 | str->_Wend = str->_Buf; 35 | if (fflush(str)) 36 | return (EOF); 37 | } 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /MATH/ATAN2.C: -------------------------------------------------------------------------------- 1 | /* atan2 function */ 2 | #include "xmath.h" 3 | 4 | double (atan2)(double y, double x) 5 | { /* compute atan(y/x) */ 6 | double z; 7 | const short errx = _Dtest(&x); 8 | const short erry = _Dtest(&y); 9 | unsigned short hex; 10 | 11 | if (errx <= 0 && erry <= 0) 12 | { /* x & y both finite or 0 */ 13 | if (y < 0.0) 14 | y = -y, hex = 0x8; 15 | else 16 | hex = 0x0; 17 | if (x < 0.0) 18 | x = -x, hex ^= 0x6; 19 | if (x < y) 20 | z = x / y, hex ^= 0x2; 21 | else if (0.0 < x) 22 | z = y / x; 23 | else 24 | return (0.0); /* atan(0, 0) */ 25 | } 26 | else if (errx == NAN || erry == NAN) 27 | { /* return one of the NaNs */ 28 | errno = EDOM; 29 | return (errx == NAN ? x : y); 30 | } 31 | else 32 | { /* at least one INF */ 33 | z = errx == erry ? 1.0 : 0.0; 34 | hex = DSIGN(y) ? 0x8 : 0x0; 35 | if (DSIGN(x)) 36 | hex ^= 0x6; 37 | if (erry == INF) 38 | hex ^= 0x2; 39 | } 40 | return (_Atan(z, hex)); 41 | } 42 | -------------------------------------------------------------------------------- /STDIO/SETVBUF.C: -------------------------------------------------------------------------------- 1 | /* setvbuf function */ 2 | #include 3 | #include 4 | #include "xstdio.h" 5 | 6 | int (setvbuf)(FILE *str, char *abuf, int smode, size_t size) 7 | { /* set up buffer for a stream */ 8 | int mode; 9 | unsigned char *buf = (unsigned char *)abuf; 10 | 11 | if (str->_Mode & (_MREAD|_MWRITE)) 12 | return (-1); 13 | mode = smode == _IOFBF ? 0 14 | : smode == _IOLBF ? _MLBF 15 | : smode == _IONBF ? _MNBF : -1; 16 | if (mode == -1) 17 | return (-1); 18 | if (size == 0) 19 | buf = &str->_Cbuf, size = 1; 20 | else if (INT_MAX < size) 21 | size = INT_MAX; 22 | if (buf) 23 | ; 24 | else if ((buf = (unsigned char *)malloc(size)) == NULL) 25 | return (-1); 26 | else 27 | mode |= _MALBUF; 28 | if (str->_Mode & _MALBUF) 29 | free(str->_Buf), str->_Mode &= ~_MALBUF; 30 | str->_Mode |= mode; 31 | str->_Buf = buf; 32 | str->_Bend = buf + size; 33 | str->_Next = buf; 34 | str->_Rend = buf; 35 | str->_Wend = buf; 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /LOCALE/LOCALECO.C: -------------------------------------------------------------------------------- 1 | /* localeconv function */ 2 | #include 3 | #include 4 | 5 | /* static data */ 6 | static char null[] = ""; 7 | struct lconv _Locale = { 8 | /* LC_MONETARY */ 9 | null, /* currency_symbol */ 10 | null, /* int_curr_symbol */ 11 | null, /* mon_decimal_point */ 12 | null, /* mon_grouping */ 13 | null, /* mon_thousands_sep */ 14 | null, /* negative_sign */ 15 | null, /* positive_sign */ 16 | CHAR_MAX, /* frac_digits */ 17 | CHAR_MAX, /* int_frac_digits */ 18 | CHAR_MAX, /* n_cs_precedes */ 19 | CHAR_MAX, /* n_sep_by_space */ 20 | CHAR_MAX, /* n_sign_posn */ 21 | CHAR_MAX, /* p_cs_precedes */ 22 | CHAR_MAX, /* p_sep_by_space */ 23 | CHAR_MAX, /* p_sign_posn */ 24 | /* LC_NUMERIC */ 25 | ".", /* decimal_point */ 26 | null, /* grouping */ 27 | null}; /* thousands_sep */ 28 | 29 | struct lconv *(localeconv)(void) 30 | { /* get pointer to current locale */ 31 | return (&_Locale); 32 | } 33 | -------------------------------------------------------------------------------- /TIME/ASCTIME.C: -------------------------------------------------------------------------------- 1 | /* asctime function */ 2 | #include "xtime.h" 3 | 4 | /* static data */ 5 | static const char ampm[] = {":AM:PM"}; 6 | static const char days[] = { 7 | ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:Wednesday" 8 | ":Thu:Thursday:Fri:Friday:Sat:Saturday"}; 9 | static const char fmts[] = { 10 | "|%b %D %H:%M:%S %Y|%b %D %Y|%H:%M:%S"}; 11 | static const char isdst[] = {""}; 12 | static const char mons[] = { 13 | ":Jan:January:Feb:February:Mar:March" 14 | ":Apr:April:May:May:Jun:June" 15 | ":Jul:July:Aug:August:Sep:September" 16 | ":Oct:October:Nov:November:Dec:December"}; 17 | static const char zone[] = {""}; /* adapt by default */ 18 | static _Tinfo ctinfo = {ampm, days, fmts, isdst, mons, zone}; 19 | _Tinfo _Times = {ampm, days, fmts, isdst, mons, zone}; 20 | 21 | char *(asctime)(const struct tm *t) 22 | { /* format time as "Day Mon dd hh:mm:ss yyyy\n" */ 23 | static char tbuf[] = "Day Mon dd hh:mm:ss yyyy\n"; 24 | 25 | _Strftime(tbuf, sizeof (tbuf), "%a %c\n", t, &ctinfo); 26 | return (tbuf); 27 | } 28 | -------------------------------------------------------------------------------- /STRING/STRERROR.C: -------------------------------------------------------------------------------- 1 | /* strerror function */ 2 | #include 3 | #include 4 | 5 | char *_Strerror(int errcode, char *buf) 6 | { /* copy error message into buffer as needed */ 7 | static char sbuf[] = {"error #xxx"}; 8 | 9 | if (buf == NULL) 10 | buf = sbuf; 11 | switch (errcode) 12 | { /* switch on known error codes */ 13 | case 0: 14 | return ("no error"); 15 | case EDOM: 16 | return ("domain error"); 17 | case ERANGE: 18 | return ("range error"); 19 | case EFPOS: 20 | return ("file positioning error"); 21 | default: 22 | if (errcode < 0 || _NERR <= errcode) 23 | return ("unknown error"); 24 | else 25 | { /* generate numeric error code */ 26 | strcpy(buf, "error #xxx"); 27 | buf[9] = errcode % 10 + '0'; 28 | buf[8] = (errcode /= 10) % 10 + '0'; 29 | buf[7] = (errcode / 10) % 10 + '0'; 30 | return (buf); 31 | } 32 | } 33 | } 34 | 35 | char *(strerror)(int errcode) 36 | { /* find error message corresponding to errcode */ 37 | return (_Strerror(errcode, NULL)); 38 | } 39 | -------------------------------------------------------------------------------- /MATH/SQRT.C: -------------------------------------------------------------------------------- 1 | /* sqrt function */ 2 | #include 3 | #include "xmath.h" 4 | 5 | double (sqrt)(double x) 6 | { /* compute sqrt(x) */ 7 | short xexp; 8 | 9 | switch (_Dunscale(&xexp, &x)) 10 | { /* test for special codes */ 11 | case NAN: 12 | errno = EDOM; 13 | return (x); 14 | case INF: 15 | if (DSIGN(x)) 16 | { /* -INF */ 17 | errno = EDOM; 18 | return (_Nan._D); 19 | } 20 | else 21 | { /* +INF */ 22 | errno = ERANGE; 23 | return (_Inf._D); 24 | } 25 | case 0: 26 | return (0.0); 27 | default: /* finite */ 28 | if (x < 0.0) 29 | { /* sqrt undefined for reals */ 30 | errno = EDOM; 31 | return (_Nan._D); 32 | } 33 | { /* 0 < x, compute sqrt(x) */ 34 | double y; 35 | static const double sqrt2 = 1.41421356237309505; 36 | 37 | y = (-0.1984742 * x + 0.8804894) * x + 0.3176687; 38 | y = 0.5 * (y + x / y); 39 | y += x / y; 40 | x = 0.25 * y + x / y; 41 | if ((unsigned int)xexp & 1) 42 | x *= sqrt2, --xexp; 43 | _Dscale(&x, xexp / 2); 44 | return (x); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /_TEST/TTIME.C: -------------------------------------------------------------------------------- 1 | /* test time functions */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { /* test basic workings of time functions */ 9 | char buf[32]; 10 | clock_t tc = clock(); 11 | struct tm ts1; 12 | time_t tt1, tt2; 13 | static char *dstr = "Sun Dec 2 06:55:15 1979\n"; 14 | 15 | tt1 = time(&tt2); 16 | assert(tt1 == tt2); 17 | ts1.tm_sec = 15; 18 | ts1.tm_min = 55; 19 | ts1.tm_hour = 6; 20 | ts1.tm_mday = 2; 21 | ts1.tm_mon = 11; 22 | ts1.tm_year = 79; 23 | ts1.tm_isdst = -1; 24 | tt1 = mktime(&ts1); 25 | assert(ts1.tm_wday == 0); 26 | assert(ts1.tm_yday == 335); 27 | ++ts1.tm_sec; 28 | tt2 = mktime(&ts1); 29 | assert(difftime(tt1, tt2) < 0.0); 30 | assert(strcmp(asctime(localtime(&tt1)), dstr) == 0); 31 | assert(strftime(buf, sizeof (buf), "%S", 32 | gmtime(&tt2)) == 2); 33 | assert(strcmp(buf, "16") == 0); 34 | assert(tc <= clock()); 35 | fputs("Current date -- ", stdout); 36 | time(&tt1); 37 | fputs(ctime(&tt1), stdout); 38 | puts("SUCCESS testing "); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /MATH/XDNORM.C: -------------------------------------------------------------------------------- 1 | /* _Dnorm function -- IEEE 754 version */ 2 | #include "xmath.h" 3 | 4 | short _Dnorm(unsigned short *ps) 5 | { /* normalize double fraction */ 6 | short xchar; 7 | unsigned short sign = ps[_D0] & _DSIGN; 8 | 9 | xchar = 1; 10 | if ((ps[_D0] &= _DFRAC) != 0 || ps[_D1] 11 | || ps[_D2] || ps[_D3]) 12 | { /* nonzero, scale */ 13 | for (; ps[_D0] == 0; xchar -= 16) 14 | { /* shift left by 16 */ 15 | ps[_D0] = ps[_D1], ps[_D1] = ps[_D2]; 16 | ps[_D2] = ps[_D3], ps[_D3] = 0; 17 | } 18 | for (; ps[_D0] < 1<<_DOFF; --xchar) 19 | { /* shift left by 1 */ 20 | ps[_D0] = ps[_D0] << 1 | ps[_D1] >> 15; 21 | ps[_D1] = ps[_D1] << 1 | ps[_D2] >> 15; 22 | ps[_D2] = ps[_D2] << 1 | ps[_D3] >> 15; 23 | ps[_D3] <<= 1; 24 | } 25 | for (; 1<<_DOFF+1 <= ps[_D0]; ++xchar) 26 | { /* shift right by 1 */ 27 | ps[_D3] = ps[_D3] >> 1 | ps[_D2] << 15; 28 | ps[_D2] = ps[_D2] >> 1 | ps[_D1] << 15; 29 | ps[_D1] = ps[_D1] >> 1 | ps[_D0] << 15; 30 | ps[_D0] >>= 1; 31 | } 32 | ps[_D0] &= _DFRAC; 33 | } 34 | ps[_D0] |= sign; 35 | return (xchar); 36 | } 37 |  -------------------------------------------------------------------------------- /_HEADERS/LIMITS.H: -------------------------------------------------------------------------------- 1 | /* limits.h standard header -- 8-bit version */ 2 | #ifndef _LIMITS 3 | #define _LIMITS 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* char properties */ 8 | #define CHAR_BIT 8 9 | #if _CSIGN 10 | #define CHAR_MAX 127 11 | #define CHAR_MIN (-127-_C2) 12 | #else 13 | #define CHAR_MAX 255 14 | #define CHAR_MIN 0 15 | #endif 16 | /* int properties */ 17 | #if _ILONG 18 | #define INT_MAX 2147483647 19 | #define INT_MIN (-2147483647-_C2) 20 | #define UINT_MAX 4294967295U 21 | #else 22 | #define INT_MAX 32767 23 | #define INT_MIN (-32767-_C2) 24 | #define UINT_MAX 65535U 25 | #endif 26 | /* long properties */ 27 | #define LONG_MAX 2147483647 28 | #define LONG_MIN (-2147483647-_C2) 29 | /* multibyte properties */ 30 | #define MB_LEN_MAX _MBMAX 31 | /* signed char properties */ 32 | #define SCHAR_MAX 127 33 | #define SCHAR_MIN (-127-_C2) 34 | /* short properties */ 35 | #define SHRT_MAX 32767 36 | #define SHRT_MIN (-32767-_C2) 37 | /* unsigned properties */ 38 | #define UCHAR_MAX 255 39 | #define ULONG_MAX 4294967295 40 | #define USHRT_MAX 65535U 41 | #endif 42 |  -------------------------------------------------------------------------------- /_TEST/TASSERT.C: -------------------------------------------------------------------------------- 1 | /* test assert macro */ 2 | #define NDEBUG 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /* static data */ 9 | static int val = 0; 10 | 11 | static void field_abort(int sig) 12 | { /* handle SIGABRT */ 13 | if (val == 1) 14 | { /* expected result */ 15 | puts("SUCCESS testing "); 16 | exit(EXIT_SUCCESS); 17 | } 18 | else 19 | { /* unexpected result */ 20 | puts("FAILURE testing "); 21 | exit(EXIT_FAILURE); 22 | } 23 | } 24 | 25 | static void dummy() 26 | { /* test dummy assert macro */ 27 | int i = 0; 28 | 29 | assert(i == 0); 30 | assert(i == 1); 31 | } 32 | 33 | #undef NDEBUG 34 | #include 35 | 36 | int main() 37 | { /* test both dummy and working forms */ 38 | assert(signal(SIGABRT, &field_abort) != SIG_ERR); 39 | dummy(); 40 | assert(val == 0); /* should not abort */ 41 | ++val; 42 | fputs("Sample assertion failure message --\n", stderr); 43 | assert(val == 0); /* should abort */ 44 | puts("FAILURE testing "); 45 | return (EXIT_FAILURE); 46 | } 47 | -------------------------------------------------------------------------------- /STDIO/FWRITE.C: -------------------------------------------------------------------------------- 1 | /* fwrite function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | size_t (fwrite)(const void *ptr, size_t size, 6 | size_t nelem, FILE *str) 7 | { /* write to stream from array */ 8 | char *s = (char *)ptr; 9 | size_t ns = size * nelem; 10 | 11 | if (ns == 0) 12 | return (0); 13 | while (0 < ns) 14 | { /* ensure room in buffer */ 15 | if (str->_Next < str->_Wend) 16 | ; 17 | else if (_Fwprep(str) < 0) 18 | break; 19 | { /* copy in as many as possible */ 20 | char *s1 = str->_Mode & _MLBF 21 | ? (char *)memchr(s, '\n', ns) : NULL; 22 | size_t m = s1 ? s1 - s + 1 : ns; 23 | size_t n = str->_Wend - str->_Next; 24 | 25 | if (n < m) 26 | s1 = NULL, m = n; 27 | memcpy(str->_Next, s, m); 28 | s += m, ns -= m; 29 | str->_Next += m; 30 | if (s1 && fflush(str)) 31 | { /* disable macros on failure */ 32 | str->_Wend = str->_Buf; 33 | break; 34 | } 35 | } 36 | } 37 | if (str->_Mode & _MNBF) 38 | { /* disable and drain */ 39 | str->_Wend = str->_Buf; 40 | fflush(str); 41 | } 42 | return ((size * nelem - ns) / size); 43 | } 44 | -------------------------------------------------------------------------------- /_UNIX68K/XFSPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fspos function -- UNIX version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* UNIX system call */ 6 | long lseek(int, long, int); 7 | 8 | int _Fspos(FILE *str, const fpos_t *ptr, long loff, int way) 9 | { /* position a file */ 10 | if (fflush(str)) 11 | { /* write error */ 12 | errno = EFPOS; 13 | return (EOF); 14 | } 15 | if (ptr) 16 | loff += ptr->_Off; /* fsetpos */ 17 | if (way == SEEK_CUR && str->_Mode & _MREAD) 18 | loff -= str->_Nback 19 | ? str->_Rsave - str->_Next + str->_Nback 20 | : str->_Rend - str->_Next; 21 | if (way == SEEK_CUR && loff != 0 22 | || way != SEEK_SET || loff != -1) 23 | loff = lseek(str->_Handle, loff, way); 24 | if (loff == -1) 25 | { /* request failed */ 26 | errno = EFPOS; 27 | return (EOF); 28 | } 29 | else 30 | { /* success */ 31 | if (str->_Mode & (_MREAD|_MWRITE)) 32 | { /* empty buffer */ 33 | str->_Next = str->_Buf; 34 | str->_Rend = str->_Buf; 35 | str->_Wend = str->_Buf; 36 | str->_Nback = 0; 37 | } 38 | str->_Mode &= ~(_MEOF|_MREAD|_MWRITE); 39 | return (0); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /_UNIXVAX/XFSPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fspos function -- UNIX version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* UNIX system call */ 6 | long _Lseek(int, long, int); 7 | 8 | int _Fspos(FILE *str, const fpos_t *ptr, long loff, int way) 9 | { /* position a file */ 10 | if (fflush(str)) 11 | { /* write error */ 12 | errno = EFPOS; 13 | return (EOF); 14 | } 15 | if (ptr) 16 | loff += ptr->_Off; /* fsetpos */ 17 | if (way == SEEK_CUR && str->_Mode & _MREAD) 18 | loff -= str->_Nback 19 | ? str->_Rsave - str->_Next + str->_Nback 20 | : str->_Rend - str->_Next; 21 | if (way == SEEK_CUR && loff != 0 22 | || way != SEEK_SET || loff != -1) 23 | loff = _Lseek(str->_Handle, loff, way); 24 | if (loff == -1) 25 | { /* request failed */ 26 | errno = EFPOS; 27 | return (EOF); 28 | } 29 | else 30 | { /* success */ 31 | if (str->_Mode & (_MREAD|_MWRITE)) 32 | { /* empty buffer */ 33 | str->_Next = str->_Buf; 34 | str->_Rend = str->_Buf; 35 | str->_Wend = str->_Buf; 36 | str->_Nback = 0; 37 | } 38 | str->_Mode &= ~(_MEOF|_MREAD|_MWRITE); 39 | return (0); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /_TEST/TSTDARG.C: -------------------------------------------------------------------------------- 1 | /* test stdarg macros */ 2 | #include 3 | #include 4 | #include 5 | 6 | /* type definitions */ 7 | typedef struct { 8 | char c; 9 | } Cstruct; 10 | 11 | static int tryit(const char *fmt, ...) 12 | { /* test variable argument list */ 13 | int ctr = 0; 14 | va_list ap; 15 | 16 | va_start(ap, fmt); 17 | for (; *fmt; ++fmt) 18 | switch (*fmt) 19 | { /* switch on argument type */ 20 | case 'i': 21 | assert(va_arg(ap, int) == ++ctr); 22 | break; 23 | case 'd': 24 | assert(va_arg(ap, double) == ++ctr); 25 | break; 26 | case 'p': 27 | assert(va_arg(ap, char *)[0] == ++ctr); 28 | break; 29 | case 's': 30 | assert(va_arg(ap, Cstruct).c == ++ctr); 31 | } 32 | va_end(ap); 33 | return (ctr); 34 | } 35 | 36 | int main() 37 | { /* test basic workings of stdarg macros */ 38 | Cstruct x = {3}; 39 | 40 | assert(tryit("iisdi", '\1', 2, x, 4.0, 5) == 5); 41 | assert(tryit("") == 0); 42 | assert(tryit("pdp", "\1", 2.0, "\3") == 3); 43 | printf("sizeof (va_list) = %u\n", sizeof (va_list)); 44 | puts("SUCCESS testing "); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /_TEST/TSTDDEF.C: -------------------------------------------------------------------------------- 1 | /* test stddef definitions */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /* type definitions */ 8 | typedef struct { 9 | char f1; 10 | struct { 11 | float flt; 12 | } f2; 13 | int f3; 14 | } Str; 15 | 16 | /* static data */ 17 | static char *pc = NULL; 18 | static size_t offs[] = { 19 | offsetof(Str, f1), 20 | offsetof(Str, f2), 21 | offsetof(Str, f3)}; 22 | 23 | int main() 24 | { /* test basic workings of stddef definitions */ 25 | ptrdiff_t pdiff = &pc[INT_MAX] - &pc[0]; 26 | wchar_t wc = L'Z'; 27 | Str x = {1, 2, 3}; 28 | char *ps = (char *)&x; 29 | 30 | assert(sizeof (size_t) == sizeof (sizeof (char))); 31 | assert(pdiff == &pc[INT_MAX] - &pc[0]); 32 | assert(wc == L'Z'); 33 | assert(offs[0] < offs[1]); 34 | assert(offs[1] < offs[2]); 35 | assert(*(char *)(ps + offs[0]) == 1); 36 | assert(*(float *)(ps + offs[1]) == 2); 37 | assert(*(int *)(ps + offs[2]) == 3); 38 | printf("sizeof (size_t) = %u\n", sizeof (size_t)); 39 | printf("sizeof (wchar_t) = %u\n", sizeof (wchar_t)); 40 | puts("SUCCESS testing "); 41 | return (0); 42 | } 43 |  -------------------------------------------------------------------------------- /MATH/FMOD.C: -------------------------------------------------------------------------------- 1 | /* fmod function */ 2 | #include "xmath.h" 3 | 4 | double (fmod)(double x, double y) 5 | { /* compute fmod(x, y) */ 6 | const short errx = _Dtest(&x); 7 | const short erry = _Dtest(&y); 8 | 9 | if (errx == NAN || erry == NAN || errx == INF || erry == 0) 10 | { /* fmod undefined */ 11 | errno = EDOM; 12 | return (errx == NAN ? x : erry == NAN ? y : _Nan._D); 13 | } 14 | else if (errx == 0 || erry == INF) 15 | return (x); /* fmod(0,nonzero) or fmod(finite,INF) */ 16 | else 17 | { /* fmod(finite,finite) */ 18 | double t; 19 | short n, neg, ychar; 20 | 21 | if (y < 0.0) 22 | y = -y; 23 | if (x < 0.0) 24 | x = -x, neg = 1; 25 | else 26 | neg = 0; 27 | for (t = y, _Dunscale(&ychar, &t), n = 0; ; ) 28 | { /* subtract |y| until |x|<|y| */ 29 | short xchar; 30 | 31 | t = x; 32 | if (n < 0 || _Dunscale(&xchar, &t) == 0 33 | || (n = xchar - ychar) < 0) 34 | return (neg ? -x : x); 35 | for (; 0 <= n; --n) 36 | { /* try to subtract |y|*2^n */ 37 | t = y, _Dscale(&t, n); 38 | if (t <= x) 39 | { 40 | x -= t; 41 | break; 42 | } 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LOCALE/XREADLOC.C: -------------------------------------------------------------------------------- 1 | /* _Readloc function */ 2 | #include 3 | #include "xlocale.h" 4 | 5 | /* static data */ 6 | static const char kc[] = /* keyword chars */ 7 | "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 8 | 9 | _Locitem *_Readloc(FILE *lf, char *buf, const char **ps) 10 | { /* get a line from locale file */ 11 | for (; ; ) 12 | { /* loop until EOF or full line */ 13 | size_t n; 14 | 15 | for (buf[0] = ' ', n = 1; ; n -= 2) 16 | if (fgets(buf + n, MAXLIN - n, lf) == NULL 17 | || buf[(n += strlen(buf + n)) - 1] != '\n') 18 | return (NULL); /* EOF or line too long */ 19 | else if (n <= 1 || buf[n - 2] != '\\') 20 | break; /* continue only if ends in \ */ 21 | buf[n - 1] = '\0'; /* overwrite newline */ 22 | { /* look for keyword on line */ 23 | const char *s = _Skip(buf); 24 | _Locitem *q; 25 | 26 | if (0 < (n = strspn(s, kc))) 27 | for (q = _Loctab; q->_Name; ++q) 28 | if (strncmp(q->_Name, s, n) == 0 29 | && strlen(q->_Name) == n) 30 | { /* found a match */ 31 | *ps = _Skip(s + n - 1); 32 | return (q); 33 | } 34 | return (NULL); /* unknown or missing keyword */ 35 | } 36 | } 37 | } 38 |  -------------------------------------------------------------------------------- /STDLIB/REALLOC.C: -------------------------------------------------------------------------------- 1 | /* realloc function */ 2 | #include 3 | #include "xalloc.h" 4 | 5 | void *(realloc)(void *ptr, size_t size) 6 | { /* reallocate a data object on the heap */ 7 | _Cell *q; 8 | 9 | if (ptr == NULL) 10 | return (malloc(size)); 11 | if (size < SIZE_CELL) /* round up size */ 12 | size = SIZE_CELL; 13 | else if ((size = (size + _MEMBND) & ~_MEMBND) == 0 14 | || size + CELL_OFF < size) 15 | return (0); 16 | q = (_Cell *)((char *)ptr - CELL_OFF); 17 | if (q->_Size < SIZE_CELL || (q->_Size & _MEMBND) != 0) 18 | return (0); /* erroneous call */ 19 | if (size + CELL_OFF <= q->_Size - SIZE_CELL) 20 | { /* free excess space */ 21 | _Cell *const new_q = (_Cell *)((char *)ptr + size); 22 | 23 | new_q->_Size = q->_Size - CELL_OFF - size; 24 | q->_Size = size; 25 | free((char *)new_q + CELL_OFF); 26 | return (ptr); 27 | } 28 | else if (size <= q->_Size) 29 | return (ptr); /* leave cell alone */ 30 | else 31 | { /* try to buy a larger cell */ 32 | char *const new_p = (char *)malloc(size); 33 | 34 | if (new_p == NULL) 35 | return (NULL); 36 | memcpy(new_p, ptr, q->_Size); 37 | free(ptr); 38 | return (new_p); 39 | } 40 | } 41 |  -------------------------------------------------------------------------------- /CTYPE/XCTYPE.C: -------------------------------------------------------------------------------- 1 | /* _Ctype conversion table -- ASCII version */ 2 | #include 3 | #include 4 | #include 5 | #if EOF != -1 || UCHAR_MAX != 255 6 | #error WRONG CTYPE TABLE 7 | #endif 8 | 9 | /* macros */ 10 | #define XDI (_DI|_XD) 11 | #define XLO (_LO|_XD) 12 | #define XUP (_UP|_XD) 13 | 14 | /* static data */ 15 | static const short ctyp_tab[257] = {0, /* EOF */ 16 | 0, _BB, _BB, _BB, _BB, _BB, _BB, _BB, 17 | _BB, _CN, _CN, _CN, _CN, _CN, _BB, _BB, 18 | _BB, _BB, _BB, _BB, _BB, _BB, _BB, _BB, 19 | _BB, _BB, _BB, _BB, _BB, _BB, _BB, _BB, 20 | _SP, _PU, _PU, _PU, _PU, _PU, _PU, _PU, 21 | _PU, _PU, _PU, _PU, _PU, _PU, _PU, _PU, 22 | XDI, XDI, XDI, XDI, XDI, XDI, XDI, XDI, 23 | XDI, XDI, _PU, _PU, _PU, _PU, _PU, _PU, 24 | _PU, XUP, XUP, XUP, XUP, XUP, XUP, _UP, 25 | _UP, _UP, _UP, _UP, _UP, _UP, _UP, _UP, 26 | _UP, _UP, _UP, _UP, _UP, _UP, _UP, _UP, 27 | _UP, _UP, _UP, _PU, _PU, _PU, _PU, _PU, 28 | _PU, XLO, XLO, XLO, XLO, XLO, XLO, _LO, 29 | _LO, _LO, _LO, _LO, _LO, _LO, _LO, _LO, 30 | _LO, _LO, _LO, _LO, _LO, _LO, _LO, _LO, 31 | _LO, _LO, _LO, _PU, _PU, _PU, _PU, _BB, 32 | }; /* rest all match nothing */ 33 | 34 | const short *_Ctype = &ctyp_tab[1]; 35 |  -------------------------------------------------------------------------------- /STDIO/XFOPREP.C: -------------------------------------------------------------------------------- 1 | /* _Foprep function */ 2 | #include "xstdio.h" 3 | 4 | /* open a stream */ 5 | FILE *_Foprep(const char *name, const char *mods, 6 | FILE *str) 7 | { /* make str safe for fclose, macros */ 8 | str->_Handle = -1; 9 | str->_Tmpnam = NULL; 10 | str->_Buf = NULL; 11 | str->_Next = &str->_Cbuf; 12 | str->_Rend = &str->_Cbuf; 13 | str->_Wend = &str->_Cbuf; 14 | str->_Nback = 0; 15 | str->_Mode = (str->_Mode & _MALFIL) 16 | | (*mods == 'r' ? _MOPENR 17 | : *mods == 'w' ? _MCREAT|_MOPENW|_MTRUNC 18 | : *mods == 'a' ? _MCREAT|_MOPENW|_MOPENA 19 | : 0); 20 | if ((str->_Mode & (_MOPENR|_MOPENW)) == 0) 21 | { /* bad mods */ 22 | fclose(str); 23 | return (NULL); 24 | } 25 | while (*++mods== 'b' || *mods == '+') 26 | if (*mods == 'b') 27 | if (str->_Mode & _MBIN) 28 | break; 29 | else 30 | str->_Mode |= _MBIN; 31 | else 32 | if ((str->_Mode & (_MOPENR|_MOPENW)) 33 | == (_MOPENR|_MOPENW)) 34 | break; 35 | else 36 | str->_Mode |= _MOPENR|_MOPENW; 37 | str->_Handle = _Fopen(name, str->_Mode, mods); 38 | if (str->_Handle < 0) 39 | { /* open failed */ 40 | fclose(str); 41 | return (NULL); 42 | } 43 | return (str); 44 | } 45 | -------------------------------------------------------------------------------- /_UNIXVAX/YVALS.H: -------------------------------------------------------------------------------- 1 | /* yvals.h values header -- VAX ULTRIX version */ 2 | #define _YVALS 3 | /* errno properties */ 4 | #define _EDOM 33 5 | #define _ERANGE 34 6 | #define _EFPOS 35 7 | #define _ERRMAX 36 8 | /* float properties */ 9 | #define _D0 0 10 | #define _DBIAS 0x80 11 | #define _DLONG 0 12 | #define _DOFF 7 13 | #define _FBIAS 0x80 14 | #define _FOFF 7 15 | #define _FRND 1 16 | #define _LBIAS 0x80 17 | #define _LOFF 7 18 | /* integer properties */ 19 | #define _C2 1 20 | #define _CSIGN 1 21 | #define _ILONG 1 22 | #define _MBMAX 8 23 | typedef unsigned short _Wchart; 24 | /* pointer properties */ 25 | #define _NULL (void *)0 26 | typedef int _Ptrdifft; 27 | typedef unsigned int _Sizet; 28 | /* setjmp properties */ 29 | #define _NSETJMP 80 30 | int _Setjmp(int *); 31 | /* signal properties */ 32 | #define _SIGABRT 6 33 | #define _SIGMAX 32 34 | /* stdio properties */ 35 | #define _FNAMAX 64 36 | #define _FOPMAX 16 37 | #define _TNAMAX 16 38 | /* stdlib properties */ 39 | #define _EXFAIL 1 40 | /* storage alignment properties */ 41 | #define _AUPBND 3U 42 | #define _ADNBND 3U 43 | #define _MEMBND 7U 44 | /* time properties */ 45 | #define _CPS 1 46 | #define _TBIAS 0 47 |  -------------------------------------------------------------------------------- /STDIO/XLITOB.C: -------------------------------------------------------------------------------- 1 | /* _Litob function */ 2 | #include 3 | #include 4 | #include "xmath.h" 5 | #include "xstdio.h" 6 | 7 | static char ldigs[] = "0123456789abcdef"; 8 | static char udigs[] = "0123456789ABCDEF"; 9 | 10 | void _Litob(_Pft *px, char code) 11 | { /* convert unsigned long to text */ 12 | char ac[24]; /* safe for 64-bit integers */ 13 | char *digs = code == 'X' ? udigs : ldigs; 14 | int base = code == 'o' ? 8 : 15 | code != 'x' && code != 'X' ? 10 : 16; 16 | int i = sizeof (ac); 17 | unsigned long ulval = px->v.li; 18 | 19 | if ((code == 'd' || code == 'i') && px->v.li < 0) 20 | ulval = -ulval; /* safe against overflow */ 21 | if (ulval != 0 || px->prec != 0) 22 | ac[--i] = digs[ulval % base]; 23 | px->v.li = ulval / base; 24 | while (0 < px->v.li && 0 < i) 25 | { /* convert digits */ 26 | ldiv_t qr = ldiv(px->v.li, base); 27 | 28 | px->v.li = qr.quot; 29 | ac[--i] = digs[qr.rem]; 30 | } 31 | px->n1 = sizeof (ac) - i; 32 | memcpy(px->s, &ac[i], px->n1); 33 | if (px->n1 < px->prec) 34 | px->nz0 = px->prec - px->n1; 35 | if (px->prec < 0 && (px->flags & (_FMI|_FZE)) == _FZE 36 | && 0 < (i = px->width - px->n0 - px->nz0 - px->n1)) 37 | px->nz0 += i; 38 | } 39 |  -------------------------------------------------------------------------------- /STDIO/XFGPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fgpos function -- Turbo C++ version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* macros */ 6 | #define NNL(str, m1, m2) ((str)->_Mode & _MBIN ? 0 \ 7 | : _Nnl((str)->m1, (str)->m2)) 8 | 9 | long lseek(int, long, int); /* Turbo C++ system call */ 10 | 11 | int _Nnl(const unsigned char *p1, const unsigned char *p2) 12 | { /* count newlines in a buffer */ 13 | int n; 14 | 15 | for (n = 0; p1 < p2; ++p1) 16 | if (*p1 == '\n') 17 | ++n; 18 | return (n); 19 | } 20 | 21 | long _Fgpos(FILE *str, fpos_t *ptr) 22 | { /* get file position */ 23 | long loff = lseek(str->_Handle, 0L, 1); 24 | 25 | if (!(str->_Mode & (_MOPENR|_MOPENW)) || loff == -1) 26 | { /* query failed */ 27 | errno = EFPOS; 28 | return (EOF); 29 | } 30 | if (str->_Mode & _MWRITE) 31 | loff += str->_Next - str->_Buf + NNL(str, _Buf, _Next); 32 | else if (str->_Mode & _MREAD) 33 | loff -= str->_Nback 34 | ? str->_Rsave - str->_Next + str->_Nback 35 | + NNL(str, _Next, _Rsave) 36 | : str->_Rend - str->_Next + NNL(str, _Next, _Rend); 37 | if (ptr == NULL) 38 | return (loff); /* ftell */ 39 | else 40 | { /* fgetpos */ 41 | ((fpos_t *)ptr)->_Off = loff; 42 | return (0); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /_TC86/XFGPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fgpos function -- Turbo C++ version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* macros */ 6 | #define NNL(str, m1, m2) ((str)->_Mode & _MBIN ? 0 \ 7 | : _Nnl((str)->m1, (str)->m2)) 8 | 9 | long lseek(int, long, int); /* Turbo C++ system call */ 10 | 11 | int _Nnl(const unsigned char *p1, const unsigned char *p2) 12 | { /* count newlines in a buffer */ 13 | int n; 14 | 15 | for (n = 0; p1 < p2; ++p1) 16 | if (*p1 == '\n') 17 | ++n; 18 | return (n); 19 | } 20 | 21 | long _Fgpos(FILE *str, fpos_t *ptr) 22 | { /* get file position */ 23 | long loff = lseek(str->_Handle, 0L, 1); 24 | 25 | if (!(str->_Mode & (_MOPENR|_MOPENW)) || loff == -1) 26 | { /* query failed */ 27 | errno = EFPOS; 28 | return (EOF); 29 | } 30 | if (str->_Mode & _MWRITE) 31 | loff += str->_Next - str->_Buf + NNL(str, _Buf, _Next); 32 | else if (str->_Mode & _MREAD) 33 | loff -= str->_Nback 34 | ? str->_Rsave - str->_Next + str->_Nback 35 | + NNL(str, _Next, _Rsave) 36 | : str->_Rend - str->_Next + NNL(str, _Next, _Rend); 37 | if (ptr == NULL) 38 | return (loff); /* ftell */ 39 | else 40 | { /* fgetpos */ 41 | ((fpos_t *)ptr)->_Off = loff; 42 | return (0); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /_UNIX68K/YVALS.H: -------------------------------------------------------------------------------- 1 | /* yvals.h values header -- UNIX 680X0 version */ 2 | #define _YVALS 3 | /* errno properties */ 4 | #define _EDOM 33 5 | #define _ERANGE 34 6 | #define _EFPOS 35 7 | #define _ERRMAX 36 8 | /* float properties */ 9 | #define _D0 0 10 | #define _DBIAS 0x3fe 11 | #define _DLONG 0 12 | #define _DOFF 4 13 | #define _FBIAS 0x7e 14 | #define _FOFF 7 15 | #define _FRND 1 16 | #define _LBIAS 0x3fe 17 | #define _LOFF 4 18 | /* integer properties */ 19 | #define _C2 1 20 | #define _CSIGN 1 21 | #define _ILONG 1 22 | #define _MBMAX 8 23 | typedef unsigned short _Wchart; 24 | /* pointer properties */ 25 | #define _NULL (void *)0 26 | typedef int _Ptrdifft; 27 | typedef unsigned int _Sizet; 28 | /* setjmp properties */ 29 | #define _NSETJMP 80 30 | extern int _Setjmp(int *); 31 | /* signal properties */ 32 | #define _SIGABRT 6 33 | #define _SIGMAX 32 34 | /* stdio properties */ 35 | #define _FNAMAX 64 36 | #define _FOPMAX 16 37 | #define _TNAMAX 16 38 | /* stdlib properties */ 39 | #define _EXFAIL 1 40 | /* storage alignment properties */ 41 | #define _AUPBND 3U 42 | #define _ADNBND 0U 43 | #define _MEMBND 3U 44 | /* time properties */ 45 | #define _CPS 1 46 | #define _TBIAS ((70 * 365LU + 17) * 86400) 47 |  -------------------------------------------------------------------------------- /_HEADERS/MATH.H: -------------------------------------------------------------------------------- 1 | /* math.h standard header */ 2 | #ifndef _MATH 3 | #define _MATH 4 | /* macros */ 5 | #define HUGE_VAL _Hugeval._D 6 | /* type definitions */ 7 | typedef const union { 8 | unsigned short _W[4]; 9 | double _D; 10 | } _Dconst; 11 | /* declarations */ 12 | double acos(double); 13 | double asin(double); 14 | double atan(double); 15 | double atan2(double, double); 16 | double ceil(double); 17 | double cos(double); 18 | double cosh(double); 19 | double exp(double); 20 | double fabs(double); 21 | double floor(double); 22 | double fmod(double, double); 23 | double frexp(double, int *); 24 | double ldexp(double, int); 25 | double log(double); 26 | double log10(double); 27 | double modf(double, double *); 28 | double pow(double, double); 29 | double sin(double); 30 | double sinh(double); 31 | double sqrt(double); 32 | double tan(double); 33 | double tanh(double); 34 | double _Asin(double, int); 35 | double _Log(double, int); 36 | double _Sin(double, unsigned int); 37 | extern _Dconst _Hugeval; 38 | /* macro overrides */ 39 | #define acos(x) _Asin(x, 1) 40 | #define asin(x) _Asin(x, 0) 41 | #define cos(x) _Sin(x, 1) 42 | #define log(x) _Log(x, 0) 43 | #define log10(x) _Log(x, 1) 44 | #define sin(x) _Sin(x, 0) 45 | #endif 46 | -------------------------------------------------------------------------------- /_HEADERS/LOCALE.H: -------------------------------------------------------------------------------- 1 | /* locale.h standard header */ 2 | #ifndef _LOCALE 3 | #define _LOCALE 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* macros */ 8 | #define NULL _NULL 9 | /* locale codes */ 10 | #define LC_ALL 0 11 | #define LC_COLLATE 1 12 | #define LC_CTYPE 2 13 | #define LC_MONETARY 3 14 | #define LC_NUMERIC 4 15 | #define LC_TIME 5 16 | /* ADD YOURS HERE */ 17 | #define _NCAT 6 /* one more than last */ 18 | /* type definitions */ 19 | struct lconv { 20 | /* controlled by LC_MONETARY */ 21 | char *currency_symbol; 22 | char *int_curr_symbol; 23 | char *mon_decimal_point; 24 | char *mon_grouping; 25 | char *mon_thousands_sep; 26 | char *negative_sign; 27 | char *positive_sign; 28 | char frac_digits; 29 | char int_frac_digits; 30 | char n_cs_precedes; 31 | char n_sep_by_space; 32 | char n_sign_posn; 33 | char p_cs_precedes; 34 | char p_sep_by_space; 35 | char p_sign_posn; 36 | /* controlled by LC_NUMERIC */ 37 | char *decimal_point; 38 | char *grouping; 39 | char *thousands_sep; 40 | }; 41 | /* declarations */ 42 | struct lconv *localeconv(void); 43 | char *setlocale(int, const char *); 44 | extern struct lconv _Locale; 45 | /* macro overrides */ 46 | #define localeconv() (&_Locale) 47 | #endif 48 | -------------------------------------------------------------------------------- /STDIO/GETS.C: -------------------------------------------------------------------------------- 1 | /* gets function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | char *(gets)(char *buf) 6 | { /* get a line from stdio */ 7 | unsigned char *s; 8 | 9 | for (s = (unsigned char *)buf; 0 < stdin->_Nback; ) 10 | { /* deliver pushed back chars */ 11 | *s = stdin->_Back[--stdin->_Nback]; 12 | if (stdin->_Nback == 0) 13 | stdin->_Rend = stdin->_Rsave; 14 | if (*s++ == '\n') 15 | { /* terminate full line */ 16 | s[-1] = '\0'; 17 | return (buf); 18 | } 19 | } 20 | for (; ; ) 21 | { /* ensure chars in buffer */ 22 | if (stdin->_Next < stdin->_Rend) 23 | ; 24 | else if (_Frprep(stdin) < 0) 25 | return (NULL); 26 | else if (stdin->_Mode & _MEOF) 27 | break; 28 | { /* deliver as many as possible */ 29 | unsigned char *s1 30 | = (unsigned char *)memchr(stdin->_Next, '\n', 31 | stdin->_Rend - stdin->_Next); 32 | size_t m = (s1 ? s1 + 1 : stdin->_Rend) 33 | - stdin->_Next; 34 | 35 | memcpy(s, stdin->_Next, m); 36 | s += m; stdin->_Next += m; 37 | if (s1) 38 | { /* terminate full line */ 39 | s[-1] = '\0'; 40 | return (buf); 41 | } 42 | } 43 | } 44 | if (s == (unsigned char *)buf) 45 | return (NULL); 46 | else 47 | { /* terminate partial line */ 48 | *s = '\0'; 49 | return (buf); 50 | } 51 | } 52 |  -------------------------------------------------------------------------------- /MATH/TANH.C: -------------------------------------------------------------------------------- 1 | /* tanh function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients, after Cody & Waite, Chapter 13 */ 5 | static const double p[3] = { 6 | -0.96437492777225469787e+0, 7 | -0.99225929672236083313e+2, 8 | -0.16134119023996228053e+4}; 9 | static const double q[4] = { 10 | 0.10000000000000000000e+1, 11 | 0.11274474380534949335e+3, 12 | 0.22337720718962312926e+4, 13 | 0.48402357071988688686e+4}; 14 | static const double ln3by2 = 0.54930614433405484570; 15 | 16 | double (tanh)(double x) 17 | { /* compute tanh(x) */ 18 | switch (_Dtest(&x)) 19 | { /* test for special codes */ 20 | case NAN: 21 | errno = EDOM; 22 | return (x); 23 | case INF: 24 | return (DSIGN(x) ? -1.0 : 1.0); 25 | case 0: 26 | return (0.0); 27 | default: /* finite */ 28 | { /* compute tanh(finite) */ 29 | short neg; 30 | 31 | if (x < 0.0) 32 | x = -x, neg = 1; 33 | else 34 | neg = 0; 35 | if (x < _Rteps._D) 36 | ; /* x tiny */ 37 | else if (x < ln3by2) 38 | { /* |x| < ln(3)/2 */ 39 | const double g = x * x; 40 | 41 | x += x * g * ((p[0] * g + p[1]) * g + p[2]) 42 | / (((q[0] * g + q[1]) * g + q[2]) * g + q[3]); 43 | } 44 | else if (_Exp(&x, 0) < 0) 45 | x = 1.0 - 2.0 / (x * x + 1.0); 46 | else 47 | x = 1.0; /* x large */ 48 | return (neg ? -x : x); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /STDLIB/FREE.C: -------------------------------------------------------------------------------- 1 | /* free function */ 2 | #include "xalloc.h" 3 | 4 | void (free)(void *ptr) 5 | { /* free an allocated data object */ 6 | _Cell *q; 7 | 8 | if (ptr == NULL) 9 | return; 10 | q = (_Cell *)((char *)ptr - CELL_OFF); 11 | if (q->_Size < SIZE_CELL || (q->_Size & _MEMBND) != 0) 12 | return; /* erroneous call */ 13 | _Aldata._Plast = 0; 14 | if (_Aldata._Head == NULL || q < _Aldata._Head) 15 | { /* insert at head of list */ 16 | q->_Next = _Aldata._Head; 17 | _Aldata._Head = q; 18 | } 19 | else 20 | { /* scan for insertion point */ 21 | _Cell *qp; 22 | char *qpp; 23 | 24 | for (qp = _Aldata._Head; 25 | qp->_Next != NULL && qp->_Next < q; ) 26 | qp = qp->_Next; 27 | qpp = (char *)qp + CELL_OFF + qp->_Size; 28 | if ((char *)q < qpp) 29 | return; /* erroneous call */ 30 | else if ((char *)q == qpp) 31 | { /* merge qp and q */ 32 | qp->_Size += CELL_OFF + q->_Size; 33 | q = qp; 34 | } 35 | else 36 | { /* splice q after qp and resume scan with q */ 37 | q->_Next = qp->_Next; 38 | qp->_Next = q; 39 | _Aldata._Plast = &qp->_Next; 40 | } 41 | } 42 | if (q->_Next 43 | && (char *)q + CELL_OFF + q->_Size == (char *)q->_Next) 44 | { /* merge q and q->_Next */ 45 | q->_Size += CELL_OFF + q->_Next->_Size; 46 | q->_Next = q->_Next->_Next; 47 | } 48 | } 49 |  -------------------------------------------------------------------------------- /MATH/SINH.C: -------------------------------------------------------------------------------- 1 | /* sinh function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients, after Cody & Waite, Chapter 12 */ 5 | static const double p[4] = { 6 | -0.78966127417357099479e+0, 7 | -0.16375798202630751372e+3, 8 | -0.11563521196851768270e+5, 9 | -0.35181283430177117881e+6}; 10 | static const double q[4] = { 11 | 1.0, 12 | -0.27773523119650701667e+3, 13 | 0.36162723109421836460e+5, 14 | -0.21108770058106271242e+7}; 15 | 16 | double (sinh)(double x) 17 | { /* compute sinh(x) */ 18 | switch (_Dtest(&x)) 19 | { /* test for special codes */ 20 | case NAN: 21 | errno = EDOM; 22 | return (x); 23 | case INF: 24 | errno = ERANGE; 25 | return (DSIGN(x) ? -_Inf._D : _Inf._D); 26 | case 0: 27 | return (0.0); 28 | default: /* finite */ 29 | { /* compute sinh(finite) */ 30 | short neg; 31 | 32 | if (x < 0.0) 33 | x = -x, neg = 1; 34 | else 35 | neg = 0; 36 | if (x < _Rteps._D) 37 | ; /* x tiny */ 38 | else if (x < 1.0) 39 | { /* |x| < 1 */ 40 | const double y = x * x; 41 | 42 | x += x * y 43 | * (((p[0] * y + p[1]) * y + p[2]) * y + p[3]) 44 | / (((q[0] * y + q[1]) * y + q[2]) * y + q[3]); 45 | } 46 | else if (0 <= _Exp(&x, -1)) 47 | errno = ERANGE; /* x large */ 48 | else if (x < _Xbig._D) 49 | x -= 0.25 / x; 50 | return (neg ? -x : x); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /_HEADERS/YVALS.H: -------------------------------------------------------------------------------- 1 | /* yvals.h values header -- Turbo C++ version */ 2 | #define _YVALS 3 | /* errno properties */ 4 | #define _EDOM 33 5 | #define _ERANGE 34 6 | #define _EFPOS 35 7 | #define _ERRMAX 36 8 | /* float properties */ 9 | #define _D0 3 10 | #define _DBIAS 0x3fe 11 | #define _DLONG 1 12 | #define _DOFF 4 13 | #define _FBIAS 0x7e 14 | #define _FOFF 7 15 | #define _FRND 1 16 | #define _LBIAS 0x3ffe 17 | #define _LOFF 15 18 | /* integer properties */ 19 | #define _C2 1 20 | #define _CSIGN 1 21 | #define _ILONG 0 22 | #define _MBMAX 8 23 | typedef unsigned short _Wchart; 24 | /* pointer properties */ 25 | #define _NULL (void *)0 26 | typedef int _Ptrdifft; 27 | typedef unsigned int _Sizet; 28 | /* setjmp properties */ 29 | #define _NSETJMP 10 30 | #define _Setjmp(env) setjmp(env) 31 | #ifdef __cplusplus 32 | extern "C" int setjmp(int *); 33 | #else 34 | int setjmp(int *); 35 | #endif 36 | /* signal properties */ 37 | #define _SIGABRT 22 38 | #define _SIGMAX 32 39 | /* stdio properties */ 40 | #define _FNAMAX 64 41 | #define _FOPMAX 16 42 | #define _TNAMAX 16 43 | /* stdlib properties */ 44 | #define _EXFAIL 1 45 | /* storage alignment properties */ 46 | #define _AUPBND 1U 47 | #define _ADNBND 1U 48 | #define _MEMBND 1U 49 | /* time properties */ 50 | #define _CPS 1 51 | #define _TBIAS ((70 * 365LU + 17) * 86400) 52 |  -------------------------------------------------------------------------------- /_TC86/YVALS.H: -------------------------------------------------------------------------------- 1 | /* yvals.h values header -- Turbo C++ version */ 2 | #define _YVALS 3 | /* errno properties */ 4 | #define _EDOM 33 5 | #define _ERANGE 34 6 | #define _EFPOS 35 7 | #define _ERRMAX 36 8 | /* float properties */ 9 | #define _D0 3 10 | #define _DBIAS 0x3fe 11 | #define _DLONG 1 12 | #define _DOFF 4 13 | #define _FBIAS 0x7e 14 | #define _FOFF 7 15 | #define _FRND 1 16 | #define _LBIAS 0x3ffe 17 | #define _LOFF 15 18 | /* integer properties */ 19 | #define _C2 1 20 | #define _CSIGN 1 21 | #define _ILONG 0 22 | #define _MBMAX 8 23 | typedef unsigned short _Wchart; 24 | /* pointer properties */ 25 | #define _NULL (void *)0 26 | typedef int _Ptrdifft; 27 | typedef unsigned int _Sizet; 28 | /* setjmp properties */ 29 | #define _NSETJMP 10 30 | #define _Setjmp(env) setjmp(env) 31 | #ifdef __cplusplus 32 | extern "C" int setjmp(int *); 33 | #else 34 | int setjmp(int *); 35 | #endif 36 | /* signal properties */ 37 | #define _SIGABRT 22 38 | #define _SIGMAX 32 39 | /* stdio properties */ 40 | #define _FNAMAX 64 41 | #define _FOPMAX 16 42 | #define _TNAMAX 16 43 | /* stdlib properties */ 44 | #define _EXFAIL 1 45 | /* storage alignment properties */ 46 | #define _AUPBND 1U 47 | #define _ADNBND 1U 48 | #define _MEMBND 1U 49 | /* time properties */ 50 | #define _CPS 1 51 | #define _TBIAS ((70 * 365LU + 17) * 86400) 52 |  -------------------------------------------------------------------------------- /STDIO/XFRPREP.C: -------------------------------------------------------------------------------- 1 | /* _Frprep function */ 2 | #include 3 | #include "xstdio.h" 4 | #include "yfuns.h" 5 | 6 | int _Frprep(FILE *str) 7 | { /* prepare stream for reading */ 8 | if (str->_Next < str->_Rend) 9 | return (1); 10 | else if (str->_Mode & _MEOF) 11 | return (0); 12 | else if ((str->_Mode & (_MOPENR|_MWRITE)) != _MOPENR) 13 | { /*can't read after write */ 14 | str->_Mode |= _MERR; 15 | return (-1); 16 | } 17 | if (str->_Buf) 18 | ; 19 | else if ((str->_Buf = (unsigned char *)malloc(BUFSIZ)) 20 | == NULL) 21 | { /* use 1-char _Cbuf */ 22 | str->_Buf = &str->_Cbuf; 23 | str->_Bend = str->_Buf + 1; 24 | } 25 | else 26 | { /* set up allocated buffer */ 27 | str->_Mode |= _MALBUF; 28 | str->_Bend = str->_Buf + BUFSIZ; 29 | } 30 | str->_Next = str->_Buf; 31 | str->_Rend = str->_Buf; 32 | str->_Wend = str->_Buf; 33 | { /* try to read into buffer */ 34 | int n = _Fread(str, str->_Buf, str->_Bend - str->_Buf); 35 | 36 | if (n < 0) 37 | { /* report error and fail */ 38 | str->_Mode |= _MERR; 39 | return (-1); 40 | } 41 | else if (n == 0) 42 | { /* report end of file */ 43 | str->_Mode = (str->_Mode & ~_MREAD) | _MEOF; 44 | return (0); 45 | } 46 | else 47 | { /* set up data read */ 48 | str->_Mode |= _MREAD; 49 | str->_Rend += n; 50 | return (1); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /_HEADERS/XMATH.H: -------------------------------------------------------------------------------- 1 | /* xmath.h internal header -- IEEE 754 version */ 2 | #include 3 | #include 4 | #include 5 | #ifndef _YVALS 6 | #include 7 | #endif 8 | /* IEEE 754 properties */ 9 | #define _DFRAC ((1<<_DOFF)-1) 10 | #define _DMASK (0x7fff&~_DFRAC) 11 | #define _DMAX ((1<<(15-_DOFF))-1) 12 | #define _DNAN (0x8000|_DMAX<<_DOFF|1<<(_DOFF-1)) 13 | #define _DSIGN 0x8000 14 | #define DSIGN(x) (((unsigned short *)&(x))[_D0] & _DSIGN) 15 | #define HUGE_EXP (int)(_DMAX * 900L / 1000) 16 | #define HUGE_RAD 3.37e9 17 | #define SAFE_EXP (_DMAX>>1) 18 | /* word offsets within double */ 19 | #if _D0==3 20 | #define _D1 2 /* little-endian order */ 21 | #define _D2 1 22 | #define _D3 0 23 | #else 24 | #define _D1 1 /* big-endian order */ 25 | #define _D2 2 26 | #define _D3 3 27 | #endif 28 | /* return values for _D functions */ 29 | #define FINITE -1 30 | #define INF 1 31 | #define NAN 2 32 | /* declarations */ 33 | double _Atan(double, unsigned short); 34 | short _Dint(double *, short); 35 | short _Dnorm(unsigned short *); 36 | short _Dscale(double *, short); 37 | double _Dtento(double, short); 38 | short _Dtest(double *); 39 | short _Dunscale(short *, double *); 40 | short _Exp(double *, short); 41 | short _Ldunscale(short *, long double *); 42 | double _Poly(double, const double *, int); 43 | extern _Dconst _Inf, _Nan, _Rteps, _Xbig; 44 |  -------------------------------------------------------------------------------- /SIGNAL/RAISE.C: -------------------------------------------------------------------------------- 1 | /* raise function -- simple version */ 2 | #include 3 | #include 4 | #include 5 | 6 | /* static data */ 7 | _Sigfun *_Sigtable[_NSIG] = {0}; /* handler table */ 8 | 9 | int (raise)(int sig) 10 | { /* raise a signal */ 11 | _Sigfun *s; 12 | 13 | if (sig <= 0 || _NSIG <= sig) 14 | return (-1); /* bad signal */ 15 | if ((s = _Sigtable[sig]) != SIG_IGN && s != SIG_DFL) 16 | { /* revert and call handler */ 17 | _Sigtable[sig] = SIG_DFL; 18 | (*s)(sig); 19 | } 20 | else if (s == SIG_DFL) 21 | { /* default handling */ 22 | char ac[10], *p; 23 | 24 | switch (sig) 25 | { /* print known signals by name */ 26 | case SIGABRT: 27 | p = "abort"; 28 | break; 29 | case SIGFPE: 30 | p = "arithmetic error"; 31 | break; 32 | case SIGILL: 33 | p = "invalid executable code"; 34 | break; 35 | case SIGINT: 36 | p = "interruption"; 37 | break; 38 | case SIGSEGV: 39 | p = "invalid storage access"; 40 | break; 41 | case SIGTERM: 42 | p = "termination request"; 43 | break; 44 | default: 45 | *(p = &ac[(sizeof ac) - 1]) = '\0'; 46 | do *--p = sig % 10 + '0'; 47 | while ((sig /= 10) != 0); 48 | fputs("signal #", stderr); 49 | } 50 | fputs(p, stderr); 51 | fputs(" -- terminating\n", stderr); 52 | exit(EXIT_FAILURE); 53 | } 54 | return (0); 55 | } 56 |  -------------------------------------------------------------------------------- /STDIO/FGETS.C: -------------------------------------------------------------------------------- 1 | /* fgets function */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | char *(fgets)(char *buf, int n, FILE *str) 6 | { /* get a line from stream */ 7 | unsigned char *s; 8 | 9 | if (n <= 1) 10 | return (NULL); 11 | for (s = (unsigned char *)buf; 0 < --n && str->_Nback; ) 12 | { /* deliver pushed back chars */ 13 | *s = str->_Back[--str->_Nback]; 14 | if (str->_Nback == 0) 15 | str->_Rend = str->_Rsave; 16 | if (*s++ == '\n') 17 | { /* terminate full line */ 18 | *s = '\0'; 19 | return (buf); 20 | } 21 | } 22 | while (0 < n) 23 | { /* ensure buffer has chars */ 24 | if (str->_Next < str->_Rend) 25 | ; 26 | else if (_Frprep(str) < 0) 27 | return (NULL); 28 | else if (str->_Mode & _MEOF) 29 | break; 30 | { /* copy as many as possible */ 31 | unsigned char *s1 = (unsigned char *)memchr(str->_Next, 32 | '\n', str->_Rend - str->_Next); 33 | size_t m = (s1 ? s1 + 1 : str->_Rend) - str->_Next; 34 | 35 | if (n < m) 36 | s1 = NULL, m = n; 37 | memcpy(s, str->_Next, m); 38 | s += m, n -= m; 39 | str->_Next += m; 40 | if (s1) 41 | { /* terminate full line */ 42 | *s = '\0'; 43 | return (buf); 44 | } 45 | } 46 | } 47 | if (s == (unsigned char *)buf) 48 | return (NULL); 49 | else 50 | { /* terminate partial line */ 51 | *s = '\0'; 52 | return (buf); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /STDIO/XFSPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fspos function -- Turbo C++ version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* macros */ 6 | #define NNL(str, m1, m2) ((str)->_Mode & _MBIN ? 0 \ 7 | : _Nnl((str)->m1, (str)->m2)) 8 | 9 | long lseek(int, long, int); /* Turbo C++ system call */ 10 | int _Nnl(const unsigned char *, const unsigned char *); 11 | 12 | int _Fspos(FILE *str, const fpos_t *ptr, long loff, int way) 13 | { /* position a file */ 14 | if (!(str->_Mode & (_MOPENR|_MOPENW)) || fflush(str)) 15 | { /* not-open or write error */ 16 | errno = EFPOS; 17 | return (EOF); 18 | } 19 | if (ptr) 20 | loff += ptr->_Off; /* fsetpos */ 21 | if (way == SEEK_CUR && str->_Mode & _MREAD) 22 | loff -= str->_Nback 23 | ? str->_Rsave - str->_Next + str->_Nback 24 | + NNL(str, _Next, _Rsave) 25 | : str->_Rend - str->_Next + NNL(str, _Next, _Rend); 26 | if (way == SEEK_CUR && loff != 0 27 | || way != SEEK_SET || loff != -1) 28 | loff = lseek(str->_Handle, loff, way); 29 | if (loff == -1) 30 | { /* request failed */ 31 | errno = EFPOS; 32 | return (EOF); 33 | } 34 | else 35 | { /* success */ 36 | if (str->_Mode & (_MREAD|_MWRITE)) 37 | { /* empty buffer */ 38 | str->_Next = str->_Buf; 39 | str->_Rend = str->_Buf; 40 | str->_Wend = str->_Buf; 41 | str->_Nback = 0; 42 | } 43 | str->_Mode &= ~(_MEOF|_MREAD|_MWRITE); 44 | return (0); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /_TC86/XFSPOS.C: -------------------------------------------------------------------------------- 1 | /* _Fspos function -- Turbo C++ version */ 2 | #include 3 | #include "xstdio.h" 4 | 5 | /* macros */ 6 | #define NNL(str, m1, m2) ((str)->_Mode & _MBIN ? 0 \ 7 | : _Nnl((str)->m1, (str)->m2)) 8 | 9 | long lseek(int, long, int); /* Turbo C++ system call */ 10 | int _Nnl(const unsigned char *, const unsigned char *); 11 | 12 | int _Fspos(FILE *str, const fpos_t *ptr, long loff, int way) 13 | { /* position a file */ 14 | if (!(str->_Mode & (_MOPENR|_MOPENW)) || fflush(str)) 15 | { /* not-open or write error */ 16 | errno = EFPOS; 17 | return (EOF); 18 | } 19 | if (ptr) 20 | loff += ptr->_Off; /* fsetpos */ 21 | if (way == SEEK_CUR && str->_Mode & _MREAD) 22 | loff -= str->_Nback 23 | ? str->_Rsave - str->_Next + str->_Nback 24 | + NNL(str, _Next, _Rsave) 25 | : str->_Rend - str->_Next + NNL(str, _Next, _Rend); 26 | if (way == SEEK_CUR && loff != 0 27 | || way != SEEK_SET || loff != -1) 28 | loff = lseek(str->_Handle, loff, way); 29 | if (loff == -1) 30 | { /* request failed */ 31 | errno = EFPOS; 32 | return (EOF); 33 | } 34 | else 35 | { /* success */ 36 | if (str->_Mode & (_MREAD|_MWRITE)) 37 | { /* empty buffer */ 38 | str->_Next = str->_Buf; 39 | str->_Rend = str->_Buf; 40 | str->_Wend = str->_Buf; 41 | str->_Nback = 0; 42 | } 43 | str->_Mode &= ~(_MEOF|_MREAD|_MWRITE); 44 | return (0); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /_HEADERS/STRING.H: -------------------------------------------------------------------------------- 1 | /* string.h standard header */ 2 | #ifndef _STRING 3 | #define _STRING 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* macros */ 8 | #define NULL _NULL 9 | /* type definitions */ 10 | #ifndef _SIZET 11 | #define _SIZET 12 | typedef _Sizet size_t; 13 | #endif 14 | /* declarations */ 15 | void *memchr(const void *, int, size_t); 16 | int memcmp(const void *, const void *, size_t); 17 | void *memcpy(void *, const void *, size_t); 18 | void *memmove(void *, const void *, size_t); 19 | void *memset(void *, int, size_t); 20 | char *strcat(char *, const char *); 21 | char *strchr(const char *, int); 22 | int strcmp(const char *, const char *); 23 | int strcoll(const char *, const char *); 24 | char *strcpy(char *, const char *); 25 | size_t strcspn(const char *, const char *); 26 | char *strerror(int); 27 | size_t strlen(const char *); 28 | char *strncat(char *, const char *, size_t); 29 | int strncmp(const char *, const char *, size_t); 30 | char *strncpy(char *, const char *, size_t); 31 | char *strpbrk(const char *, const char *); 32 | char *strrchr(const char *, int); 33 | size_t strspn(const char *, const char *); 34 | char *strstr(const char *, const char *); 35 | char *strtok(char *, const char *); 36 | size_t strxfrm(char *, const char *, size_t); 37 | char *_Strerror(int, char *); 38 | /* macro overrides */ 39 | #define strerror(errcode) _Strerror(errcode, _NULL) 40 | #endif 41 | -------------------------------------------------------------------------------- /TIME/XGETZONE.C: -------------------------------------------------------------------------------- 1 | /* _Getzone function */ 2 | #include 3 | #include 4 | #include 5 | #include "xtime.h" 6 | 7 | /* static data */ 8 | static const char *defzone = ":UTC:UTC:0"; 9 | static char *tzone = NULL; 10 | 11 | static char *reformat(const char *s) 12 | { /* reformat TZ */ 13 | int i, val; 14 | static char tzbuf[] = ":EST:EDT:+0300"; 15 | 16 | for (i = 1; i <= 3; ++i) 17 | if (isalpha(*s)) 18 | tzbuf[i] = *s, tzbuf[i + 4] = *s++; 19 | else 20 | return (NULL); 21 | tzbuf[9] = *s == '-' || *s == '+' ? *s++ : '+'; 22 | if (!isdigit(*s)) 23 | return (NULL); 24 | val = *s++ - '0'; 25 | if (isdigit(*s)) 26 | val = 10 * val + *s++ - '0'; 27 | for (val *= 60, i = 13; 10 <= i; --i, val /= 10) 28 | tzbuf[i] = val % 10 + '0'; 29 | if (isalpha(*s)) 30 | for (i = 5; i <= 7; ++i) 31 | if (isalpha(*s)) 32 | tzbuf[i] = *s++; 33 | else 34 | return (NULL); 35 | return (*s == '\0' ? tzbuf : NULL); 36 | } 37 | 38 | const char *_Getzone(void) 39 | { /* get time zone information */ 40 | const char *s; 41 | 42 | if (tzone) 43 | ; 44 | else if ((s = getenv("TIMEZONE")) != NULL) 45 | { /* copy desired format */ 46 | if ((tzone = (char *)malloc(strlen(s) + 1)) != NULL) 47 | strcpy(tzone, s); 48 | } 49 | else if ((s = getenv("TZ")) != NULL) 50 | tzone = reformat(s); 51 | if (tzone == NULL) 52 | tzone = (char *)defzone; 53 | return (tzone); 54 | } 55 | -------------------------------------------------------------------------------- /MATH/XATAN.C: -------------------------------------------------------------------------------- 1 | /* _Atan function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients, after Cody & Waite, Chapter 11 */ 5 | static const double a[8] = { 6 | 0.0, 7 | 0.52359877559829887308, 8 | 1.57079632679489661923, 9 | 1.04719755119659774615, 10 | 1.57079632679489661923, 11 | 2.09439510239319549231, 12 | 3.14159265358979323846, 13 | 2.61799387799149436538}; 14 | static const double p[4] = { 15 | -0.83758299368150059274e+0, 16 | -0.84946240351320683534e+1, 17 | -0.20505855195861651981e+2, 18 | -0.13688768894191926929e+2}; 19 | static const double q[5] = { 20 | 0.10000000000000000000e+1, 21 | 0.15024001160028576121e+2, 22 | 0.59578436142597344465e+2, 23 | 0.86157349597130242515e+2, 24 | 0.41066306682575781263e+2}; 25 | static const double fold = 0.26794919243112270647; 26 | static const double sqrt3 = 1.73205080756887729353; 27 | static const double sqrt3m1 = 0.73205080756887729353; 28 | 29 | double _Atan(double x, unsigned short idx) 30 | { /* compute atan(x), 0 <= x <= 1.0 */ 31 | if (fold < x) 32 | { /* 2-sqrt(3) < x */ 33 | x = (((sqrt3m1 * x - 0.5) - 0.5) + x) / (sqrt3 + x); 34 | idx |= 0x1; 35 | } 36 | if (x < -_Rteps._D || _Rteps._D < x) 37 | { /* x*x worth computing */ 38 | const double g = x * x; 39 | 40 | x += x * g / _Poly(g, q, 4) 41 | * (((p[0] * g + p[1]) * g + p[2]) * g + p[3]); 42 | } 43 | if (idx & 0x2) 44 | x = -x; 45 | x += a[idx & 07]; 46 | return (idx & 0x8 ? -x : x); 47 | } 48 | -------------------------------------------------------------------------------- /STDLIB/XWCTOMB.C: -------------------------------------------------------------------------------- 1 | /* _Wctomb function */ 2 | #include 3 | #include 4 | #include "xstate.h" 5 | 6 | int _Wctomb(char *s, wchar_t wcin, char *ps) 7 | { /* translate widechar to multibyte */ 8 | static const char initial = 0; 9 | 10 | if (s == NULL) 11 | { /* set initial state */ 12 | *ps = initial; 13 | return (_Mbstate._Tab[0][0] & ST_STATE); 14 | } 15 | { /* run finite state machine */ 16 | char state = *ps; 17 | int leave = 0; 18 | int limit = 0; 19 | int nout = 0; 20 | unsigned short wc = wcin; 21 | 22 | for (; ; ) 23 | { /* perform a state transformation */ 24 | unsigned short code; 25 | const unsigned short *stab; 26 | 27 | if (_NSTATE <= state 28 | || (stab = _Wcstate._Tab[state]) == NULL 29 | || MB_CUR_MAX <= nout 30 | || (_NSTATE*UCHAR_MAX) <= ++limit 31 | || (code = stab[wc & UCHAR_MAX]) == 0) 32 | break; 33 | state = (code & ST_STATE) >> ST_STOFF; 34 | if (code & ST_FOLD) 35 | wc = wc & ~UCHAR_MAX | code & ST_CH; 36 | if (code & ST_ROTATE) 37 | wc = wc >> CHAR_BIT & UCHAR_MAX | wc << CHAR_BIT; 38 | if (code & ST_OUTPUT) 39 | { /* produce an output char */ 40 | if ((s[nout++] = code & ST_CH ? code : wc) == '\0') 41 | leave = 1; 42 | limit = 0; 43 | } 44 | if (code & ST_INPUT || leave) 45 | { /* consume input */ 46 | *ps = state; 47 | return (nout); 48 | } 49 | } 50 | *ps = _NSTATE; 51 | return (-1); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /STRING/XSTRXFRM.C: -------------------------------------------------------------------------------- 1 | /* _Strxfrm function */ 2 | #include 3 | #include "xstrxfrm.h" 4 | 5 | size_t _Strxfrm(char *sout, const unsigned char **psin, 6 | size_t size, _Cosave *ps) 7 | { /* translate string to collatable form */ 8 | unsigned char state = ps->_State; 9 | int leave = 0; 10 | int limit = 0; 11 | int nout = 0; 12 | const unsigned char *sin = *psin; 13 | unsigned short wc = ps->_Wchar; 14 | 15 | for (; ; ) 16 | { /* perform a state transformation */ 17 | unsigned short code; 18 | const unsigned short *stab; 19 | 20 | if (_NSTATE <= state 21 | || (stab = _Costate._Tab[state]) == NULL 22 | || (_NSTATE*UCHAR_MAX) <= ++limit 23 | || (code = stab[*sin]) == 0) 24 | break; 25 | state = (code & ST_STATE) >> ST_STOFF; 26 | if (code & ST_FOLD) 27 | wc = wc & ~UCHAR_MAX | code & ST_CH; 28 | if (code & ST_ROTATE) 29 | wc = wc >> CHAR_BIT & UCHAR_MAX | wc << CHAR_BIT; 30 | if (code & ST_OUTPUT && ((sout[nout++] 31 | = code & ST_CH ? code : wc) == '\0' 32 | || size <= nout)) 33 | leave = 1; 34 | if (code & ST_INPUT) 35 | if (*sin != '\0') 36 | ++sin, limit = 0; 37 | else 38 | leave = 1; 39 | if (leave) 40 | { /* return for now */ 41 | *psin = sin; 42 | ps->_State = state; 43 | ps->_Wchar = wc; 44 | return (nout); 45 | } 46 | } 47 | sout[nout++] = '\0'; /* error return */ 48 | *psin = sin; 49 | ps->_State = _NSTATE; 50 | return (nout); 51 | } 52 |  -------------------------------------------------------------------------------- /STDIO/XGETFLOA.C: -------------------------------------------------------------------------------- 1 | /* _Getfloat function */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "xstdio.h" 7 | 8 | int _Getfloat(_Sft *px) 9 | { /* get a floating point value for _Scanf */ 10 | char *p; 11 | int ch; 12 | char ac[FMAX+1]; 13 | char seendig = 0; 14 | 15 | px->nget = px->width <= 0 16 | || FMAX < px->width ? FMAX : px->width; 17 | p = ac, ch = GETN(px); 18 | if (ch == '+' || ch == '-') 19 | *p++ = ch, ch = GETN(px); 20 | for (; 0 <= ch && isdigit(ch); seendig = 1) 21 | *p++ = ch, ch = GETN(px); 22 | if (ch == localeconv()->decimal_point[0]) 23 | *p++ = ch, ch = GETN(px); 24 | for (; 0 <= ch && isdigit(ch); seendig = 1) 25 | *p++ = ch, ch = GETN(px); 26 | if ((ch == 'e' || ch == 'E') && seendig) 27 | { /* parse exponent */ 28 | *p++ = ch, ch = GETN(px); 29 | if (ch == '+' || ch == '-') 30 | *p++ = ch, ch = GETN(px); 31 | for (seendig = 0; 0 <= ch && isdigit(ch); seendig = 1) 32 | *p++ = ch, ch = GETN(px); 33 | } 34 | UNGETN(px, ch); 35 | if (!seendig) 36 | return (p == ac && ch < 0 ? EOF : 0); 37 | *p = '\0'; 38 | if (!px->noconv) 39 | { /* convert and store */ 40 | double dval = strtod(ac, NULL); 41 | 42 | px->stored = 1; 43 | if (px->qual == 'l') 44 | *va_arg(px->ap, double *) = dval; 45 | else if (px->qual != 'L') 46 | *va_arg(px->ap, float *) = dval; 47 | else 48 | *va_arg(px->ap, long double *) = dval; 49 | } 50 | return (1); 51 | } 52 |  -------------------------------------------------------------------------------- /TIME/XSTRFTIM.C: -------------------------------------------------------------------------------- 1 | /* _Strftime function */ 2 | #include 3 | #include 4 | #include "xtime.h" 5 | 6 | /* macros */ 7 | #define PUT(s, na) (void)(nput = (na), \ 8 | 0 < nput && (nchar += nput) <= bufsize ? \ 9 | (memcpy(buf, s, nput), buf += nput) : 0) 10 | 11 | size_t _Strftime(char *buf, size_t bufsize, const char *fmt, 12 | const struct tm *t, _Tinfo *tin) 13 | { /* format time information */ 14 | const char *fmtsav, *s; 15 | size_t len, lensav, nput; 16 | size_t nchar = 0; 17 | 18 | for (s = fmt, len = strlen(fmt), fmtsav = NULL; ; fmt = s) 19 | { /* parse format string */ 20 | int n; 21 | wchar_t wc; 22 | _Mbsave state = {0}; 23 | 24 | while (0 < (n = _Mbtowc(&wc, s, len, &state))) 25 | { /* scan for '%' or '\0' */ 26 | s += n, len -= n; 27 | if (wc == '%') 28 | break; 29 | } 30 | if (fmt < s) /* copy any literal text */ 31 | PUT(fmt, s - fmt - (0 < n ? 1 : 0)); 32 | if (0 < n) 33 | { /* do the conversion */ 34 | char ac[20]; 35 | int m; 36 | const char *p = _Gentime(t, tin, *s, &m, ac); 37 | 38 | ++s, --len; 39 | if (0 <= m) 40 | PUT(p, m); 41 | else if (fmtsav == NULL) 42 | fmtsav = s, s = p, lensav = len, len = -m; 43 | } 44 | if (0 == len && fmtsav == NULL || n < 0) 45 | { /* format end or bad multibyte char */ 46 | PUT("", 1); /* null termination */ 47 | return (nchar <= bufsize ? nchar - 1 : 0); 48 | } 49 | else if (0 == len) 50 | s = fmtsav, fmtsav = NULL, len = lensav; 51 | } 52 | } 53 |  -------------------------------------------------------------------------------- /MATH/XEXP.C: -------------------------------------------------------------------------------- 1 | /* _Exp function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients, after Cody & Waite, Chapter 6 */ 5 | static const double p[3] = { 6 | 0.31555192765684646356e-4, 7 | 0.75753180159422776666e-2, 8 | 0.25000000000000000000e+0}; 9 | static const double q[4] = { 10 | 0.75104028399870046114e-6, 11 | 0.63121894374398503557e-3, 12 | 0.56817302698551221787e-1, 13 | 0.50000000000000000000e+0}; 14 | static const double c1 = 22713.0 / 32768.0; 15 | static const double c2 = 1.428606820309417232e-6; 16 | static const double hugexp = (double)HUGE_EXP; 17 | static const double invln2 = 1.4426950408889634074; 18 | 19 | short _Exp(double *px, short eoff) 20 | { /* compute e^(*px)*2^eoff, x finite */ 21 | int neg; 22 | 23 | if (*px < 0) 24 | *px = -*px, neg = 1; 25 | else 26 | neg = 0; 27 | if (hugexp < *px) 28 | { /* certain underflow or overflow */ 29 | *px = neg ? 0.0 : _Inf._D; 30 | return (neg ? 0 : INF); 31 | } 32 | else 33 | { /* xexp won't overflow */ 34 | double g = *px * invln2; 35 | short xexp = (short)(g + 0.5); 36 | 37 | g = (double)xexp; 38 | g = (*px - g * c1) - g * c2; 39 | if (-_Rteps._D < g && g < _Rteps._D) 40 | *px = 1.0; 41 | else 42 | { /* g*g worth computing */ 43 | const double y = g * g; 44 | 45 | g *= (p[0] * y + p[1]) * y + p[2]; 46 | *px = 0.5 + g / (((q[0] * y + q[1]) * y + q[2]) * y 47 | + q[3] - g); 48 | ++xexp; 49 | } 50 | if (neg) 51 | *px = 1.0 / *px, xexp = -xexp; 52 | return (_Dscale(px, eoff + xexp)); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /STDLIB/XMBTOWC.C: -------------------------------------------------------------------------------- 1 | /* _Mbtowc function */ 2 | #include 3 | #include 4 | #include "xstate.h" 5 | 6 | int _Mbtowc(wchar_t *pwc, const char *s, size_t nin, 7 | _Mbsave *ps) 8 | { /* translate multibyte to widechar */ 9 | static const _Mbsave initial = {0}; 10 | 11 | if (s == NULL) 12 | { /* set initial state */ 13 | *ps = initial; 14 | return (_Mbstate._Tab[0][0] & ST_STATE); 15 | } 16 | { /* run finite state machine */ 17 | char state = ps->_State; 18 | int limit = 0; 19 | unsigned char *su = (unsigned char *)s; 20 | unsigned short wc = ps->_Wchar; 21 | 22 | if (MB_CUR_MAX < nin) 23 | nin = MB_CUR_MAX; 24 | for (; ; ) 25 | { /* perform a state transformation */ 26 | unsigned short code; 27 | const unsigned short *stab; 28 | 29 | if (_NSTATE <= state 30 | || (stab = _Mbstate._Tab[state]) == NULL 31 | || nin == 0 32 | || (_NSTATE*UCHAR_MAX) <= ++limit 33 | || (code = stab[*su]) == 0) 34 | break; 35 | state = (code & ST_STATE) >> ST_STOFF; 36 | if (code & ST_FOLD) 37 | wc = wc & ~UCHAR_MAX | code & ST_CH; 38 | if (code & ST_ROTATE) 39 | wc = wc >> CHAR_BIT & UCHAR_MAX | wc << CHAR_BIT; 40 | if (code & ST_INPUT && *su != '\0') 41 | ++su, --nin, limit = 0; 42 | if (code & ST_OUTPUT) 43 | { /* produce an output wchar */ 44 | if (pwc) 45 | *pwc = wc; 46 | ps->_State = state; 47 | ps->_Wchar = wc; 48 | return ((const char *)su - s); 49 | } 50 | } 51 | ps->_State = _NSTATE; /* error return */ 52 | return (-1); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /STRING/STRCOLL.C: -------------------------------------------------------------------------------- 1 | /* strcoll function */ 2 | #include "xstrxfrm.h" 3 | 4 | /* type definitions */ 5 | typedef struct { 6 | char buf[32]; 7 | const unsigned char *s1, *s2, *sout; 8 | _Cosave state; 9 | } Sctl; 10 | 11 | static size_t getxfrm(Sctl *p) 12 | { /* get transformed chars */ 13 | size_t i; 14 | 15 | do { /* loop until chars delivered */ 16 | p->sout = (const unsigned char *)p->buf; 17 | i = _Strxfrm(p->buf, &p->s1, sizeof (p->buf), &p->state); 18 | if (0 < i && p->buf[i - 1] == '\0') 19 | return (i - 1); 20 | else if (*p->s1 == '\0') 21 | p->s1 = p->s2; /* rescan */ 22 | } while (i == 0); 23 | return (i); 24 | } 25 | 26 | int (strcoll)(const char *s1, const char *s2) 27 | { /* compare s1[], s2[] using locale-dependent rule */ 28 | size_t n1, n2; 29 | Sctl st1, st2; 30 | static const _Cosave initial = {0}; 31 | 32 | st1.s1 = (const unsigned char *)s1; 33 | st1.s2 = (const unsigned char *)s1; 34 | st1.state = initial; 35 | st2.s1 = (const unsigned char *)s2; 36 | st2.s2 = (const unsigned char *)s2; 37 | st2.state = initial; 38 | for (n1 = n2 = 0; ; ) 39 | { /* compare transformed chars */ 40 | int ans; 41 | size_t n; 42 | 43 | if (n1 == 0) 44 | n1 = getxfrm(&st1); 45 | if (n2 == 0) 46 | n2 = getxfrm(&st2); 47 | n = n1 < n2 ? n1 : n2; 48 | if (n == 0) 49 | return (n1 == n2 ? 0 : 0 < n2 ? -1 : +1); 50 | else if ((ans = memcmp(st1.sout, st2.sout, n)) != 0) 51 | return (ans); 52 | st1.sout += n, n1 -= n; 53 | st2.sout += n, n2 -= n; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /_HEADERS/CTYPE.H: -------------------------------------------------------------------------------- 1 | /* ctype.h standard header */ 2 | #ifndef _CTYPE 3 | #define _CTYPE 4 | /* _Ctype code bits */ 5 | #define _XA 0x200 /* extra alphabetic */ 6 | #define _XS 0x100 /* extra space */ 7 | #define _BB 0x80 /* BEL, BS, etc. */ 8 | #define _CN 0x40 /* CR, FF, HT, NL, VT */ 9 | #define _DI 0x20 /* '0'-'9' */ 10 | #define _LO 0x10 /* 'a'-'z' */ 11 | #define _PU 0x08 /* punctuation */ 12 | #define _SP 0x04 /* space */ 13 | #define _UP 0x02 /* 'A'-'Z' */ 14 | #define _XD 0x01 /* '0'-'9', 'A'-'F', 'a'-'f' */ 15 | /* declarations */ 16 | int isalnum(int), isalpha(int), iscntrl(int), isdigit(int); 17 | int isgraph(int), islower(int), isprint(int), ispunct(int); 18 | int isspace(int), isupper(int), isxdigit(int); 19 | int tolower(int), toupper(int); 20 | extern const short *_Ctype, *_Tolower, *_Toupper; 21 | /* macro overrides */ 22 | #define isalnum(c) (_Ctype[(int)(c)] & (_DI|_LO|_UP|_XA)) 23 | #define isalpha(c) (_Ctype[(int)(c)] & (_LO|_UP|_XA)) 24 | #define iscntrl(c) (_Ctype[(int)(c)] & (_BB|_CN)) 25 | #define isdigit(c) (_Ctype[(int)(c)] & _DI) 26 | #define isgraph(c) (_Ctype[(int)(c)] & (_DI|_LO|_PU|_UP|_XA)) 27 | #define islower(c) (_Ctype[(int)(c)] & _LO) 28 | #define isprint(c) \ 29 | (_Ctype[(int)(c)] & (_DI|_LO|_PU|_SP|_UP|_XA)) 30 | #define ispunct(c) (_Ctype[(int)(c)] & _PU) 31 | #define isspace(c) (_Ctype[(int)(c)] & (_CN|_SP|_XS)) 32 | #define isupper(c) (_Ctype[(int)(c)] & _UP) 33 | #define isxdigit(c) (_Ctype[(int)(c)] & _XD) 34 | #define tolower(c) _Tolower[(int)(c)] 35 | #define toupper(c) _Toupper[(int)(c)] 36 | #endif 37 | -------------------------------------------------------------------------------- /MATH/XDINT.C: -------------------------------------------------------------------------------- 1 | /* _Dint function -- IEEE 754 version */ 2 | #include "xmath.h" 3 | 4 | short _Dint(double *px, short xexp) 5 | { /* test and drop (scaled) fraction bits */ 6 | unsigned short *ps = (unsigned short *)px; 7 | unsigned short frac = ps[_D0] & _DFRAC 8 | || ps[_D1] || ps[_D2] || ps[_D3]; 9 | short xchar = (ps[_D0] & _DMASK) >> _DOFF; 10 | 11 | if (xchar == 0 && !frac) 12 | return (0); /* zero */ 13 | else if (xchar != _DMAX) 14 | ; /* finite */ 15 | else if (!frac) 16 | return (INF); 17 | else 18 | { /* NaN */ 19 | errno = EDOM; 20 | return (NAN); 21 | } 22 | xchar = (_DBIAS+48+_DOFF+1) - xchar - xexp; 23 | if (xchar <= 0) 24 | return (0); /* no frac bits to drop */ 25 | else if ((48+_DOFF) < xchar) 26 | { /* all frac bits */ 27 | ps[_D0] = 0, ps[_D1] = 0; 28 | ps[_D2] = 0, ps[_D3] = 0; 29 | return (FINITE); 30 | } 31 | else 32 | { /* strip out frac bits */ 33 | static const unsigned short mask[] = { 34 | 0x0000, 0x0001, 0x0003, 0x0007, 35 | 0x000f, 0x001f, 0x003f, 0x007f, 36 | 0x00ff, 0x01ff, 0x03ff, 0x07ff, 37 | 0x0fff, 0x1fff, 0x3fff, 0x7fff}; 38 | static const size_t sub[] = {_D3, _D2, _D1, _D0}; 39 | 40 | frac = mask[xchar & 0xf]; 41 | xchar >>= 4; 42 | frac &= ps[sub[xchar]]; 43 | ps[sub[xchar]] ^= frac; 44 | switch (xchar) 45 | { /* cascade through! */ 46 | case 3: 47 | frac |= ps[_D1], ps[_D1] = 0; 48 | case 2: 49 | frac |= ps[_D2], ps[_D2] = 0; 50 | case 1: 51 | frac |= ps[_D3], ps[_D3] = 0; 52 | } 53 | return (frac ? FINITE : 0); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /MATH/TAN.C: -------------------------------------------------------------------------------- 1 | /* tan function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients, after Cody & Waite, Chapter 9 */ 5 | static const double p[3] = { 6 | -0.17861707342254426711e-4, 7 | 0.34248878235890589960e-2, 8 | -0.13338350006421960681e+0}; 9 | static const double q[4] = { 10 | 0.49819433993786512270e-6, 11 | -0.31181531907010027307e-3, 12 | 0.25663832289440112864e-1, 13 | -0.46671683339755294240e+0}; 14 | static const double c1 = 3294198.0 / 2097152.0; 15 | static const double c2 = 3.139164786504813217e-7; 16 | static const double twobypi = 0.63661977236758134308; 17 | static const double twopi = 6.28318530717958647693; 18 | 19 | double (tan)(double x) 20 | { /* compute tan(x) */ 21 | double g, gd; 22 | long quad; 23 | 24 | switch (_Dtest(&x)) 25 | { 26 | case NAN: 27 | errno = EDOM; 28 | return (x); 29 | case INF: 30 | errno = EDOM; 31 | return (_Nan._D); 32 | case 0: 33 | return (0.0); 34 | default: /* finite */ 35 | if (x < -HUGE_RAD || HUGE_RAD < x) 36 | { /* x huge, sauve qui peut */ 37 | g = x / twopi; 38 | _Dint(&g, 0); 39 | x -= g * twopi; 40 | } 41 | g = x * twobypi; 42 | quad = (long)(0 < g ? g + 0.5 : g - 0.5); 43 | g = (double)quad; 44 | g = (x - g * c1) - g * c2; 45 | gd = 1.0; 46 | if (_Rteps._D < (g < 0.0 ? -g : g)) 47 | { /* g*g worth computing */ 48 | double y = g * g; 49 | 50 | gd += (((q[0] * y + q[1]) * y + q[2]) * y + q[3]) * y; 51 | g += ((p[0] * y + p[1]) * y + p[2]) * y * g; 52 | } 53 | return ((unsigned int)quad & 0x1 ? -gd / g : g / gd); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /MATH/XDTENTO.C: -------------------------------------------------------------------------------- 1 | /* _Dtento function -- IEEE 754 version */ 2 | #include 3 | #include 4 | #include "xmath.h" 5 | 6 | /* macros */ 7 | #define NPOWS (sizeof pows / sizeof pows[0] - 1) 8 | /* static data */ 9 | static const double pows[] = { 10 | 1e1, 1e2, 1e4, 1e8, 1e16, 1e32, 11 | #if 0x100 < _DBIAS /* assume IEEE 754 8-byte */ 12 | 1e64, 1e128, 1e256, 13 | #endif 14 | }; 15 | static const size_t npows = NPOWS; 16 | 17 | static short dmul(double *px, double y) 18 | { /* multiply y by *px with checking */ 19 | short xexp; 20 | 21 | _Dunscale(&xexp, px); 22 | *px *= y; 23 | return (_Dscale(px, xexp)); 24 | } 25 | 26 | double _Dtento(double x, short n) 27 | { /* compute x * 10**n */ 28 | double factor; 29 | short errx; 30 | size_t i; 31 | 32 | if (n == 0 || x == 0.0) 33 | return (x); 34 | factor = 1.0; 35 | if (n < 0) 36 | { /* scale down */ 37 | unsigned int nu = -(unsigned int)n; 38 | 39 | for (i = 0; 0 < nu && i < npows; nu >>= 1, ++i) 40 | if (nu & 1) 41 | factor *= pows[i]; 42 | errx = dmul(&x, 1.0 / factor); 43 | if (errx < 0 && 0 < nu) 44 | for (factor = 1.0 / pows[npows]; 0 < nu; --nu) 45 | if (0 <= (errx = dmul(&x, factor))) 46 | break; 47 | } 48 | else if (0 < n) 49 | { /* scale up */ 50 | for (i = 0; 0 < n && i < npows; n >>= 1, ++i) 51 | if (n & 1) 52 | factor *= pows[i]; 53 | errx = dmul(&x, factor); 54 | if (errx < 0 && 0 < n) 55 | for (factor = pows[npows]; 0 < n; --n) 56 | if (0 <= (errx = dmul(&x, factor))) 57 | break; 58 | } 59 | if (0 <= errx) 60 | errno = ERANGE; 61 | return (x); 62 | } 63 | -------------------------------------------------------------------------------- /_TEST/TSETJMP.C: -------------------------------------------------------------------------------- 1 | /* test setjmp functions */ 2 | #include 3 | #include 4 | #include 5 | 6 | /* static data */ 7 | static int ctr; 8 | static jmp_buf b0; 9 | 10 | static void jmpto(int n) 11 | { /* jump on static buffer */ 12 | longjmp(b0, n); 13 | } 14 | 15 | static char *stackptr(void) 16 | { /* test for stack creep */ 17 | char ch; 18 | 19 | return (&ch); 20 | } 21 | 22 | static int tryit(void) 23 | { /* exercise jumps */ 24 | jmp_buf b1; 25 | char *sp = stackptr(); 26 | 27 | ctr = 0; 28 | switch (setjmp(b0)) 29 | { /* jump among cases */ 30 | case 0: 31 | assert(sp == stackptr()); 32 | assert(ctr == 0); 33 | ++ctr; 34 | jmpto(0); /* should return 1 */ 35 | break; 36 | case 1: 37 | assert(sp == stackptr()); 38 | assert(ctr == 1); 39 | ++ctr; 40 | jmpto(2); 41 | break; 42 | case 2: 43 | assert(sp == stackptr()); 44 | assert(ctr == 2); 45 | ++ctr; 46 | switch (setjmp(b1)) 47 | { /* test nesting */ 48 | case 0: 49 | assert(sp == stackptr()); 50 | assert(ctr == 3); 51 | ++ctr; 52 | longjmp(b1, -7); 53 | break; 54 | case -7: 55 | assert(sp == stackptr()); 56 | assert(ctr == 4); 57 | ++ctr; 58 | jmpto(3); 59 | case 5: 60 | return (13); 61 | default: 62 | return (0); 63 | } 64 | case 3: 65 | longjmp(b1, 5); 66 | break; 67 | } 68 | return (-1); 69 | } 70 | 71 | int main() 72 | { /* test basic workings of setjmp functions */ 73 | assert(tryit() == 13); 74 | printf("sizeof (jmp_buf) = %u\n", sizeof (jmp_buf)); 75 | puts("SUCCESS testing "); 76 | return (0); 77 | } 78 | -------------------------------------------------------------------------------- /TIME/XISDST.C: -------------------------------------------------------------------------------- 1 | /* _Isdst function */ 2 | #include 3 | #include "xtime.h" 4 | 5 | int _Isdst(const struct tm *t) 6 | { /* test whether Daylight Savings Time in effect */ 7 | Dstrule *pr; 8 | static const char *olddst = NULL; 9 | static Dstrule *rules = NULL; 10 | 11 | if (olddst != _Times._Isdst) 12 | { /* find current dst_rules */ 13 | if (_Times._Isdst[0] == '\0') 14 | { /* look beyond time_zone info */ 15 | int n; 16 | 17 | if (_Times._Tzone[0] == '\0') 18 | _Times._Tzone = _Getzone(); 19 | _Times._Isdst = _Gettime(_Times._Tzone, 3, &n); 20 | if (_Times._Isdst[0] != '\0') 21 | --_Times._Isdst; /* point to delimiter */ 22 | } 23 | if ((pr = _Getdst(_Times._Isdst)) == NULL) 24 | return (-1); 25 | free(rules); 26 | rules = pr; 27 | olddst = _Times._Isdst; 28 | } 29 | { /* check time against rules */ 30 | int ans = 0; 31 | const int d0 = _Daysto(t->tm_year, 0); 32 | const int hour = t->tm_hour + 24 * t->tm_yday; 33 | const int wd0 = (365L * t->tm_year + d0 + WDAY) % 7 + 14; 34 | 35 | for (pr = rules; pr->wday != (unsigned char)-1; ++pr) 36 | if (pr->year <= t->tm_year) 37 | { /* found early enough year */ 38 | int rday = _Daysto(t->tm_year, pr->mon) - d0 39 | + pr->day; 40 | 41 | if (0 < pr->wday) 42 | { /* shift to specific weekday */ 43 | int wd = (rday + wd0 - pr->wday) % 7; 44 | 45 | rday += wd == 0 ? 0 : 7 - wd; 46 | if (pr->wday <= 7) 47 | rday -= 7; /* strictly before */ 48 | } 49 | if (hour < rday * 24 + pr->hour) 50 | return (ans); 51 | ans = pr->year == (pr + 1)->year ? !ans : 0; 52 | } 53 | return (ans); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /_HEADERS/XLOCALE.H: -------------------------------------------------------------------------------- 1 | /* xlocale.h internal header */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "xstate.h" 7 | #include "xtinfo.h" 8 | 9 | /* macros for _Getloc and friends */ 10 | #define ADDR(p, q, ty) (*(ty *)((char *)p + q->_Offset)) 11 | #define NEWADDR(p, q, ty) \ 12 | (ADDR(p, q, ty) != ADDR(&_Clocale, q, ty)) 13 | #define MAXLIN 256 14 | #define TABSIZ ((UCHAR_MAX + 2) * sizeof (short)) 15 | /* type definitions */ 16 | enum _Lcode { 17 | L_GSTRING, L_NAME, L_NOTE, L_SET, 18 | L_STATE, L_STRING, L_TABLE, L_VALUE 19 | }; 20 | typedef const struct { 21 | const char *_Name; 22 | size_t _Offset; 23 | enum _Lcode _Code; 24 | } _Locitem; 25 | typedef struct _Linfo { 26 | const char *_Name; /* must be first */ 27 | struct _Linfo *_Next; 28 | /* controlled by LC_COLLATE */ 29 | _Statab _Costate; 30 | /* controlled by LC_CTYPE */ 31 | const short *_Ctype; 32 | const short *_Tolower; 33 | const short *_Toupper; 34 | unsigned char _Mbcurmax; 35 | _Statab _Mbstate; 36 | _Statab _Wcstate; 37 | /* controlled by LC_MONETARY and LC_NUMERIC */ 38 | struct lconv _Lc; 39 | /* controlled by LC_TIME */ 40 | _Tinfo _Times; 41 | } _Linfo; 42 | /* declarations */ 43 | const char *_Defloc(void); 44 | void _Freeloc(_Linfo *); 45 | _Linfo *_Getloc(const char *, const char *); 46 | int _Locterm(const char **, unsigned short *); 47 | int _Locvar(char, short); 48 | int _Makeloc(FILE *, char *, _Linfo *); 49 | _Locitem *_Readloc(FILE *, char *, const char **); 50 | _Linfo *_Setloc(int, _Linfo *); 51 | const char *_Skip(const char *); 52 | extern _Linfo _Clocale; 53 | extern _Locitem _Loctab[]; 54 | -------------------------------------------------------------------------------- /LOCALE/XSETLOC.C: -------------------------------------------------------------------------------- 1 | /* _Setloc function */ 2 | #include 3 | #include "xlocale.h" 4 | 5 | _Linfo *_Setloc(int cat, _Linfo *p) 6 | { /* set category for locale */ 7 | switch (cat) 8 | { /* set a category */ 9 | case LC_COLLATE: 10 | _Costate = p->_Costate; 11 | break; 12 | case LC_CTYPE: 13 | _Ctype = p->_Ctype; 14 | _Tolower = p->_Tolower; 15 | _Toupper = p->_Toupper; 16 | _Mbcurmax = p->_Mbcurmax <= MB_LEN_MAX 17 | ? p->_Mbcurmax : MB_LEN_MAX; 18 | _Mbstate = p->_Mbstate; 19 | _Wcstate = p->_Wcstate; 20 | break; 21 | case LC_MONETARY: 22 | _Locale.currency_symbol = p->_Lc.currency_symbol; 23 | _Locale.int_curr_symbol = p->_Lc.int_curr_symbol; 24 | _Locale.mon_decimal_point = p->_Lc.mon_decimal_point; 25 | _Locale.mon_grouping = p->_Lc.mon_grouping; 26 | _Locale.mon_thousands_sep = p->_Lc.mon_thousands_sep; 27 | _Locale.negative_sign = p->_Lc.negative_sign; 28 | _Locale.positive_sign = p->_Lc.positive_sign; 29 | _Locale.frac_digits = p->_Lc.frac_digits; 30 | _Locale.int_frac_digits = p->_Lc.int_frac_digits; 31 | _Locale.n_cs_precedes = p->_Lc.n_cs_precedes; 32 | _Locale.n_sep_by_space = p->_Lc.n_sep_by_space; 33 | _Locale.n_sign_posn = p->_Lc.n_sign_posn; 34 | _Locale.p_cs_precedes = p->_Lc.p_cs_precedes; 35 | _Locale.p_sep_by_space = p->_Lc.p_sep_by_space; 36 | _Locale.p_sign_posn = p->_Lc.p_sign_posn; 37 | break; 38 | case LC_NUMERIC: 39 | _Locale.decimal_point = p->_Lc.decimal_point[0] != '\0' 40 | ? p->_Lc.decimal_point : "."; 41 | _Locale.grouping = p->_Lc.grouping; 42 | _Locale.thousands_sep = p->_Lc.thousands_sep; 43 | break; 44 | case LC_TIME: 45 | _Times = p->_Times; 46 | break; 47 | } 48 | return (p); 49 | } 50 |  -------------------------------------------------------------------------------- /_HEADERS/FLOAT.H: -------------------------------------------------------------------------------- 1 | /* float.h standard header -- IEEE 754 version */ 2 | #ifndef _FLOAT 3 | #define _FLOAT 4 | #ifndef _YVALS 5 | #include 6 | #endif 7 | /* type definitions */ 8 | typedef struct { 9 | int _Ddig, _Dmdig, _Dmax10e, _Dmaxe, _Dmin10e, _Dmine; 10 | union { 11 | unsigned short _Us[5]; 12 | float _F; 13 | double _D; 14 | long double _Ld; 15 | } _Deps, _Dmax, _Dmin; 16 | } _Dvals; 17 | /* declarations */ 18 | extern _Dvals _Dbl, _Flt, _Ldbl; 19 | /* double properties */ 20 | #define DBL_DIG _Dbl._Ddig 21 | #define DBL_EPSILON _Dbl._Deps._D 22 | #define DBL_MANT_DIG _Dbl._Dmdig 23 | #define DBL_MAX _Dbl._Dmax._D 24 | #define DBL_MAX_10_EXP _Dbl._Dmax10e 25 | #define DBL_MAX_EXP _Dbl._Dmaxe 26 | #define DBL_MIN _Dbl._Dmin._D 27 | #define DBL_MIN_10_EXP _Dbl._Dmin10e 28 | #define DBL_MIN_EXP _Dbl._Dmine 29 | /* float properties */ 30 | #define FLT_DIG _Flt._Ddig 31 | #define FLT_EPSILON _Flt._Deps._F 32 | #define FLT_MANT_DIG _Flt._Dmdig 33 | #define FLT_MAX _Flt._Dmax._F 34 | #define FLT_MAX_10_EXP _Flt._Dmax10e 35 | #define FLT_MAX_EXP _Flt._Dmaxe 36 | #define FLT_MIN _Flt._Dmin._F 37 | #define FLT_MIN_10_EXP _Flt._Dmin10e 38 | #define FLT_MIN_EXP _Flt._Dmine 39 | /* common properties */ 40 | #define FLT_RADIX 2 41 | #define FLT_ROUNDS _FRND 42 | /* long double properties */ 43 | #define LDBL_DIG _Ldbl._Ddig 44 | #define LDBL_EPSILON _Ldbl._Deps._Ld 45 | #define LDBL_MANT_DIG _Ldbl._Dmdig 46 | #define LDBL_MAX _Ldbl._Dmax._Ld 47 | #define LDBL_MAX_10_EXP _Ldbl._Dmax10e 48 | #define LDBL_MAX_EXP _Ldbl._Dmaxe 49 | #define LDBL_MIN _Ldbl._Dmin._Ld 50 | #define LDBL_MIN_10_EXP _Ldbl._Dmin10e 51 | #define LDBL_MIN_EXP _Ldbl._Dmine 52 | #endif 53 | -------------------------------------------------------------------------------- /LOCALE/XLOCTERM.C: -------------------------------------------------------------------------------- 1 | /* _Locterm and _Locvar functions */ 2 | #include 3 | #include 4 | #include "xlocale.h" 5 | 6 | /* static data */ 7 | static const char dollars[] = { /* PLUS $@ and $$ */ 8 | "^abfnrtv" /* character codes */ 9 | "01234567" /* state values */ 10 | "ACDHLMPSUW" /* ctype codes */ 11 | "#FIOR"}; /* state commands */ 12 | static const unsigned short dolvals[] = { 13 | CHAR_MAX, '\a', '\b', '\f', '\n', '\r', '\t', '\v', 14 | 0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 15 | _XA, _BB, _DI, _XD, _LO, _CN, _PU, _SP, _UP, _XS, 16 | UCHAR_MAX, ST_FOLD, ST_INPUT, ST_OUTPUT, ST_ROTATE}; 17 | static const char uppers[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 18 | static short vars[sizeof (uppers) - 1] = {0}; 19 | 20 | int _Locvar(char ch, short val) 21 | { /* set a $ variable */ 22 | const char *s = strchr(uppers, ch); 23 | 24 | if (s == NULL) 25 | return (0); 26 | vars[s - uppers] = val; 27 | return (1); 28 | } 29 | 30 | int _Locterm(const char **ps, unsigned short *ans) 31 | { /* evaluate a term on a locale file line */ 32 | const char *s = *ps; 33 | const char *s1; 34 | int mi; 35 | 36 | for (mi = 0; *s == '+' || *s == '-'; s = _Skip(s)) 37 | mi = *s == '-' ? !mi : mi; 38 | if (isdigit(s[0])) 39 | *ans = strtol(s, (char **)&s, 0); 40 | else if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'') 41 | *ans = ((unsigned char *)s)[1], s += 3; 42 | else if (s[0] && (s1 = strchr(uppers, s[0])) != NULL) 43 | *ans = vars[s1 - uppers], ++s; 44 | else if (s[0] == '$' && s[1] 45 | && (s1 = strchr(dollars, s[1])) != NULL) 46 | *ans = dolvals[s1 - dollars], s += 2; 47 | else 48 | return (0); 49 | if (mi) 50 | *ans = -*ans; 51 | *ps = _Skip(s - 1); 52 | return (1); 53 | } 54 |  -------------------------------------------------------------------------------- /MATH/XDSCALE.C: -------------------------------------------------------------------------------- 1 | /* _Dscale function -- IEEE 754 version */ 2 | #include "xmath.h" 3 | 4 | short _Dscale(double *px, short xexp) 5 | { /* scale *px by 2^xexp with checking */ 6 | long lexp; 7 | unsigned short *ps = (unsigned short *)px; 8 | short xchar = (ps[_D0] & _DMASK) >> _DOFF; 9 | 10 | if (xchar == _DMAX) /* NaN or INF */ 11 | return (ps[_D0] & _DFRAC || ps[_D1] 12 | || ps[_D2] || ps[_D3] ? NAN : INF); 13 | else if (0 < xchar) 14 | ; /* finite */ 15 | else if (0 < (xchar = _Dnorm(ps))) 16 | return (0); /* zero */ 17 | lexp = (long)xexp + xchar; 18 | if (_DMAX <= lexp) 19 | { /* overflow, return +/-INF */ 20 | *px = ps[_D0] & _DSIGN ? -_Inf._D : _Inf._D; 21 | return (INF); 22 | } 23 | else if (0 < lexp) 24 | { /* finite result, repack */ 25 | ps[_D0] = ps[_D0] & ~_DMASK | (short)lexp << _DOFF; 26 | return (FINITE); 27 | } 28 | else 29 | { /* denormalized, scale */ 30 | unsigned short sign = ps[_D0] & _DSIGN; 31 | 32 | ps[_D0] = 1 << _DOFF | ps[_D0] & _DFRAC; 33 | if (--lexp < -(48+_DOFF)) 34 | { /* underflow, return +/-0 */ 35 | ps[_D0] = sign, ps[_D1] = 0; 36 | ps[_D2] = 0, ps[_D3] = 0; 37 | return (0); 38 | } 39 | else 40 | { /* nonzero, align fraction */ 41 | for (xexp = lexp; xexp <= -16; xexp += 16) 42 | { /* scale by words */ 43 | ps[_D3] = ps[_D2], ps[_D2] = ps[_D1]; 44 | ps[_D1] = ps[_D0], ps[_D0] = 0; 45 | } 46 | if ((xexp = -xexp) != 0) 47 | { /* scale by bits */ 48 | ps[_D3] = ps[_D3] >> xexp 49 | | ps[_D2] << 16 - xexp; 50 | ps[_D2] = ps[_D2] >> xexp 51 | | ps[_D1] << 16 - xexp; 52 | ps[_D1] = ps[_D1] >> xexp 53 | | ps[_D0] << 16 - xexp; 54 | ps[_D0] >>= xexp; 55 | } 56 | ps[_D0] |= sign; 57 | return (FINITE); 58 | } 59 | } 60 | } 61 |  -------------------------------------------------------------------------------- /MATH/XSIN.C: -------------------------------------------------------------------------------- 1 | /* _Sin function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients */ 5 | static const double c[8] = { 6 | -0.000000000011470879, 7 | 0.000000002087712071, 8 | -0.000000275573192202, 9 | 0.000024801587292937, 10 | -0.001388888888888893, 11 | 0.041666666666667325, 12 | -0.500000000000000000, 13 | 1.0}; 14 | static const double s[8] = { 15 | -0.000000000000764723, 16 | 0.000000000160592578, 17 | -0.000000025052108383, 18 | 0.000002755731921890, 19 | -0.000198412698412699, 20 | 0.008333333333333372, 21 | -0.166666666666666667, 22 | 1.0}; 23 | static const double c1 = 3294198.0 / 2097152.0; 24 | static const double c2 = 3.139164786504813217e-7; 25 | static const double twobypi = 0.63661977236758134308; 26 | static const double twopi = 6.28318530717958647693; 27 | 28 | double _Sin(double x, unsigned int qoff) 29 | { /* compute sin(x) or cos(x) */ 30 | switch (_Dtest(&x)) 31 | { 32 | case NAN: 33 | errno = EDOM; 34 | return (x); 35 | case 0: 36 | return (qoff ? 1.0 : 0.0); 37 | case INF: 38 | errno = EDOM; 39 | return (_Nan._D); 40 | default: /* finite */ 41 | { /* compute sin/cos */ 42 | double g; 43 | long quad; 44 | 45 | if (x < -HUGE_RAD || HUGE_RAD < x) 46 | { /* x huge, sauve qui peut */ 47 | g = x / twopi; 48 | _Dint(&g, 0); 49 | x -= g * twopi; 50 | } 51 | g = x * twobypi; 52 | quad = (long)(0 < g ? g + 0.5 : g - 0.5); 53 | qoff += (unsigned long)quad & 0x3; 54 | g = (double)quad; 55 | g = (x - g * c1) - g * c2; 56 | if ((g < 0.0 ? -g : g) < _Rteps._D) 57 | { /* sin(tiny)==tiny, cos(tiny)==1 */ 58 | if (qoff & 0x1) 59 | g = 1.0; /* cos(tiny) */ 60 | } 61 | else if (qoff & 0x1) 62 | g = _Poly(g * g, c, 7); 63 | else 64 | g *= _Poly(g * g, s, 7); 65 | return (qoff & 0x2 ? -g : g); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /STDIO/XSCANF.C: -------------------------------------------------------------------------------- 1 | /* _Scanf function */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "xstdio.h" 7 | 8 | int _Scanf(int (*pfn)(void *, int), void *arg, 9 | const char *fmt, va_list ap) 10 | { /* read formatted */ 11 | int nconv = 0; 12 | _Sft x; 13 | 14 | x.pfn = pfn; 15 | x.arg = arg; 16 | x.ap = ap; 17 | x.nchar = 0; 18 | for (x.s = fmt; ; ++x.s) 19 | { /* parse format string */ 20 | int ch; 21 | 22 | { /* match any literal or white-space */ 23 | int n; 24 | wchar_t wc; 25 | _Mbsave state = {0}; 26 | 27 | while (0 < (n = _Mbtowc(&wc, x.s, MB_CUR_MAX, &state))) 28 | { /* check type of multibyte char */ 29 | x.s += n; 30 | if (wc == '%') 31 | break; 32 | else if (wc <= UCHAR_MAX && isspace(wc)) 33 | { /* match any white-space */ 34 | while (isspace(*x.s)) 35 | ++x.s; 36 | while (isspace(ch = GET(&x))) 37 | ; 38 | UNGETN(&x, ch); 39 | } 40 | else /* match literal text */ 41 | for (x.s -= n; 0 <= --n; ) 42 | if ((ch = GET(&x)) != *x.s++) 43 | { /* bad match */ 44 | UNGETN(&x, ch); 45 | return (nconv); 46 | } 47 | } 48 | if (*x.s == '\0') 49 | return (nconv); 50 | } 51 | { /* process a conversion specifier */ 52 | int code; 53 | 54 | x.noconv = *x.s == '*' ? *x.s++ : '\0'; 55 | for (x.width = 0; isdigit(*x.s); ++x.s) 56 | if (x.width < _WMAX) 57 | x.width = x.width * 10 + *x.s - '0'; 58 | x.qual = strchr("hlL", *x.s) ? *x.s++ : '\0'; 59 | if (!strchr("cn[", *x.s)) 60 | { /* match leading white-space */ 61 | while (isspace(ch = GET(&x))) 62 | ; 63 | UNGETN(&x, ch); 64 | } 65 | if ((code = _Getfld(&x)) <= 0) 66 | return (nconv == 0 ? code : nconv); 67 | if (x.stored) 68 | ++nconv; 69 | } 70 | } 71 | } 72 |  -------------------------------------------------------------------------------- /MATH/XLOG.C: -------------------------------------------------------------------------------- 1 | /* _Log function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients, after Cody & Waite, Chapter 5 */ 5 | static const double p[3] = { 6 | -0.78956112887491257267e+0, 7 | 0.16383943563021534222e+2, 8 | -0.64124943423745581147e+2}; 9 | static const double q[3] = { 10 | -0.35667977739034646171e+2, 11 | 0.31203222091924532844e+3, 12 | -0.76949932108494879777e+3}; 13 | static const double c1 = 22713.0 / 32768.0; 14 | static const double c2 = 1.428606820309417232e-6; 15 | static const double loge = 0.43429448190325182765; 16 | static const double rthalf = 0.70710678118654752440; 17 | 18 | double _Log(double x, int decflag) 19 | { /* compute ln(x) */ 20 | short xexp; 21 | 22 | switch (_Dunscale(&xexp, &x)) 23 | { /* test for special codes */ 24 | case NAN: 25 | errno = EDOM; 26 | return (x); 27 | case INF: 28 | if (DSIGN(x)) 29 | { /* -INF */ 30 | errno = EDOM; 31 | return (_Nan._D); 32 | } 33 | else 34 | { /* INF */ 35 | errno = ERANGE; 36 | return (_Inf._D); 37 | } 38 | case 0: 39 | errno = ERANGE; 40 | return (-_Inf._D); 41 | default: /* finite */ 42 | if (x < 0.0) 43 | { /* ln(negative) undefined */ 44 | errno = EDOM; 45 | return (_Nan._D); 46 | } 47 | else 48 | { /* 1/2 <= x < 1 */ 49 | double z = x - 0.5; 50 | double w; 51 | 52 | if (rthalf < x) 53 | z = (z - 0.5) / (x * 0.5 + 0.5); 54 | else 55 | { /* x <= sqrt(1/2) */ 56 | --xexp; 57 | z /= (z * 0.5 + 0.5); 58 | } 59 | w = z * z; 60 | z += z * w * ((p[0] * w + p[1]) * w + p[2]) 61 | / (((w + q[0]) * w + q[1]) * w + q[2]); 62 | if (xexp != 0) 63 | { /* form z += ln2 * xexp safely */ 64 | const double xn = (double)xexp; 65 | 66 | z = (xn * c2 + z) + xn * c1; 67 | } 68 | return (decflag ? loge * z : z); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /_TC86/YTCPP.CPP: -------------------------------------------------------------------------------- 1 | void abort(void); /* C++ declarations */ 2 | void exit(int); 3 | 4 | extern "C" { /* C definitions */ 5 | void abort(int junk) 6 | {abort(); } 7 | void exit(int status, int junk) 8 | {exit(status); } 9 | 10 | unsigned int clock(int); /* C declarations */ 11 | int _close(int, int); 12 | void _exit(int, int); 13 | char *getenv(const char *, int); 14 | void longjmp(char *, int); 15 | long lseek(int, long, int, int); 16 | int open(const char *, unsigned int, unsigned int, int); 17 | int read(int, unsigned char *, int, int); 18 | int rename(const char *, const char *, int); 19 | void *sbrk(int, int); 20 | int system(const char *, int); 21 | unsigned long time(unsigned long *, int); 22 | int unlink(const char *, int); 23 | int write(int, const unsigned char *, int, int); 24 | } 25 | 26 | unsigned int clock(void) /* C++ definitions */ 27 | {return (clock(0)); } 28 | int _close(int fd) 29 | {return (_close(fd, 0)); } 30 | void _exit(int status) 31 | {_exit(status, 0); } 32 | char *getenv(const char *name) 33 | {return (getenv(name, 0)); } 34 | void longjmp(int *env, int val) 35 | {longjmp((char *)env, val); } 36 | long lseek(int fd, long off, int way) 37 | {return (lseek(fd, off, way, 0)); } 38 | int open(const char *name, unsigned int mode, unsigned int mod) 39 | {return (open(name, mode, mod, 0)); } 40 | int read(int fd, unsigned char *buf, int size) 41 | {return (read(fd, buf, size, 0)); } 42 | int rename(const char *oldnm, const char *newnm) 43 | {return (rename(oldnm, newnm, 0)); } 44 | void *sbrk(int size) 45 | {return (sbrk(size, 0)); } 46 | int system(const char *cmd) 47 | {return (system(cmd, 0)); } 48 | unsigned long time(unsigned long *pt) 49 | {return (time(pt, 0)); } 50 | int unlink(const char *name) 51 | {return (unlink(name, 0)); } 52 | int write(int fd, const unsigned char *buf, int size) 53 | {return (write(fd, buf, size, 0)); } 54 | -------------------------------------------------------------------------------- /STDLIB/QSORT.C: -------------------------------------------------------------------------------- 1 | /* qsort function */ 2 | #include 3 | #include 4 | 5 | /* macros */ 6 | #define MAX_BUF 256 /* chunk to copy on swap */ 7 | 8 | void (qsort)(void *base, size_t n, size_t size, _Cmpfun *cmp) 9 | { /* sort (char base[size])[n] using quicksort */ 10 | while (1 < n) 11 | { /* worth sorting */ 12 | size_t i = 0; 13 | size_t j = n - 1; 14 | char *qi = (char *)base; 15 | char *qj = qi + size * j; 16 | char *qp = qj; 17 | 18 | while (i < j) 19 | { /* partition about pivot */ 20 | while (i < j && (*cmp)(qi, qp) <= 0) 21 | ++i, qi += size; 22 | while (i < j && (*cmp)(qp, qj) <= 0) 23 | --j, qj -= size; 24 | if (i < j) 25 | { /* swap elements i and j */ 26 | char buf[MAX_BUF]; 27 | char *q1 = qi; 28 | char *q2 = qj; 29 | size_t m, ms; 30 | 31 | for (ms = size; 0 < ms; 32 | ms -= m, q1 += m, q2 += m) 33 | { /* swap as many as possible */ 34 | m = ms < sizeof (buf) ? ms : sizeof (buf); 35 | memcpy(buf, q1, m); 36 | memcpy(q1, q2, m); 37 | memcpy(q2, buf, m); 38 | } 39 | ++i, qi += size; 40 | } 41 | } 42 | if (qi != qp) 43 | { /* swap elements i and pivot */ 44 | char buf[MAX_BUF]; 45 | char *q1 = qi; 46 | char *q2 = qp; 47 | size_t m, ms; 48 | 49 | for (ms = size; 0 < ms; ms -= m, q1 += m, q2 += m) 50 | { /* swap as many as possible */ 51 | m = ms < sizeof (buf) ? ms : sizeof (buf); 52 | memcpy(buf, q1, m); 53 | memcpy(q1, q2, m); 54 | memcpy(q2, buf, m); 55 | } 56 | } 57 | j = n - i - 1, qi += size; 58 | if (j < i) 59 | { /* recurse on smaller partition */ 60 | if (1 < j) 61 | qsort(qi, j, size, cmp); 62 | n = i; 63 | } 64 | else 65 | { /* lower partition is smaller */ 66 | if (1 < i) 67 | qsort(base, i, size, cmp); 68 | base = qi; 69 | n = j; 70 | } 71 | } 72 | } 73 |  -------------------------------------------------------------------------------- /TIME/XTTOTM.C: -------------------------------------------------------------------------------- 1 | /* _Ttotm and _Daysto functions */ 2 | #include "xtime.h" 3 | 4 | /* macros */ 5 | #define MONTAB(year) \ 6 | ((year) & 03 || (year) == 0 ? mos : lmos) 7 | 8 | /* static data */ 9 | static const short lmos[] = {0, 31, 60, 91, 121, 152, 10 | 182, 213, 244, 274, 305, 335}; 11 | static const short mos[] = {0, 31, 59, 90, 120, 151, 12 | 181, 212, 243, 273, 304, 334}; 13 | 14 | int _Daysto(int year, int mon) 15 | { /* compute extra days to start of month */ 16 | int days; 17 | 18 | if (0 < year) /* correct for leap year: 1801-2099 */ 19 | days = (year - 1) / 4; 20 | else if (year <= -4) 21 | days = 1 + (4 - year) / 4; 22 | else 23 | days = 0; 24 | return (days + MONTAB(year)[mon]); 25 | } 26 | 27 | struct tm *_Ttotm(struct tm *t, time_t secsarg, int isdst) 28 | { /* convert scalar time to time structure */ 29 | int year; 30 | long days; 31 | time_t secs; 32 | static struct tm ts; 33 | 34 | secsarg += _TBIAS; 35 | if (t == NULL) 36 | t = &ts; 37 | t->tm_isdst = isdst; 38 | for (secs = secsarg; ; secs = secsarg + 3600) 39 | { /* loop to correct for DST */ 40 | days = secs / 86400; 41 | t->tm_wday = (days + WDAY) % 7; 42 | { /* determine year */ 43 | long i; 44 | 45 | for (year = days / 365; 46 | days < (i = _Daysto(year, 0) + 365L * year); ) 47 | --year; /* correct guess and recheck */ 48 | days -= i; 49 | t->tm_year = year; 50 | t->tm_yday = days; 51 | } 52 | { /* determine month */ 53 | int mon; 54 | const short *pm = MONTAB(year); 55 | 56 | for (mon = 12; days < pm[--mon]; ) 57 | ; 58 | t->tm_mon = mon; 59 | t->tm_mday = days - pm[mon] + 1; 60 | } 61 | secs %= 86400; 62 | t->tm_hour = secs / 3600; 63 | secs %= 3600; 64 | t->tm_min = secs / 60; 65 | t->tm_sec = secs % 60; 66 | if (0 <= t->tm_isdst || (t->tm_isdst = _Isdst(t)) <= 0) 67 | return (t); /* loop only if <0 => 1 */ 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /_TEST/TMATH3.C: -------------------------------------------------------------------------------- 1 | /* test math functions -- part 3 */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | static double eps; 8 | 9 | static int approx(double d1, double d2) 10 | { /* test for approximate equality */ 11 | return ((d2 ? fabs((d2 - d1) / d2) : fabs(d1)) < eps); 12 | } 13 | 14 | int main() 15 | { /* test basic workings of math functions */ 16 | static double e = {2.71828182845904523536}; 17 | static double ln2 = {0.69314718055994530942}; 18 | static double rthalf = {0.70710678118654752440}; 19 | 20 | eps = DBL_EPSILON * 4.0; 21 | assert(approx(cosh(-1.0), (e + 1.0 / e) / 2.0)); 22 | assert(approx(cosh(0.0), 1.0)); 23 | assert(approx(cosh(1.0), (e + 1.0 / e) / 2.0)); 24 | assert(approx(exp(-1.0), 1.0 / e)); 25 | assert(approx(exp(0.0), 1.0)); 26 | assert(approx(exp(ln2), 2.0)); 27 | assert(approx(exp(1.0), e)); 28 | assert(approx(exp(3.0), e * e * e)); 29 | assert(log(1.0) == 0.0); 30 | assert(approx(log(e), 1.0)); 31 | assert(approx(log(e * e * e), 3.0)); 32 | assert(approx(log10(1.0), 0.0)); 33 | assert(approx(log10(5.0), 1.0 - log10(2.0))); 34 | assert(approx(log10(1e5), 5.0)); 35 | assert(approx(pow(-2.5, 2.0), 6.25)); 36 | assert(approx(pow(-2.0, -3.0), -0.125)); 37 | assert(pow(0.0, 6.0) == 0.0); 38 | assert(approx(pow(2.0, -0.5), rthalf)); 39 | assert(approx(pow(3.0, 4.0), 81.0)); 40 | assert(approx(sinh(-1.0), -(e - 1.0 / e) / 2.0)); 41 | assert(approx(sinh(0.0), 0.0)); 42 | assert(approx(sinh(1.0), (e - 1.0 / e) / 2.0)); 43 | assert(approx(sqrt(0.0), 0.0)); 44 | assert(approx(sqrt(0.5), rthalf)); 45 | assert(approx(sqrt(1.0), 1.0)); 46 | assert(approx(sqrt(2.0), 1.0 / rthalf)); 47 | assert(approx(sqrt(144.0), 12.0)); 48 | assert(approx(tanh(-1.0), -(e * e - 1.0) / (e * e + 1.0))); 49 | assert(approx(tanh(0.0), 0.0)); 50 | assert(approx(tanh(1.0), (e * e - 1.0) / (e * e + 1.0))); 51 | puts("SUCCESS testing , part 3"); 52 | return (0); 53 | } 54 |  -------------------------------------------------------------------------------- /CTYPE/XTOUPPER.C: -------------------------------------------------------------------------------- 1 | /* _Toupper conversion table -- ASCII version */ 2 | #include 3 | #include 4 | #include 5 | #if EOF != -1 || UCHAR_MAX != 255 6 | #error WRONG TOUPPER TABLE 7 | #endif 8 | 9 | /* static data */ 10 | static const short toup_tab[257] = {EOF, 11 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 12 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 13 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 14 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 15 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 16 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 17 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 18 | 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 19 | 0x40, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 20 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 21 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 22 | 'X', 'Y', 'Z', 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 23 | 0x60, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 24 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 25 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 26 | 'X', 'Y', 'Z', 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 27 | 28 | 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 29 | 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 30 | 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 31 | 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 32 | 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 33 | 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 34 | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 35 | 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 36 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 37 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 38 | 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 39 | 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 40 | 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 41 | 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 42 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 43 | 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}; 44 | 45 | const short *_Toupper = &toup_tab[1]; 46 | -------------------------------------------------------------------------------- /CTYPE/XTOLOWER.C: -------------------------------------------------------------------------------- 1 | /* _Tolower conversion table -- ASCII version */ 2 | #include 3 | #include 4 | #include 5 | #if EOF != -1 || UCHAR_MAX != 255 6 | #error WRONG TOLOWER TABLE 7 | #endif 8 | 9 | /* static data */ 10 | static const short tolow_tab[257] = {EOF, 11 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 12 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 13 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 14 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 15 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 16 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 17 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 18 | 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 19 | 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 20 | 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 21 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 22 | 'x', 'y', 'z', 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 23 | 0x60, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 24 | 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 25 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 26 | 'x', 'y', 'z', 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 27 | 28 | 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 29 | 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 30 | 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 31 | 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 32 | 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 33 | 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 34 | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 35 | 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 36 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 37 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 38 | 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 39 | 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 40 | 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 41 | 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 42 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 43 | 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}; 44 | 45 | const short *_Tolower = &tolow_tab[1]; 46 | -------------------------------------------------------------------------------- /_TEST/TSTDIO2.C: -------------------------------------------------------------------------------- 1 | /* test stdio functions, part 2 */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { /* test basic workings of stdio functions */ 9 | char buf[32], tname[L_tmpnam], *tn; 10 | FILE *pf; 11 | static int macs[] = { 12 | _IOFBF, _IOLBF, _IONBF, BUFSIZ, EOF, FILENAME_MAX, 13 | FOPEN_MAX, TMP_MAX, SEEK_CUR, SEEK_END, SEEK_SET}; 14 | 15 | assert(256 <= BUFSIZ && EOF < 0); 16 | assert(8 <= FOPEN_MAX && 25 <= TMP_MAX); 17 | assert(tmpnam(tname) == tname && strlen(tname) < L_tmpnam); 18 | assert((tn = tmpnam(NULL)) != NULL 19 | && strcmp(tn, tname) != 0); 20 | pf = fopen(tname, "w"); 21 | assert(pf != NULL 22 | && pf != stdin && pf != stdout && pf != stderr); 23 | assert(feof(pf) == 0 && ferror(pf) == 0); 24 | assert(fgetc(pf) == EOF 25 | && feof(pf) == 0 && ferror(pf) != 0); 26 | clearerr(pf); 27 | assert(ferror(pf) == 0); 28 | assert(fputc('a', pf) == 'a' && putc('b', pf) == 'b'); 29 | assert(0 <= fputs("cde\n", pf)); 30 | assert(0 <= fputs("fghij\n", pf)); 31 | assert(fflush(pf) == 0); 32 | assert(fwrite("klmnopq\n", 2, 4, pf) == 4); 33 | assert(fclose(pf) == 0); 34 | assert(freopen(tname, "r", stdin) == stdin); 35 | assert(fgetc(stdin) == 'a' && getc(stdin) == 'b'); 36 | assert(getchar() == 'c'); 37 | assert(fgets(buf, sizeof (buf), stdin) == buf 38 | && strcmp(buf, "de\n") == 0); 39 | assert(ungetc('x', stdin) == 'x'); 40 | assert(gets(buf) == buf && strcmp(buf, "xfghij") == 0); 41 | assert(fread(buf, 2, 4, stdin) == 4 42 | && strncmp(buf, "klmnopq\n", 8) == 0); 43 | assert(getchar() == EOF && feof(stdin) != 0); 44 | remove(tn); 45 | assert(rename(tname, tn) == 0 46 | && fopen(tname, "r") == NULL); 47 | assert((pf = fopen(tn, "r")) != NULL && fclose(pf) == 0); 48 | assert(remove(tn) == 0 && fopen(tn, "r") == NULL); 49 | assert((pf = tmpfile()) != NULL && fputc('x', pf) == 'x'); 50 | errno = EDOM; 51 | perror("Domain error reported as"); 52 | putchar('S'), puts("UCCESS testing , part 2"); 53 | return (0); 54 | } 55 | -------------------------------------------------------------------------------- /_TEST/TLOCALE.C: -------------------------------------------------------------------------------- 1 | /* test locales */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static void testclocale(struct lconv *p) 9 | { /* test properties of "C" locale */ 10 | assert(strcmp(p->currency_symbol, "") == 0); 11 | assert(strcmp(p->decimal_point, ".") == 0); 12 | assert(strcmp(p->grouping, "") == 0); 13 | assert(strcmp(p->int_curr_symbol, "") == 0); 14 | assert(strcmp(p->mon_decimal_point, "") == 0); 15 | assert(strcmp(p->mon_grouping, "") == 0); 16 | assert(strcmp(p->mon_thousands_sep, "") == 0); 17 | assert(strcmp(p->negative_sign, "") == 0); 18 | assert(strcmp(p->positive_sign, "") == 0); 19 | assert(strcmp(p->thousands_sep, "") == 0); 20 | assert(p->frac_digits == CHAR_MAX); 21 | assert(p->int_frac_digits == CHAR_MAX); 22 | assert(p->n_cs_precedes == CHAR_MAX); 23 | assert(p->n_sep_by_space == CHAR_MAX); 24 | assert(p->n_sign_posn == CHAR_MAX); 25 | assert(p->p_cs_precedes == CHAR_MAX); 26 | assert(p->p_sep_by_space == CHAR_MAX); 27 | assert(p->p_sign_posn == CHAR_MAX); 28 | } 29 | 30 | int main() 31 | { /* test basic properties of locales */ 32 | static int cats[] = {LC_ALL, LC_COLLATE, LC_CTYPE, 33 | LC_MONETARY, LC_NUMERIC, LC_TIME}; 34 | struct lconv *p = NULL; 35 | char buf[32], *s; 36 | 37 | assert((p = localeconv()) != NULL); 38 | testclocale(p); 39 | assert((s = setlocale(LC_ALL, NULL)) != NULL); 40 | assert(strlen(s) < sizeof (buf)); /* OK if longer */ 41 | strcpy(buf, s); /* but not safe for this program */ 42 | assert(setlocale(LC_ALL, "") != NULL); 43 | assert(localeconv() != NULL); 44 | assert((s = setlocale(LC_MONETARY, "C")) != NULL); 45 | puts(strcmp(s, "C") ? "Native locale differs from \"C\"" 46 | : "Native locale same as \"C\""); 47 | assert(setlocale(LC_NUMERIC, "C") != NULL); 48 | assert((p = localeconv()) != NULL); 49 | testclocale(p); 50 | assert(setlocale(LC_ALL, buf) != NULL); 51 | assert((p = localeconv()) != NULL); 52 | testclocale(p); 53 | puts("SUCCESS testing "); 54 | return (0); 55 | } 56 | -------------------------------------------------------------------------------- /STDIO/XGETINT.C: -------------------------------------------------------------------------------- 1 | /* _Getint function */ 2 | #include 3 | #include 4 | #include "xstdio.h" 5 | 6 | int _Getint(_Sft *px) 7 | { /* get an integer value for _Scanf */ 8 | char ac[FMAX+1], *p; 9 | char seendig = 0; 10 | int ch; 11 | static const char digits[] 12 | = "0123456789abcdefABCDEF"; 13 | static const char flit[] = "diouxXp"; 14 | static const char barr[] = {10, 0, 8, 10, 16, 16, 16}; 15 | int base = barr[(const char *)strchr(flit, *px->s) - flit]; 16 | int dlen; 17 | 18 | px->nget = px->width <= 0 19 | || FMAX < px->width ? FMAX : px->width; 20 | p = ac, ch = GETN(px); 21 | if (ch == '+' || ch == '-') 22 | *p++ = ch, ch = GETN(px); 23 | if (ch == '0') 24 | { /* match possible prefix */ 25 | seendig = 1; 26 | *p++ = ch, ch = GETN(px); 27 | if ((ch == 'x' || ch == 'X') 28 | && (base == 0 || base == 16)) 29 | base = 16, *p++ = ch, ch = GETN(px); 30 | else if (base == 0) 31 | base = 8; 32 | } 33 | dlen = base == 0 || base == 10 ? 10 : base == 8 ? 8 : 16+6; 34 | for (; 0 <= ch && memchr(digits, ch, dlen); seendig = 1) 35 | *p++ = ch, ch = GETN(px); 36 | UNGETN(px, ch); 37 | if (!seendig) 38 | return (p == ac && ch < 0 ? EOF : 0); 39 | *p = '\0'; 40 | if (px->noconv) 41 | ; 42 | else if (*px->s == 'd' || *px->s == 'i') 43 | { /* deliver a signed integer */ 44 | long lval = strtol(ac, NULL, base); 45 | 46 | px->stored = 1; 47 | if (px->qual == 'h') 48 | *va_arg(px->ap, short *) = lval; 49 | else if (px->qual != 'l') 50 | *va_arg(px->ap, int *) = lval; 51 | else 52 | *va_arg(px->ap, long *) = lval; 53 | } 54 | else 55 | { /* deliver an unsigned integer */ 56 | unsigned long ulval = strtoul(ac, NULL, base); 57 | 58 | px->stored = 1; 59 | if (*px->s == 'p') 60 | *va_arg(px->ap, void **) = (void *)ulval; 61 | else if (px->qual == 'h') 62 | *va_arg(px->ap, unsigned short *) = ulval; 63 | else if (px->qual != 'l') 64 | *va_arg(px->ap, unsigned int *) = ulval; 65 | else 66 | *va_arg(px->ap, unsigned long *) = ulval; 67 | } 68 | return (1); 69 | } 70 |  -------------------------------------------------------------------------------- /TIME/XGETDST.C: -------------------------------------------------------------------------------- 1 | /* _Getdst function */ 2 | #include 3 | #include 4 | #include 5 | #include "xtime.h" 6 | 7 | static int getint(const char *s, int n) 8 | { /* accumulate digits */ 9 | int value; 10 | 11 | for (value = 0; 0 <= --n && isdigit(*s); ++s) 12 | value = value * 10 + *s - '0'; 13 | return (0 <= n ? -1 : value); 14 | } 15 | 16 | Dstrule *_Getdst(const char *s) 17 | { /* parse DST rules */ 18 | const char delim = *s++; 19 | Dstrule *pr, *rules; 20 | 21 | if (delim == '\0') 22 | return (NULL); 23 | { /* buy space for rules */ 24 | const char *s1, *s2; 25 | int i; 26 | 27 | for (s1 = s, i = 2; (s2 = strchr(s1, delim)) != NULL; ++i) 28 | s1 = s2 + 1; 29 | if ((rules = (Dstrule *)malloc(sizeof (Dstrule) * i)) 30 | == NULL) 31 | return (NULL); 32 | } 33 | { /* parse rules */ 34 | int year = 0; 35 | 36 | for (pr = rules; ; ++pr, ++s) 37 | { /* parse next rule */ 38 | if (*s == '(') 39 | { /* got a year qualifier */ 40 | year = getint(s + 1, 4) - 1900; 41 | if (year < 0 || s[5] != ')') 42 | break; /* invalid year */ 43 | s += 6; 44 | } 45 | pr->year = year; 46 | pr->mon = getint(s, 2) - 1, s += 2; 47 | pr->day = getint(s, 2) - 1, s += 2; 48 | if (isdigit(*s)) 49 | pr->hour = getint(s, 2), s += 2; 50 | else 51 | pr->hour = 0; 52 | if (12 <= pr->mon || 99 < pr->day || 99 < pr->hour) 53 | break; /* invalid month, day, or hour */ 54 | if (*s != '+' && *s != '-') 55 | pr->wday = 0; 56 | else if (s[1] < '0' || '6' < s[1]) 57 | break; /* invalid week day */ 58 | else 59 | { /* compute week day field */ 60 | pr->wday = s[1] == '0' ? 7 : s[1] - '0'; 61 | if (*s == '+') /* '-': strictly before */ 62 | pr->wday += 7; /* '+': on or after */ 63 | s += 2; 64 | } 65 | if (*s == '\0') 66 | { /* done, terminate list */ 67 | (pr + 1)->wday = (unsigned char)-1; 68 | (pr + 1)->year = year; 69 | return (rules); 70 | } 71 | else if (*s != delim) 72 | break; 73 | } 74 | free(rules); 75 | return (NULL); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /_TEST/TLIMITS.C: -------------------------------------------------------------------------------- 1 | /* test limits macros */ 2 | #include 3 | #include 4 | 5 | int main() 6 | { /* test basic properties of limits.h macros */ 7 | printf("CHAR_BIT = %2i MB_LEN_MAX = %2i\n\n", 8 | CHAR_BIT, MB_LEN_MAX); 9 | printf(" CHAR_MAX = %10i CHAR_MIN = %10i\n", 10 | CHAR_MAX, CHAR_MIN); 11 | printf("SCHAR_MAX = %10i SCHAR_MIN = %10i\n", 12 | SCHAR_MAX, SCHAR_MIN); 13 | printf("UCHAR_MAX = %10u\n\n", UCHAR_MAX); 14 | printf(" SHRT_MAX = %10i SHRT_MIN = %10i\n", 15 | SHRT_MAX, SHRT_MIN); 16 | printf("USHRT_MAX = %10u\n\n", USHRT_MAX); 17 | printf(" INT_MAX = %10i INT_MIN = %10i\n", 18 | INT_MAX, INT_MIN); 19 | printf(" UINT_MAX = %10u\n\n", UINT_MAX); 20 | printf(" LONG_MAX = %10li LONG_MIN = %10li\n", 21 | LONG_MAX, LONG_MIN); 22 | printf("ULONG_MAX = %10lu\n", ULONG_MAX); 23 | #if CHAR_BIT < 8 || CHAR_MAX < 127 || 0 < CHAR_MIN \ 24 | || CHAR_MAX != SCHAR_MAX && CHAR_MAX != UCHAR_MAX 25 | #error bad char properties 26 | #endif 27 | #if INT_MAX < 32767 || -32767 < INT_MIN || INT_MAX < SHRT_MAX 28 | #error bad int properties 29 | #endif 30 | #if LONG_MAX < 2147483647 || -2147483647 < LONG_MIN \ 31 | || LONG_MAX < INT_MAX 32 | #error bad long properties 33 | #endif 34 | #if MB_LEN_MAX < 1 35 | #error bad MB_LEN_MAX 36 | #endif 37 | #if SCHAR_MAX < 127 || -127 < SCHAR_MIN 38 | #error bad signed char properties 39 | #endif 40 | #if SHRT_MAX < 32767 || -32767 < SHRT_MIN \ 41 | || SHRT_MAX < SCHAR_MAX 42 | #error bad short properties 43 | #endif 44 | #if UCHAR_MAX < 255 || UCHAR_MAX / 2 < SCHAR_MAX 45 | #error bad unsigned char properties 46 | #endif 47 | #if UINT_MAX < 65535 || UINT_MAX / 2 < INT_MAX \ 48 | || UINT_MAX < USHRT_MAX 49 | #error bad unsigned int properties 50 | #endif 51 | #if ULONG_MAX < 4294967295 || ULONG_MAX / 2 < LONG_MAX \ 52 | || ULONG_MAX < UINT_MAX 53 | #error bad unsigned long properties 54 | #endif 55 | #if USHRT_MAX < 65535 || USHRT_MAX / 2 < SHRT_MAX \ 56 | || USHRT_MAX < UCHAR_MAX 57 | #error bad unsigned short properties 58 | #endif 59 | puts("SUCCESS testing "); 60 | return (0); 61 | } 62 | -------------------------------------------------------------------------------- /MATH/XASIN.C: -------------------------------------------------------------------------------- 1 | /* _Asin function */ 2 | #include "xmath.h" 3 | 4 | /* coefficients, after Cody & Waite, Chapter 10 */ 5 | static const double p[5] = { 6 | -0.69674573447350646411e+0, 7 | 0.10152522233806463645e+2, 8 | -0.39688862997504877339e+2, 9 | 0.57208227877891731407e+2, 10 | -0.27368494524164255994e+2}; 11 | static const double q[6] = { 12 | 0.10000000000000000000e+1, 13 | -0.23823859153670238830e+2, 14 | 0.15095270841030604719e+3, 15 | -0.38186303361750149284e+3, 16 | 0.41714430248260412556e+3, 17 | -0.16421096714498560795e+3}; 18 | static const double piby2 = 1.57079632679489661923; 19 | static const double piby4 = 0.78539816339744830962; 20 | 21 | double _Asin(double x, int idx) 22 | { /* compute asin(x) or acos(x) */ 23 | double g, y; 24 | const short errx = _Dtest(&x); 25 | 26 | if (0 < errx) 27 | { /* INF, NaN */ 28 | errno = EDOM; 29 | return (errx == NAN ? x : _Nan._D); 30 | } 31 | if (x < 0.0) 32 | y = -x, idx |= 2; 33 | else 34 | y = x; 35 | if (y < _Rteps._D) 36 | ; 37 | else if (y < 0.5) 38 | { /* y*y worth computing */ 39 | g = y * y; 40 | y += y * g * _Poly(g, p, 4) / _Poly(g, q, 5); 41 | } 42 | else if (y < 1.0) 43 | { /* find 2*asin(sqrt((1-x)/2)) */ 44 | idx |= 4; 45 | g = (1.0 - y) / 2.0; /* NOT * 0.5! */ 46 | y = sqrt(g); 47 | y += y; 48 | y += y * g * _Poly(g, p, 4) / _Poly(g, q, 5); 49 | } 50 | else if (y == 1.0) 51 | idx |= 4, y = 0.0; 52 | else 53 | { /* 1.0 < |x|, undefined */ 54 | errno = EDOM; 55 | return (_Nan._D); 56 | } 57 | switch (idx) 58 | { /* flip and fold */ 59 | default: /* shouldn't happen */ 60 | case 0: /* asin, [0, 1/2) */ 61 | case 5: /* acos, [1/2, 1] */ 62 | return (y); 63 | case 1: /* acos, [0, 1/2) */ 64 | case 4: /* asin, [1/2, 1] */ 65 | return ((piby4 - y) + piby4); 66 | case 2: /* asin, (-1/2, 0) */ 67 | return (-y); 68 | case 3: /* acos, (-1/2, 0) */ 69 | return ((piby4 + y) + piby4); 70 | case 6: /* asin, [-1, -1/2] */ 71 | return ((-piby4 + y) - piby4); 72 | case 7: /* acos, [-1, -1/2] */ 73 | return ((piby2 - y) + piby2); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /_HEADERS/XSTDIO.H: -------------------------------------------------------------------------------- 1 | /* xstdio.h internal header */ 2 | #include 3 | #include 4 | /* bits for _Mode in FILE */ 5 | #define _MOPENR 0x1 6 | #define _MOPENW 0x2 7 | #define _MOPENA 0x4 8 | #define _MTRUNC 0x8 9 | #define _MCREAT 0x10 10 | #define _MBIN 0x20 11 | #define _MALBUF 0x40 12 | #define _MALFIL 0x80 13 | #define _MEOF 0x100 14 | #define _MERR 0x200 15 | #define _MLBF 0x400 16 | #define _MNBF 0x800 17 | #define _MREAD 0x1000 18 | #define _MWRITE 0x2000 19 | /* codes for _Printf and _Scanf */ 20 | #define _FSP 0x01 21 | #define _FPL 0x02 22 | #define _FMI 0x04 23 | #define _FNO 0x08 24 | #define _FZE 0x10 25 | #define _WMAX 999 26 | #define _WANT (EOF-1) 27 | /* macros for _Scanf */ 28 | #define FMAX 512 /* widest supported field */ 29 | #define GET(px) (++(px)->nchar, (*(px)->pfn)((px)->arg, _WANT)) 30 | #define GETN(px) (0 <= --(px)->nget ? GET(px) : _WANT) 31 | #define UNGET(px, ch) \ 32 | (--(px)->nchar, (*(px)->pfn)((px)->arg, ch)) 33 | #define UNGETN(px, ch) {if ((ch) != _WANT) UNGET(px, ch); } 34 | /* type definitions */ 35 | typedef struct { 36 | union { 37 | long li; 38 | long double ld; 39 | } v; 40 | char *s; 41 | int n0, nz0, n1, nz1, n2, nz2, prec, width; 42 | size_t nchar; 43 | unsigned int flags; 44 | char qual; 45 | } _Pft; 46 | typedef struct { 47 | int (*pfn)(void *, int); 48 | void *arg; 49 | va_list ap; 50 | const char *s; 51 | int nchar, nget, width; 52 | char noconv, qual, stored; 53 | } _Sft; 54 | /* declarations */ 55 | FILE *_Foprep(const char *, const char *, FILE *); 56 | int _Fopen(const char *, unsigned int, const char *); 57 | int _Frprep(FILE *); 58 | int _Ftmpnam(char *, int); 59 | int _Fwprep(FILE *); 60 | void _Genld(_Pft *, char, char *, short, short); 61 | int _Getfld(_Sft *); 62 | int _Getfloat(_Sft *); 63 | int _Getint(_Sft *); 64 | void _Ldtob(_Pft *, char); 65 | void _Litob(_Pft *, char); 66 | int _Printf(void *(*)(void *, const char *, size_t), 67 | void *, const char *, va_list); 68 | void _Putfld(_Pft *, va_list *, char, char *); 69 | int _Scanf(int (*)(void *, int), 70 | void *, const char *, va_list); 71 | -------------------------------------------------------------------------------- /STDLIB/MALLOC.C: -------------------------------------------------------------------------------- 1 | /* malloc function */ 2 | #include "xalloc.h" 3 | #include "yfuns.h" 4 | 5 | /* static data */ 6 | _Altab _Aldata = {0}; /* heap initially empty */ 7 | 8 | static _Cell **findmem(size_t size) 9 | { /* find storage */ 10 | _Cell *q, **qb; 11 | 12 | for (; ; ) 13 | { /* check freed space first */ 14 | if ((qb = _Aldata._Plast) == NULL) 15 | { /* take it from the top */ 16 | for (qb = &_Aldata._Head; *qb; 17 | qb = &(*qb)->_Next) 18 | if (size <= (*qb)->_Size) 19 | return (qb); 20 | } 21 | else 22 | { /* resume where we left off */ 23 | for (; *qb; qb = &(*qb)->_Next) 24 | if (size <= (*qb)->_Size) 25 | return (qb); 26 | q = *_Aldata._Plast; 27 | for (qb = &_Aldata._Head; *qb != q; 28 | qb = &(*qb)->_Next) 29 | if (size <= (*qb)->_Size) 30 | return (qb); 31 | } 32 | { /* try to buy more space */ 33 | size_t bs; 34 | const size_t sz = size + CELL_OFF; 35 | 36 | for (bs = SIZE_BLOCK; ; bs >>= 1) 37 | { /* try larger blocks first */ 38 | if (bs < sz) 39 | bs = sz; 40 | if ((q = (_Cell *)_Getmem(bs)) != NULL) 41 | break; 42 | else if (bs == sz) 43 | return (NULL); /* no storage */ 44 | } 45 | /* got storage: add to heap and retry */ 46 | q->_Size = bs - CELL_OFF; 47 | free((char *)q + CELL_OFF); 48 | } 49 | } 50 | } 51 | 52 | void *(malloc)(size_t size) 53 | { /* allocate a data object on the heap */ 54 | _Cell *q, **qb; 55 | 56 | if (size < SIZE_CELL) /* round up size */ 57 | size = SIZE_CELL; 58 | else if ((size = (size + _MEMBND) & ~_MEMBND) == 0 59 | || size + CELL_OFF < size) 60 | return (0); 61 | if ((qb = findmem(size)) == NULL) 62 | return (NULL); 63 | q = *qb; 64 | if (q->_Size - SIZE_CELL < size + CELL_OFF) 65 | *qb = q->_Next; /* use entire cell */ 66 | else 67 | { /* peel off a residual cell */ 68 | *qb = (_Cell *)((char *)q + CELL_OFF + size); 69 | (*qb)->_Next = q->_Next; 70 | (*qb)->_Size = q->_Size - CELL_OFF - size; 71 | q->_Size = size; 72 | } 73 | _Aldata._Plast = qb; /* resume here */ 74 | return ((char *)q + CELL_OFF); 75 | } 76 |  -------------------------------------------------------------------------------- /LOCALE/XLOCTAB.C: -------------------------------------------------------------------------------- 1 | /* _Loctab data object */ 2 | #include 3 | #include "xlocale.h" 4 | 5 | /* macros */ 6 | #define OFF(member) offsetof(_Linfo, member) 7 | /* static data */ 8 | _Locitem _Loctab[] = { /* locale file info */ 9 | {"LOCALE", OFF(_Name), L_NAME}, 10 | {"NOTE", 0, L_NOTE}, 11 | {"SET", 0, L_SET}, 12 | /* controlled by LC_COLLATE */ 13 | {"collate", OFF(_Costate._Tab), L_STATE}, 14 | /* controlled by LC_CTYPE */ 15 | {"ctype", OFF(_Ctype), L_TABLE}, 16 | {"tolower", OFF(_Tolower), L_TABLE}, 17 | {"toupper", OFF(_Toupper), L_TABLE}, 18 | {"mb_cur_max", OFF(_Mbcurmax), L_VALUE}, 19 | {"mbtowc", OFF(_Mbstate._Tab), L_STATE}, 20 | {"wctomb", OFF(_Wcstate._Tab), L_STATE}, 21 | /* controlled by LC_MONETARY */ 22 | {"currency_symbol", OFF(_Lc.currency_symbol), L_STRING}, 23 | {"int_curr_symbol", OFF(_Lc.int_curr_symbol), L_STRING}, 24 | {"mon_decimal_point", OFF(_Lc.mon_decimal_point), L_STRING}, 25 | {"mon_grouping", OFF(_Lc.mon_grouping), L_GSTRING}, 26 | {"mon_thousands_sep", OFF(_Lc.mon_thousands_sep), L_STRING}, 27 | {"negative_sign", OFF(_Lc.negative_sign), L_STRING}, 28 | {"positive_sign", OFF(_Lc.positive_sign), L_STRING}, 29 | {"frac_digits", OFF(_Lc.frac_digits), L_VALUE}, 30 | {"int_frac_digits", OFF(_Lc.int_frac_digits), L_VALUE}, 31 | {"n_cs_precedes", OFF(_Lc.n_cs_precedes), L_VALUE}, 32 | {"n_sep_by_space", OFF(_Lc.n_sep_by_space), L_VALUE}, 33 | {"n_sign_posn", OFF(_Lc.n_sign_posn), L_VALUE}, 34 | {"p_cs_precedes", OFF(_Lc.p_cs_precedes), L_VALUE}, 35 | {"p_sep_by_space", OFF(_Lc.p_sep_by_space), L_VALUE}, 36 | {"p_sign_posn", OFF(_Lc.p_sign_posn), L_VALUE}, 37 | /* controlled by LC_NUMERIC */ 38 | {"decimal_point", OFF(_Lc.decimal_point), L_STRING}, 39 | {"grouping", OFF(_Lc.grouping), L_GSTRING}, 40 | {"thousands_sep", OFF(_Lc.thousands_sep), L_STRING}, 41 | /* controlled by LC_TIME */ 42 | {"am_pm", OFF(_Times._Ampm), L_STRING}, 43 | {"days", OFF(_Times._Days), L_STRING}, 44 | {"dst_rules", OFF(_Times._Isdst), L_STRING}, 45 | {"time_formats", OFF(_Times._Formats), L_STRING}, 46 | {"months", OFF(_Times._Months), L_STRING}, 47 | {"time_zone", OFF(_Times._Tzone), L_STRING}, 48 | {NULL}}; 49 | --------------------------------------------------------------------------------