Solved

remove in UNIX and ANSI C

Posted on 1997-10-17
6
783 Views
Last Modified: 2008-03-10
Hi,

 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?

0
Comment
Question by:jfbeaulieu
  • 4
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1255911
Both ANSI and POSIX specify a remove(const char *filename) function
0
 
LVL 84

Expert Comment

by:ozo
ID: 1255912
I don't know where there's a list of ANSI functions, but here's a list of POSIX functions:
http://www.ora.com/catalog/posix/toc.html
For a free compiler, or a free Unix, you might try:
http://www.fsf.org/
0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 30 total points
ID: 1255913
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).
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
ID: 1255914
On what system does it not work?
I thought remove was quivalent to unlink on a file, and equivalent to rmdir on a directory.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1255915
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1255916
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
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now