Re: Porting git to HP NonStop
From: Junio C Hamano <hidden>
Date: 2016-06-15 22:54:33
"Joachim Schmitz" [off-list ref] writes:
quoted
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.