mirror of /home/gitosis/repositories/libowfat.git

commit
28486b8367
156 changed files with 3007 additions and 0 deletions
@ -0,0 +1,30 @@
|
||||
0.6: |
||||
changed name to libowfat |
||||
|
||||
0.5: |
||||
made subdirectories for the different libraries. |
||||
moved the sources into the corresponding subdirectory. |
||||
imported my man pages from libdjb. |
||||
removed fmt_int.c and fmt_uint.c (they are macros in fmt.h). |
||||
corrected comment in open.h for open_excl. |
||||
wrote new man pages for fmt_double, scan_double, the sign fmt_ and |
||||
scan_ routines, the whitespace and charset scan_ routines, and the |
||||
str and stralloc routines. |
||||
|
||||
0.4: |
||||
implemented stralloc. |
||||
|
||||
0.3: |
||||
implemented uint16, uint32 and uint64. The header files try to |
||||
define shortcut endianness conversion routines that do not convert |
||||
anything. |
||||
implemented open (I hope I got open_excl right, I couldn't find an |
||||
implementationen). |
||||
|
||||
0.2: |
||||
implemented the scan, fmt and str interfaces. |
||||
added adapted fmt_double and scan_double from diet libc. |
||||
|
||||
0.1: |
||||
initial release. |
||||
implemented the byte interface. |
@ -0,0 +1,50 @@
|
||||
all: t byte.a fmt.a scan.a str.a uint.a open.a stralloc.a unix.a socket.a |
||||
|
||||
VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket
|
||||
|
||||
CC=egcc
|
||||
#CFLAGS=-I. -pipe -Wall -Os -march=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall
|
||||
CFLAGS=-I. -I../dietlibc/include -pipe -Wall -g #-Os -march=athlon -mcpu=athlon -fomit-frame-pointer -fschedule-insns2
|
||||
#CFLAGS=-I../dietlibc/include -I. -pipe -Wall -Os -march=pentiumpro -mcpu=athlon -fomit-frame-pointer -fschedule-insns2 -Wall
|
||||
#CFLAGS=-I../dietlibc/include -pipe -Os -march=pentiumpro -mcpu=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall
|
||||
|
||||
BYTE_OBJS=$(patsubst byte/%.c,%.o,$(wildcard byte/*.c))
|
||||
FMT_OBJS=$(patsubst fmt/%.c,%.o,$(wildcard fmt/*.c))
|
||||
SCAN_OBJS=$(patsubst scan/%.c,%.o,$(wildcard scan/*.c))
|
||||
STR_OBJS=$(patsubst str/%.c,%.o,$(wildcard str/*.c))
|
||||
UINT_OBJS=$(patsubst uint/%.c,%.o,$(wildcard uint/*.c))
|
||||
OPEN_OBJS=$(patsubst open/%.c,%.o,$(wildcard open/*.c))
|
||||
STRA_OBJS=$(patsubst stralloc/%.c,%.o,$(wildcard stralloc/*.c))
|
||||
UNIX_OBJS=$(patsubst unix/%.c,%.o,$(wildcard unix/*.c))
|
||||
SOCKET_OBJS=$(patsubst socket/%.c,%.o,$(wildcard socket/*.c))
|
||||
|
||||
$(BYTE_OBJS): byte.h |
||||
$(FMT_OBJS): fmt.h |
||||
$(SCAN_OBJS): scan.h |
||||
$(STR_OBJS): str.h |
||||
$(UINT_OBJS): uint16.h uint32.h |
||||
$(STRA_OBJS): stralloc.h |
||||
$(SOCKET_OBJS): socket.h |
||||
|
||||
byte.a: $(BYTE_OBJS) |
||||
fmt.a: $(FMT_OBJS) |
||||
scan.a: $(SCAN_OBJS) |
||||
str.a: $(STR_OBJS) |
||||
uint.a: $(UINT_OBJS) |
||||
open.a: $(OPEN_OBJS) |
||||
stralloc.a: $(STRA_OBJS) |
||||
unix.a: $(UNIX_OBJS) |
||||
socket.a: $(SOCKET_OBJS) |
||||
|
||||
%.a: |
||||
ar cr $@ $^
|
||||
|
||||
t: t.o socket.a stralloc.a str.a fmt.a scan.a str.a uint.a open.a byte.a |
||||
gcc -g -o $@ $^
|
||||
|
||||
.PHONY: clean tar |
||||
clean: |
||||
rm -f *.o *.a core t
|
||||
|
||||
tar: |
||||
cd .. && tar cIf libowfat.tar.bz2 libowfat
|
@ -0,0 +1,41 @@
|
||||
#ifndef BYTE_H |
||||
#define BYTE_H |
||||
|
||||
#include <sys/cdefs.h> |
||||
|
||||
#ifndef __pure__ |
||||
#define __pure__ |
||||
#endif |
||||
#ifndef __THROW |
||||
#define __THROW |
||||
#endif |
||||
|
||||
/* byte_chr returns the smallest integer i between 0 and len-1
|
||||
* inclusive such that one[i] equals needle, or len it not found. */ |
||||
unsigned int byte_chr(const void* haystack, unsigned int len, char needle) __THROW __pure__; |
||||
|
||||
/* byte_rchr returns the largest integer i between 0 and len-1 inclusive
|
||||
* such that one[i] equals needle, or len if not found. */ |
||||
unsigned int byte_rchr(const void* haystack,unsigned int len,char needle) __THROW __pure__; |
||||
|
||||
/* byte_copy copies in[0] to out[0], in[1] to out[1], ... and in[len-1]
|
||||
* to out[len-1]. */ |
||||
void byte_copy(void* out, unsigned int len, const void* in) __THROW; |
||||
|
||||
/* byte_copyr copies in[len-1] to out[len-1], in[len-2] to out[len-2],
|
||||
* ... and in[0] to out[0] */ |
||||
void byte_copyr(void* out, unsigned int len, const void* in) __THROW; |
||||
|
||||
/* byte_diff returns negative, 0, or positive, depending on whether the
|
||||
* string a[0], a[1], ..., a[len-1] is lexicographically smaller |
||||
* than, equal to, or greater than the string b[0], b[1], ..., |
||||
* b[len-1]. When the strings are different, byte_diff does not read |
||||
* bytes past the first difference. */ |
||||
int byte_diff(const void* a, unsigned int len, const void* b) __THROW __pure__; |
||||
|
||||
/* byte_zero sets the bytes out[0], out[1], ..., out[len-1] to 0 */ |
||||
void byte_zero(void* out, unsigned len) __THROW; |
||||
|
||||
#define byte_equal(s,n,t) (!byte_diff((s),(n),(t))) |
||||
|
||||
#endif |
@ -0,0 +1,19 @@
|
||||
.TH byte_chr 3 |
||||
.SH NAME |
||||
byte_chr \- search for a byte in a string |
||||
.SH SYNTAX |
||||
.B #include <byte.h> |
||||
|
||||
int \fBbyte_chr\fP(const char *\fIhaystack\fR,unsigned int \fIlen\fR,char \fIneedle\fR); |
||||
.SH DESCRIPTION |
||||
\fIbyte_chr\fR returns the smallest integer \fIi\fR between 0 and |
||||
\fIlen\fR-1 inclusive such that \fIone\fR[\fIi\fR] equals \fIneedle\fR. |
||||
|
||||
If no such integer exists, byte_chr returns \fIlen\fR. |
||||
|
||||
byte_chr may read all bytes \fIone\fR[0], \fIone\fR[1], ..., |
||||
\fIone\fR[\fIlen\fR-1], even if not all the bytes are relevant to the |
||||
answer. |
||||
|
||||
.SH "SEE ALSO" |
||||
byte_rchr(3) |
@ -0,0 +1,16 @@
|
||||
#include "byte.h" |
||||
|
||||
/* byte_chr returns the smallest integer i between 0 and len-1
|
||||
* inclusive such that one[i] equals needle, or len it not found. */ |
||||
unsigned int byte_chr(const void* haystack, unsigned int len, char needle) { |
||||
register char c=needle; |
||||
register const char* s=haystack; |
||||
register const char* t=s+len; |
||||
for (;;) { |
||||
if (s==t) break; if (*s==c) break; ++s; |
||||
if (s==t) break; if (*s==c) break; ++s; |
||||
if (s==t) break; if (*s==c) break; ++s; |
||||
if (s==t) break; if (*s==c) break; ++s; |
||||
} |
||||
return s-(const char*)haystack; |
||||
} |
@ -0,0 +1,14 @@
|
||||
.TH byte_copy 3 |
||||
.SH NAME |
||||
byte_copy \- copy a string |
||||
.SH SYNTAX |
||||
.B #include <byte.h> |
||||
|
||||
void \fBbyte_copy\fP(char *\fIout\fR,unsigned int \fIlen\fR,const char *\fIin\fR); |
||||
.SH DESCRIPTION |
||||
\fIbyte_copy\fR copies \fIin\fR[0] to \fIout\fR[0], \fIin\fR[1] to |
||||
\fIout\fR[1], etc., and finally \fIin\fR[\fIlen\fR-1] to |
||||
\fIout\fR[\fIlen\fR-1]. |
||||
|
||||
.SH "SEE ALSO" |
||||
byte_copyr(3) |
@ -0,0 +1,15 @@
|
||||
#include "byte.h" |
||||
|
||||
/* byte_copy copies in[0] to out[0], in[1] to out[1], ... and in[len-1]
|
||||
* to out[len-1]. */ |
||||
void byte_copy(void* out, unsigned int len, const void* in) { |
||||
register char* s=out; |
||||
register const char* t=in; |
||||
register const char* u=in+len; |
||||
for (;;) { |
||||
if (t==u) break; *s=*t; ++s; ++t; |
||||
if (t==u) break; *s=*t; ++s; ++t; |
||||
if (t==u) break; *s=*t; ++s; ++t; |
||||
if (t==u) break; *s=*t; ++s; ++t; |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
.TH byte_copyr 3 |
||||
.SH NAME |
||||
byte_copyr \- copy a string |
||||
.SH SYNTAX |
||||
.B #include <byte.h> |
||||
|
||||
void \fBbyte_copyr\fP(char *\fIout\fR,unsigned int \fIlen\fR,const char *\fIin\fR); |
||||
.SH DESCRIPTION |
||||
\fIbyte_copyr\fR copies \fIin\fR[\fIlen\fR-1] to \fIout\fR[\fIlen\fR-1], |
||||
\fIin\fR[\fIlen\fR-2] to \fIout\fR[\fIlen\fR-2], etc., and |
||||
\fIin\fR[0] to \fIout\fR[0]. |
||||
|
||||
.SH "SEE ALSO" |
||||
byte_copy(3) |
@ -0,0 +1,15 @@
|
||||
#include "byte.h" |
||||
|
||||
/* byte_copyr copies in[len-1] to out[len-1], in[len-2] to out[len-2],
|
||||
* ... and in[0] to out[0] */ |
||||
void byte_copyr(void* out, unsigned int len, const void* in) { |
||||
register char* s=out+len; |
||||
register const char* t=in; |
||||
register const char* u=t+len; |
||||
for (;;) { |
||||
if (t>=u) break; --u; --s; *s=*u; |
||||
if (t>=u) break; --u; --s; *s=*u; |
||||
if (t>=u) break; --u; --s; *s=*u; |
||||
if (t>=u) break; --u; --s; *s=*u; |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
.TH byte_diff 3 |
||||
.SH NAME |
||||
byte_diff \- compare two strings |
||||
.SH SYNTAX |
||||
.B #include <byte.h> |
||||
|
||||
int \fBbyte_diff\fP(const char *\fIone\fR,unsigned int \fIlen\fR,const char *\fItwo\fR); |
||||
.SH DESCRIPTION |
||||
\fIbyte_diff\fR returns negative, 0, or positive, depending on whether |
||||
the string \fIone\fR[0], \fIone\fR[1], ..., \fIone\fR[\fIlen\fR-1] is |
||||
lexicographically smaller than, equal to, or greater than the string |
||||
\fIone\fR[0], \fIone\fR[1], ..., \fIone\fR[\fIlen\fR-1]. |
||||
|
||||
When the strings are different, byte_diff does not read bytes past the |
||||
first difference. |
||||
|
||||
.SH "SEE ALSO" |
||||
byte_equal(3) |
@ -0,0 +1,21 @@
|
||||
#include "byte.h" |
||||
|
||||
/* byte_diff returns negative, 0, or positive, depending on whether the
|
||||
* string one[0], one[1], ..., one[len-1] is lexicographically smaller |
||||
* than, equal to, or greater than the string one[0], one[1], ..., |
||||
* one[len-1]. When the strings are different, byte_diff does not read |
||||
* bytes past the first difference. */ |
||||
int byte_diff(const void* a, unsigned int len, const void* b) { |
||||
register const char* s=a; |
||||
register const char* t=b; |
||||
register const char* u=b+len; |
||||
register int j; |
||||
j=0; |
||||
for (;;) { |
||||
if (t==u) break; if ((j=(*s-*t))) break; ++s; ++t; |
||||
if (t==u) break; if ((j=(*s-*t))) break; ++s; ++t; |
||||
if (t==u) break; if ((j=(*s-*t))) break; ++s; ++t; |
||||
if (t==u) break; if ((j=(*s-*t))) break; ++s; ++t; |
||||
} |
||||
return j; |
||||
} |
@ -0,0 +1,15 @@
|
||||
.TH byte_equal 3 |
||||
.SH NAME |
||||
byte_equal \- compare two strings |
||||
.SH SYNTAX |
||||
.B #include <byte.h> |
||||
|
||||
int \fBbyte_equal\fP(const char *\fIone\fR,unsigned int \fIlen\fR,const char *\fItwo\fR); |
||||
.SH DESCRIPTION |
||||
\fIbyte_equal\fR returns 1 if the strings are equal, 0 otherwise. |
||||
|
||||
When the strings are different, byte_equal does not read bytes past the |
||||
first difference. |
||||
|
||||
.SH "SEE ALSO" |
||||
byte_diff(3) |
@ -0,0 +1,19 @@
|
||||
.TH byte_rchr 3 |
||||
.SH NAME |
||||
byte_rchr \- search for a byte in a string |
||||
.SH SYNTAX |
||||
.B #include <byte.h> |
||||
|
||||
int \fBbyte_rchr\fP(const char *\fIhaystack\fR,unsigned int \fIlen\fR,char \fIneedle\fR); |
||||
.SH DESCRIPTION |
||||
\fIbyte_chr\fR returns the largest integer \fIi\fR between 0 and |
||||
\fIlen\fR-1 inclusive such that \fIone\fR[\fIi\fR] equals \fIneedle\fR. |
||||
|
||||
If no such integer exists, byte_chr returns \fIlen\fR. |
||||
|
||||
byte_rchr may read all bytes \fIone\fR[0], \fIone\fR[1], ..., |
||||
\fIone\fR[\fIlen\fR-1], even if not all the bytes are relevant to the |
||||
answer. |
||||
|
||||
.SH "SEE ALSO" |
||||
byte_chr(3) |
@ -0,0 +1,16 @@
|
||||
#include "byte.h" |
||||
|
||||
/* byte_rchr returns the largest integer i between 0 and len-1 inclusive
|
||||
* such that one[i] equals needle, or len if not found. */ |
||||
unsigned int byte_rchr(const void* haystack,unsigned int len,char needle) { |
||||
register char c=needle; |
||||
register const char* s=haystack; |
||||
register const char* t=s+len; |
||||
for (;;) { |
||||
--t; if (s<=t) break; if (*t==c) break; |
||||
--t; if (s<=t) break; if (*t==c) break; |
||||
--t; if (s<=t) break; if (*t==c) break; |
||||
--t; if (s<=t) break; if (*t==c) break; |
||||
} |
||||
return t-s; |
||||
} |
@ -0,0 +1,13 @@
|
||||
.TH byte_zero 3 |
||||
.SH NAME |
||||
byte_zero \- initialize a string |
||||
.SH SYNTAX |
||||
.B #include <byte.h> |
||||
|
||||
void \fBbyte_zero\fP(char *\fIout\fR,unsigned int \fIlen\fR); |
||||
.SH DESCRIPTION |
||||
\fIbyte_zero\fR sets \fIout\fR[0], \fIout\fR[1], ..., |
||||
\fIout\fR[\fIlen\fR-1] to 0. |
||||
|
||||
.SH "SEE ALSO" |
||||
byte_copy(3), byte_copyr(3) |
@ -0,0 +1,13 @@
|
||||
#include "byte.h" |
||||
|
||||
/* byte_zero sets the bytes out[0], out[1], ..., out[len-1] to 0 */ |
||||
void byte_zero(void* out, unsigned len) { |
||||
register char* s=out; |
||||
register const char* t=s+len; |
||||
for (;;) { |
||||
if (s==t) break; *s=0; ++s; |
||||
if (s==t) break; *s=0; ++s; |
||||
if (s==t) break; *s=0; ++s; |
||||
if (s==t) break; *s=0; ++s; |
||||
} |
||||
} |
@ -0,0 +1,61 @@
|
||||
#ifndef FMT_H |
||||
#define FMT_H |
||||
|
||||
#include "str.h" |
||||
|
||||
#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */ |
||||
#define FMT_8LONG 44 /* enough space to hold 2^128 - 1 in octal, plus \0 */ |
||||
#define FMT_XLONG 33 /* enough space to hold 2^128 - 1 in hexadecimal, plus \0 */ |
||||
#define FMT_LEN ((char *) 0) /* convenient abbreviation */ |
||||
|
||||
/* The formatting routines do not append \0!
|
||||
* Use them like this: buf[fmt_ulong(buf,number)]=0; */ |
||||
|
||||
/* convert signed src integer -23 to ASCII '-','2','3', return length.
|
||||
* If dest is not NULL, write result to dest */ |
||||
unsigned int fmt_long(char *dest,signed long src) __THROW; |
||||
|
||||
/* convert unsigned src integer 23 to ASCII '2','3', return length.
|
||||
* If dest is not NULL, write result to dest */ |
||||
unsigned int fmt_ulong(char *dest,unsigned long src) __THROW; |
||||
|
||||
/* convert unsigned src integer 0x23 to ASCII '2','3', return length.
|
||||
* If dest is not NULL, write result to dest */ |
||||
unsigned int fmt_xlong(char *dest,unsigned long src) __THROW; |
||||
|
||||
/* convert unsigned src integer 023 to ASCII '2','3', return length.
|
||||
* If dest is not NULL, write result to dest */ |
||||
unsigned int fmt_8long(char *dest,unsigned long src) __THROW; |
||||
|
||||
#define fmt_uint(dest,src) fmt_ulong(dest,src) |
||||
#define fmt_int(dest,src) fmt_long(dest,src) |
||||
#define fmt_xint(dest,src) fmt_xlong(dest,src) |
||||
#define fmt_8int(dest,src) fmt_8long(dest,src) |
||||
|
||||
/* Like fmt_ulong, but prepend '0' while length is smaller than padto.
|
||||
* Does not truncate! */ |
||||
unsigned int fmt_ulong0(char *,unsigned long src,unsigned int padto) __THROW; |
||||
|
||||
#define fmt_uint0(buf,src,padto) fmt_ulong0(buf,src,padto) |
||||
|
||||
/* convert src double 1.7 to ASCII '1','.','7', return length.
|
||||
* If dest is not NULL, write result to dest */ |
||||
unsigned int fmt_double(char *dest, double d,int max,int prec) __THROW; |
||||
|
||||
/* if src is negative, write '-' and return 1.
|
||||
* if src is positive, write '+' and return 1. |
||||
* otherwise return 0 */ |
||||
unsigned int fmt_plusminus(char *dest,int src) __THROW; |
||||
|
||||
/* if src is negative, write '-' and return 1.
|
||||
* otherwise return 0. */ |
||||
unsigned int fmt_minus(char *dest,int src) __THROW; |
||||
|
||||
/* copy str to dest until \0 byte, return number of copied bytes. */ |
||||
unsigned int fmt_str(char *dest,const char *src) __THROW; |
||||
|
||||
/* copy str to dest until \0 byte or limit bytes copied.
|
||||
* return number of copied bytes. */ |
||||
unsigned int fmt_strn(char *dest,const char *src,unsigned int limit) __THROW; |
||||
|
||||
#endif |
@ -0,0 +1,20 @@
|
||||
.TH fmt_8long 3 |
||||
.SH NAME |
||||
fmt_8long \- write an octal ASCII representation of an unsigned long integer |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_8long\fP(char *\fIdest\fR,unsigned long \fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_8long writes an ASCII representation ('0' to '7', base 8) of |
||||
\fIsource\fR to \fIdest\fR and returns the number of bytes written. |
||||
|
||||
fmt_8long does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_8long returns the |
||||
number of bytes it would have written. |
||||
|
||||
For convenience, fmt.h defines the integer FMT_8LONG to be big enough to |
||||
contain every possible fmt_8long output plus \\0. |
||||
.SH "SEE ALSO" |
||||
scan_8long(3) |
@ -0,0 +1,11 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_8long(char *dest,unsigned long i) { |
||||
register unsigned long len,tmp; |
||||
/* first count the number of bytes needed */ |
||||
for (len=1, tmp=i; tmp>7; ++len) tmp/=8; |
||||
if (dest) |
||||
for (tmp=i, dest+=len; tmp; tmp/=8) |
||||
*--dest = (tmp&7)+'0'; |
||||
return len; |
||||
} |
@ -0,0 +1,20 @@
|
||||
.TH fmt_double 3 |
||||
.SH NAME |
||||
fmt_double \- write an ASCII representation of a double |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_double\fP(char *\fIdest\fR,double \fId\fR,int |
||||
\fImaxlen\fR,int \fIprec\fR); |
||||
.SH DESCRIPTION |
||||
fmt_double writes an ASCII representation ('0' to '9', base 10) of |
||||
\fId\fR to \fIdest\fR and returns the number of bytes written. No more |
||||
than \fImaxlen\fR bytes will be written. \fIprec\fR digits will be |
||||
written, using scientific notation if necessary. |
||||
|
||||
fmt_double does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_double returns the |
||||
number of bytes it would have written. |
||||
.SH "SEE ALSO" |
||||
scan_double(3) |
@ -0,0 +1,82 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_double(char *dest, double d,int maxlen,int prec) { |
||||
unsigned long long *x=(unsigned long long *)&d; |
||||
/* step 1: extract sign, mantissa and exponent */ |
||||
signed int s=*x>>63; |
||||
signed long e=((*x>>52)&((1<<11)-1))-1023; |
||||
/* unsigned long long m=*x & ((1ull<<52)-1); */ |
||||
/* step 2: exponent is base 2, compute exponent for base 10 */ |
||||
signed long e10=1+(long)(e*0.30102999566398119802); /* log10(2) */ |
||||
/* step 3: calculate 10^e10 */ |
||||
int i; |
||||
double tmp=10.0; |
||||
char *oldbuf=dest; |
||||
int initial=1; |
||||
int writeok=(dest!=0); |
||||
|
||||
if (s) { d=-d; if (writeok) *dest='-'; --maxlen; dest++; } |
||||
if ((i=e10)>=0) { |
||||
while (i>10) { tmp=tmp*1e10; i-=10; } |
||||
while (i>1) { tmp=tmp*10; --i; } |
||||
} else { |
||||
i=(e10=-e10); |
||||
while (i>10) { tmp=tmp*1e-10; i-=10; } |
||||
while (i>1) { tmp=tmp/10; --i; } |
||||
} |
||||
while (d/tmp<1) { |
||||
--e10; |
||||
tmp/=10.0; |
||||
} |
||||
/* step 4: see if precision is sufficient to display all digits */ |
||||
if (e10>prec) { |
||||
/* use scientific notation */ |
||||
int len=fmt_double(writeok?dest:0,d/tmp,maxlen,prec); |
||||
if (len==0) return 0; |
||||
maxlen-=len; dest+=len; |
||||
if (--maxlen>=0) { |
||||
if (writeok) *dest='e'; |
||||
++dest; |
||||
} |
||||
for (len=1000; len>0; len/=10) { |
||||
if (e10>=len || !initial) { |
||||
if (--maxlen>=0) { |
||||
if (writeok) *dest=(e10/len)+'0'; |
||||
++dest; |
||||
} |
||||
initial=0; |
||||
e10=e10%len; |
||||
} |
||||
} |
||||
if (maxlen>=0) return dest-oldbuf; |
||||
return 0; |
||||
} |
||||
/* step 5: loop through the digits, inserting the decimal point when
|
||||
* appropriate */ |
||||
for (; prec>0; ) { |
||||
double tmp2=d/tmp; |
||||
char c; |
||||
d-=((int)tmp2*tmp); |
||||
c=((int)tmp2); |
||||
if ((!initial)||c) { |
||||
if (--maxlen>=0) { |
||||
initial=0; |
||||
if (writeok) *dest=c+'0'; |
||||
++dest; |
||||
} else |
||||
return 0; |
||||
--prec; |
||||
} |
||||
if (tmp>0.5 && tmp<1.5) { |
||||
tmp=1e-1; |
||||
initial=0; |
||||
if (--maxlen>=0) { |
||||
if (writeok) *dest='.'; |
||||
++dest; |
||||
} else |
||||
return 0; |
||||
} else |
||||
tmp/=10.0; |
||||
} |
||||
return dest-oldbuf; |
||||
} |
@ -0,0 +1,20 @@
|
||||
.TH fmt_long 3 |
||||
.SH NAME |
||||
fmt_long \- write an ASCII representation of a long integer |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_long\fP(char *\fIdest\fR,unsigned int \fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_long writes an ASCII representation ('-' and '0' to '9', base 10) of |
||||
\fIsource\fR to \fIdest\fR and returns the number of bytes written. |
||||
|
||||
fmt_long does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_long returns the number |
||||
of bytes it would have written. |
||||
|
||||
For convenience, fmt.h defines the integer FMT_ULONG to be big enough to |
||||
contain every possible fmt_ulong output plus \\0. |
||||
.SH "SEE ALSO" |
||||
scan_long(3) |
@ -0,0 +1,9 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_long(char *dest,long int i) { |
||||
if (i<0) { |
||||
if (dest) *dest='-'; |
||||
return fmt_ulong(dest+1,-i); |
||||
} else |
||||
return fmt_ulong(dest,i); |
||||
} |
@ -0,0 +1,17 @@
|
||||
.TH fmt_minus 3 |
||||
.SH NAME |
||||
fmt_minus \- write '-' for negative integers |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_minus\fP(char *\fIdest\fR,signed int \fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_minus writes '-' if \fIsource\fR is negative, nothing otherwise. It |
||||
returns the number of bytes written. |
||||
|
||||
fmt_minus does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_minus returns the number |
||||
of bytes it would have written. |
||||
.SH "SEE ALSO" |
||||
fmt_plusminus(3), scan_plusminus(3) |
@ -0,0 +1,9 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_minus(char *dest,int i) { |
||||
if (i<0) { |
||||
if (dest) *dest='-'; |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
@ -0,0 +1,18 @@
|
||||
.TH fmt_plusminus 3 |
||||
.SH NAME |
||||
fmt_plusminus \- write '+' or '-' |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_plusminus\fP(char *\fIdest\fR,signed int \fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_plusminus writes '-' to \fIdest\fR if \fIsource\fR is negative, '+' |
||||
if \fIsource\fR is positive, nothing otherwise. It returns the number |
||||
of bytes written. |
||||
|
||||
fmt_plusminus does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_plusminus returns the number |
||||
of bytes it would have written. |
||||
.SH "SEE ALSO" |
||||
fmt_minus(3), scan_plusminus(3) |
@ -0,0 +1,9 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_plusminus(char *dest,int i) { |
||||
if (i) { |
||||
if (dest) *dest=(i>=0?'+':'-'); |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
@ -0,0 +1,18 @@
|
||||
.TH fmt_str 3 |
||||
.SH NAME |
||||
fmt_str \- write an ASCII string |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_str\fP(char *\fIdest\fR,const char *\fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_str copies all leading nonzero bytes from \fIsource\fR to \fIdest\fR |
||||
and returns the number of bytes it copied. |
||||
|
||||
fmt_str does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_str returns the number |
||||
of bytes it would have written, i.e. the number of leading nonzero bytes |
||||
of \fIsource\fR. |
||||
.SH "SEE ALSO" |
||||
strcpy(3) |
@ -0,0 +1,13 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_str(char *out,const char *in) { |
||||
register char* s=out; |
||||
register const char* t=in; |
||||
for (;;) { |
||||
if (!*t) break; *s=*t; ++s; ++t; |
||||
if (!*t) break; *s=*t; ++s; ++t; |
||||
if (!*t) break; *s=*t; ++s; ++t; |
||||
if (!*t) break; *s=*t; ++s; ++t; |
||||
} |
||||
return s-out; |
||||
} |
@ -0,0 +1,17 @@
|
||||
.TH fmt_strn 3 |
||||
.SH NAME |
||||
fmt_str \- write an ASCII string |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_strn\fP(char *\fIdest\fR,const char *\fIsource\fR,unsigned int maxlen); |
||||
.SH DESCRIPTION |
||||
fmt_str copies at most \fImaxlen\fR leading nonzero bytes from |
||||
\fIsource\fR to \fIdest\fR and returns the number of bytes it copied. |
||||
|
||||
fmt_str does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_strn returns the number |
||||
of bytes it would have written. |
||||
.SH "SEE ALSO" |
||||
strncpy(3) |
@ -0,0 +1,14 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_strn(char *out,const char *in,unsigned int limit) { |
||||
register char* s=out; |
||||
register const char* t=in; |
||||
register const char* u=out+limit; |
||||
for (;;) { |
||||
if (!*t) break; *s=*t; if (s==u) break; ++s; ++t; |
||||
if (!*t) break; *s=*t; if (s==u) break; ++s; ++t; |
||||
if (!*t) break; *s=*t; if (s==u) break; ++s; ++t; |
||||
if (!*t) break; *s=*t; if (s==u) break; ++s; ++t; |
||||
} |
||||
return s-out; |
||||
} |
@ -0,0 +1,20 @@
|
||||
.TH fmt_uint 3 |
||||
.SH NAME |
||||
fmt_uint \- write an ASCII representation of an unsigned integer |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_uint\fP(char *\fIdest\fR,unsigned int \fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_uint writes an ASCII representation ('0' to '9', base 10) of |
||||
\fIsource\fR to \fIdest\fR and returns the number of bytes written. |
||||
|
||||
fmt_uint does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_uint returns the number |
||||
of bytes it would have written. |
||||
|
||||
For convenience, fmt.h defines the integer FMT_ULONG to be big enough to |
||||
contain every possible fmt_uint output plus \\0. |
||||
.SH "SEE ALSO" |
||||
scan_uint(3), fmt_uint0(3) |
@ -0,0 +1,22 @@
|
||||
.TH fmt_uint0 3 |
||||
.SH NAME |
||||
fmt_uint0 \- write a zero-padded ASCII representation of an unsigned integer |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_uint0\fP(char *\fIdest\fR, unsigned int \fIsource\fR, unsigned int \fIn\fR); |
||||
.SH DESCRIPTION |
||||
fmt_uint0 writes an ASCII representation ('0' to '9', base 10) of |
||||
\fIsource\fR to \fIdest\fR and returns the number of bytes written. |
||||
The output is padded with '0'-bytes until it encompasses at least |
||||
\fIn\fR bytes, but it will not be truncated if it does not fit. |
||||
|
||||
fmt_uint0 does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_uint0 returns the number |
||||
of bytes it would have written. |
||||
|
||||
For convenience, fmt.h defines the integer FMT_ULONG to be big enough to |
||||
contain every possible fmt_uint output plus \\0. |
||||
.SH "SEE ALSO" |
||||
scan_uint(3), fmt_uint(3) |
@ -0,0 +1,20 @@
|
||||
.TH fmt_ulong 3 |
||||
.SH NAME |
||||
fmt_ulong \- write an ASCII representation of an unsigned long integer |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_ulong\fP(char *\fIdest\fR,unsigned long \fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_ulong writes an ASCII representation ('0' to '9', base 10) of |
||||
\fIsource\fR to \fIdest\fR and returns the number of bytes written. |
||||
|
||||
fmt_ulong does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_ulong returns the |
||||
number of bytes it would have written. |
||||
|
||||
For convenience, fmt.h defines the integer FMT_ULONG to be big enough to |
||||
contain every possible fmt_ulong output plus \\0. |
||||
.SH "SEE ALSO" |
||||
scan_ulong(3) |
@ -0,0 +1,11 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_ulong(char *dest,unsigned long i) { |
||||
register unsigned long len,tmp; |
||||
/* first count the number of bytes needed */ |
||||
for (len=1, tmp=i; tmp>9; ++len) tmp/=10; |
||||
if (dest) |
||||
for (tmp=i, dest+=len; tmp; tmp/=10) |
||||
*--dest = (tmp%10)+'0'; |
||||
return len; |
||||
} |
@ -0,0 +1,22 @@
|
||||
.TH fmt_ulong0 3 |
||||
.SH NAME |
||||
fmt_ulong0 \- write a zero-padded ASCII representation of an unsigned long integer |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_uint0\fP(char *\fIdest\fR, unsigned long \fIsource\fR, unsigned int \fIn\fR); |
||||
.SH DESCRIPTION |
||||
fmt_ulong0 writes an ASCII representation ('0' to '9', base 10) of |
||||
\fIsource\fR to \fIdest\fR and returns the number of bytes written. |
||||
The output is padded with '0'-bytes until it encompasses at least |
||||
\fIn\fR bytes, but it will not be truncated if it does not fit. |
||||
|
||||
fmt_ulong0 does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_ulong0 returns the number |
||||
of bytes it would have written. |
||||
|
||||
For convenience, fmt.h defines the integer FMT_ULONG to be big enough to |
||||
contain every possible fmt_ulong output plus \\0. |
||||
.SH "SEE ALSO" |
||||
scan_ulong(3), fmt_ulong(3) |
@ -0,0 +1,15 @@
|
||||
#include "fmt.h" |
||||
|
||||
unsigned int fmt_ulong0(char *dest,unsigned long i,unsigned int pad) { |
||||
register unsigned int len; |
||||
register unsigned long tmp; |
||||
/* first count the number of bytes needed */ |
||||
for (len=1, tmp=i; tmp>9; ++len) tmp/=10; |
||||
/* now see if we need to pad */ |
||||
if (dest) { |
||||
while (len<pad) { *dest='0'; ++dest; ++len; } |
||||
fmt_uint(dest,i); |
||||
return len; |
||||
} else |
||||
return (len<pad?pad:len); |
||||
} |
@ -0,0 +1,21 @@
|
||||
.TH fmt_xlong 3 |
||||
.SH NAME |
||||
fmt_xlong \- write a hexadecimal ASCII representation of an unsigned long integer |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
unsigned int \fBfmt_xlong\fP(char *\fIdest\fR,unsigned long \fIsource\fR); |
||||
.SH DESCRIPTION |
||||
fmt_xlong writes an ASCII representation ('0' to '9' and 'a' to 'f', |
||||
base 16) of \fIsource\fR to \fIdest\fR and returns the number of bytes |
||||
written. |
||||
|
||||
fmt_xlong does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_xlong returns the |
||||
number of bytes it would have written. |
||||
|
||||
For convenience, fmt.h defines the integer FMT_XLONG to be big enough to |
||||
contain every possible fmt_xlong output plus \\0. |
||||
.SH "SEE ALSO" |
||||
scan_xlong(3) |
@ -0,0 +1,15 @@
|
||||
#include "fmt.h" |
||||
|
||||
static inline char tohex(char c) { |
||||
return c>10?c-10+'a':c+'0'; |
||||
} |
||||
|
||||
unsigned int fmt_xlong(char *dest,unsigned long i) { |
||||
register unsigned long len,tmp; |
||||
/* first count the number of bytes needed */ |
||||
for (len=1, tmp=i; tmp>15; ++len) tmp>>=4; |
||||
if (dest) |
||||
for (tmp=i, dest+=len; tmp; tmp>>=4) |
||||
*--dest = tohex(tmp&15); |
||||
return len; |
||||
} |
@ -0,0 +1,28 @@
|
||||
#ifndef IP6_H |
||||
#define IP6_H |
||||
|
||||
extern unsigned int ip6_scan(const char *src,char *ip); |
||||
extern unsigned int ip6_fmt(char *dest,const char *ip); |
||||
|
||||
extern unsigned int ip6_scan_flat(const char *src,char *); |
||||
extern unsigned int ip6_fmt_flat(char *dest,const char *); |
||||
|
||||
/*
|
||||
ip6 address syntax: (h = hex digit), no leading '0' required |
||||
1. hhhh:hhhh:hhhh:hhhh:hhhh:hhhh:hhhh:hhhh |
||||
2. any number of 0000 may be abbreviated as "::", but only once |
||||
flat ip6 address syntax: |
||||
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh |
||||
*/ |
||||
|
||||
#define IP6_FMT 40 |
||||
|
||||
static const unsigned char V4mappedprefix[12]={0,0,0,0,0,0,0,0,0,0,0xff,0xff}; |
||||
static const unsigned char V6loopback[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; |
||||
static const unsigned char V6any[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
||||
|
||||
#define ip6_isv4mapped(ip) (byte_equal(ip,12,V4mappedprefix)) |
||||
|
||||
static const char ip4loopback[4] = {127,0,0,1}; |
||||
|
||||
#endif |
@ -0,0 +1,7 @@
|
||||
#ifndef NDELAY_H |
||||
#define NDELAY_H |
||||
|
||||
extern int ndelay_on(int); |
||||
extern int ndelay_off(int); |
||||
|
||||
#endif |
@ -0,0 +1,29 @@
|
||||
#ifndef OPEN_H |
||||
#define OPEN_H |
||||
|
||||
#include <sys/cdefs.h> |
||||
|
||||
/* open filename for reading and return the file handle or -1 on error */ |
||||
extern int open_read(const char *filename) __THROW; |
||||
|
||||
/* create filename for exclusive write only use (mode 0600) and return
|
||||
* the file handle or -1 on error */ |
||||
extern int open_excl(const char *filename) __THROW; |
||||
|
||||
/* open filename for appending write only use (mode 0600)
|
||||
* and return the file handle or -1 on error. |
||||
* All write operation will append after the last byte, regardless of |
||||
* seeking or other processes also appending to the file. The file will |
||||
* be created if it does not exist. */ |
||||
extern int open_append(const char *filename) __THROW; |
||||
|
||||
/* open filename for writing (mode 0644). Create the file if it does
|
||||
* not exist, truncate it to zero length otherwise. Return the file |
||||
* handle or -1 on error. */ |
||||
extern int open_trunc(const char *filename) __THROW; |
||||
|
||||
/* open filename for writing. Create the file if it does not exist.
|
||||
* Return the file handle or -1 on error. */ |
||||
extern int open_write(const char *filename) __THROW; |
||||
|
||||
#endif |
@ -0,0 +1,18 @@
|
||||
.TH open_append 3 |
||||
.SH NAME |
||||
open_append \- open a file for appending |
||||
.SH SYNTAX |
||||
.B #include <open.h> |
||||
|
||||
extern int \fBopen_append\fP(const char *\fIfilename\fR); |
||||
.SH DESCRIPTION |
||||
open_append opens the file \fIfilename\fR for appending write-only use |
||||
and returns the file handle. If it does not exist, it will be created |
||||
with mode 0600. If there was an error opening or creating the file, |
||||
open_append returns -1 and sets errno accordingly. |
||||
|
||||
All write operations will append after the last byte, regardless of |
||||
previous calls to lseek(2) or other processes also appending to the |
||||
same file. |
||||
.SH "SEE ALSO" |
||||
open(2) |
@ -0,0 +1,7 @@
|
||||
#include <unistd.h> |
||||
#include <sys/fcntl.h> |
||||
#include "open.h" |
||||
|
||||
extern int open_append(const char *filename) { |
||||
return open(filename,O_WRONLY|O_NDELAY|O_APPEND|O_CREAT,0600); |
||||
} |
@ -0,0 +1,22 @@
|
||||
.TH open_excl 3 |
||||
.SH NAME |
||||
open_excl \- open a file for exclusive writing |
||||
.SH SYNTAX |
||||
.B #include <open.h> |
||||
|
||||
extern int \fBopen_excl\fP(const char *\fIfilename\fR); |
||||
.SH DESCRIPTION |
||||
open_excl opens the file \fIfilename\fR for writing and returns the file |
||||
handle. The file may not exist before the call to \fBopen_excl\fR. The |
||||
file will be created with mode 0600. |
||||
|
||||
If there was an error creating the file, open_excl returns -1 |
||||
and sets errno accordingly. |
||||
|
||||
Since open_excl relies on the O_EXCL flag to open, it does not work |
||||
reliably over NFS (the NFS protocol is broken) and must be emulated |
||||
using a lock file (create a file with a unique file name and link(2) it |
||||
to the lock file. Then stat the lock file and see if the link count is |
||||
2). |
||||
.SH "SEE ALSO" |
||||
open(2) |
@ -0,0 +1,7 @@
|
||||
#include <unistd.h> |
||||
#include <sys/fcntl.h> |
||||
#include "open.h" |
||||
|
||||
extern int open_excl(const char *filename) { |
||||
return open(filename,O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT|O_EXCL,0600); |
||||
} |
@ -0,0 +1,13 @@
|
||||
.TH open_read 3 |
||||
.SH NAME |
||||
open_read \- open a file for reading |
||||
.SH SYNTAX |
||||
.B #include <open.h> |
||||
|
||||
extern int \fBopen_read\fP(const char *\fIfilename\fR); |
||||
.SH DESCRIPTION |
||||
open_read opens the file \fIfilename\fR for reading and returns the file |
||||
handle. If there was an error opening the file, open_read returns -1 |
||||
and sets errno accordingly. |
||||
.SH "SEE ALSO" |
||||
open(2) |
@ -0,0 +1,7 @@
|
||||
#include <unistd.h> |
||||
#include <sys/fcntl.h> |
||||
#include "open.h" |
||||
|
||||
extern int open_read(const char *filename) { |
||||
return open(filename,O_RDONLY|O_NDELAY); |
||||
} |