clang-format

This commit is contained in:
2024-09-10 13:03:02 -04:00
parent 53c617d779
commit d66450e427
381 changed files with 28864 additions and 34170 deletions

View File

@@ -52,7 +52,6 @@
#include <stdarg.h>
/*
* Do we want to support "long long" types with %lld?
*
@@ -67,12 +66,11 @@
* Define a type that holds the longest signed integer we intend to support.
*/
#ifdef USE_LONGLONG
#define INTTYPE long long
#define INTTYPE long long
#else
#define INTTYPE long
#define INTTYPE long
#endif
/*
* Space for a long long in base 8, plus a NUL, plus one
* character extra for slop.
@@ -87,52 +85,52 @@
* Structure holding the state for printf.
*/
typedef struct {
/* Callback for sending printed string data */
void (*sendfunc)(void *clientdata, const char *str, size_t len);
void *clientdata;
/* Callback for sending printed string data */
void (*sendfunc)(void *clientdata, const char *str, size_t len);
void *clientdata;
/* The varargs argument pointer */
va_list ap;
/* The varargs argument pointer */
va_list ap;
/* Total count of characters printed */
int charcount;
/* Total count of characters printed */
int charcount;
/* Flag that's true if we are currently looking in a %-format */
int in_pct;
/* Flag that's true if we are currently looking in a %-format */
int in_pct;
/* Size of the integer argument to retrieve */
enum {
INTSZ,
LONGSZ,
/* Size of the integer argument to retrieve */
enum {
INTSZ,
LONGSZ,
#ifdef USE_LONGLONG
LLONGSZ,
LLONGSZ,
#endif
SIZETSZ,
} size;
SIZETSZ,
} size;
/* The value of the integer argument retrieved */
unsigned INTTYPE num;
/* The value of the integer argument retrieved */
unsigned INTTYPE num;
/* Sign of the integer argument (0 = positive; -1 = negative) */
int sign;
/* Sign of the integer argument (0 = positive; -1 = negative) */
int sign;
/* Field width (number of spaces) */
int spacing;
/* Field width (number of spaces) */
int spacing;
/* Flag: align to left in field instead of right */
int rightspc;
/* Flag: align to left in field instead of right */
int rightspc;
/* Character to pad to field size with (space or 0) */
int fillchar;
/* Character to pad to field size with (space or 0) */
int fillchar;
/* Number base to print the integer argument in (8, 10, 16) */
int base;
/* Number base to print the integer argument in (8, 10, 16) */
int base;
/* Flag: if set, print 0x before hex and 0 before octal numbers */
int baseprefix;
/* Flag: if set, print 0x before hex and 0 before octal numbers */
int baseprefix;
/* Flag: alternative output format selected with %#... */
int altformat;
/* Flag: alternative output format selected with %#... */
int altformat;
} PF;
/*
@@ -141,31 +139,25 @@ typedef struct {
* We count the total length we send out so we can return it from __vprintf,
* since that's what most printf-like functions want to return.
*/
static
void
__pf_print(PF *pf, const char *txt, size_t len)
{
pf->sendfunc(pf->clientdata, txt, len);
pf->charcount += len;
static void __pf_print(PF *pf, const char *txt, size_t len) {
pf->sendfunc(pf->clientdata, txt, len);
pf->charcount += len;
}
/*
* Reset the state for the next %-field.
*/
static
void
__pf_endfield(PF *pf)
{
pf->in_pct = 0;
pf->size = INTSZ;
pf->num = 0;
pf->sign = 0;
pf->spacing = 0;
pf->rightspc = 0;
pf->fillchar = ' ';
pf->base = 0;
pf->baseprefix = 0;
pf->altformat = 0;
static void __pf_endfield(PF *pf) {
pf->in_pct = 0;
pf->size = INTSZ;
pf->num = 0;
pf->sign = 0;
pf->spacing = 0;
pf->rightspc = 0;
pf->fillchar = ' ';
pf->base = 0;
pf->baseprefix = 0;
pf->altformat = 0;
}
/*
@@ -177,59 +169,54 @@ __pf_endfield(PF *pf)
* 0-9 field width
* leading 0 pad with zeros instead of spaces
*/
static
void
__pf_modifier(PF *pf, int ch)
{
switch (ch) {
case '#':
pf->altformat = 1;
break;
case '-':
pf->rightspc = 1;
break;
case 'l':
if (pf->size==LONGSZ) {
static void __pf_modifier(PF *pf, int ch) {
switch (ch) {
case '#':
pf->altformat = 1;
break;
case '-':
pf->rightspc = 1;
break;
case 'l':
if (pf->size == LONGSZ) {
#ifdef USE_LONGLONG
pf->size = LLONGSZ;
pf->size = LLONGSZ;
#endif
}
else {
pf->size = LONGSZ;
}
break;
case 'z':
pf->size = SIZETSZ;
break;
case '0':
if (pf->spacing>0) {
/*
* Already seen some digits; this is part of the
* field size.
*/
pf->spacing = pf->spacing*10;
}
else {
/*
* Leading zero; set the padding character to 0.
*/
pf->fillchar = '0';
}
break;
default:
/*
* Invalid characters should be filtered out by a
* higher-level function, so if this assert goes off
* it's our fault.
*/
assert(ch>'0' && ch<='9');
} else {
pf->size = LONGSZ;
}
break;
case 'z':
pf->size = SIZETSZ;
break;
case '0':
if (pf->spacing > 0) {
/*
* Already seen some digits; this is part of the
* field size.
*/
pf->spacing = pf->spacing * 10;
} else {
/*
* Leading zero; set the padding character to 0.
*/
pf->fillchar = '0';
}
break;
default:
/*
* Invalid characters should be filtered out by a
* higher-level function, so if this assert goes off
* it's our fault.
*/
assert(ch > '0' && ch <= '9');
/*
* Got a digit; accumulate the field size.
*/
pf->spacing = pf->spacing*10 + (ch-'0');
break;
}
/*
* Got a digit; accumulate the field size.
*/
pf->spacing = pf->spacing * 10 + (ch - '0');
break;
}
}
/*
@@ -237,77 +224,71 @@ __pf_modifier(PF *pf, int ch)
* in pf->num, according to the size recorded in pf->size and using
* the numeric type specified by ch.
*/
static
void
__pf_getnum(PF *pf, int ch)
{
if (ch=='p') {
/*
* Pointer.
*
* uintptr_t is a C99 standard type that's an unsigned
* integer the same size as a pointer.
*/
pf->num = (uintptr_t) va_arg(pf->ap, void *);
}
else if (ch=='d') {
/* signed integer */
INTTYPE signednum=0;
switch (pf->size) {
case INTSZ:
/* %d */
signednum = va_arg(pf->ap, int);
break;
case LONGSZ:
/* %ld */
signednum = va_arg(pf->ap, long);
break;
static void __pf_getnum(PF *pf, int ch) {
if (ch == 'p') {
/*
* Pointer.
*
* uintptr_t is a C99 standard type that's an unsigned
* integer the same size as a pointer.
*/
pf->num = (uintptr_t)va_arg(pf->ap, void *);
} else if (ch == 'd') {
/* signed integer */
INTTYPE signednum = 0;
switch (pf->size) {
case INTSZ:
/* %d */
signednum = va_arg(pf->ap, int);
break;
case LONGSZ:
/* %ld */
signednum = va_arg(pf->ap, long);
break;
#ifdef USE_LONGLONG
case LLONGSZ:
/* %lld */
signednum = va_arg(pf->ap, long long);
break;
case LLONGSZ:
/* %lld */
signednum = va_arg(pf->ap, long long);
break;
#endif
case SIZETSZ:
/* %zd */
signednum = va_arg(pf->ap, ssize_t);
break;
}
case SIZETSZ:
/* %zd */
signednum = va_arg(pf->ap, ssize_t);
break;
}
/*
* Check for negative numbers.
*/
if (signednum < 0) {
pf->sign = -1;
pf->num = -signednum;
}
else {
pf->num = signednum;
}
}
else {
/* unsigned integer */
switch (pf->size) {
case INTSZ:
/* %u (or %o, %x) */
pf->num = va_arg(pf->ap, unsigned int);
break;
case LONGSZ:
/* %lu (or %lo, %lx) */
pf->num = va_arg(pf->ap, unsigned long);
break;
/*
* Check for negative numbers.
*/
if (signednum < 0) {
pf->sign = -1;
pf->num = -signednum;
} else {
pf->num = signednum;
}
} else {
/* unsigned integer */
switch (pf->size) {
case INTSZ:
/* %u (or %o, %x) */
pf->num = va_arg(pf->ap, unsigned int);
break;
case LONGSZ:
/* %lu (or %lo, %lx) */
pf->num = va_arg(pf->ap, unsigned long);
break;
#ifdef USE_LONGLONG
case LLONGSZ:
/* %llu, %llo, %llx */
pf->num = va_arg(pf->ap, unsigned long long);
break;
case LLONGSZ:
/* %llu, %llo, %llx */
pf->num = va_arg(pf->ap, unsigned long long);
break;
#endif
case SIZETSZ:
/* %zu, %zo, %zx */
pf->num = va_arg(pf->ap, size_t);
break;
}
}
case SIZETSZ:
/* %zu, %zo, %zx */
pf->num = va_arg(pf->ap, size_t);
break;
}
}
}
/*
@@ -320,40 +301,34 @@ __pf_getnum(PF *pf, int ch)
* If the "alternate format" was requested, or always for pointers,
* note to print the C prefix for the type.
*/
static
void
__pf_setbase(PF *pf, int ch)
{
switch (ch) {
case 'd':
case 'u':
pf->base = 10;
break;
case 'x':
case 'p':
pf->base = 16;
break;
case 'o':
pf->base = 8;
break;
}
if (pf->altformat || ch=='p') {
pf->baseprefix = 1;
}
static void __pf_setbase(PF *pf, int ch) {
switch (ch) {
case 'd':
case 'u':
pf->base = 10;
break;
case 'x':
case 'p':
pf->base = 16;
break;
case 'o':
pf->base = 8;
break;
}
if (pf->altformat || ch == 'p') {
pf->baseprefix = 1;
}
}
/*
* Function to print "spc" instances of the fill character.
*/
static
void
__pf_fill(PF *pf, int spc)
{
char f = pf->fillchar;
int i;
for (i=0; i<spc; i++) {
__pf_print(pf, &f, 1);
}
static void __pf_fill(PF *pf, int spc) {
char f = pf->fillchar;
int i;
for (i = 0; i < spc; i++) {
__pf_print(pf, &f, 1);
}
}
/*
@@ -362,45 +337,40 @@ __pf_fill(PF *pf, int spc)
* and the other is the sign) get printed *after* space padding but
* *before* zero padding, if padding is on the left.
*/
static
void
__pf_printstuff(PF *pf,
const char *prefix, const char *prefix2,
const char *stuff)
{
/* Total length to print. */
int len = strlen(prefix)+strlen(prefix2)+strlen(stuff);
static void __pf_printstuff(PF *pf, const char *prefix, const char *prefix2,
const char *stuff) {
/* Total length to print. */
int len = strlen(prefix) + strlen(prefix2) + strlen(stuff);
/* Get field width and compute amount of padding in "spc". */
int spc = pf->spacing;
if (spc > len) {
spc -= len;
}
else {
spc = 0;
}
/* Get field width and compute amount of padding in "spc". */
int spc = pf->spacing;
if (spc > len) {
spc -= len;
} else {
spc = 0;
}
/* If padding on left and the fill char is not 0, pad first. */
if (spc > 0 && pf->rightspc==0 && pf->fillchar!='0') {
__pf_fill(pf, spc);
}
/* If padding on left and the fill char is not 0, pad first. */
if (spc > 0 && pf->rightspc == 0 && pf->fillchar != '0') {
__pf_fill(pf, spc);
}
/* Print the prefixes. */
__pf_print(pf, prefix, strlen(prefix));
__pf_print(pf, prefix2, strlen(prefix2));
/* Print the prefixes. */
__pf_print(pf, prefix, strlen(prefix));
__pf_print(pf, prefix2, strlen(prefix2));
/* If padding on left and the fill char *is* 0, pad here. */
if (spc > 0 && pf->rightspc==0 && pf->fillchar=='0') {
__pf_fill(pf, spc);
}
/* If padding on left and the fill char *is* 0, pad here. */
if (spc > 0 && pf->rightspc == 0 && pf->fillchar == '0') {
__pf_fill(pf, spc);
}
/* Print the actual string. */
__pf_print(pf, stuff, strlen(stuff));
/* Print the actual string. */
__pf_print(pf, stuff, strlen(stuff));
/* If padding on the right, pad afterwards. */
if (spc > 0 && pf->rightspc!=0) {
__pf_fill(pf, spc);
}
/* If padding on the right, pad afterwards. */
if (spc > 0 && pf->rightspc != 0) {
__pf_fill(pf, spc);
}
}
/*
@@ -410,156 +380,142 @@ __pf_printstuff(PF *pf,
* NUMBER_BUF_SIZE is set so that the longest number string we can
* generate (a long long printed in octal) will fit. See above.
*/
static
void
__pf_printnum(PF *pf)
{
/* Digits to print with. */
const char *const digits = "0123456789abcdef";
static void __pf_printnum(PF *pf) {
/* Digits to print with. */
const char *const digits = "0123456789abcdef";
char buf[NUMBER_BUF_SIZE]; /* Accumulation buffer for string. */
char *x; /* Current pointer into buf. */
unsigned INTTYPE xnum; /* Current value to print. */
const char *bprefix; /* Base prefix (0, 0x, or nothing) */
const char *sprefix; /* Sign prefix (- or nothing) */
char buf[NUMBER_BUF_SIZE]; /* Accumulation buffer for string. */
char *x; /* Current pointer into buf. */
unsigned INTTYPE xnum; /* Current value to print. */
const char *bprefix; /* Base prefix (0, 0x, or nothing) */
const char *sprefix; /* Sign prefix (- or nothing) */
/* Start in the last slot of the buffer. */
x = buf+sizeof(buf)-1;
/* Start in the last slot of the buffer. */
x = buf + sizeof(buf) - 1;
/* Insert null terminator. */
*x-- = 0;
/* Insert null terminator. */
*x-- = 0;
/* Initialize value. */
xnum = pf->num;
/* Initialize value. */
xnum = pf->num;
/*
* Convert a single digit.
* Do this loop at least once - that way 0 prints as 0 and not "".
*/
do {
/*
* Get the digit character for the least significant
* part of xnum.
*/
*x = digits[xnum % pf->base];
/*
* Convert a single digit.
* Do this loop at least once - that way 0 prints as 0 and not "".
*/
do {
/*
* Get the digit character for the least significant
* part of xnum.
*/
*x = digits[xnum % pf->base];
/*
* Back up the pointer to point to the next space to the left.
*/
x--;
/*
* Back up the pointer to point to the next space to the left.
*/
x--;
/*
* Drop the value of the digit we just printed from xnum.
*/
xnum = xnum / pf->base;
/*
* Drop the value of the digit we just printed from xnum.
*/
xnum = xnum / pf->base;
/*
* If xnum hits 0 there's no more number left.
*/
} while (xnum > 0);
/*
* If xnum hits 0 there's no more number left.
*/
} while (xnum > 0);
/*
* x points to the *next* slot in the buffer to use.
* However, we're done printing the number. So it's pointing
* one slot *before* the start of the actual number text.
* So advance it by one so it actually points at the number.
*/
x++;
/*
* x points to the *next* slot in the buffer to use.
* However, we're done printing the number. So it's pointing
* one slot *before* the start of the actual number text.
* So advance it by one so it actually points at the number.
*/
x++;
/*
* If a base prefix was requested, select it.
*/
if (pf->baseprefix && pf->base==16) {
bprefix = "0x";
}
else if (pf->baseprefix && pf->base==8) {
bprefix = "0";
}
else {
bprefix = "";
}
/*
* If a base prefix was requested, select it.
*/
if (pf->baseprefix && pf->base == 16) {
bprefix = "0x";
} else if (pf->baseprefix && pf->base == 8) {
bprefix = "0";
} else {
bprefix = "";
}
/*
* Choose the sign prefix.
*/
sprefix = pf->sign ? "-" : "";
/*
* Choose the sign prefix.
*/
sprefix = pf->sign ? "-" : "";
/*
* Now actually print the string we just generated.
*/
__pf_printstuff(pf, sprefix, bprefix, x);
/*
* Now actually print the string we just generated.
*/
__pf_printstuff(pf, sprefix, bprefix, x);
}
/*
* Process a single character out of the format string.
*/
static
void
__pf_send(PF *pf, int ch)
{
/* Cannot get NULs here. */
assert(ch!=0);
static void __pf_send(PF *pf, int ch) {
/* Cannot get NULs here. */
assert(ch != 0);
if (pf->in_pct==0 && ch!='%') {
/*
* Not currently in a format, and not a %. Just send
* the character on through.
*/
char c = ch;
__pf_print(pf, &c, 1);
}
else if (pf->in_pct==0) {
/*
* Not in a format, but got a %. Start a format.
*/
pf->in_pct = 1;
}
else if (strchr("#-lz0123456789", ch)) {
/*
* These are the modifier characters we recognize.
* (These are the characters between the % and the type.)
*/
__pf_modifier(pf, ch);
}
else if (strchr("doupx", ch)) {
/*
* Integer types.
* Fetch the number, set the base, print it, then
* reset for the next format.
*/
__pf_getnum(pf, ch);
__pf_setbase(pf, ch);
__pf_printnum(pf);
__pf_endfield(pf);
}
else if (ch=='s') {
/*
* Print a string.
*/
const char *str = va_arg(pf->ap, const char *);
if (str==NULL) {
str = "(null)";
}
__pf_printstuff(pf, "", "", str);
__pf_endfield(pf);
}
else {
/*
* %%, %c, or illegal character.
* Illegal characters are printed like %%.
* for example, %5k prints " k".
*/
char x[2];
if (ch=='c') {
x[0] = va_arg(pf->ap, int);
}
else {
x[0] = ch;
}
x[1] = 0;
__pf_printstuff(pf, "", "", x);
__pf_endfield(pf);
}
if (pf->in_pct == 0 && ch != '%') {
/*
* Not currently in a format, and not a %. Just send
* the character on through.
*/
char c = ch;
__pf_print(pf, &c, 1);
} else if (pf->in_pct == 0) {
/*
* Not in a format, but got a %. Start a format.
*/
pf->in_pct = 1;
} else if (strchr("#-lz0123456789", ch)) {
/*
* These are the modifier characters we recognize.
* (These are the characters between the % and the type.)
*/
__pf_modifier(pf, ch);
} else if (strchr("doupx", ch)) {
/*
* Integer types.
* Fetch the number, set the base, print it, then
* reset for the next format.
*/
__pf_getnum(pf, ch);
__pf_setbase(pf, ch);
__pf_printnum(pf);
__pf_endfield(pf);
} else if (ch == 's') {
/*
* Print a string.
*/
const char *str = va_arg(pf->ap, const char *);
if (str == NULL) {
str = "(null)";
}
__pf_printstuff(pf, "", "", str);
__pf_endfield(pf);
} else {
/*
* %%, %c, or illegal character.
* Illegal characters are printed like %%.
* for example, %5k prints " k".
*/
char x[2];
if (ch == 'c') {
x[0] = va_arg(pf->ap, int);
} else {
x[0] = ch;
}
x[1] = 0;
__pf_printstuff(pf, "", "", x);
__pf_endfield(pf);
}
}
/*
@@ -567,26 +523,24 @@ __pf_send(PF *pf, int ch)
* Create and initialize a printf state object,
* then send it each character from the format string.
*/
int
__vprintf(void (*func)(void *clientdata, const char *str, size_t len),
void *clientdata, const char *format, va_list ap)
{
PF pf;
int i;
int __vprintf(void (*func)(void *clientdata, const char *str, size_t len),
void *clientdata, const char *format, va_list ap) {
PF pf;
int i;
pf.sendfunc = func;
pf.clientdata = clientdata;
pf.sendfunc = func;
pf.clientdata = clientdata;
#ifdef va_copy
va_copy(pf.ap, ap);
va_copy(pf.ap, ap);
#else
pf.ap = ap;
pf.ap = ap;
#endif
pf.charcount = 0;
__pf_endfield(&pf);
pf.charcount = 0;
__pf_endfield(&pf);
for (i=0; format[i]; i++) {
__pf_send(&pf, format[i]);
}
for (i = 0; format[i]; i++) {
__pf_send(&pf, format[i]);
}
return pf.charcount;
return pf.charcount;
}

View File

@@ -47,7 +47,6 @@
* Standard C string/IO function: printf into a character buffer.
*/
/*
* Context structure for snprintf: buffer to print into, maximum
* length, and index of the next character to write.
@@ -58,9 +57,9 @@
*/
typedef struct {
char *buf;
size_t buflen;
size_t bufpos;
char *buf;
size_t buflen;
size_t bufpos;
} SNP;
/*
@@ -70,88 +69,79 @@ typedef struct {
* null-terminated.
*/
static
void
__snprintf_send(void *mydata, const char *data, size_t len)
{
SNP *snp = mydata;
unsigned i;
static void __snprintf_send(void *mydata, const char *data, size_t len) {
SNP *snp = mydata;
unsigned i;
/* For each character we're sent... */
for (i=0; i<len; i++) {
/* For each character we're sent... */
for (i = 0; i < len; i++) {
/* If we aren't past the length, */
if (snp->bufpos < snp->buflen) {
/* If we aren't past the length, */
if (snp->bufpos < snp->buflen) {
/* store the character */
snp->buf[snp->bufpos] = data[i];
/* store the character */
snp->buf[snp->bufpos] = data[i];
/* and increment the position. */
snp->bufpos++;
}
}
/* and increment the position. */
snp->bufpos++;
}
}
}
/*
* The va_list version of snprintf.
*/
int
vsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
{
int chars;
SNP snp;
int vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
int chars;
SNP snp;
/*
* Fill in the context structure.
* We set snp.buflen to the number of characters that can be
* written (excluding the null terminator) so as not to have
* to special-case the possibility that we got passed a length
* of zero elsewhere.
*/
snp.buf = buf;
if (len==0) {
snp.buflen = 0;
}
else {
snp.buflen = len-1;
}
snp.bufpos = 0;
/*
* Fill in the context structure.
* We set snp.buflen to the number of characters that can be
* written (excluding the null terminator) so as not to have
* to special-case the possibility that we got passed a length
* of zero elsewhere.
*/
snp.buf = buf;
if (len == 0) {
snp.buflen = 0;
} else {
snp.buflen = len - 1;
}
snp.bufpos = 0;
/* Call __vprintf to do the actual work. */
chars = __vprintf(__snprintf_send, &snp, fmt, ap);
/* Call __vprintf to do the actual work. */
chars = __vprintf(__snprintf_send, &snp, fmt, ap);
/*
* Add a null terminator. If the length *we were passed* is greater
* than zero, we reserved a space in the buffer for the terminator,
* so this won't overflow. If the length we were passed is zero,
* nothing will have been or should be written anyway, and buf
* might even be NULL. (C99 explicitly endorses this possibility.)
*/
if (len > 0) {
buf[snp.bufpos] = 0;
}
/*
* Add a null terminator. If the length *we were passed* is greater
* than zero, we reserved a space in the buffer for the terminator,
* so this won't overflow. If the length we were passed is zero,
* nothing will have been or should be written anyway, and buf
* might even be NULL. (C99 explicitly endorses this possibility.)
*/
if (len > 0) {
buf[snp.bufpos] = 0;
}
/*
* Return the number of characters __vprintf processed.
* According to C99, snprintf should return this number, not
* the number of characters actually stored, and should not
* return -1 on overflow but only on other errors. (All none
* of them since we don't do multibyte characters...)
*/
return chars;
/*
* Return the number of characters __vprintf processed.
* According to C99, snprintf should return this number, not
* the number of characters actually stored, and should not
* return -1 on overflow but only on other errors. (All none
* of them since we don't do multibyte characters...)
*/
return chars;
}
/*
* snprintf - hand off to vsnprintf.
*/
int
snprintf(char *buf, size_t len, const char *fmt, ...)
{
int chars;
va_list ap;
va_start(ap, fmt);
chars = vsnprintf(buf, len, fmt, ap);
va_end(ap);
return chars;
int snprintf(char *buf, size_t len, const char *fmt, ...) {
int chars;
va_list ap;
va_start(ap, fmt);
chars = vsnprintf(buf, len, fmt, ap);
va_end(ap);
return chars;
}

View File

@@ -46,56 +46,53 @@
* really report syntax errors or overflow in any useful way.
*/
int
atoi(const char *s)
{
static const char digits[] = "0123456789"; /* legal digits in order */
unsigned val=0; /* value we're accumulating */
int neg=0; /* set to true if we see a minus sign */
int atoi(const char *s) {
static const char digits[] = "0123456789"; /* legal digits in order */
unsigned val = 0; /* value we're accumulating */
int neg = 0; /* set to true if we see a minus sign */
/* skip whitespace */
while (*s==' ' || *s=='\t') {
s++;
}
/* skip whitespace */
while (*s == ' ' || *s == '\t') {
s++;
}
/* check for sign */
if (*s=='-') {
neg=1;
s++;
}
else if (*s=='+') {
s++;
}
/* check for sign */
if (*s == '-') {
neg = 1;
s++;
} else if (*s == '+') {
s++;
}
/* process each digit */
while (*s) {
const char *where;
unsigned digit;
/* process each digit */
while (*s) {
const char *where;
unsigned digit;
/* look for the digit in the list of digits */
where = strchr(digits, *s);
if (where==NULL) {
/* not found; not a digit, so stop */
break;
}
/* look for the digit in the list of digits */
where = strchr(digits, *s);
if (where == NULL) {
/* not found; not a digit, so stop */
break;
}
/* get the index into the digit list, which is the value */
digit = (where - digits);
/* get the index into the digit list, which is the value */
digit = (where - digits);
/* could (should?) check for overflow here */
/* could (should?) check for overflow here */
/* shift the number over and add in the new digit */
val = val*10 + digit;
/* shift the number over and add in the new digit */
val = val * 10 + digit;
/* look at the next character */
s++;
}
/* look at the next character */
s++;
}
/* handle negative numbers */
if (neg) {
return -val;
}
/* handle negative numbers */
if (neg) {
return -val;
}
/* done */
return val;
/* done */
return val;
}

View File

@@ -45,32 +45,28 @@
* memory.
*/
void
bzero(void *vblock, size_t len)
{
char *block = vblock;
size_t i;
void bzero(void *vblock, size_t len) {
char *block = vblock;
size_t i;
/*
* For performance, optimize the common case where the pointer
* and the length are word-aligned, and write word-at-a-time
* instead of byte-at-a-time. Otherwise, write bytes.
*
* The alignment logic here should be portable. We rely on the
* compiler to be reasonably intelligent about optimizing the
* divides and moduli out. Fortunately, it is.
*/
/*
* For performance, optimize the common case where the pointer
* and the length are word-aligned, and write word-at-a-time
* instead of byte-at-a-time. Otherwise, write bytes.
*
* The alignment logic here should be portable. We rely on the
* compiler to be reasonably intelligent about optimizing the
* divides and moduli out. Fortunately, it is.
*/
if ((uintptr_t)block % sizeof(long) == 0 &&
len % sizeof(long) == 0) {
long *lb = (long *)block;
for (i=0; i<len/sizeof(long); i++) {
lb[i] = 0;
}
}
else {
for (i=0; i<len; i++) {
block[i] = 0;
}
}
if ((uintptr_t)block % sizeof(long) == 0 && len % sizeof(long) == 0) {
long *lb = (long *)block;
for (i = 0; i < len / sizeof(long); i++) {
lb[i] = 0;
}
} else {
for (i = 0; i < len; i++) {
block[i] = 0;
}
}
}

View File

@@ -44,43 +44,39 @@
* C standard function - copy a block of memory.
*/
void *
memcpy(void *dst, const void *src, size_t len)
{
size_t i;
void *memcpy(void *dst, const void *src, size_t len) {
size_t i;
/*
* memcpy does not support overlapping buffers, so always do it
* forwards. (Don't change this without adjusting memmove.)
*
* For speedy copying, optimize the common case where both pointers
* and the length are word-aligned, and copy word-at-a-time instead
* of byte-at-a-time. Otherwise, copy by bytes.
*
* The alignment logic below should be portable. We rely on
* the compiler to be reasonably intelligent about optimizing
* the divides and modulos out. Fortunately, it is.
*/
/*
* memcpy does not support overlapping buffers, so always do it
* forwards. (Don't change this without adjusting memmove.)
*
* For speedy copying, optimize the common case where both pointers
* and the length are word-aligned, and copy word-at-a-time instead
* of byte-at-a-time. Otherwise, copy by bytes.
*
* The alignment logic below should be portable. We rely on
* the compiler to be reasonably intelligent about optimizing
* the divides and modulos out. Fortunately, it is.
*/
if ((uintptr_t)dst % sizeof(long) == 0 &&
(uintptr_t)src % sizeof(long) == 0 &&
len % sizeof(long) == 0) {
if ((uintptr_t)dst % sizeof(long) == 0 &&
(uintptr_t)src % sizeof(long) == 0 && len % sizeof(long) == 0) {
long *d = dst;
const long *s = src;
long *d = dst;
const long *s = src;
for (i=0; i<len/sizeof(long); i++) {
d[i] = s[i];
}
}
else {
char *d = dst;
const char *s = src;
for (i = 0; i < len / sizeof(long); i++) {
d[i] = s[i];
}
} else {
char *d = dst;
const char *s = src;
for (i=0; i<len; i++) {
d[i] = s[i];
}
}
for (i = 0; i < len; i++) {
d[i] = s[i];
}
}
return dst;
return dst;
}

View File

@@ -45,73 +45,69 @@
* regions correctly.
*/
void *
memmove(void *dst, const void *src, size_t len)
{
size_t i;
void *memmove(void *dst, const void *src, size_t len) {
size_t i;
/*
* If the buffers don't overlap, it doesn't matter what direction
* we copy in. If they do, it does, so just assume they always do.
* We don't concern ourselves with the possibility that the region
* to copy might roll over across the top of memory, because it's
* not going to happen.
*
* If the destination is above the source, we have to copy
* back to front to avoid overwriting the data we want to
* copy.
*
* dest: dddddddd
* src: ssssssss ^
* | ^ |___|
* |___|
*
* If the destination is below the source, we have to copy
* front to back.
*
* dest: dddddddd
* src: ^ ssssssss
* |___| ^ |
* |___|
*/
/*
* If the buffers don't overlap, it doesn't matter what direction
* we copy in. If they do, it does, so just assume they always do.
* We don't concern ourselves with the possibility that the region
* to copy might roll over across the top of memory, because it's
* not going to happen.
*
* If the destination is above the source, we have to copy
* back to front to avoid overwriting the data we want to
* copy.
*
* dest: dddddddd
* src: ssssssss ^
* | ^ |___|
* |___|
*
* If the destination is below the source, we have to copy
* front to back.
*
* dest: dddddddd
* src: ^ ssssssss
* |___| ^ |
* |___|
*/
if ((uintptr_t)dst < (uintptr_t)src) {
/*
* As author/maintainer of libc, take advantage of the
* fact that we know memcpy copies forwards.
*/
return memcpy(dst, src, len);
}
if ((uintptr_t)dst < (uintptr_t)src) {
/*
* As author/maintainer of libc, take advantage of the
* fact that we know memcpy copies forwards.
*/
return memcpy(dst, src, len);
}
/*
* Copy by words in the common case. Look in memcpy.c for more
* information.
*/
/*
* Copy by words in the common case. Look in memcpy.c for more
* information.
*/
if ((uintptr_t)dst % sizeof(long) == 0 &&
(uintptr_t)src % sizeof(long) == 0 &&
len % sizeof(long) == 0) {
if ((uintptr_t)dst % sizeof(long) == 0 &&
(uintptr_t)src % sizeof(long) == 0 && len % sizeof(long) == 0) {
long *d = dst;
const long *s = src;
long *d = dst;
const long *s = src;
/*
* The reason we copy index i-1 and test i>0 is that
* i is unsigned -- so testing i>=0 doesn't work.
*/
/*
* The reason we copy index i-1 and test i>0 is that
* i is unsigned -- so testing i>=0 doesn't work.
*/
for (i=len/sizeof(long); i>0; i--) {
d[i-1] = s[i-1];
}
}
else {
char *d = dst;
const char *s = src;
for (i = len / sizeof(long); i > 0; i--) {
d[i - 1] = s[i - 1];
}
} else {
char *d = dst;
const char *s = src;
for (i=len; i>0; i--) {
d[i-1] = s[i-1];
}
}
for (i = len; i > 0; i--) {
d[i - 1] = s[i - 1];
}
}
return dst;
return dst;
}

View File

@@ -38,15 +38,13 @@
* C standard function - initialize a block of memory
*/
void *
memset(void *ptr, int ch, size_t len)
{
char *p = ptr;
size_t i;
void *memset(void *ptr, int ch, size_t len) {
char *p = ptr;
size_t i;
for (i=0; i<len; i++) {
p[i] = ch;
}
for (i = 0; i < len; i++) {
p[i] = ch;
}
return ptr;
return ptr;
}

View File

@@ -43,12 +43,10 @@
* Standard C string function: append one string to another.
*/
char *
strcat(char *dest, const char *src)
{
size_t offset;
char *strcat(char *dest, const char *src) {
size_t offset;
offset = strlen(dest);
strcpy(dest+offset, src);
return dest;
offset = strlen(dest);
strcpy(dest + offset, src);
return dest;
}

View File

@@ -43,26 +43,24 @@
* C standard string function: find leftmost instance of a character
* in a string.
*/
char *
strchr(const char *s, int ch_arg)
{
/* avoid sign-extension problems */
const char ch = ch_arg;
char *strchr(const char *s, int ch_arg) {
/* avoid sign-extension problems */
const char ch = ch_arg;
/* scan from left to right */
while (*s) {
/* if we hit it, return it */
if (*s == ch) {
return (char *)s;
}
s++;
}
/* scan from left to right */
while (*s) {
/* if we hit it, return it */
if (*s == ch) {
return (char *)s;
}
s++;
}
/* if we were looking for the 0, return that */
if (*s == ch) {
return (char *)s;
}
/* if we were looking for the 0, return that */
if (*s == ch) {
return (char *)s;
}
/* didn't find it */
return NULL;
/* didn't find it */
return NULL;
}

View File

@@ -44,47 +44,44 @@
* sort order.
*/
int
strcmp(const char *a, const char *b)
{
size_t i;
int strcmp(const char *a, const char *b) {
size_t i;
/*
* Walk down both strings until either they're different
* or we hit the end of A.
*
* If A and B strings are not the same length, when the
* shorter one ends, the two will be different, and we'll
* stop before running off the end of either.
*
* If they *are* the same length, it's sufficient to check
* that we haven't run off the end of A, because that's the
* same as checking to make sure we haven't run off the end of
* B.
*/
/*
* Walk down both strings until either they're different
* or we hit the end of A.
*
* If A and B strings are not the same length, when the
* shorter one ends, the two will be different, and we'll
* stop before running off the end of either.
*
* If they *are* the same length, it's sufficient to check
* that we haven't run off the end of A, because that's the
* same as checking to make sure we haven't run off the end of
* B.
*/
for (i=0; a[i]!=0 && a[i]==b[i]; i++) {
/* nothing */
}
for (i = 0; a[i] != 0 && a[i] == b[i]; i++) {
/* nothing */
}
/*
* If A is greater than B, return 1. If A is less than B,
* return -1. If they're the same, return 0. Since we have
* stopped at the first character of difference (or the end of
* both strings) checking the character under I accomplishes
* this.
*
* Note that strcmp does not handle accented characters,
* internationalization, or locale sort order; strcoll() does
* that.
*
* The rules say we compare order in terms of *unsigned* char.
*/
if ((unsigned char)a[i] > (unsigned char)b[i]) {
return 1;
}
else if (a[i] == b[i]) {
return 0;
}
return -1;
/*
* If A is greater than B, return 1. If A is less than B,
* return -1. If they're the same, return 0. Since we have
* stopped at the first character of difference (or the end of
* both strings) checking the character under I accomplishes
* this.
*
* Note that strcmp does not handle accented characters,
* internationalization, or locale sort order; strcoll() does
* that.
*
* The rules say we compare order in terms of *unsigned* char.
*/
if ((unsigned char)a[i] > (unsigned char)b[i]) {
return 1;
} else if (a[i] == b[i]) {
return 0;
}
return -1;
}

View File

@@ -42,22 +42,20 @@
/*
* Standard C string function: copy one string to another.
*/
char *
strcpy(char *dest, const char *src)
{
size_t i;
char *strcpy(char *dest, const char *src) {
size_t i;
/*
* Copy characters until we hit the null terminator.
*/
for (i=0; src[i]; i++) {
dest[i] = src[i];
}
/*
* Copy characters until we hit the null terminator.
*/
for (i = 0; src[i]; i++) {
dest[i] = src[i];
}
/*
* Add null terminator to result.
*/
dest[i] = 0;
/*
* Add null terminator to result.
*/
dest[i] = 0;
return dest;
return dest;
}

View File

@@ -43,13 +43,11 @@
* C standard string function: get length of a string
*/
size_t
strlen(const char *str)
{
size_t ret = 0;
size_t strlen(const char *str) {
size_t ret = 0;
while (str[ret]) {
ret++;
}
return ret;
while (str[ret]) {
ret++;
}
return ret;
}

View File

@@ -43,27 +43,25 @@
* C standard string function: find rightmost instance of a character
* in a string.
*/
char *
strrchr(const char *s, int ch_arg)
{
/* avoid sign-extension problems */
const char ch = ch_arg;
char *strrchr(const char *s, int ch_arg) {
/* avoid sign-extension problems */
const char ch = ch_arg;
/* start one past the last character INCLUDING NULL TERMINATOR */
size_t i = strlen(s)+1;
/* start one past the last character INCLUDING NULL TERMINATOR */
size_t i = strlen(s) + 1;
/* go from right to left; stop at 0 */
while (i > 0) {
/* go from right to left; stop at 0 */
while (i > 0) {
/* decrement first */
i--;
/* decrement first */
i--;
/* now check the character we're over */
if (s[i] == ch) {
return (char *)(s+i);
}
}
/* now check the character we're over */
if (s[i] == ch) {
return (char *)(s + i);
}
}
/* didn't find it */
return NULL;
/* didn't find it */
return NULL;
}

View File

@@ -46,51 +46,48 @@
* The "context" argument should point to a "char *" that is preserved
* between calls to strtok_r that wish to operate on same string.
*/
char *
strtok_r(char *string, const char *seps, char **context)
{
char *head; /* start of word */
char *tail; /* end of word */
char *strtok_r(char *string, const char *seps, char **context) {
char *head; /* start of word */
char *tail; /* end of word */
/* If we're starting up, initialize context */
if (string) {
*context = string;
}
/* If we're starting up, initialize context */
if (string) {
*context = string;
}
/* Get potential start of this next word */
head = *context;
if (head == NULL) {
return NULL;
}
/* Get potential start of this next word */
head = *context;
if (head == NULL) {
return NULL;
}
/* Skip any leading separators */
while (*head && strchr(seps, *head)) {
head++;
}
/* Skip any leading separators */
while (*head && strchr(seps, *head)) {
head++;
}
/* Did we hit the end? */
if (*head == 0) {
/* Nothing left */
*context = NULL;
return NULL;
}
/* Did we hit the end? */
if (*head == 0) {
/* Nothing left */
*context = NULL;
return NULL;
}
/* skip over word */
tail = head;
while (*tail && !strchr(seps, *tail)) {
tail++;
}
/* skip over word */
tail = head;
while (*tail && !strchr(seps, *tail)) {
tail++;
}
/* Save head for next time in context */
if (*tail == 0) {
*context = NULL;
}
else {
*tail = 0;
tail++;
*context = tail;
}
/* Save head for next time in context */
if (*tail == 0) {
*context = NULL;
} else {
*tail = 0;
tail++;
*context = tail;
}
/* Return current word */
return head;
/* Return current word */
return head;
}