LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-03-2009, 06:48 PM   #1
platinumedge
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
Question how to simulate "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist


I'm trying to make use of mkdir(char *pathname, S_IRWXU) to create the directories.

but it only creates one directory at a time. so I have to separate the tokens for "/home/blah1/blah2/blah3" as "home blah1 blah2 blah3" using delimiter "/", but it is again hectic to create such directory structure.

I know Linux command "mkdir -p /home/blah1/blah2/blah3" would create all the sub-directories if it doesn't exist.

how to achieve the same "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist

I'm using Redhat Linux 9 on Intel board 915GLVG

Last edited by platinumedge; 10-03-2009 at 06:55 PM. Reason: hardware & os details were missing
 
Old 10-03-2009, 07:32 PM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by platinumedge View Post
I'm trying to make use of mkdir(char *pathname, S_IRWXU) to create the directories.

but it only creates one directory at a time. so I have to separate the tokens for "/home/blah1/blah2/blah3" as "home blah1 blah2 blah3" using delimiter "/", but it is again hectic to create such directory structure.

I know Linux command "mkdir -p /home/blah1/blah2/blah3" would create all the sub-directories if it doesn't exist.

how to achieve the same "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist

I'm using Redhat Linux 9 on Intel board 915GLVG
Break up the path and repeat mkdir() as required until you get to the target.
 
Old 10-03-2009, 09:01 PM   #3
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
here is an emulation of mkdir -p:

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

int mkdir2(char *pathname)
{
char pathname2[512 + 1];
int i;

if (strlen(pathname) > 512)
return 1;
else {
for (i = 0; i <= strlen(pathname); i++)
if (pathname[i] == '/' || pathname[i] == '\0') {
strncpy(pathname2, pathname, i);
pathname2[i] = '\0';
mkdir(pathname2, S_IRWXU);
}
return 0;
}
}

int main(int argc, char *argv[])
{
if (argc == 2) {
mkdir2(argv[1]);
return 0;
}
else {
printf("Usage: mkdir2 pathname\n");
return 1;
}
}
 
Old 10-03-2009, 09:04 PM   #4
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
sorry, better like this:

Code:
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

int mkdir2(char *pathname)
{
	char	pathname2[512 + 1];
	int	i;

	if (strlen(pathname) > 512)
		return 1;
	else {
		for (i = 0; i <= strlen(pathname); i++)
			if (pathname[i] == '/' || pathname[i] == '\0') {
				strncpy(pathname2, pathname, i);
				pathname2[i] = '\0';
				mkdir(pathname2, S_IRWXU);
			}
		return 0;
	}
}

int main(int argc, char *argv[])
{
	if (argc == 2) {
		mkdir2(argv[1]);
		return 0;
	}
	else {
		printf("Usage: mkdir2 pathname\n");
		return 1;
	}
}
 
Old 10-04-2009, 06:28 PM   #5
platinumedge
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up thanks a lot

thanks a lot for your help manu-tm! I really appreciate your efforts. thanks once again
 
  


Reply

Tags
mkdir


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 might I restore kmail folders/mail/settings from a "badly" saved "home"? deh6 Linux - Software 5 03-08-2008 09:25 PM
KDE - "Home" and "My Computer" Icons Missing rrrssssss Fedora 7 07-19-2007 05:08 PM
"cp -p" fails with "cp: setting permissions for `/home/svbld/t': Invalid argument" mkhesin Red Hat 1 02-28-2007 12:57 AM
C/C++ functions similar to BASH's "cp", "mv", "mkdir", etc? kornerr Programming 10 04-23-2006 09:48 AM
RH9 NIS login error: "home directory does not exist" airrr Linux - Networking 2 08-06-2003 01:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:07 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