Ver Fonte

- replaced strndup() in get_abs_pathname() since strndup is a GNU extensions
not present in non glibc based OSes

Andrei Pelinescu-Onciul há 17 anos atrás
pai
commit
6957153c0e
1 ficheiros alterados com 8 adições e 5 exclusões
  1. 8 5
      ut.c

+ 8 - 5
ut.c

@@ -28,7 +28,6 @@
  *
  */
 
-#define _GNU_SOURCE 1 /* strndup in get_abs_pathname */
 #include <string.h>
 #include <sys/types.h>
 #include <pwd.h>
@@ -232,18 +231,22 @@ char* get_abs_pathname(str* base, str* file)
 	if (file->s[0] == '/') {
 		/* This is an absolute pathname, make a zero terminated
 		 * copy and use it as it is */
-		if ((res = strndup(file->s, file->len)) == NULL) {
-			ERR("get_abs_pathname: No memory left (strndup failed)\n");
+		if ((res = malloc(file->len+1)) == NULL) {
+			ERR("get_abs_pathname: No memory left (malloc failed)\n");
 		}
+		memcpy(res, file->s, file->len);
+		res[file->len]=0;
 	} else {
 		/* This is not an absolute pathname, make it relative
 		 * to the location of the base file
 		 */
 		/* Make a copy, function dirname may modify the string */
-		if ((buf = strndup(base->s, base->len)) == NULL) {
-			ERR("get_abs_pathname: No memory left (strdup failed)\n");
+		if ((buf = malloc(base->len+1)) == NULL) {
+			ERR("get_abs_pathname: No memory left (malloc failed)\n");
 			return NULL;
 		}
+		memcpy(buf, base->s, base->len);
+		buf[base->len]=0;
 		dir = dirname(buf);
 		
 		len = strlen(dir);