diff --git a/Makefile b/Makefile index c366a8c..0d952e2 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ PREFIX = /usr/local CC = cc CFLAGS = -std=c99 -pedantic -fpic -O2 -Wall -Wextra -Wno-override-init -I. -Ilua-5.4 CPPFLAGS = -D_DEFAULT_SOURCE -DLUA_USE_READLINE -LDFLAGS = -Wl,--gc-sections -lbsd -ldl -lm -lreadline +LDFLAGS = -lm -lreadline OBJS = csto.o lcallisto.o lcl.o lenvironment.o lfile.o \ ljson.o lmath.o los.o lprocess.o lsocket.o util.o diff --git a/lcl.c b/lcl.c index efd67c6..1e5d5d6 100644 --- a/lcl.c +++ b/lcl.c @@ -9,10 +9,7 @@ #include #include #include -#ifdef __linux__ -# include -#else -/* assume BSD */ +#ifdef BSD # include #endif diff --git a/lenvironment.c b/lenvironment.c index f226658..9f22001 100644 --- a/lenvironment.c +++ b/lenvironment.c @@ -6,10 +6,7 @@ #include #include -#ifdef __linux__ -# include -#else -/* assume BSD */ +#ifdef BSD # include #endif @@ -18,6 +15,7 @@ #include "lenvironment.h" #include "lcallisto.h" +#include "util.h" /*** diff --git a/lfile.c b/lfile.c index 5411872..747b8d6 100644 --- a/lfile.c +++ b/lfile.c @@ -9,10 +9,7 @@ #include #include #include -#ifdef __linux__ -# include -#else -/* assume BSD */ +#ifdef BSD # include #endif diff --git a/lprocess.c b/lprocess.c index ddb2c47..7afe04b 100644 --- a/lprocess.c +++ b/lprocess.c @@ -9,10 +9,7 @@ #include #include #include -#ifdef __linux__ -# include -#else -/* assume BSD */ +#ifdef BSD # include #endif diff --git a/util.c b/util.c index b0d4db4..5e18bcb 100644 --- a/util.c +++ b/util.c @@ -6,32 +6,94 @@ * of these functions. */ -#include #include - #include #include -int lfail(lua_State *L, const char* mesg) +#include "util.h" + +int +lfail(lua_State *L, const char* mesg) { luaL_pushfail(L); lua_pushstring(L, mesg); return LFAIL_RET; } -void strprepend(char *s, const char *t) +#ifndef BSD +/* + * Appends src to string dst of size dsize (unlike strncat, dsize is the + * full size of dst, not space left). At most dsize-1 characters + * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). + * Returns strlen(src) + MIN(dsize, strlen(initial dst)). + * If retval >= dsize, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t dsize) +{ + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; + + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; + n--; + } + src++; + } + *dst = '\0'; + + return(dlen + (src - osrc)); /* count does not include NUL */ +} +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} +#endif + +/* + * Prepends t to s. + */ +void +strprepend(char *s, const char *t) { size_t len = strlen(t); memmove(s + len, s, strlen(s) + 1); memcpy(s, t, len); } - -void strslice(const char *s, char *buf, size_t start, size_t end) -{ - size_t j = 0; - for (size_t i = start; i <= end; ++i) { - buf[j++] = s[i]; - } - buf[j] = 0; -} diff --git a/util.h b/util.h index e1346b4..0942afd 100644 --- a/util.h +++ b/util.h @@ -9,20 +9,10 @@ lua_setfield(L, -2, lib[i].name); \ } -/* - * Pushes the fail value and the string mesg - * onto the stack, then returns 2. - */ -int lfail(lua_State *L, const char* mesg); -/* - * Prepends t onto s. - * Assumes s has enough space allocated - * for the combined string. - */ -void strprepend(char* s, const char* t); +int lfail(lua_State *, const char *); -/* - * Slices the string s into buf. - * The sliced string has the range [start .. end]. - */ -void strslice(const char *s, char *buf, size_t start, size_t end); +#ifndef BSD +size_t strlcat(char *, const char *, size_t); +size_t strlcpy(char *, const char *, size_t); +#endif +void strprepend(char *, const char *);