The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Distribution Release: Gentoox 7.0 "Home", 5.0 "Pro" iBot Software Releases - RSS News 0 01-25-2009 10:20 AM
iCal, Mac OS X 10.5: Empty "Home" and "Work" calendars may appear after installing Le iBot OS X Support RSS 0 10-13-2008 10:20 PM
iCal, Mac OS X 10.5: Empty "Home" and "Work" calendars may appear after installing Le iBot OS X Support RSS 0 10-03-2008 04:40 AM
extracting first 1000 lines containing "home"... Kaminski UNIX for Dummies Questions & Answers 2 07-22-2008 03:47 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-03-2009
platinumedge platinumedge is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 1
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
  #2 (permalink)  
Old 10-03-2009
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Here's the code for how it's done in Solaris, as you can see you do have to tokenize and create the structure.
  #3 (permalink)  
Old 10-04-2009
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,383
Quote:
Originally Posted by reborg View Post
Here's the code for how it's done in Solaris, as you can see ...
I see nothing
Quote:
Originally Posted by platinumedge
how to achieve the same "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist
The easiest way (ok hardly pure C):
Code:
system("mkdir -p /home/blah1/blah2/blah3");
  #4 (permalink)  
Old 10-04-2009
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Quote:
Originally Posted by jlliagre View Post
I see nothing

Good point I forgot the code:

c Code:
  1. if (mkdir(arg, mode) < 0)
  2.                 {
  3.                         if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
  4.                         {
  5.                                 error(ERROR_system(0), "%s:", arg);
  6.                                 continue;
  7.                         }
  8.                         if (errno == EEXIST)
  9.                                 continue;
  10.  
  11.                         /*
  12.                          * -p option, preserve intermediates
  13.                          * first eliminate trailing /'s
  14.                          */
  15.  
  16.                         n = strlen(arg);
  17.                         while (n > 0 && arg[--n] == '/');
  18.                         arg[n + 1] = 0;
  19.                         for (name = arg, n = *arg; n;)
  20.                         {
  21.                                 /* skip over slashes */
  22.                                 while (*arg == '/')
  23.                                         arg++;
  24.                                 /* skip to next component */
  25.                                 while ((n = *arg) && n != '/')
  26.                                         arg++;
  27.                                 *arg = 0;
  28.                                 if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0)
  29.                                 {
  30.                                         *arg = n;
  31.                                         error(ERROR_system(0), "%s:", name);
  32.                                         break;
  33.                                 }
  34.                                 if (!(*arg = n) && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
  35.                                 {
  36.                                         if (stat(name, &st))
  37.                                         {
  38.                                                 error(ERROR_system(0), "%s: cannot stat", name);
  39.                                                 break;
  40.                                         }
  41.                                         if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(name, mode))
  42.                                         {
  43.                                                 error(ERROR_system(0), "%s: cannot change mode from %s to %s", name, fmtperm(st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)), fmtperm(mode));
  44.                                                 break;
  45.                                         }
  46.                                 }
  47.                         }
  48.                 }
  #5 (permalink)  
Old 10-06-2009
emitrax emitrax is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 37
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
The best way is probably to iterate and call mkdir and chdir every time.
I'm actually in the process of doing something similar, and that's the way I'll go.
Constructing the (full) path every time is quite annoying, while changing the working
directory should be more easy.

S.
  #6 (permalink)  
Old 10-06-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,929
Quote:
Originally Posted by emitrax View Post
Constructing the (full) path every time is quite annoying, while changing the working directory should be more easy.
Why not just append to the string instead of recreating every time?
  #7 (permalink)  
Old 10-07-2009
emitrax emitrax is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 37
Quote:
Originally Posted by Corona688 View Post
Why not just append to the string instead of recreating every time?
There is nothing to recreate.

I m doing a sequence of

mkdir(dir);
chdir(dir)
for(subdir to create)
mkdir(subdir[i]);
chdir("..");

and so on, which in my case seems perfect, as I have to create a director hierarchy from a structure (list of list).

In his case, it could do something similar.

---------- Post updated at 01:50 AM ---------- Previous update was at 01:44 AM ----------

Quote:
Originally Posted by achenle View Post
That solution introduces an effect that impacts the entire process it's running in: the changing of the current directory.

If you do something like that you will then have what's almost certainly an undocumented and definitely an unnecessary dependency on the internal implementation of what should in theory be reusable code. Unless you fully document all the internals of such calls, then you'll only have an unnecessary dependency.

And you'll outright break multithreaded apps because threads other than the one calling a mkdir() call that changes the current directory will have the current directory modified right out from under their processing.
Hmm.. Although I thought about the change of the current directory ( I use chdir(".."); to restore the current directory), I honestly didn't think about the multithreading problem. Thanks for the heads up.

S.
Reply

Bookmarks

Tags
directory, mkdir

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0