Remove() for directories

Q

Quentin Pope

Hi I am trying to delete a directory portably, using remove(). However it
always returns EOF and doesn't delete the directory.

Is there another function I should be using?

Thanks
QP
 
J

Joe Pfeiffer

Quentin Pope said:
Hi I am trying to delete a directory portably, using remove(). However it
always returns EOF and doesn't delete the directory.

Is there another function I should be using?

As somebody else has posted, this can't be done portably. It's an OS
question, not a C question.

In a Unix-derived OS, you want the rmdir() call.
 
B

Ben Pfaff

Quentin Pope said:
Hi I am trying to delete a directory portably, using remove(). However it
always returns EOF and doesn't delete the directory.

ISO C doesn't say anything about directories, but POSIX says that
remove() will delete a directory. That's about as portable as
you're going to get.

Are you sure that the directory is empty?
 
L

Lew Pitcher

Hi I am trying to delete a directory portably, using remove().

remove() is not a portable function, and does not exist in ISO C. For that
matter, the concept of directories also does not exist in ISO C,
However it always returns EOF

Not likely. You need to reread the documentation on that function.

For instance, if you use the POSIX remove() function, then it either returns
0 for success or -1 for failure. If it returns -1, then "errno is
set appropriately" for the error encountered.

and doesn't delete the directory.

Is there another function I should be using?

Perhaps. But then again, you probably should learn how to properly use the
function you first selected.
 
J

Joe Pfeiffer

Quentin Pope said:
Hi I am trying to delete a directory portably, using remove(). However it
always returns EOF and doesn't delete the directory.

Is there another function I should be using?

Taking a quick look at remove()... it's returning -1, which is of
course the same value as EOF, but isn't EOF. It also sets errno when an
error happens, so you ought to be able to use perror() to find out what
went wrong.
 
B

Ben Pfaff

Lew Pitcher said:
remove() is not a portable function, and does not exist in ISO C.

Sorry, you're wrong. From C99 (but it wasn't new in C99):

7.19.4.1 The remove function

Synopsis
1 #include <stdio.h>
int remove(const char *filename);

Description
2 The remove function causes the file whose name is the string
pointed to by filename to be no longer accessible by that
name. A subsequent attempt to open that file using that name
will fail, unless it is created anew. If the file is open,
the behavior of the remove function is
implementation-defined.

Returns
3 The remove function returns zero if the operation succeeds,
nonzero if it fails.

For that matter, the concept of directories also does not exist
in ISO C,

You're right about that.
Not likely. You need to reread the documentation on that function.
For instance, if you use the POSIX remove() function, then it either returns
0 for success or -1 for failure. If it returns -1, then "errno is
set appropriately" for the error encountered.

EOF is commonly -1, so returning EOF on error is actually a
fairly likely outcome.
 
B

Ben Pfaff

Joe Pfeiffer said:
As somebody else has posted, this can't be done portably. It's an OS
question, not a C question.

In a Unix-derived OS, you want the rmdir() call.

In a POSIX OS, remove() will remove a directory, just like
rmdir().

The Rationale even mentions UNIX in the description of remove():

7.19.4.1 The remove function

/usr/group provides the unlink system call to remove files.
The UNIX-specific definition of this function prompted the
C89 Committee to replace it with a portable function.
 
D

Dr Nick

Quentin Pope said:
Hi I am trying to delete a directory portably, using remove(). However it
always returns EOF and doesn't delete the directory.

Is there another function I should be using?

What does errno (or perror for that matter) show the actual problem to
be?

Is the directory empty?
 
Q

Quentin Pope

Taking a quick look at remove()... it's returning -1, which is of
course the same value as EOF, but isn't EOF. It also sets errno when an
error happens, so you ought to be able to use perror() to find out what
went wrong.

Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My system
is GNU/Linux.

Cheers
QP
 
Q

Quentin Pope

Taking a quick look at remove()... it's returning -1, which is of
course the same value as EOF, but isn't EOF. It also sets errno when an
error happens, so you ought to be able to use perror() to find out what
went wrong.

Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My system
is GNU/Linux.

Cheers
QP
 
Q

Quentin Pope

Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My
system is GNU/Linux.

Cheers
QP

Please ignore this post, it was posted twice in error.

Cheers
QP
 
K

Keith Thompson

Quentin Pope said:
Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My system
is GNU/Linux.

So is mine (Ubuntu 11.04), and it works for me. The Linux man page for
remove(3) says:

remove() deletes a name from the file system. It calls unlink(2)
for files, and rmdir(2) for directories.
 
K

Keith Thompson

Dr Nick said:
What does errno (or perror for that matter) show the actual problem to
be?

Is the directory empty?

Note that surprisingly few standard functions are required *by the C
standard* to set errno on failure. Of the <stdio.h> functions, only
fgetpos(), fsetpos(), and ftell() are explicitly required to do so, and
they all set it to an implementation-defined positive value (in
addition, I/O functions set errno to EILSEQ on an encoding error.

But on most systems, most or all <stdio.h> functions probably set errno
to a meaningful value on failure. POSIX has stronger requirements for
this than the C standard does.

Conclusion: You can probably count on a meaningful errno after an I/O
function has failed (assuming you set errno to 0 before the call), but
if you don't get one you probably can't blame the system for failing to
conform to the C standard.
 
J

Joe Pfeiffer

Quentin Pope said:
Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My system
is GNU/Linux.

Since it is documented as working on directories, this is very odd (to
say the least). You are deleting a straightforward directory, not a
symbolic link to a directory or something? That shouldn't matter, but
the only way I can imagine getting the error you are getting is somehow
remove() is being fooled into calling unlink() instead of rmdir(), and
your error is coming back from unlink().

Just to check, presumably you are setting errno to 0 before the call
(shouldn't be necessary, but it won't hurt), and your call to perror()
is immediately after the return?

Since rmdir() and remove() are called identically, it couldn't hurt to
try calling rmdir() directly and see what happens.
 
A

Alan Curry

Hi perror() says :-
remove: Is a directory

This would suggest to me that remove cannot act on directories. My system
is GNU/Linux.

That's quite unexpected. Several questions occur to me:

Are you using glibc, or one of the alternative C libraries?

Does the directory still actually exist afterward?

Is it possible you've accidentally redefined "remove" elsewhere in your
program or in a third-party library?

Is the directory on a foreign or experimental filesystem, where imperfections
are to be expected?

strace it. The call to remove() should show an unlink attempt failing with
EISDIR, followed by a rmdir attempt that should succeed on an empty
directory. What do you actually get?

Because of the unlink attempt, it's quite normal for perror to say "Is a
directory" after a successful remove(). Be sure it actually returned nonzero
before you call perror.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top