summaryrefslogtreecommitdiff
path: root/libc/string
diff options
context:
space:
mode:
Diffstat (limited to 'libc/string')
-rw-r--r--libc/string/Makefile4
-rw-r--r--libc/string/strerror.c43
-rw-r--r--libc/string/strsignal.c73
3 files changed, 87 insertions, 33 deletions
diff --git a/libc/string/Makefile b/libc/string/Makefile
index b532ec4d2..1c8c6283a 100644
--- a/libc/string/Makefile
+++ b/libc/string/Makefile
@@ -53,8 +53,8 @@ $(MOBJ1): $(MSRC1)
$(CC) $(CFLAGS) -DL_$* $< -c -o $*.o
$(STRIPTOOL) -x -R .note -R .comment $*.o
-$(COBJS):
- $(CC) $(CFLAGS) $< -c $*.c -o $*.o
+$(COBJS): %.o : %.c
+ $(CC) $(CFLAGS) -c $< -o $@
$(STRIPTOOL) -x -R .note -R .comment $*.o
clean:
diff --git a/libc/string/strerror.c b/libc/string/strerror.c
index 3aa8b57e2..36c467ef4 100644
--- a/libc/string/strerror.c
+++ b/libc/string/strerror.c
@@ -23,13 +23,19 @@ Cambridge, MA 02139, USA. */
* are smaller than the previous functions and don't require static buffers.
* Removed dependence on strcat in the process.
*
- * Also appended a test routine ( -DSTRERROR_TEST ) to allow a quick check
+ * Also appended a test routine ( -DCHECK_BUF ) to allow a quick check
* on the buffer length when the sys_errorlist is modified.
+ *
+ * Added the option WANT_ERRORLIST for low-memory applications to omit the
+ * error message strings and only output the error number.
*/
+#define WANT_ERRORLIST 1
+
#include <stdio.h>
#include <string.h>
#include <errno.h>
+
#include <limits.h>
#if (INT_MAX >> 31)
@@ -39,7 +45,11 @@ Cambridge, MA 02139, USA. */
extern char *__ltostr(char *buf, long uval, int base, int uppercase);
+#if WANT_ERRORLIST
+static char retbuf[48];
+#else
static char retbuf[33]; /* 33 is sufficient for 32 bit ints */
+#endif
static const char unknown_error[] = "Unknown Error: errno"; /* = */
/* Return a string descibing the errno code in ERRNUM.
@@ -49,10 +59,12 @@ char *strerror(int err)
{
char *pos;
+#if WANT_ERRORLIST
if ((err >= 0) && (err < sys_nerr)) {
strcpy(retbuf, sys_errlist[err]);
return retbuf;
}
+#endif
/* unknown error */
pos = __ltostr(retbuf + sizeof(retbuf) + 1, err, 10, 0)
@@ -62,24 +74,39 @@ char *strerror(int err)
return pos;
}
-#if STRERROR_TEST
-/* quick way to check for sufficient buffer length */
+#ifdef CHECK_BUF
+/* quick way to check buffer length */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int max = 0;
- int i, j;
+ int j, retcode;
char *p;
+#if WANT_ERRORLIST
+ int i;
+#endif
+
+ retcode = EXIT_SUCCESS;
+
+#if WANT_ERRORLIST
for ( i=0 ; i < sys_nerr ; i++ ) {
j = strlen(sys_errlist[i])+1;
if (j > max) max = j;
}
- printf("max len = %i\n", j);
+#endif
p = strerror(INT_MIN);
- printf("<%s> %d\n", p, strlen(p)+1);
- printf("current buffer length is %d\n", sizeof(retbuf));
- return EXIT_SUCCESS;
+ j = strlen(p)+1;
+ if (j > max) max = j;
+ printf("strerror.c - Test of INT_MIN: <%s> %d\n", p, j);
+
+ if (sizeof(retbuf) != max) {
+ printf("Error: strerror.c - dimension of retbuf should be = %d\n", max);
+ retcode = EXIT_FAILURE;
+ }
+ printf("strerror.c - dimension of retbuf correct at %d\n", max);
+
+ return retcode;
}
#endif
diff --git a/libc/string/strsignal.c b/libc/string/strsignal.c
index 1a0a6ca47..60acc65b0 100644
--- a/libc/string/strsignal.c
+++ b/libc/string/strsignal.c
@@ -14,16 +14,31 @@
* Also fixed a bug in the signal name lookup code. While the table is
* declared with dimension > 60, there are currently on 32 signals listed.
*
- * Also appended a test routine ( -DSTRSIGNAL_TEST ) to allow a quick check
- * on the buffer length when the sys_errorlist is modified.
+ * Also appended a test routine ( -DCHECK_BUF ) to allow a quick check
+ * on the buffer length and the number of known signals when the sys_errorlist
+ * is modified.
+ *
+ * Added the option WANT_SIGLIST for low-memory applications to omit the
+ * signal message strings and only output the signal number.
*/
+#define WANT_SIGLIST 1
+
#include <string.h>
#include <malloc.h>
#include <signal.h>
+#include <limits.h>
+
+#if (INT_MAX >> 31)
+/* We're set up for 32 bit ints */
+#error need to check size allocation for static buffer 'retbuf'
+#endif
+
extern char *__ltostr(char *buf, long uval, int base, int uppercase);
+#if WANT_SIGLIST
+
const char *const sys_siglist[] = {
"Unknown signal",
"Hangup",
@@ -60,26 +75,27 @@ const char *const sys_siglist[] = {
NULL
};
-#include <limits.h>
-
-#if (INT_MAX >> 31)
-/* We're set up for 32 bit ints */
-#error need to check size allocation for static buffer 'retbuf'
#endif
+#define NUM_KNOWN_SIGNALS 32
+
/********************** Function strsignal ************************************/
+static char retbuf[28]; /* 28 is sufficient for 32 bit ints */
+static const char unknown_signal[] = "Unknown Signal:";
+
char *strsignal(int sig)
{
- static char retbuf[28]; /* 28 is sufficient for 32 bit ints */
- static const char unknown_signal[] = "Unknown Signal:";
char *pos;
+#ifdef WANT_SIGLIST
/* if ((sig >= 0) && (sig < _NSIG)) { */
- if ((sig >= 0) && (sig < 32)) { /* WARNING!!! NOT ALL _NSIG DEFINED!!! */
+ /* WARNING!!! NOT ALL _NSIG DEFINED!!! */
+ if ((sig >= 0) && (sig < NUM_KNOWN_SIGNALS)) {
strcpy(retbuf, sys_siglist[sig]);
return retbuf;
}
+#endif
pos = __ltostr(retbuf + sizeof(unknown_signal) + 1, sig, 10, 0)
- sizeof(unknown_signal);
@@ -90,33 +106,44 @@ char *strsignal(int sig)
/********************** THE END ********************************************/
-#if STRSIGNAL_TEST
+#ifdef CHECK_BUF
/* quick way to check for sufficient buffer length */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int max = 0;
- int i, j;
+ int j, retcode;
+
const char *p;
+#if WANT_SIGLIST
+ int i;
+#endif
+ retcode = EXIT_SUCCESS;
+#if WANT_SIGLIST
printf("_NSIG = %d from headers\n", _NSIG);
- for ( i=0 ; i < _NSIG ; i++ ) {
- p = sys_siglist[i];
- if (!p) {
- printf("Warning! I only count %d signals!\n", i);
- break;
- }
+ for ( i=0 ; sys_siglist[i] ; i++ ) {
j = strlen(sys_siglist[i])+1;
if (j > max) max = j;
}
- printf("max len = %i\n", j);
+ if (i != NUM_KNOWN_SIGNALS) {
+ printf("Error: strsignal.c - NUM_KNOWN_SIGNALS should be %d\n", i);
+ retcode = EXIT_FAILURE;
+ }
+#endif
p = strsignal(INT_MIN);
- printf("<%s> %d\n", p, strlen(p)+1);
+ j = strlen(p)+1;
+ if (j > max) max = j;
+ printf("strsignal.c - Test of INT_MIN: <%s> %d\n", p, j);
+
+ if (sizeof(retbuf) != max) {
+ printf("Error: strsignal.c - dimension of retbuf should be = %d\n", max);
+ retcode = EXIT_FAILURE;
+ }
+ printf("strsignal.c - dimension of retbuf correct at %d\n", max);
- p = strsignal(i-1);
- printf("last signal %d is %s\n", i-1, p);
- return EXIT_SUCCESS;
+ return retcode;
}
#endif