mkdir problem

E

eoindeb

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;
}
 
G

Gordon Burditt

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
 
E

eoindeb

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 said:
mkdir() is not part of standard C. This might be better discussed
in comp.unix.programmer.


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..

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...
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!!!
 
C

CBFalconer

eoindeb said:
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/>
 
S

SM Ryan

# 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.
 
K

Keith Thompson

SM Ryan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top