Remove() for directories

Discussion in 'C Programming' started by Quentin Pope, Aug 25, 2011.

  1. Quentin Pope

    Quentin Pope Guest

    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
     
    Quentin Pope, Aug 25, 2011
    #1
    1. Advertising

  2. Quentin Pope

    Joe Pfeiffer Guest

    Quentin Pope <> writes:

    > 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.
     
    Joe Pfeiffer, Aug 25, 2011
    #2
    1. Advertising

  3. Quentin Pope

    Ben Pfaff Guest

    Quentin Pope <> writes:

    > 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?
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Aug 25, 2011
    #3
  4. Quentin Pope

    Lew Pitcher Guest

    On August 25, 2011 12:29, in comp.lang.c, wrote:

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

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Aug 25, 2011
    #4
  5. Quentin Pope

    Joe Pfeiffer Guest

    Quentin Pope <> writes:

    > 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.
     
    Joe Pfeiffer, Aug 25, 2011
    #5
  6. Quentin Pope

    Ben Pfaff Guest

    Lew Pitcher <> writes:

    > On August 25, 2011 12:29, in comp.lang.c, wrote:
    >
    >> Hi I am trying to delete a directory portably, using remove().

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

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


    EOF is commonly -1, so returning EOF on error is actually a
    fairly likely outcome.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Aug 25, 2011
    #6
  7. Quentin Pope

    Ben Pfaff Guest

    Joe Pfeiffer <> writes:

    > Quentin Pope <> writes:
    >
    >> 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.


    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.

    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Aug 25, 2011
    #7
  8. Quentin Pope

    Dr Nick Guest

    Quentin Pope <> writes:

    > 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?
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
     
    Dr Nick, Aug 25, 2011
    #8
  9. Quentin Pope

    Quentin Pope Guest

    On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:

    > Quentin Pope <> writes:
    >
    >> 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.


    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
     
    Quentin Pope, Aug 25, 2011
    #9
  10. Quentin Pope

    Quentin Pope Guest

    On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:

    > Quentin Pope <> writes:
    >
    >> 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.


    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
     
    Quentin Pope, Aug 25, 2011
    #10
  11. Quentin Pope

    Quentin Pope Guest

    On Thu, 25 Aug 2011 19:41:24 +0000, Quentin Pope wrote:

    > On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
    >
    >> Quentin Pope <> writes:
    >>
    >>> 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.

    >
    > 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
     
    Quentin Pope, Aug 25, 2011
    #11
  12. Quentin Pope <> writes:
    > On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
    >> Quentin Pope <> writes:
    >>> 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.

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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 25, 2011
    #12
  13. Dr Nick <> writes:
    > Quentin Pope <> writes:
    >> 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?


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 25, 2011
    #13
  14. Quentin Pope

    Joe Pfeiffer Guest

    Quentin Pope <> writes:

    > On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
    >
    >> Quentin Pope <> writes:
    >>
    >>> 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.

    >
    > 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.
     
    Joe Pfeiffer, Aug 25, 2011
    #14
  15. Quentin Pope

    Alan Curry Guest

    In article <j368g6$b9e$>,
    Quentin Pope <> wrote:
    >On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
    >
    >> Quentin Pope <> writes:
    >>
    >>> 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.

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

    --
    Alan Curry
     
    Alan Curry, Aug 25, 2011
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    510
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    551
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    840
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Paul F
    Replies:
    4
    Views:
    651
    Mark Fitzpatrick
    Jun 25, 2008
  5. Adam Petrie
    Replies:
    8
    Views:
    333
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page