*BSD News Article 67393


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!news.unimelb.EDU.AU!munnari.OZ.AU!news.ecn.uoknor.edu!news.ysu.edu!news.cps.udayton.edu!news.engr.udayton.edu!blackbird.afit.af.mil!zombie.ncsc.mil!news.mathworks.com!newsfeed.internetmci.com!howland.reston.ans.net!Germany.EU.net!Dortmund.Germany.EU.net!interface-business.de!usenet
From: j@ida.interface-business.de (J Wunsch)
Newsgroups: comp.unix.misc,comp.unix.bsd.misc
Subject: Re: How to delete files within C programs
Date: 1 May 1996 17:52:33 GMT
Organization: interface business GmbH Dresden
Lines: 30
Message-ID: <4m88d1$3bp@innocence.interface-business.de>
References: <Oum-El-Kheir.Benkahla-3004961724540001@mac-ugm-3.imag.fr>
Reply-To: joerg_wunsch@interface-business.de (Joerg Wunsch)
NNTP-Posting-Host: ida.interface-business.de
X-Newsreader: knews 0.9.6
Xref: euryale.cc.adfa.oz.au comp.unix.misc:22324 comp.unix.bsd.misc:882

Oum-El-Kheir.Benkahla@imag.fr (Oum-El-Kheir Benkahla) writes:

>   I am searching a way for deleting files within C programs. I tried the
>exec command for executing rm (execlp("rm", "rm", "myfile", 0), which
>indeed deletes myfile but the problem here is that the exec command
>doesn't return on success. 

Ouch! :-)

Btw., you forgot to specify ``-f'' to rm(1), it will otherwise hang
indefinitely when you try removing a file you don't have write
permissions for (since it asks back then and expects a response from
stdin).

It's as simple as:

	if (unlink(myfile) == -1) {
		perror("Couldn't unlink");
	}

Note that you can't specify to _delete_ a file in Unix, all you can do
is unlinking a directory entry.  The file will be deleted if the last
reference to it has been removed, i.e. it doesn't have any directory
entries, nor is it considered ``active'' in any way (held open by a
process, or being currently executed).

-- 
J"org Wunsch					       Unix support engineer
joerg_wunsch@interface-business.de       http://www.interface-business.de/~j