From: Rafał Długołęcki Date: Thu, 18 Aug 2016 23:24:17 +0000 (+0200) Subject: Initial commit X-Git-Tag: 0.1^0 X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=21546c9dda3da46d98e84f477e3168866c37cb67;p=mkdir_p.git Initial commit --- 21546c9dda3da46d98e84f477e3168866c37cb67 diff --git a/README b/README new file mode 100644 index 0000000..83b8181 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +mkdir_p, a very simple `mkdir -p` implementation in C +====================================================== + +Just a simple helper code for recursively creating directories. diff --git a/mkdir_p.c b/mkdir_p.c new file mode 100644 index 0000000..54f1377 --- /dev/null +++ b/mkdir_p.c @@ -0,0 +1,89 @@ +/* + * mkdir_p, a very simple `mkdir -p` implementation in C + * Copyright (C) 2016 Rafał Długołęcki + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include + +#include "mkdir_p.h" + +int mkdir_p(const char *pathname, mode_t mode) +{ + int i = 0; + int error = 0; + char * path = NULL; + int len = 0; + + if (pathname == NULL) { + return -1; + } + + len = strlen(pathname); + + if (pathname[0] == MKDIR_P_SEPARATOR) { + i = 1; + } + + while ((i <= len) && !error) { + if ((i + 1 < len) && (pathname[i + 1] == MKDIR_P_SEPARATOR)) { + path = malloc(sizeof(char) * (i + 2)); + strncpy(path, pathname, i + 1); + path[i + 1] = '\0'; + } + else if ((i + 1 == len) && (pathname[i] != MKDIR_P_SEPARATOR)) { + path = malloc(sizeof(char) * (i + 2)); + strncpy(path, pathname, i + 1); + path[i + 1] = '\0'; + } + + + if (path != NULL) { + error = mkdir(path, mode); + + if ((error != 0) && (errno != EEXIST)) { + perror("Unable to create directory"); + } + else { + error = 0; + } + + free(path); + path = NULL; + } + i++; + } + + return error; +} + +#ifdef _MKDIR_P_STANDALONE + +int main(int argc, char ** argv) +{ + if (argc == 2) { + return mkdir_p(argv[1], 0700); + } + else { + puts("mkdir_p, a very simple `mkdir -p` implementation in C"); + puts("Version: " MKDIR_P_VERSION); + puts("\n\tUsage: mkdir_p PATH\n\n"); + } + + return 0; +} + +#endif /* _MKDIR_P_STANDALONE */ diff --git a/mkdir_p.h b/mkdir_p.h new file mode 100644 index 0000000..b15c58b --- /dev/null +++ b/mkdir_p.h @@ -0,0 +1,32 @@ +/* + * mkdir_p, a very simple `mkdir -p` implementation in C + * Copyright (C) 2016 Rafał Długołęcki + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef __MKDIR_P_H +#define __MKDIR_P_H + +#include + +#define MKDIR_P_VERSION "0.1" +#define MKDIR_P_SEPARATOR '/' + +/** + * Creates directory, when parent directories are missing, creates them too + * + * @param pathname NULL terminated string containing directory path + * @param mode specifies permissions for created directories (parameter is + * passed directly to the mkdir) + */ +int mkdir_p(const char *pathname, mode_t mode); + +#endif /* __MKDIR_P_H */