rewinddir()

Rewind a directory stream to the start of the directory

Synopsis:

#include <sys/types.h>
#include <dirent.h>

void rewinddir( DIR * dirp );

Arguments:

dirp
A pointer to the directory stream of the directory to rewind.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The rewinddir() function rewinds the directory stream specified by dirp to the start of the directory. The directory stream will now refer to the current state of the directory, as if the calling thread had called opendir() again.


Note: The result of using a directory stream after one of the exec*() or spawn*() family of functions is undefined. After a call to fork(), either the parent or the child (but not both) can continue processing the directory stream, using the readdir() and rewinddir() functions. If both the parent and child processes use these functions, the result is undefined. Either (or both) processes may use closedir().

Examples:

List all the files in a directory, create a new file, and then list the directory contents again:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>

int main( void )
{
    DIR *dirp;
    struct dirent *direntp;
    int filedes;

    dirp = opendir( "/home/fred" );
    if( dirp != NULL ) {
        printf( "Old directory listing\n" );
        for(;;) {
            direntp = readdir( dirp );
            if( direntp == NULL ) break;
            printf( "%s\n", direntp->d_name );
        }

        filedes = creat( "/home/fred/file.new",
            S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
        close( filedes );

        rewinddir( dirp );
        printf( "New directory listing\n" );
        for(;;) {
            direntp = readdir( dirp );
            if( direntp == NULL ) break;
            printf( "%s\n", direntp->d_name );
        }
        closedir( dirp );
    }
    
    return EXIT_SUCCESS;
}

Classification:

POSIX 1003.1

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

closedir(), dirent opendir(), readdir(), readdir_r(), seekdir()