473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

mkdir problem

I am trying to create a directory on Solaris using the mkdir()
function. This works fine when I pass a string literal ("/etc/hosts")
to mkdir, but if I try passing a directory pointer to mkdir, it returns
a -1 error. The directory error works fine with fopen - does anyone
know what I am doing wrong?? Here's a snippet of the code:

int Check_Directory() //check for existence of directory - if not
there, create the file
{
int status, ret = 0;

char * directory;
directory = Directory(); //returns a char pointer - changes
depending on user input

FILE * infile, *outfile ;
infile = fopen (directory, "r");
if (infile) //no need to create directory
{
ret = 1; //the directory does exist
fclose( infile);
}
else
{
status = mkdir (directory, 0777);
printf("%i",status);

outfile = fopen (directory, "w");
fprintf(outfile,"%s","testing");
fclose (outfile);
}
return ret;
}

May 22 '06 #1
5 9221
>I am trying to create a directory on Solaris using the mkdir()
function.
mkdir() is not part of standard C. This might be better discussed
in comp.unix.programmer.
This works fine when I pass a string literal ("/etc/hosts")
Why on earth would you want to do "mkdir /etc/hosts"?
On most systems that have that file, it's supposed to be a text
file, not a directory.
to mkdir, but if I try passing a directory pointer to mkdir, it returns
a -1 error. The directory error works fine with fopen
What does it mean for an *error* to work fine with fopen?
- does anyone
know what I am doing wrong?? Here's a snippet of the code:
Print the filename 'directory' points at. Is it valid? Does the
path contain parent directories that don't exist?

After mkdir() fails, call perror("mkdir"); . What error
does it print?

You may not be permitted to open a directory for reading with open()
or fopen() (as opposed to opendir()), particularly over NFS, (Fails
with EISDIR on FreeBSD) which makes fopen() particularly unsuited
for checking the existence of a directory. It also doesn't check that
it *is* a directory (vs. ordinary file, named pipe, socket, etc.)

I know of *NO* system which allows you to open a directory for
writing, even as root, since something like

echo foo > /
even if done by root would be catastrophic.

Gordon L. Burditt
int Check_Directory() //check for existence of directory - if not
there, create the file
Well, what is the result supposed to be, a DIRECTORY or a FILE?
{
int status, ret = 0;

char * directory;
directory = Directory(); //returns a char pointer - changes
depending on user input

FILE * infile, *outfile ;
infile = fopen (directory, "r");
if (infile) //no need to create directory
{
ret = 1; //the directory does exist
fclose( infile);
}
else
{
status = mkdir (directory, 0777);
printf("%i",status);

outfile = fopen (directory, "w");
fprintf(outfile,"%s","testing");
fclose (outfile);
}
return ret;
}


Gordon L. Burditt
May 22 '06 #2
Sorry for the confusion - let me try explain myself better...I have
figured out the problem, but just want to clarify anyway for others...

Gordon Burditt wrote:
I am trying to create a directory on Solaris using the mkdir()
function.
mkdir() is not part of standard C. This might be better discussed
in comp.unix.programmer.
This works fine when I pass a string literal ("/etc/hosts")


Why on earth would you want to do "mkdir /etc/hosts"?
On most systems that have that file, it's supposed to be a text
file, not a directory.


I used /etc/hosts as an example only - say for instance I am trying to
create "/etc/Test" directory..

to mkdir, but if I try passing a directory pointer to mkdir, it returns
a -1 error. The directory error works fine with fopen
What does it mean for an *error* to work fine with fopen?

the *error* was a typo - what I meant was that when I pass the
directory pointer to fopen() it opens the file if it exists - no
problems...
- does anyone
know what I am doing wrong?? Here's a snippet of the code:


Print the filename 'directory' points at. Is it valid? Does the
path contain parent directories that don't exist?


Yes the filename directory points to is valid. The problem is that I am
trying to make 2 levels of directories - that is I am trying to create
a sub-directory of a directory that doesn't exist yet - here is my
problem (a pretty stupid one really!!!)

Thanks for the help - it solved my problem!!!

After mkdir() fails, call perror("mkdir"); . What error
does it print?

You may not be permitted to open a directory for reading with open()
or fopen() (as opposed to opendir()), particularly over NFS, (Fails
with EISDIR on FreeBSD) which makes fopen() particularly unsuited
for checking the existence of a directory. It also doesn't check that
it *is* a directory (vs. ordinary file, named pipe, socket, etc.)

I know of *NO* system which allows you to open a directory for
writing, even as root, since something like

echo foo > /
even if done by root would be catastrophic.

Gordon L. Burditt
int Check_Directory() //check for existence of directory - if not
there, create the file


Well, what is the result supposed to be, a DIRECTORY or a FILE?
{
int status, ret = 0;

char * directory;
directory = Directory(); //returns a char pointer - changes
depending on user input

FILE * infile, *outfile ;
infile = fopen (directory, "r");
if (infile) //no need to create directory
{
ret = 1; //the directory does exist
fclose( infile);
}
else
{
status = mkdir (directory, 0777);
printf("%i",status);

outfile = fopen (directory, "w");
fprintf(outfile,"%s","testing");
fclose (outfile);
}
return ret;
}


Gordon L. Burditt


May 22 '06 #3
eoindeb wrote:

I am trying to create a directory on Solaris using the mkdir()
function. This works fine when I pass a string literal ("/etc/hosts")
to mkdir, but if I try passing a directory pointer to mkdir, it returns
a -1 error. The directory error works fine with fopen - does anyone
know what I am doing wrong?? Here's a snippet of the code:


You failed to define a directory, or to show the code for the
mkdir() function (which is not standard). You are either in the
wrong newsgroup, or very sloppy.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 23 '06 #4
"eoindeb" <Eo*****@gmail.com> wrote:
# I am trying to create a directory on Solaris using the mkdir()
# function. This works fine when I pass a string literal ("/etc/hosts")
# to mkdir, but if I try passing a directory pointer to mkdir, it returns
# a -1 error. The directory error works fine with fopen - does anyone
# know what I am doing wrong?? Here's a snippet of the code:

If you're going to use POSIX mkdir, you might as well use
stat() as well. A failed open doesn't mean the name doesn't
exists, it can also mean you don't have read and/or write
permission. stat() will let you know if the name really exists
and if so what the permission modes are.

# else
# {
# status = mkdir (directory, 0777);

if (status==-1) perror(directory);

That will output to stderr two important pieces of information:
(1) The name of the directory you're trying to create
so that you can verify you're trying to create the
directory you think you're trying to create.
(2) The actual error the system is returning, like
you don't have permission, already exists, your
momma wears combat boots, missing intermediate
directory, etc.

Get used to using perror or strerror as well as printing things
out--knowing is better than guessing.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Quit killing people. That's high profile.
May 23 '06 #5
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
"eoindeb" <Eo*****@gmail.com> wrote:
I am trying to create a directory on Solaris using the mkdir()
function. This works fine when I pass a string literal ("/etc/hosts")
to mkdir, but if I try passing a directory pointer to mkdir, it returns
a -1 error. The directory error works fine with fopen - does anyone
know what I am doing wrong?? Here's a snippet of the code:


If you're going to use POSIX mkdir, you might as well use
stat() as well. A failed open doesn't mean the name doesn't
exists, it can also mean you don't have read and/or write
permission. stat() will let you know if the name really exists
and if so what the permission modes are.


If this had been posted to a newsgroup where it's topical (say,
comp.unix.programmer or comp.unix.solaris), someone would have pointed
out a serious problem with this approach. (Briefly, things can happen
between the call to stat() and the call to mkdir().)

If you want to discuss this further, please take it somewhere else.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Salmo Bytes | last post by:
I have a script that wants to mirror a directory structure, reading from location1 and writing (mkdir) at location2. This code works fine on a my own desktop test box. But fails at 'mkdir' when...
8
by: Sue | last post by:
AccessXP in Access2000 Mode: In my code I use the MkDir method to create a folder and then I want to use the transfertext method to create a delimited text file in that folder. MkDir runs and...
4
by: ProvoWallis | last post by:
Hi, I'm trying to write a script that will create a new directory and then write the results to this newly created directory but it doesn't seem to work for me and I don't know why. I'm hoping...
30
by: MikeC | last post by:
Good People, I'm writing a backup utility that uses a chdir() to go into the source directory (in which the files reside that I want to back up), so I don't want to use chdir() to get into the...
8
by: vj | last post by:
How do I do the following unix command: mkdir -m770 test with the os.mkdir command. Using os.mkdir(mode=0770) ends with the incorrect permissions. Thanks, VJ
0
by: james.mcdonagh | last post by:
Hi I am a newbie using nAnt for .net 2.0. As such I have not come across this bug before, and I would be happy of any help that you may be able to provide. In order to help I have included the...
1
by: jamesmcdonagh | last post by:
Hi newbie using nAnt for .net 2.0. I would be happy of any help that you may be able to provide. The weird thing is that VS.net builds without a problem. And the intellisense within the object...
3
by: webandwe | last post by:
Hi, I got form that let you state a folder name, on sumbit thw script creates the folder with the name you stated, the problem is I want it to upload the php file "view.php" when the folder is...
2
by: _q_u_a_m_i_s's | last post by:
Hy, i encountered a weird problem on a server running php5, and apache. Seems like i cannot create folders that end with "/". for example: mkdir("test/") will fail mkdir("test") will work Is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.