Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <stdio.h>
int rename(const char *oldname, const char *newname);
Changes the name of the file, from the name pointed to by oldname to the name pointed to by newname.
The oldname pointer must point to the name of an existing file. The newname pointer must not specify the name of an existing file. You cannot rename an open file. In case of an error, the name of the file is not changed.
The rename() function renames memory files and DASD data sets. (Non-DASD data sets, such as tapes, are not supported.) It also renames individual members of PDSs (and PDSEs); it even renames files that simulate PDSs.
Special behavior for POSIX C: Memory files must be closed unless you are working under z/OS® UNIX services.
The interpretation of the file name passed to rename() depends on whether the program is running POSIX(ON) or POSIX(OFF).
You cannot rename an HFS file to an MVS™ data set name or rename an MVS data set to an HFS file name.
Both oldname and newname must be of the same type, that is, both directories or both files.
If newname already exists, it is removed before oldname is renamed to newname. Thus, if newname specifies the name of an existing directory, it must be an empty directory.
If the oldname argument points to a symbolic link, the symbolic link is renamed. If the newname argument points to a symbolic link, the link is removed and oldname is renamed to newname. rename() does not affect any file or directory named by the contents of the symbolic link.
For rename() to succeed, the process needs write permission on the directory containing oldname and the directory containing newname. If oldname and newname are directories, rename() also needs write permission on the directories themselves.
If oldname and newname both refer to the same file, rename() returns successfully and performs no other action.
When rename() is successful, it updates the change and modification times for the parent directories of oldname and newname.
If successful, rename() returns 0.
⁄* CELEBR13
This example takes two file names as input and uses rename() to change
the file name from the first name to the second name.
*⁄
#include <stdio.h>
int main(int argc, char ** argv )
{
if ( argc != 3 )
printf( "Usage: %s old_fn new_fn\n", argv[0] );
else if ( rename( argv[1], argv[2] ) != 0 )
printf( "Could not rename file\n" );
}