-/* $OpenBSD: mkpath.c,v 1.4 2019/05/08 21:30:11 benno Exp $ */
+/* $OpenBSD: mkpath.c,v 1.5 2021/05/17 11:52:10 claudio Exp $ */
/*
* Copyright (c) 1983, 1992, 1993
* The Regents of the University of California. All rights reserved.
{
struct stat sb;
char *slash;
- int done = 0;
+ int done;
slash = path;
- while (!done) {
+ for (;;) {
slash += strspn(slash, "/");
slash += strcspn(slash, "/");
done = (*slash == '\0');
*slash = '\0';
- if (stat(path, &sb)) {
- if (errno != ENOENT || (mkdir(path, 0777) &&
- errno != EEXIST)) {
- ERR("%s: stat", path);
+ if (mkdir(path, 0777) != 0) {
+ int mkdir_errno = errno;
+
+ if (stat(path, &sb) == -1) {
+ /* Not there; use mkdir()s errno */
+ errno = mkdir_errno;
+ return (-1);
+ }
+ if (!S_ISDIR(sb.st_mode)) {
+ /* Is there, but isn't a directory */
+ errno = ENOTDIR;
return (-1);
}
- } else if (!S_ISDIR(sb.st_mode)) {
- errno = ENOTDIR;
- ERR("%s: stat", path);
- return (-1);
}
+ if (done)
+ break;
+
*slash = '/';
}