*BSD News Article 67340


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!solace!nntp.uio.no!news.cais.net!newsfeed.internetmci.com!news.inc.net!cs.uwp.edu!nelson
From: nelson@cs.uwp.edu (Jeremy Nelson)
Newsgroups: comp.unix.misc,comp.unix.bsd.misc
Subject: Re: How to delete files within C programs
Date: 1 May 1996 16:21:52 GMT
Organization: University of Wisconsin - Parkside
Lines: 80
Message-ID: <4m8330$ebv@news.inc.net>
References: <Oum-El-Kheir.Benkahla-3004961724540001@mac-ugm-3.imag.fr> <4m5p3k$3nq@dfw-ixnews2.ix.netcom.com> <4m7sr3$rf9@news.rhrz.uni-bonn.de>
NNTP-Posting-Host: 131.210.1.4
Xref: euryale.cc.adfa.oz.au comp.unix.misc:22306 comp.unix.bsd.misc:874


Topic originally under discussion:  How to remove a file under C in unix?

Henry G. Juengst <juengst@saph1.physik.uni-bonn.de> wrote:
>This might be a simple solution if you know the solution, but why should
>a beginner know that the function to delete a file is not something like
>'delete_file', but 'unlink' in the unix mud ? 

* A beginner does not attempt to program without a good tutorial and 
  reference book:  Such books would explain these functions reasonably;

Therefore:
* A good book on ANSI C will have a reference to the function "remove",
  which is mandated by standard C.
* A good book on programming in C under unix would mention "unlink".
* The original poster said "I want to know how to do what 'rm' does in C".
  Almost without exception, if you do a 'man' on a simple system utility,
  there will be cross-references to related and interesting C functions.  By
  checking the man pages for those functions, within a few hits, you will
  surely find what you are looking for.  The man page for 'rm' is no exception.


rm(1)                     User Commands                     rm(1)

NAME
     rm, rmdir - remove files or directories

SYNOPSIS
     rm [-f] [-i] filename...

     rm -r [-f] [-i] dirname...[filename...]

     rmdir [-p] [-s] dirname...

[....]

SEE ALSO
     rmdir(2), unlink(2), environ(5)


Since the person wants to remove a file, i would assume they would realize
that "rmdir" is probably not the right call:  Therefore, looking at "unlink"
and "environ" would be in order.  (Of course, after looking at a few hundred
man pages, a programmer realizes that C function calls typically fall under
section 2 and 3 of the man pages.)


unlink(2)                 System Calls                  unlink(2)

NAME
     unlink - remove directory entry

SYNOPSIS
     #include <unistd.h>

     int unlink(const char *path);

DESCRIPTION
     unlink() removes the directory entry named by the path  name
     pointed  to  by  path.  and decrements the link count of the
     file referenced by the directory entry.  When all links to a
     file have been removed and no process has the file open, the
     space occupied by the file is freed and the file  ceases  to
     exist.
[....]

Looks like we have a winner!



>Same for 'sprintf'. This is not a function for fast sprinter. .-)

All reasonably non-novice C programmers should be familiar with
the presence and use of the sprintf() function.  If they are not, its time
to go back and refer to your C tutorial book.  I dont think its unreasonable
for someone to expect another C programmer to know about sprintf(). (IMO)

The information is all there:  You just have to learn how to go find it.

Jeremy Nelson