LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2011, 01:42 PM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
How to create directories using mkdir functio


Hello, how is it possible to create directories in C using function mkdir or system similar to command "mkdir -p"

in variable char dir I have the directory, but don't know how to call in using function system

system("mkdir -p",dir) // doesn't work

and function mkdir from C doesn't support "-p".

Last edited by robertjinx; 08-13-2011 at 01:51 PM.
 
Old 08-13-2011, 02:36 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
Old 08-13-2011, 02:38 PM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
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.
 
Old 08-13-2011, 02:40 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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);
	}
}

Last edited by ta0kira; 08-13-2011 at 08:36 PM.
 
1 members found this post helpful.
Old 08-13-2011, 02:53 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by robertjinx View Post
they are created dynamically
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.

--- rod.
 
Old 08-13-2011, 03:27 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
if mkdir is not a shell built-in
It's not a built-in, it's part of coreutils .
 
Old 08-14-2011, 04:20 AM   #7
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Ok guys, thank you. This helps to clear some stuff, but still doesn't help me
 
Old 08-14-2011, 06:00 AM   #8
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
@ta0kira: now I realized the coolness in your small code, thx!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to give options like '-v' for mkdir using system call mkdir() ? nehapawar Linux - Newbie 2 02-05-2010 02:13 AM
mkdir : cannot create directory : no such file or directory patcheezy Linux - Newbie 6 05-13-2009 11:26 AM
Samba: "homes" share, cannot create directories, can create files Herg Linux - Software 1 09-14-2006 08:48 AM
mkdir: cannot create directory `test': Read-only file system punt Linux - General 2 04-16-2005 09:58 PM
"mkdir: cannot create directory `foo': Read-only file system" on FAT32 maddes Linux - Hardware 1 11-26-2003 06:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration