gtpc2m98C/C++ Language Support User's Guide

unlink-Remove a Directory Entry

This function removes a directory entry.

Format

#include <unistd.h>
int unlink(const char *pathname);

pathname
The link to be deleted.

The unlink function deletes the link named by pathname and decrements the link count for the file itself.

pathname can refer to a path name, a link, or a symbolic link. If pathname refers to a symbolic link, the unlink function removes the symbolic link, but not any file or directory named by the contents of the symbolic link.

If any processes have the file open when the unlink function removes the last link and the directory entry, the file itself remains available to these processes until they close it. The last process to be completed closes the file.

The unlink function cannot be used to remove a directory; use the rmdir function instead.

TPF deviation from POSIX

The TPF system does not support the ctime (change time) time stamp.

Normal Return

If successful, the unlink function updates the modification time for the parent directory and returns a value of 0.

Error Return

If unsuccessful, the unlink function returns a value of -1 and sets errno to one of the following:

EACCES
The process did not have search permission for some component of pathname, or did not have write permission for the directory containing the link to be removed.

EBUSY
The link specified by pathname cannot be unlinked because it is currently being used by the system or some other process.

ELOOP
A loop exists in symbolic links. This error is issued if the number of symbolic links found while resolving the pathname argument is greater than POSIX_SYMLOOP.

ENAMETOOLONG
pathname is longer than PATH_MAX characters or some component of pathname is longer than NAME_MAX characters. For symbolic links, the length of the path name string substituted for a symbolic link exceeds PATH_MAX.

ENOENT
pathname does not exist or it is an empty string.

ENOTDIR
Some component of the pathname prefix is not a directory.

EPERM
pathname is a directory and the unlink function cannot be used on directories.

ETPFNPIPWSYS
In a loosely coupled environment, there was an attempt to access a FIFO special file (or named pipe) from a processor other than the processor on which the file was created.

Programming Considerations

 The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation. 

Examples

The following example removes a directory entry.

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
 
main() {
  int fd;
  char fn[]="unlink.file";
 
  if ((fd = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    close(fd);
    if (unlink(fn) != 0)
      perror("unlink() error");
  }
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.