mkdir: Add -p option
This commit is contained in:
parent
bf1cbb4cde
commit
eb79c6c47c
|
@ -1,23 +1,55 @@
|
||||||
|
#include <limits.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int create_directory(const char* path, bool create_parents)
|
||||||
{
|
{
|
||||||
if (argc <= 1)
|
const size_t pathlen = strlen(path);
|
||||||
|
if (pathlen == 0 || pathlen >= PATH_MAX)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Missing operand\n");
|
fprintf(stderr, "mkdir: %s\n", strerror(ENOENT));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = 0;
|
if (!create_parents)
|
||||||
for (int i = 1; i < argc; i++)
|
|
||||||
{
|
{
|
||||||
if (mkdir(argv[i], 0755) == -1)
|
const int ret = mkdir(path, 0755);
|
||||||
{
|
if (ret == -1)
|
||||||
perror("mkdir");
|
perror("mkdir");
|
||||||
ret = 1;
|
return -ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
for (size_t i = 0; path[i];)
|
||||||
|
{
|
||||||
|
for (; path[i] && path[i] != '/'; i++)
|
||||||
|
buffer[i] = path[i];
|
||||||
|
for (; path[i] && path[i] == '/'; i++)
|
||||||
|
buffer[i] = path[i];
|
||||||
|
buffer[i] = '\0';
|
||||||
|
ret = mkdir(buffer, 0755);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
const bool create_parents = argc >= 2 && strcmp(argv[1], "-p") == 0;
|
||||||
|
|
||||||
|
if (argc <= 1 + create_parents)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "missing operand\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
for (int i = 1 + create_parents; i < argc; i++)
|
||||||
|
if (create_directory(argv[i], create_parents) == -1)
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue