rewinddir()--Reset Directory Stream to Beginning


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

 void rewinddir(DIR *dirp);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Yes

The rewinddir() function "rewinds" the position of an open directory stream to the beginning. dirp points to a DIR associated with an open directory stream.

The next call to readdir() reads the first entry in the directory. If the contents of the directory have changed since the directory was opened and rewinddir() is called, subsequent calls to readdir() read the changed contents.


Parameters

dirp
(Input) A pointer to a DIR that refers to the open directory stream to be rewound. This pointer is returned by the opendir() function.

Authorities

No authorization is required. Authorization is verified during opendir().


Return Value

None.


Error Conditions

None.


Error Messages

The following messages may be sent from this function:

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPF1F05 E Directory handle not valid.
CPF3CF2 E Error(s) occurred during running of &1 API.


Usage Notes

  1. If the dirp argument passed to rewinddir() does not refer to an open directory, unexpected results could occur.
  2. Files that are added to the directory after opendir() or rewinddir() may not be returned on calls to readdir().

Related Information


Example

The following example produces the contents of a directory by opening it, rewinding it, and closing it.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

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

main() {
  DIR *dir;
  struct dirent *entry;

  if ((dir = opendir("/")) == NULL)
    perror("opendir() error");
  else {
    puts("contents of root:");
    while ((entry = readdir(dir)) != NULL)
      printf("%s ", entry->d_name);
    rewinddir(dir);
    puts("");
    while ((entry = readdir(dir)) != NULL)
      printf("%s ", entry->d_name);
    closedir(dir);
    puts("");
  }
}

Output:

contents of root:
  . .. QSYS.LIB QDLS QOpenSys QOPT home
  . .. QSYS.LIB QDLS QOpenSys QOPT home newdir


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]