468,505 Members | 1,559 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

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 8852
>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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Salmo Bytes | last post: by
8 posts views Thread by Sue | last post: by
4 posts views Thread by ProvoWallis | last post: by
30 posts views Thread by MikeC | last post: by
8 posts views Thread by vj | last post: by
reply views Thread by james.mcdonagh | last post: by
2 posts views Thread by _q_u_a_m_i_s's | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.