[Aptitude-svn-commit] r4292 - in branches/aptitude-0.3/aptitude: . src/generic/util

Daniel Burrows dburrows at costa.debian.org
Mon Sep 26 20:26:58 UTC 2005


Author: dburrows
Date: Mon Sep 26 20:26:55 2005
New Revision: 4292

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/generic/util/util.cc
   branches/aptitude-0.3/aptitude/src/generic/util/util.h
Log:
Add a strerror_r wrapper.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Mon Sep 26 20:26:55 2005
@@ -1,5 +1,9 @@
 2005-09-26  Daniel Burrows  <dburrows at debian.org>
 
+	* src/generic/util/util.cc, src/generic/util/util.h:
+
+	  Add a safe wrapper for strerror_r.
+
 	* src/generic/util/util.cc:
 
 	  Correct get_homedir(): it was using the non-array form of delete

Modified: branches/aptitude-0.3/aptitude/src/generic/util/util.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/util.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/util.cc	Mon Sep 26 20:26:55 2005
@@ -20,8 +20,10 @@
 #include "util.h"
 
 #include <ctype.h>
+#include <errno.h>
 #include <pwd.h>
 #include <stdarg.h>
+#include <string.h>
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
@@ -142,6 +144,38 @@
   return "";
 }
 
+string sstrerror(int errnum)
+{
+  size_t bufsize = 512;
+
+  while(bufsize < 512 * 512)
+    {
+      char *buf = new char[bufsize];
+
+      char *result = strerror_r(errnum, buf, bufsize);
+
+      if(result == NULL)
+	{
+	  delete[] buf;
+
+	  if(errno == EINVAL)
+	    return ssprintf("Invalid error code %d", errnum);
+	  else if(errno != ERANGE)
+	    return ssprintf("Unexpected error from strerror_r: %d", errnum);
+	  else
+	    bufsize *= 2;
+	}
+      else
+	{
+	  string rval(buf);
+	  delete[] buf;
+	  return rval;
+	}
+    }
+
+  return "";
+}
+
 string get_homedir()
 {
   passwd pwbuf;

Modified: branches/aptitude-0.3/aptitude/src/generic/util/util.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/util.h	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/util.h	Mon Sep 26 20:26:55 2005
@@ -44,6 +44,11 @@
  */
 std::string sstrftime(const char *format, const tm *tm);
 
+/** Like strerror_r, but handles all memory allocation and returns a
+ *  C++ string.
+ */
+std::string sstrerror(int errnum);
+
 /** \return the home directory of the current user as given in the
  *     password database.  If no home directory can be looked up,
  *     returns an empty string.



More information about the Aptitude-svn-commit mailing list