We help IT Professionals succeed at work.

remove in UNIX and ANSI C

jfbeaulieu asked
Medium Priority
Last Modified: 2008-03-10

 I have to make a CGI in C for a UNIX station but we don't have UNIX at the office and I never used UNIX in the past (or let say 3 weeks.. 10 years ago). I would like to compile an ANSI C file with visual C++ and ship the source code to the webmaster in Toronto , but I don't know if there's an equivalent for the remove(filename) function in either ANSI C or either in the UNIX toolkit (and if so, What is the include file?)

 Also, is there something available on the web, a free compiler or either a table with functions that can allow me to check if each  function is really ANSI C?

Watch Question

Most Valuable Expert 2014
Top Expert 2015

Both ANSI and POSIX specify a remove(const char *filename) function
Most Valuable Expert 2014
Top Expert 2015

I don't know where there's a list of ANSI functions, but here's a list of POSIX functions:
For a free compiler, or a free Unix, you might try:
You can use remove() in UNIX but it won't always work.

A given file name in a given directory can be the instance of the file or a link to the file. If its a link, remove won't work.

A safer bet is to call unlink(char* filename), the draw back of this one is that if you unlink the original file, it will delete all links to it in other directories (which is usualy what you want anyways).

unlink() exists in both UNIX and ANSI C so you should have no problems (exluding security issues like write acces to the directory).

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Most Valuable Expert 2014
Top Expert 2015

On what system does it not work?
I thought remove was quivalent to unlink on a file, and equivalent to rmdir on a directory.
Although remove failed on AT&T System V R. 3.1 and on Borland C++ 4.0 that is not the issue here.

It seems that I failed to explain the issue correctly.

If you call remove() on a file name in any directory, and said file happens to be a link to another file, it will not be removed. If you call unlink() it will remove that file.

On the other hand, if a given file in a directory is the source file for a series of links, calling remove() on this file will only remove the source file, leaving all the links intact but pointing to nowhere!

The behavior for unlink() and remove() is as follows:

Calling unlink() on a file name that is a link to a source file will remove the link file name from the file system.

Calling unlink() on a file name that is a physical file to which other files may (or may not ) be linked to, will remove the physical file and all links to that file.

Calling remove() on a file name that is a link to another file will fail and will not remove the link from the file system.

Calling remove on a physical file that has linked files to it will remove the physical file, but not the links, creating potential problems for other applications.

In general it is recomended that you use unlink(), rather than remove() unless you take into account the behaviour described above and take appropiate action to control the possible side effects.
unlink therfore, is not equivalent to remove, its only similar.

Using a Win95/NT analogy (which doesn't work on 95/NT :-)), links are the equivalent of shortcuts, calling remove() to delete a shortcut is not allowed in UNIX. Calling unlink() to remove the original file, deletes the original file and all shortcuts to it. Etc.

Hope this clarifies the issue.
Most Valuable Expert 2014
Top Expert 2015

I suppose the issue then is which behavior is equivalent to the 'remove(filename)'
function that jfbeaulieu wanted.
But my understanding was that the standard POSIX unlink() on a multiply linked file
would remove the named link, leaving all other links intact.
(unless you're talking about symbolic links, rather than hard links)
This is identical to the behavior of remove().
It sounds like your AT&T System V R. 3.1 is neither POSIX nor ANSI conforming.
If jfbeaulieu needs information on a non-conforming system,
we will need to know what system that is before we can answer.

Oh, and I forgot to say what the include files were.  For remove:
#include <stdio.h>
int remove(const char *path);
and for unlink:
#include <unistd.h>
int unlink(const char *path);
At least by ANSI or POSIX standards.
I would think remove should be more portable, since unlink() is
not part of the ANSI C standard
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.