[hackers] [ubase] Implement streplace() || sin
commit 0897d999e63be4e2e6e5c385b72a02dd649086ee
Author: sin <sin_AT_2f30.org>
Date: Thu Apr 17 16:38:31 2014 +0100
Implement streplace()
Restore variable as early as possible and in error conditions
diff --git a/sysctl.c b/sysctl.c
index e9a3898..6a764e5 100644
--- a/sysctl.c
+++ b/sysctl.c
_AT_@ -7,6 +7,16 @@
#include <unistd.h>
#include "util.h"
+static void
+streplace(char *s, int a, int b)
+{
+ char *p;
+
+ for (p = s; *p; p++)
+ if (*p == a)
+ *p = b;
+}
+
static int
getsysctl(char *variable, char **value)
{
_AT_@ -17,13 +27,15 @@ getsysctl(char *variable, char **value)
ssize_t n;
size_t sz, i;
- for (p = variable; *p; p++)
- if (*p == '.')
- *p = '/';
+ streplace(variable, '.', '/');
strlcpy(path, "/proc/sys/", sizeof(path));
- if (strlcat(path, variable, sizeof(path)) >= sizeof(path))
+ if (strlcat(path, variable, sizeof(path)) >= sizeof(path)) {
+ streplace(variable, '/', '.');
return -1;
+ }
+
+ streplace(variable, '/', '.');
fd = open(path, O_RDONLY);
if (fd < 0)
_AT_@ -59,10 +71,6 @@ getsysctl(char *variable, char **value)
if (p)
*p = '
Received on Thu Apr 17 2014 - 17:42:30 CEST
This archive was generated by hypermail 2.3.0
: Thu Apr 17 2014 - 17:48:14 CEST