Browse code

solaris - adding strnlen and strndup implementations to ensure compatability with versions <= 10

Mickey Sola authored on 2017/07/29 00:34:26
Showing 3 changed files
... ...
@@ -25,6 +25,37 @@
25 25
 
26 26
 #include <stdio.h>
27 27
 #include <string.h>
28
+#if defined(C_SOLARIS)
29
+size_t strnlen(const char *s, size_t n) __attribute__((weak));
30
+size_t strnlen(const char *s, size_t n)
31
+{
32
+    size_t i = 0;
33
+    for(; (i < n) && s[i] != '\0'; ++i);
34
+    return i;
35
+}
36
+
37
+char *strndup(const char *s, size_t n) __attribute__((weak));
38
+char *strndup(const char *s, size_t n)
39
+{
40
+    char *alloc;
41
+    size_t len;
42
+
43
+    if(!s) {
44
+        return NULL;
45
+    }
46
+
47
+    len = strnlen(s, n);
48
+    alloc = malloc(len+1);
49
+
50
+    if(!alloc) {
51
+        return NULL;
52
+    } else
53
+        memcpy(alloc, s, len);
54
+
55
+    alloc[len] = '\0';
56
+    return alloc;
57
+}
58
+#endif
28 59
 #include <sys/types.h>
29 60
 #ifndef	_WIN32
30 61
 #include <sys/socket.h>
... ...
@@ -364,6 +364,17 @@ static const unsigned int fragsz[] = {
364 364
   134217728,
365 365
 };
366 366
 #endif
367
+
368
+#if defined(C_SOLARIS)
369
+size_t strnlen(const char *s, size_t n) __attribute__((weak));
370
+size_t strnlen(const char *s, size_t n)
371
+{
372
+    size_t i = 0;
373
+    for(; (i < n) && s[i] != '\0'; ++i);
374
+    return i;
375
+}
376
+#endif
377
+
367 378
 #define FRAGSBITS (sizeof(fragsz)/sizeof(fragsz[0]))
368 379
 
369 380
 struct MPMAP {
... ...
@@ -117,6 +117,17 @@ void cli_logg_unsetup(void)
117 117
 {
118 118
 }
119 119
 #endif
120
+
121
+#if defined(C_SOLARIS)
122
+size_t strnlen(const char *s, size_t n) __attribute__((weak));
123
+size_t strnlen(const char *s, size_t n)
124
+{
125
+    size_t i = 0;
126
+    for(; (i < n) && s[i] != '\0'; ++i);
127
+    return i;
128
+}
129
+#endif
130
+
120 131
 uint8_t cli_debug_flag = 0;
121 132
 uint8_t cli_always_gen_section_hash = 0;
122 133