Prechádzať zdrojové kódy

- conversion function to convert str to zero terminated string

Jan Janak 19 rokov pred
rodič
commit
7936dce83f
2 zmenil súbory, kde vykonal 30 pridanie a 0 odobranie
  1. 23 0
      ut.c
  2. 7 0
      ut.h

+ 23 - 0
ut.c

@@ -28,12 +28,16 @@
  *
  */
 
+
+#include <string.h>
 #include <sys/types.h>
 #include <pwd.h>
 #include <grp.h>
 #include <stdlib.h>
 #include <time.h>
 #include "ut.h"
+#include "mem/mem.h"
+
 
 /* converts a username into uid:gid,
  * returns -1 on error & 0 on success */
@@ -118,3 +122,22 @@ time_t _timegm(struct tm* t)
 	}
 	return (tl - (tb - tl));
 }
+
+
+/*
+ * Return str as zero terminated string allocated
+ * using pkg_malloc
+ */
+char* as_asciiz(str* s)
+{
+    char* r;
+
+    r = (char*)pkg_malloc(s->len + 1);
+    if (!r) {
+	ERR("Out of memory\n");
+	return 0;
+    }
+    memcpy(r, s->s, s->len);
+    r[s->len] = '\0';
+    return r;
+}

+ 7 - 0
ut.h

@@ -479,4 +479,11 @@ int group2gid(int* gid, char* group);
  */
 time_t _timegm(struct tm* t);
 
+
+/*
+ * Return str as zero terminated string allocated
+ * using pkg_malloc
+ */
+char* as_asciiz(str* s);
+
 #endif