Add Book to My BookshelfPurchase This Book Online

Chapter 5 - Files and Directories

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Creating and Deleting Files and Directories
Chapter 3 and Chapter 4 explained how to create files using the functions creat, open, and fopen. It is also important to be able to delete files, create links, create and delete directories, and change the names of files and directories.
Deleting Files
The function provided to delete a file is called unlink:
    #include <unistd.h>
    int unlink(const char *path);
This function removes the directory entry named by path and decrements the link count (st_nlink in the struct stat structure). When the link count reaches zero, and no processes have the file open, the space occupied by the file is deallocated and the file ceases to exist. If one or more processes has the file open when the last link is removed, the link is removed from its directory (making the file inaccessible), but the space is not freed until all references to the file are closed. The process must have write permission in the directory that contains the file in order for unlink to succeed. If it does succeed, unlink returns 0; if it fails, it returns -1 and the reason for failure is stored in the external variable errno. The unlink function is not used for deleting directories. Use the rmdir function (see the next section) for that purpose.
The ANSI C standard specifies another function, called remove:
    #include <stdio.h>
    int remove(const char *path);
The remove function is identical to unlink for files; for directories, it is identical to rmdir (see the next section). On success, remove returns 0; on failure, it returns -1 and sets the external variable errno to the reason for failure.
Creating and Deleting Directories
To create a directory, use the mkdir function:
    #include <sys/types.h>
    #include <sys/stat.h>
    int mkdir(const char *path, mode_t mode);
The mkdir function creates a new directory with the name given in path. The directory is empty except for entries for itself (.) and its parent (..). The permission bits on the directory are set from mode, which is specified as described earlier in this chapter and modified by the process' umask value (see Chapter 6, Special-Purpose File Operations). Upon successful completion, mkdir returns 0; on failure it returns -1 and sets errno to the reason for failure.
To remove a directory, use the rmdir function:
    #include <unistd.h>
    int rmdir(const char *path);
The rmdir function removes the directory named by path. The directory must be empty except for entries for itself (.) and its parent (..). When the directory's link count becomes zero and no process has the directory open, the space used by the directory is freed, and the directory ceases to exist. If one or more processes have the directory open when the last link is removed, . and .. are removed and no new entries can be created in the directory, but the directory is not removed until all references to it are closed. The process must have write permission in the directory's parent directory in order for rmdir to succeed. On success, rmdir returns 0, on failure, it returns -1 and the reason for failure is placed into the external variable errno.
Creating Links
To create a hard link, use the link function:
    #include <unistd.h>
    int link(const char *existing, const char *new);
The link function creates a new link (directory entry) with the name specified in new to an existing file whose name is given in existing. To create hard links, both files must be on the same removable filesystem. Only the superuser can create hard links to directories. Upon successful completion, link returns 0; it returns -1 on failure and stores the error indication in the external variable errno.
To create a symbolic link, use the symlink function:
    #include <unistd.h>
    int symlink(const char *name1, const char *name2);
The symlink function creates a symbolic link with the name specified in name2 that points to the file named in name1. Either name can be an arbitrary pathname, they do not have to reside on the same filesystem, and the file named by name1 does not have to exist. If symlink is successful, it returns 0. If it fails, it returns -1 and stores the reason for failure in the external variable errno.
Renaming Files and Directories
To change the name of a file or directory, use the rename function:
    #include <stdio.h>
    int rename(const char *old, const char *new);
The rename function changes the name of the file or directory whose name is contained in old to the name contained in new. If the file named in new already exists, it is deleted first. Files and directories can only be renamed within the same filesystem using this call; to move a file or directory between two different filesystems, a copy operation must be performed. The rename function is implemented such that even if the system crashes in the middle of executing the function, at least one copy of the file or directory will always exist. If it succeeds, rename returns 0. If it fails, it returns -1 and stores the failure code in the external variable errno.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback