ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello. I am trying to programmatically remove a folder (the program is in c/c++). I saw rmdir doesn't work for non-empty directories. Is deleting it's contents first the only solution? Thank you.
Hello. I am trying to programmatically remove a folder (the program is in c/c++). I saw rmdir doesn't work for non-empty directories. Is deleting it's contents first the only solution? Thank you.
) I really think you did not fully understand. I said I wanted to do it programmatically (from c), eg by calling a function. Not by using a command. And since I will have to use external programs, I will use rm..
I don't think most of us understands what you are trying to do. First time I read, you thought that the "rm" command would not work with empty directories. It does. When I read the second time, it looked like you are trying to create a c program that deletes directories. Re-reading everything, makes me think that you want to run a system command from a c program, but you couldn't because you believed that rm could not delete non-empty directories.
So what are you trying to do? If you want to call rm from a C program, something like this should help:
To clear things up. Yes, at first i was looking for some way of deleting the folder, without resorting to external commands (rm, rmdir). When I said I should have liked to use rmdir, I was talking about the function. ( )
So, if possible, I wouldn't have used system, popen or such to call the external program, be it rm or rmdir.
And yes, I know that rm can delete empty directories, where did I state it can't?
Long time since I did some C programming, but if you can't delete a non-empty directory, write a function that goes into the directory, deleting all files in it, and then delete the actual dir.
Should take to many lines to program.
Make it recursive, so dirs inside dirs are also emptied and deleted..
Yes BlackRabbit, I will try to write it in the future, although the performance could be an issue.. That folder could contain many many folders which in their turn.. You get what I mean. Thank you for your help.
Yes, if you want to code "rm -r" yourself in C, you really need to dive into the directory-tree, and delete all files at the bottom, then crawl up and remove the just-emptied directories.
Yes BlackRabbit, I will try to write it in the future, although the performance could be an issue.. That folder could contain many many folders which in their turn.. You get what I mean. Thank you for your help.
is the huge possibility of an injection exploit. What if someone tries to delete a folder (in the local directory) called "" ? You could change it to system("rm -rf ./<directory>/"); to prevent it from deleting the main directory, but it's still pretty easy to exploit (suppose <directory> is set to ' `rm -rf /` ' ). Just a thought.
I knew there was something fundamentally wrong with using external commands. Reading from other posts, I have always learned to prefer a "c" way instead of an external program.
Now I know what to say when people advise me to use external commands (it so happens that in my program the string with the directory to delete comes from a client through the network, so the possibility of an exploit would be even greater).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.