Initial revision
commit
3083708670
@ -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);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
.TH open_trunc 3
|
||||
.SH NAME
|
||||
open_trunc \- open a file for writing
|
||||
.SH SYNTAX
|
||||
.B #include <open.h>
|
||||
|
||||
extern int \fBopen_trunc\fP(const char *\fIfilename\fR);
|
||||
.SH DESCRIPTION
|
||||
open_trunc opens the file \fIfilename\fR for write-only use
|
||||
and returns the file handle. If the file exists, it will be truncated
|
||||
to zero bytes length. If it does not exist, it will be created
|
||||
with mode 0644. If there was an error opening or creating the file,
|
||||
open_trunc 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_trunc(const char *filename) {
|
||||
return open(filename,O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT,0644);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
.TH open_write 3
|
||||
.SH NAME
|
||||
open_write \- open a file for writing
|
||||
.SH SYNTAX
|
||||
.B #include <open.h>
|
||||
|
||||
extern int \fBopen_write\fP(const char *\fIfilename\fR);
|
||||
.SH DESCRIPTION
|
||||
open_write opens the file \fIfilename\fR for write-only use and returns
|
||||
the file handle. If the file does not exist, it will be created with
|
||||
mode 0644. If there was an error opening or creating the file,
|
||||
open_write 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_write(const char *filename) {
|
||||
return open(filename,O_WRONLY|O_NDELAY);
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
#ifndef SCAN_H
|
||||
#define SCAN_H
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#ifndef __pure__
|
||||
#define __pure__
|
||||
#endif
|
||||
#ifndef __THROW
|
||||
#define __THROW
|
||||
#endif
|
||||
|
||||
/* interpret src as ASCII decimal number, write number to dest and
|
||||
* return the number of bytes that were parsed */
|
||||
extern unsigned int scan_ulong(const char *src,unsigned long *dest) __THROW;
|
||||
|
||||
/* interpret src as ASCII hexadecimal number, write number to dest and
|
||||
* return the number of bytes that were parsed */
|
||||
extern unsigned int scan_xlong(const char *src,unsigned long *dest) __THROW;
|
||||
|
||||
/* interpret src as ASCII octal number, write number to dest and
|
||||
* return the number of bytes that were parsed */
|
||||
extern unsigned int scan_8long(const char *src,unsigned long *dest) __THROW;
|
||||
|
||||
/* interpret src as signed ASCII decimal number, write number to dest
|
||||
* and return the number of bytes that were parsed */
|
||||
extern unsigned int scan_long(const char *src,signed long *dest) __THROW;
|
||||
|
||||
extern unsigned int scan_uint(const char *src,unsigned int *dest) __THROW;
|
||||
extern unsigned int scan_xint(const char *src,unsigned int *dest) __THROW;
|
||||
extern unsigned int scan_8int(const char *src,unsigned int *dest) __THROW;
|
||||
extern unsigned int scan_int(const char *src,signed int *dest) __THROW;
|
||||
|
||||
extern unsigned int scan_ushort(const char *src,unsigned short *dest) __THROW;
|
||||
extern unsigned int scan_xshort(const char *src,unsigned short *dest) __THROW;
|
||||
extern unsigned int scan_8short(const char *src,unsigned short *dest) __THROW;
|
||||
extern unsigned int scan_short(const char *src,signed short *dest) __THROW;
|
||||
|
||||
/* interpret src as double precision floating point number,
|
||||
* write number to dest and return the number of bytes that were parsed */
|
||||
extern unsigned int scan_double(const char *in, double *dest) __THROW;
|
||||
|
||||
/* if *src=='-', set *dest to -1 and return 1.
|
||||
* if *src=='+', set *dest to 1 and return 1.
|
||||
* otherwise set *dest to 1 return 0. */
|
||||
extern unsigned int scan_plusminus(const char *src,signed int *dest) __THROW;
|
||||
|
||||
/* return the highest integer n<=limit so that isspace(in[i]) for all 0<=i<=n */
|
||||
extern unsigned int scan_whitenskip(const char *in,unsigned int limit) __THROW __pure__;
|
||||
|
||||
/* return the highest integer n<=limit so that !isspace(in[i]) for all 0<=i<=n */
|
||||
extern unsigned int scan_nonwhitenskip(const char *in,unsigned int limit) __THROW __pure__;
|
||||
|
||||
/* return the highest integer n<=limit so that in[i] is element of
|
||||
* charset (ASCIIZ string) for all 0<=i<=n */
|
||||
extern unsigned int scan_charsetnskip(const char *in,const char *charset,unsigned int limit) __THROW __pure__;
|
||||
|
||||
/* return the highest integer n<=limit so that in[i] is not element of
|
||||
* charset (ASCIIZ string) for all 0<=i<=n */
|
||||
extern unsigned int scan_noncharsetnskip(const char *in,const char *charset,unsigned int limit) __THROW __pure__;
|
||||
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_8int(const char* src,unsigned int* dest) {
|
||||
unsigned long l;
|
||||
register int len=scan_8long(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
.TH scan_8long 3
|
||||
.SH NAME
|
||||
scan_8long \- parse an unsigned long integer in octal ASCII representation
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_8long\fP(const char *\fIsrc\fR,unsigned long *\fIdest\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_8long parses an unsigned long integer in octal ASCII representation
|
||||
from \fIsrc\fR and writes the result into \fIdest\fR. It returns the
|
||||
number of bytes read from \fIsrc\fR.
|
||||
.SH "SEE ALSO"
|
||||
scan_xlong(3)
|
@ -0,0 +1,13 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_8long(const char *src,unsigned long *dest) {
|
||||
register const char *tmp=src;
|
||||
register int l=0;
|
||||
register unsigned char c;
|
||||
while ((c=*tmp-'0')<8) {
|
||||
l=l*8+c;
|
||||
++tmp;
|
||||
}
|
||||
*dest=l;
|
||||
return tmp-src;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_8short(const char* src,unsigned short* dest) {
|
||||
unsigned long l;
|
||||
register int len=scan_8long(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
.TH scan_charsetnskip 3
|
||||
.SH NAME
|
||||
scan_charsetnskip \- skip characters from set
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_charsetnskip\fP(const char* \fIsrc\fR, const char* \fIcharset\fR, unsigned int* \fIlimit\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_charsetnskip returns the length of the maximum prefix of \fIsrc\fR
|
||||
that consists solely of characters that occur in \fIcharset\fR (up to
|
||||
and not including the \\0).
|
||||
.SH "SEE ALSO"
|
||||
scan_noncharsetnskip(3)
|
@ -0,0 +1,15 @@
|
||||
#include "scan.h"
|
||||
#include <ctype.h>
|
||||
|
||||
unsigned int scan_charsetnskip(const char *s,const char *charset,unsigned int limit) {
|
||||
register const char *t=s;
|
||||
register const char *u=t+limit;
|
||||
register const char* i;
|
||||
while (t<u) {
|
||||
for (i=charset; *i; ++i)
|
||||
if (*i==*t) break;
|
||||
if (*i!=*t) break;
|
||||
++t;
|
||||
}
|
||||
return t-s;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
.TH scan_double 3
|
||||
.SH NAME
|
||||
scan_double \- parse a floating point number in decimal ASCII representation
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_double\fP(const char *\fIsrc\fR,double *\fIdest\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_double parses a floating point number in decimal ASCII representation
|
||||
from \fIsrc\fR and writes the result into \fIdest\fR. It returns the
|
||||
number of bytes read from \fIsrc\fR.
|
||||
.SH "SEE ALSO"
|
||||
scan_xlong(3), scan_8long(3), fmt_ulong(3)
|
@ -0,0 +1,51 @@
|
||||
#include "scan.h"
|
||||
|
||||
#ifdef __GNUC__
|
||||
static inline int isdigit(int c) { return (c>='0' && c<='9'); }
|
||||
#else
|
||||
#include <ctype.h>
|
||||
#endif
|
||||
|
||||
unsigned int scan_double(const char *in, double *dest) {
|
||||
double d=0;
|
||||
register const char *c=in;
|
||||
char neg=0;
|
||||
switch (*c) {
|
||||
case '-': neg=1;
|
||||
case '+': c++; break;
|
||||
default: break;
|
||||
}
|
||||
while (isdigit(*c)) {
|
||||
d=d*10+(*c-'0');
|
||||
++c;
|
||||
}
|
||||
if (*c=='.') {
|
||||
double factor=.1;
|
||||
while (isdigit(*++c)) {
|
||||
d=d+(factor*(*c-'0'));
|
||||
factor/=10;
|
||||
}
|
||||
}
|
||||
if ((*c|32)=='e') {
|
||||
int exp=0;
|
||||
char neg=0;
|
||||
if (c[1]<'0') {
|
||||
switch (*c) {
|
||||
case '-': neg=1;
|
||||
case '+': c++; break;
|
||||
default:
|
||||
d=0;
|
||||
c=in;
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
while (isdigit(*++c))
|
||||
exp=exp*10+(*c-'0');
|
||||
while (exp) { /* XXX: this introduces rounding errors */
|
||||
d*=10; --exp;
|
||||
}
|
||||
}
|
||||
done:
|
||||
*dest=d;
|
||||
return c-in;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_int(const char* src,int* dest) {
|
||||
long l;
|
||||
register int len=scan_long(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_long(const char *src,long *dest) {
|
||||
register const char *tmp;
|
||||
register int l;
|
||||
register unsigned char c;
|
||||
int neg;
|
||||
tmp=src; l=0; neg=0;
|
||||
switch (*tmp) {
|
||||
case '-': neg=1;
|
||||
case '+': ++tmp;
|
||||
}
|
||||
while ((c=*tmp-'0')<10) {
|
||||
l=l*10+c;
|
||||
++tmp;
|
||||
}
|
||||
*dest=(neg?-l:l);
|
||||
return tmp-src;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
.TH scan_noncharsetnskip 3
|
||||
.SH NAME
|
||||
scan_noncharsetnskip \- skip characters not from set
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_noncharsetnskip\fP(const char* \fIsrc\fR, const char* \fIcharset\fR, unsigned int* \fIlimit\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_noncharsetnskip returns the length of the maximum prefix of \fIsrc\fR
|
||||
that consists solely of characters that do not occur in \fIcharset\fR
|
||||
(up to and not including the \\0).
|
||||
.SH "SEE ALSO"
|
||||
scan_charsetnskip(3)
|
@ -0,0 +1,15 @@
|
||||
#include "scan.h"
|
||||
#include <ctype.h>
|
||||
|
||||
unsigned int scan_noncharsetnskip(const char *s,const char *charset,unsigned int limit) {
|
||||
register const char *t=s;
|
||||
register const char *u=t+limit;
|
||||
register const char* i;
|
||||
while (t<u) {
|
||||
for (i=charset; *i; ++i)
|
||||
if (*i==*t) break;
|
||||
if (*i==*t) break;
|
||||
++t;
|
||||
}
|
||||
return t-s;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
.TH scan_nonwhitenskip 3
|
||||
.SH NAME
|
||||
scan_nonwhitenskip \- skip non-whitespace
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_nonwhitenskip\fP(const char *\fIsrc\fR,unsigned int *\fIlimit\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_nonwhitenskip returns the length of the maximum prefix of \fIsrc\fR
|
||||
that consists solely of non-whitespace characters as defined by
|
||||
\fB!isspace\fR.
|
||||
|
||||
Normally, this is everything but ' ', '\\f', '\\n', '\\r', '\\t', '\\v'.
|
||||
.SH "SEE ALSO"
|
||||
scan_nonwhitenskip(3)
|
@ -0,0 +1,9 @@
|
||||
#include "scan.h"
|
||||
#include <ctype.h>
|
||||
|
||||
unsigned int scan_nonwhitenskip(const char *s,unsigned int limit) {
|
||||
register const char *t=s;
|
||||
register const char *u=t+limit;
|
||||
while (t<u && !isspace(*t)) ++t;
|
||||
return t-s;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
.TH scan_plusminus 3
|
||||
.SH NAME
|
||||
scan_plusminus \- parse '+' or '-'
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_plusminus\fP(const char *\fIsrc\fR,int *\fIdest\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_plusminus parses an ASCII '+' or '-' sign from \fIsrc\fR, sets
|
||||
\fIdest\fR 1 or -1, respectively, and returns 1.
|
||||
|
||||
If neither is found, '+' is assumed and 0 is returned.
|
@ -0,0 +1,10 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_plusminus(const char *src,signed int *dest) {
|
||||
*dest=1;
|
||||
switch (*src) {
|
||||
case '-': *dest=-1;
|
||||
case '+': return 1; break;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_short(const char* src,short* dest) {
|
||||
long l;
|
||||
register int len=scan_long(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_uint(const char* src,unsigned int* dest) {
|
||||
unsigned long l;
|
||||
register int len=scan_ulong(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
.TH scan_ulong 3
|
||||
.SH NAME
|
||||
scan_ulong \- parse an unsigned long integer in decimal ASCII representation
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_ulong\fP(const char *\fIsrc\fR,unsigned long *\fIdest\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_ulong parses an unsigned long integer in decimal ASCII representation
|
||||
from \fIsrc\fR and writes the result into \fIdest\fR. It returns the
|
||||
number of bytes read from \fIsrc\fR.
|
||||
.SH "SEE ALSO"
|
||||
scan_xlong(3), scan_8long(3), fmt_ulong(3)
|
@ -0,0 +1,13 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_ulong(const char *src,unsigned long *dest) {
|
||||
register const char *tmp=src;
|
||||
register int l=0;
|
||||
register unsigned char c;
|
||||
while ((c=*tmp-'0')<10) {
|
||||
l=l*10+c;
|
||||
++tmp;
|
||||
}
|
||||
*dest=l;
|
||||
return tmp-src;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_ushort(const char* src,unsigned short* dest) {
|
||||
unsigned long l;
|
||||
register int len=scan_ulong(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
.TH scan_whitenskip 3
|
||||
.SH NAME
|
||||
scan_whitenskip \- skip whitespace
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_whitenskip\fP(const char *\fIsrc\fR,unsigned int *\fIlimit\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_whitenskip returns the length of the maximum prefix of \fIsrc\fR
|
||||
that consists solely of whitespace characters as defined by
|
||||
\fBisspace\fR.
|
||||
|
||||
Normally, this is ' ', '\\f', '\\n', '\\r', '\\t', '\\v'.
|
||||
.SH "SEE ALSO"
|
||||
scan_nonwhitenskip(3)
|
@ -0,0 +1,9 @@
|
||||
#include "scan.h"
|
||||
#include <ctype.h>
|
||||
|
||||
unsigned int scan_whitenskip(const char *s,unsigned int limit) {
|
||||
register const char *t=s;
|
||||
register const char *u=t+limit;
|
||||
while (t<u && isspace(*t)) ++t;
|
||||
return t-s;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_xint(const char* src,unsigned int* dest) {
|
||||
unsigned long l;
|
||||
register int len=scan_xlong(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
.TH scan_xlong 3
|
||||
.SH NAME
|
||||
scan_xlong \- parse an unsigned long integer in hexadecimal ASCII representation
|
||||
.SH SYNTAX
|
||||
.B #include <scan.h>
|
||||
|
||||
int \fBscan_xlong\fP(const char *\fIsrc\fR,unsigned long *\fIdest\fR);
|
||||
.SH DESCRIPTION
|
||||
scan_xlong parses an unsigned long integer in hexadecimal ASCII
|
||||
representation from \fIsrc\fR and writes the result into \fIdest\fR. It
|
||||
returns the number of bytes read from \fIsrc\fR.
|
||||
|
||||
scan_xlong understands both upper and lower case letters.
|
||||
|
||||
scan_xlong does not expect or understand a "0x" prefix.
|
||||
.SH "SEE ALSO"
|
||||
fmt_xlong(3)
|
@ -0,0 +1,23 @@
|
||||
#include "scan.h"
|
||||
|
||||
static inline int fromhex(unsigned char c) {
|
||||
if (c>='0' && c<='9')
|
||||
return c-'0';
|
||||
else if (c>='A' && c<='F')
|
||||
return c-'A'+10;
|
||||
else if (c>='a' && c<='f')
|
||||
return c-'a'+10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned int scan_xlong(const char *src,unsigned long *dest) {
|
||||
register const char *tmp=src;
|
||||
register int l=0;
|
||||
register unsigned char c;
|
||||
while ((c=fromhex(*tmp))<16) {
|
||||
l=(l<<4)+c;
|
||||
++tmp;
|
||||
}
|
||||
*dest=l;
|
||||
return tmp-src;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "scan.h"
|
||||
|
||||
unsigned int scan_xshort(const char* src,unsigned short* dest) {
|
||||
unsigned long l;
|
||||
register int len=scan_xlong(src,&l);
|
||||
*dest=l;
|
||||
return len;
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
#ifndef SOCKET_H
|
||||
#define SOCKET_H
|
||||
|
||||
#include "uint16.h"
|
||||
#include "uint32.h"
|
||||
|
||||
extern int socket_tcp4(void);
|
||||
extern int socket_udp4(void);
|
||||
extern int socket_tcp6(void);
|
||||
extern int socket_udp6(void);
|
||||
|
||||
#define socket_tcp() socket_tcp4()
|
||||
#define socket_udp() socket_udp4()
|
||||
|
||||
extern int socket_connect4(int s,const char *ip,uint16);
|
||||
extern int socket_connect6(int s,const char *ip,uint16,uint32 scope_id);
|
||||
extern int socket_connected(int s);
|
||||
extern int socket_bind4(int s,const char *ip,uint16);
|
||||
extern int socket_bind4_reuse(int s,const char *ip,uint16);
|
||||
extern int socket_bind6(int s,const char *ip,uint16,uint32 scope_id);
|
||||
extern int socket_bind6_reuse(int s,const char *ip,uint16,uint32 scope_id);
|
||||
extern int socket_listen(int s,unsigned int backlog);
|
||||
extern int socket_accept4(int s,char *ip,uint16 *);
|
||||
extern int socket_accept6(int s,char *ip,uint16 *,uint32 *scope_id);
|
||||
extern int socket_recv4(int s,char *buf,unsigned int len,char *ip,uint16 *port);
|
||||
extern int socket_recv6(int s,char *buf,unsigned int len,char *ip,uint16 *port,uint32 *scope_id);
|
||||
extern int socket_send4(int s,const char *buf,unsigned int len,const char *ip,uint16 port);
|
||||
extern int socket_send6(int s,const char *buf,unsigned int len,const char *ip,uint16 port,uint32 scope_id);
|
||||
extern int socket_local4(int s,char *ip,uint16 *port);
|
||||
extern int socket_local6(int s,char *ip,uint16 *port,uint32 *scope_id);
|
||||
extern int socket_remote4(int s,char *ip,uint16 *port);
|
||||
extern int socket_remote6(int s,char *ip,uint16 *port,uint32 *scope_id);
|
||||
|
||||
/* enable sending udp packets to the broadcast address */
|
||||
extern int socket_broadcast(int s);
|
||||
/* join a multicast group on the given interface */
|
||||
extern int socket_mcjoin4(int s,const char *groupip,const char *interface);
|
||||
extern int socket_mcjoin6(int s,const char *groupip,int);
|
||||
/* leave a multicast group on the given interface */
|
||||
extern int socket_mcleave4(int s,const char *groupip);
|
||||
extern int socket_mcleave6(int s,const char *groupip);
|
||||
/* set multicast TTL/hop count for outgoing packets */
|
||||
extern int socket_mcttl4(int s,char hops);
|
||||
extern int socket_mcttl6(int s,char hops);
|
||||
/* enable multicast loopback */
|
||||
extern int socket_mcloop4(int s,char hops);
|
||||
extern int socket_mcloop6(int s,char hops);
|
||||
|
||||
extern void socket_tryreservein(int s,int size);
|
||||
|
||||
extern const char* socket_getifname(uint32 interface);
|
||||
extern uint32 socket_getifidx(const char *ifname);
|
||||
|
||||
extern int socket_sendfile(int out,int in,uint32 offset,uint32 bytes);
|
||||
|
||||
extern int noipv6;
|
||||
|
||||
#endif
|
@ -0,0 +1,17 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "byte.h"
|
||||
#include "socket.h"
|
||||
#include "uint16.h"
|
||||
#include "uint32.h"
|
||||
|
||||
int socket_connect4(int s,const char *ip,uint16 port) {
|
||||
struct sockaddr_in si;
|
||||
byte_zero(&si,sizeof(si));
|
||||
si.sin_family=AF_INET;
|
||||
uint16_pack_big((char*) &si.sin_port,port);
|
||||
*((uint32*)&si.sin_addr) = *((uint32*)ip);
|
||||
return (connect(s,(struct sockaddr*)&si,sizeof(si)));
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
#include <sys/param.h>
|
||||
#include "sockaddr_in6.h"
|
||||
#include "byte.h"
|
||||
#include "socket.h"
|
||||
#include "ip6.h"
|
||||
#include "haveip6.h"
|
||||
#include "error.h"
|
||||
#include "uint32.h"
|
||||
|
||||
int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id)
|
||||
{
|
||||
#ifdef LIBC_HAS_IP6
|
||||
struct sockaddr_in6 sa;
|
||||
|
||||
if (noipv6) {
|
||||
#endif
|
||||
if (ip6_isv4mapped(ip))
|
||||
return socket_connect4(s,ip+12,port);
|
||||
if (byte_equal(ip,16,V6loopback))
|
||||
return socket_connect4(s,ip4loopback,port);
|
||||
#ifdef LIBC_HAS_IP6
|
||||
}
|
||||
byte_zero(&sa,sizeof sa);
|
||||
sa.sin6_family = PF_INET6;
|
||||
uint16_pack_big((char *) &sa.sin6_port,port);
|
||||
sa.sin6_flowinfo = 0;
|
||||
sa.sin6_scope_id = scope_id;
|
||||
byte_copy((char *) &sa.sin6_addr,16,ip);
|
||||
|
||||
return connect(s,(struct sockaddr *) &sa,sizeof sa);
|
||||
#else
|
||||
errno=error_proto;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
int socket_tcp4(void) {
|
||||
return socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
int socket_tcp6(void) {
|
||||
return socket(PF_INET6,SOCK_STREAM,IPPROTO_TCP);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
int socket_udp4(void) {
|
||||
return socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
int socket_udp6(void) {
|
||||
return socket(PF_INET6,SOCK_DGRAM,IPPROTO_UDP);
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
#ifndef STR_H
|
||||
#define STR_H
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#ifndef __pure__
|
||||
#define __pure__
|
||||
#endif
|
||||
#ifndef __THROW
|
||||
#define __THROW
|
||||
#endif
|
||||
|
||||
/* str_copy copies leading bytes from in to out until \0.
|
||||
* return number of copied bytes. */
|
||||
extern unsigned int str_copy(char *out,const char *in) __THROW;
|
||||
|
||||
/* str_diff returns negative, 0, or positive, depending on whether the
|
||||
* string a[0], a[1], ..., a[n]=='\0' is lexicographically smaller than,
|
||||
* equal to, or greater than the string b[0], b[1], ..., b[m-1]=='\0'.
|
||||
* If the strings are different, str_diff does not read bytes past the
|
||||
* first difference. */
|
||||
extern int str_diff(const char *a,const char *b) __THROW __pure__;
|
||||
|
||||
/* str_diffn returns negative, 0, or positive, depending on whether the
|
||||
* string a[0], a[1], ..., a[n]=='\0' is lexicographically smaller than,
|
||||
* equal to, or greater than the string b[0], b[1], ..., b[m-1]=='\0'.
|
||||
* If the strings are different, str_diffn does not read bytes past the
|
||||
* first difference. The strings will be considered equal if the first
|
||||
* limit characters match. */
|
||||
extern int str_diffn(const char *a,const char *b,unsigned int limit) __THROW __pure__;
|
||||
|
||||
/* str_len returns the index of \0 in s */
|
||||
extern unsigned int str_len(const char *s) __THROW __pure__;
|
||||
|
||||
/* str_chr returns the index of the first occurance of needle or \0 in s */
|
||||
extern unsigned int str_chr(const char *haystack,char needle) __THROW __pure__;
|
||||
|
||||
/* str_rchr returns the index of the last occurance of needle or \0 in s */
|
||||
extern unsigned int str_rchr(const char *haystack,char needle) __THROW __pure__;
|
||||
|
||||
/* str_start returns 1 if the b is a prefix of a, 0 otherwise */
|
||||
extern int str_start(const char *a,const char *b) __THROW __pure__;
|
||||
|
||||
/* convenience shortcut to test for string equality */
|
||||
#define str_equal(s,t) (!str_diff((s),(t)))
|
||||
|
||||
#endif
|
@ -0,0 +1,12 @@
|
||||
.TH str_chr 3
|
||||
.SH NAME
|
||||
str_chr \- find character in ASCIIZ string
|
||||
.SH SYNTAX
|
||||
.B #include <str.h>
|
||||
|
||||
extern int \fBstr_chr\fP(const char* \fIhaystack\fR,char \fIneedle\fR);
|
||||
.SH DESCRIPTION
|
||||
str_chr returns the index of the first occurrance of \fIneedle\fR or \\0 in
|
||||
\fIstring\fR.
|
||||
.SH "SEE ALSO"
|
||||
strchr(3)
|
@ -0,0 +1,13 @@
|
||||
#include "str.h"
|
||||
|
||||
unsigned int str_chr(const char *in, char needle) {
|
||||
register const char* t=in;
|
||||
register const char c=needle;
|
||||
for (;;) {
|
||||
if (!*t || *t==c) break; ++t;
|
||||
if (!*t || *t==c) break; ++t;
|
||||
if (!*t || *t==c) break; ++t;
|
||||
if (!*t || *t==c) break; ++t;
|
||||
}
|
||||
return t-in;
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
.TH str_copy 3
|
||||
.SH NAME
|
||||
str_copy \- copy an ASCIIZ string
|
||||
.SH SYNTAX
|
||||
.B #include <str.h>
|
||||
|
||||
extern int \fBstr_copy\fP(char* \fIout\fR,const char* \fIin\fR);
|
||||
.SH DESCRIPTION
|
||||
str_copy copies the leading bytes of \fIin\fR to \fIout\fR up to and
|
||||
including the first occurrance of \\0.
|
||||
|
||||
str_copy returns the number of bytes copied.
|
||||
.SH "SEE ALSO"
|
||||
strlen(3)
|
@ -0,0 +1,13 @@
|
||||
#include "str.h"
|
||||
|
||||
unsigned int str_copy(char *out,const char *in) {
|
||||
register char* s=out;
|
||||
register const char* t=in;
|
||||
for (;;) {
|
||||
if (!(*s=*t)) break; ++s; ++t;
|
||||
if (!(*s=*t)) break; ++s; ++t;
|
||||
if (!(*s=*t)) break; ++s; ++t;
|
||||
if (!(*s=*t)) break; ++s; ++t;
|
||||
}
|
||||
return s-out;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
.TH str_diff 3
|
||||
.SH NAME
|
||||
str_diff \- compare two ASCIIZ strings
|
||||
.SH SYNTAX
|
||||
.B #include <str.h>
|
||||
|
||||
extern int \fBstr_diff\fP(const char* \fIa\fR,const char* \fIb\fR);
|
||||
.SH DESCRIPTION
|
||||
\fBstr_diff\fR returns negative, 0, or positive, depending on whether the
|
||||
string \fIa\fR[0], \fIa\fR[1], ..., \fIa\fR[n]=='\\0' is
|
||||
lexicographically smaller than, equal to, or greater than the string
|
||||
\fIb\fR[0], \fIb\fR[1], ..., \fIb\fR[m-1]=='\\0'.
|
||||
|
||||
If the strings are different, str_diff does not read bytes past the
|
||||
first difference.
|
||||
.SH "SEE ALSO"
|
||||
strlen(3)
|
@ -0,0 +1,20 @@
|
||||
#include "byte.h"
|
||||
|
||||
/* str_diff returns negative, 0, or positive, depending on whether the
|
||||
* string a[0], a[1], ..., a[n]=='\0' is lexicographically smaller than,
|
||||
* equal to, or greater than the string b[0], b[1], ..., b[m-1]=='\0'.
|
||||
* When the strings are different, str_diff does not read bytes past the
|
||||
* first difference. */
|
||||
int str_diff(const char* a, const char* b) {
|
||||
register const char* s=a;
|
||||
register const char* t=b;
|
||||
register int j;
|
||||
j=0;
|
||||
for (;;) {
|
||||
if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
}
|
||||
return j;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
.TH str_diffn 3
|
||||
.SH NAME
|
||||
str_diffn \- compare two ASCIIZ strings
|
||||
.SH SYNTAX
|
||||
.B #include <str.h>
|
||||
|
||||
extern int \fBstr_diffn\fP(const char* \fIa\fR,const char* \fIb\fR,unsigned int \fIlimit\fR);
|
||||
.SH DESCRIPTION
|
||||
\fBstr_diffn\fR returns negative, 0, or positive, depending on whether the
|
||||
string \fIa\fR[0], \fIa\fR[1], ..., \fIa\fR[n]=='\\0' is
|
||||
lexicographically smaller than, equal to, or greater than the string
|
||||
\fIb\fR[0], \fIb\fR[1], ..., \fIb\fR[m-1]=='\\0'.
|
||||
|
||||
If the strings are different, str_diff does not read bytes past the
|
||||
first difference. The strings will be considered equal if the first
|
||||
\fIlimit\fR characters match.
|
||||
.SH "SEE ALSO"
|
||||
strlen(3)
|
@ -0,0 +1,21 @@
|
||||
#include "byte.h"
|
||||
|
||||
/* str_diff returns negative, 0, or positive, depending on whether the
|
||||
* string a[0], a[1], ..., a[n]=='\0' is lexicographically smaller than,
|
||||
* equal to, or greater than the string b[0], b[1], ..., b[m-1]=='\0'.
|
||||
* When the strings are different, str_diff does not read bytes past the
|
||||
* first difference. */
|
||||
int str_diffn(const char* a, const char* b, unsigned int limit) {
|
||||
register const char* s=a;
|
||||
register const char* t=b;
|
||||
register const char* u=t+limit;
|
||||
register int j;
|
||||
j=0;
|
||||
for (;;) {
|
||||
if (t>=u) break; if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
if (t>=u) break; if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
if (t>=u) break; if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
if (t>=u) break; if ((j=(*s-*t))) break; if (!*t) break; ++s; ++t;
|
||||
}
|
||||
return j;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
.TH str_equal 3
|
||||
.SH NAME
|
||||
str_equal \- compare two ASCIIZ strings
|
||||
.SH SYNTAX
|
||||
.B #include <str.h>
|
||||
|
||||
extern int \fBstr_equal\fP(const char* \fIa\fR,const char* \fIb\fR);
|
||||
.SH DESCRIPTION
|
||||
\fBstr_equal\fR returns nonzero if \fIa\fR and \fIb\fR match up to and
|
||||
including the first occurrance of \\0.
|
||||
|
||||
If the strings are different, str_equal does not read bytes past the
|
||||
first difference.
|
||||
.SH "SEE ALSO"
|
||||
strlen(3)
|
@ -0,0 +1,12 @@
|
||||
.TH str_len 3
|
||||
.SH NAME
|
||||
str_len \- find length of ASCIIZ string
|
||||
.SH SYNTAX
|
||||
.B #include <str.h>
|
||||
|
||||
extern int \fBstr_len\fP(const char* \fIstring\fR);
|
||||
.SH DESCRIPTION
|
||||
str_len returns the index of the first occurrance of \\0 in
|
||||
\fIstring\fR.
|
||||
.SH "SEE ALSO"
|
||||
strlen(3)
|
@ -0,0 +1,12 @@
|
||||
#include "str.h"
|
||||
|
||||
unsigned int str_len(const char *in) {
|
||||
register const char* t=in;
|
||||
for (;;) {
|
||||
if (!*t) break; ++t;
|
||||
if (!*t) break; ++t;
|
||||
if (!*t) break; ++t;
|
||||
if (!*t) break; ++t;
|
||||
}
|
||||
return t-in;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue