From cc0a4c632aeda82ab4517355b4b4489190da013e Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sun, 28 Oct 2018 13:21:42 -0400 Subject: Lua remove strncat.c & strcspn.c Change-Id: I08256f31e733d2674054e8e589d539d1396a0ee6 --- apps/plugins/lua/README | 2 -- apps/plugins/lua/SOURCES | 2 -- apps/plugins/lua/lobject.c | 66 +++++++++++++++++++++++++++------------------- apps/plugins/lua/strcspn.c | 17 ------------ apps/plugins/lua/strncat.c | 37 -------------------------- 5 files changed, 39 insertions(+), 85 deletions(-) delete mode 100644 apps/plugins/lua/strcspn.c delete mode 100644 apps/plugins/lua/strncat.c (limited to 'apps') diff --git a/apps/plugins/lua/README b/apps/plugins/lua/README index 5b53eb354e..06cfd6f8f8 100644 --- a/apps/plugins/lua/README +++ b/apps/plugins/lua/README @@ -2,9 +2,7 @@ The following files are (with slight modifications for Rockbox) from dietlibc version 0.31 which is licensed under the GPL version 2: gmtime.c - strcspn.c strftime.c - strncat.c strpbrk.c strtol.c strtoul.c diff --git a/apps/plugins/lua/SOURCES b/apps/plugins/lua/SOURCES index 3fea681a50..ff40046eb0 100644 --- a/apps/plugins/lua/SOURCES +++ b/apps/plugins/lua/SOURCES @@ -31,9 +31,7 @@ rocklib.c rocklib_img.c tlsf_helper.c fscanf.c -strcspn.c strftime.c -strncat.c strpbrk.c strtoul.c strtol.c diff --git a/apps/plugins/lua/lobject.c b/apps/plugins/lua/lobject.c index 62ad8e9359..a351ff41da 100644 --- a/apps/plugins/lua/lobject.c +++ b/apps/plugins/lua/lobject.c @@ -178,37 +178,49 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { return msg; } +/* lua 5.2 lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ */ +/* number of chars of a literal string without the ending \0 */ +#define LL(x) (sizeof(x)/sizeof(char) - 1) + +#define RETS "..." +#define PRE "[string \"" +#define POS "\"]" + +#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) void luaO_chunkid (char *out, const char *source, size_t bufflen) { - if (*source == '=') { - strncpy(out, source+1, bufflen); /* remove first char */ - out[bufflen-1] = '\0'; /* ensures null termination */ + size_t l = strlen(source); + if (*source == '=') { /* 'literal' source */ + if (l <= bufflen) /* small enough? */ + memcpy(out, source + 1, l * sizeof(char)); + else { /* truncate it */ + addstr(out, source + 1, bufflen - 1); + *out = '\0'; + } } - else { /* out = "source", or "...source" */ - if (*source == '@') { - size_t l; - source++; /* skip the `@' */ - bufflen -= sizeof(" '...' "); - l = strlen(source); - strcpy(out, ""); - if (l > bufflen) { - source += (l-bufflen); /* get last part of file name */ - strcat(out, "..."); - } - strcat(out, source); + else if (*source == '@') { /* file name */ + if (l <= bufflen) /* small enough? */ + memcpy(out, source + 1, l * sizeof(char)); + else { /* add '...' before rest of name */ + addstr(out, RETS, LL(RETS)); + bufflen -= LL(RETS); + memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); } - else { /* out = [string "string"] */ - size_t len = strcspn(source, "\n\r"); /* stop at first newline */ - bufflen -= sizeof(" [string \"...\"] "); - if (len > bufflen) len = bufflen; - strcpy(out, "[string \""); - if (source[len] != '\0') { /* must truncate? */ - strncat(out, source, len); - strcat(out, "..."); - } - else - strcat(out, source); - strcat(out, "\"]"); + } + else { /* string; format as [string "source"] */ + const char *nl = strchr(source, '\n'); /* find first new line (if any) */ + addstr(out, PRE, LL(PRE)); /* add prefix */ + bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ + if (l < bufflen && nl == NULL) { /* small one-line source? */ + addstr(out, source, l); /* keep it */ } + else { + if (nl != NULL) l = nl - source; /* stop at first newline */ + if (l > bufflen) l = bufflen; + addstr(out, source, l); + addstr(out, RETS, LL(RETS)); + } + memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); } } + diff --git a/apps/plugins/lua/strcspn.c b/apps/plugins/lua/strcspn.c deleted file mode 100644 index 0a19eaebf2..0000000000 --- a/apps/plugins/lua/strcspn.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "rocklibc.h" - -#undef strcspn -size_t strcspn(const char *s, const char *reject) -{ - size_t l=0; - int a=1,i,al=strlen(reject); - - while((a)&&(*s)) - { - for(i=0;(a)&&(i