Re: Porting git to HP NonStop
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 22, 2012, 18:24 UTC
- Message-ID
- <7v393e943x.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <003d01cd8090$214ca710$63e5f530$@schmitz-digital.de>
"Joachim Schmitz" <jojo@schmitz-digital.de> 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<allocate>() 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.