From: Junio C Hamano Date: Wed, 22 Aug 2012 18:24:50 GMT Subject: Re: Porting git to HP NonStop Message-ID: <7v393e943x.fsf@alter.siamese.dyndns.org> In-Reply-To: <003d01cd8090$214ca710$63e5f530$@schmitz-digital.de> "Joachim Schmitz" writes: >> Nice. And we have xmemdupz() would be even better as you followed-up. > > How's that one used? I forgot that we frown upon use of any x() wrapper in the compat/ layer as J6t mentioned. So probably something along these lines... int retval; char *dir_to_free = NULL; size_t len = strlen(dir); if (len && dir[len - 1] == '/') { dir_to_free = malloc(len); if (!dir_to_free) { fprintf(stderr, "malloc failed!\n"); exit(1); } memcpy(dir_to_free, dir, len - 1); dir_to_free[len - 1] = '\0'; dir = dir_to_free; } retval = mkdir(dir, mode); free(dir_to_free); return retval; It might be possible to for the error path to get away with something like: if (!dir_to_free) return -1; if we know the callers are prepared to see mkdir() failing with ENOMEM, but that is not very likely.