[SOLVED] How to create directories using mkdir functio
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In C, the system() function takes only a single char pointer argument. If your compiler didn't warn you about this, then you should upgrade your C compiler, or upgrade the -W option. If it did warn you, then you probably should have heeded the warning. There is nothing to stop you from calling mkdir() in a loop, creating the chain of directories as the -p option of the commandline tool does. Just takes a little more code.
That doesnt help at all, I can't create the folder, they are created dynamically and to make sure which is which would modify the code way too much. I need something simple if possible, if not, then ok.
There is a mkdir (2) system call and C function you can use. You'd have to manually perform the -p operations yourself, maybe with a recursive function using mkdirat (2), basename (3), and dirname (3) (the C functions). For more info, type man[the number in ()] [name of the function], e.g. man 2 mkdir.
Kevin Barry
edit: This does the job using only C, if you're interested. It needs some more error checking, I'm sure.
Code:
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
static int open_directory(char *nName, int pParent)
{
int directory = -1;
if (pParent == 0)
directory = open(nName, O_RDONLY);
else
directory = openat(pParent, nName, O_RDONLY);
if (directory < 0) return -1;
struct stat info;
if (fstat(directory, &info) != 0)
{
close(directory);
return -1;
}
if (S_ISDIR(info.st_mode)) return directory;
close(directory);
return -1;
}
static int recursive_mkdir(char *pPath, mode_t mMode)
{
if (strcmp(pPath, ".") == 0) return 0;
if (strcmp(pPath, "/") == 0) return open("/", O_RDONLY);
char *path_copy = strdup(pPath), *head = strdup(basename(pPath)),
*rest = strdup(dirname(path_copy));
free(path_copy);
int parent = recursive_mkdir(rest, mMode);
if (parent < 0)
{
free(head);
free(rest);
return -1;
}
int directory = open_directory(head, parent);
if (directory > 0)
{
free(head);
free(rest);
if (parent > 0) close(parent);
return directory;
}
if (parent == 0)
{
if (mkdir(head, mMode) == 0) directory = open(head, O_RDONLY);
else fprintf(stderr, "cannot create directory '%s': %s\n", head, strerror(errno));
}
else
{
if (mkdirat(parent, head, mMode) == 0) directory = openat(parent, head, O_RDONLY);
else fprintf(stderr, "cannot create directory '%s': %s\n", head, strerror(errno));
}
free(head);
free(rest);
if (parent > 0) close(parent);
return directory;
}
int main(int argc, char *argv[])
{
int I;
for (I = 1; I < argc; I++)
{
char *name_copy = strdup(argv[I]);
int directory = recursive_mkdir(name_copy, 0755);
if (directory > 0)
{
fprintf(stderr, "directory '%s' created recursively\n", argv[I]);
close(directory);
}
else
fprintf(stderr, "directory '%s' NOT created\n", argv[I]);
free(name_copy);
}
}
Not sure what that means. In contrast to being created.... ?
Quote:
and to make sure which is which would modify the code way too much. I need something simple if possible
Then use the system() call method. You will have to compose the single string argument that it takes.
I haven't checked, but if mkdir is not a shell built-in, you could launch it with a fork() + exec() method, and that would save you the hassle of composing a single string argument, since there is probably an existing exec() family call that takes arguments in a method more compatible with your code.
Either way, it won't be fast, but its the kind of thing you aren't likely to be calling in a tight loop anyway.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.