delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2005/01/03/11:57:09

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
From: <ams AT ludd DOT ltu DOT se>
Message-Id: <200501031656.j03GuxjN028690@speedy.ludd.ltu.se>
Subject: strerror_r() patch
To: DJGPP-WORKERS <djgpp-workers AT delorie DOT com>
Date: Mon, 3 Jan 2005 17:56:59 +0100 (CET)
X-Mailer: ELM [version 2.4ME+ PL78 (25)]
MIME-Version: 1.0
X-ltu-MailScanner-Information: Please contact the ISP for more information
X-ltu-MailScanner: Found to be clean
X-ltu-MailScanner-SpamScore: ss
X-MailScanner-From: ams AT ludd DOT ltu DOT se
Reply-To: djgpp-workers AT delorie DOT com

Hello.

Here's my patch to add strerror_r(). Some fuzz if somebody applies it is
possible.


Right,

						MartinS

Index: djgpp/include/string.h
===================================================================
RCS file: /cvs/djgpp/djgpp/include/string.h,v
retrieving revision 1.7
diff -p -u -r1.7 string.h
--- djgpp/include/string.h	4 Feb 2003 20:24:20 -0000	1.7
+++ djgpp/include/string.h	3 Jan 2005 16:54:08 -0000
@@ -54,6 +54,9 @@ size_t	strxfrm(char *_s1, const char *_s
 
 #ifndef __STRICT_ANSI__
 
+int	strerror_r(int _errnum, char *_strerrbuf, size_t _buflen);
+
 #ifndef _POSIX_SOURCE
 
 #include <sys/movedata.h>
Index: djgpp/src/libc/posix/string/strerr_r.c
===================================================================
RCS file: djgpp/src/libc/posix/string/strerr_r.c
diff -N djgpp/src/libc/posix/string/strerr_r.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ djgpp/src/libc/posix/string/strerr_r.c	3 Jan 2005 16:54:19 -0000
@@ -0,0 +1,38 @@
+/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */
+/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
+/* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */
+/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#define UNKNOWN_ERROR_STR "Unknown error: "
+
+int 
+strerror_r(int errnum, char *strerrbuf, size_t buflen)
+{
+  char ebuf[ strlen(UNKNOWN_ERROR_STR "-2147483648") + 1 ]; /* -2147483648 is 
+							       INT_MIN. */
+  const char *p;
+  int length;
+
+
+  if (errnum >= 0 && errnum < __sys_nerr)
+  {
+    p = __sys_errlist[errnum];
+    length = strlen(p);
+  }
+  else
+  {
+    length = sprintf(ebuf, "%s%d", UNKNOWN_ERROR_STR, errnum);
+    p = ebuf;
+  }
+
+  if (length < 0 || buflen < (size_t)length+1)
+  {
+    return ERANGE;
+  }
+
+  strcpy(strerrbuf, p);
+  return 0;
+}
Index: djgpp/src/libc/posix/string/strerr_r.txh
===================================================================
RCS file: djgpp/src/libc/posix/string/strerr_r.txh
diff -N djgpp/src/libc/posix/string/strerr_r.txh
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ djgpp/src/libc/posix/string/strerr_r.txh	3 Jan 2005 16:54:19 -0000
@@ -0,0 +1,53 @@
+@node strerror_r, stdio
+@findex strerror_r
+@subheading Syntax
+
+@example
+#include <string.h>
+
+int strerror_r(int error, char *strerrbuf, size_t buflen);
+@end example
+
+@subheading Description
+
+This function fills the @var{strerrbuf} of length @var{buflen} with a
+string that describes the @var{error}.  
+
+@subheading Return Value
+
+0 if successful or @code{ERANGE} if @var{buflen} is too small.  
+
+@subheading Portability
+
+@portability !ansi, posix
+
+@subheading Example
+
+@example
+f=fopen("foo", "r");
+if( f == NULL )
+@{
+  char buf[1];
+  int error_again;
+	
+  error_again = strerror_r(errno, buf, 1);
+  if( error_again )
+  @{
+    char buf2[1024];
+    int error_again2;
+
+    error_again2 = strerror_r(errno, buf2, 1024);
+    if( error_again2 )
+    @{
+      printf("Error while trying to decode error from calling strerror_r(). I give up!\n"
+      "Calling strerror: '%s'\n", strerror(error_again2));
+    @}
+    printf("Error while trying to decode error from calling fopen(): '%s'\n", 
+	buf2);
+  @}
+  else
+  @{
+    printf("Error from fopen(): %s\n", buf);
+  @}
+@end example
+
Index: djgpp/tests/libc/posix/string/makefile
===================================================================
RCS file: djgpp/tests/libc/posix/string/makefile
diff -N djgpp/tests/libc/posix/string/makefile
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ djgpp/tests/libc/posix/string/makefile	3 Jan 2005 16:54:19 -0000
@@ -0,0 +1,5 @@
+TOP=../..
+
+SRC += strerr_r.c
+
+include $(TOP)/../makefile.inc
Index: djgpp/tests/libc/posix/string/strerr_r.c
===================================================================
RCS file: djgpp/tests/libc/posix/string/strerr_r.c
diff -N djgpp/tests/libc/posix/string/strerr_r.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ djgpp/tests/libc/posix/string/strerr_r.c	3 Jan 2005 16:54:19 -0000
@@ -0,0 +1,55 @@
+/*
+ * File strerr_r.c.
+ *
+ * Copyright (C) 2005 Martin Str@"omberg <ams AT ludd DOT ltu DOT se>.
+ *
+ * This software may be used freely so long as this copyright notice is
+ * left intact. There is no warranty on this software.
+ *
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#define FILE_NAME "Doesn't ::: exist!"
+
+int main(void)
+{
+  FILE *f;
+
+  f = fopen(FILE_NAME, "r");
+  if( f == NULL )
+  {
+    char buf[1];
+    int error_again;
+	
+    error_again = strerror_r(errno, buf, 1);
+    if( error_again )
+    {
+      char buf2[1024];
+      int error_again2;
+      
+      error_again2 = strerror_r(error_again, buf2, 1024 );
+      if( error_again2 )
+      {
+	printf("Error while trying to decode error from calling strerror_r(). I give up!\n"
+	       "Calling strerror: '%s'\n", strerror(error_again2));
+      }
+      printf("Error while trying to decode error from calling fopen(): '%s'\n", 
+	     buf2);
+    }
+    else
+    {
+      printf("Error from fopen(): %s\n", buf);
+    }
+  }
+  else
+  {
+    printf("File name '%s' could be opened! This should not be possible in DOS.\n", FILE_NAME);
+
+    return 1;
+  }
+
+  return 0;
+}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019