diff options
Diffstat (limited to 'src/regress/lib/libc/telldir/shortseek.c')
| -rw-r--r-- | src/regress/lib/libc/telldir/shortseek.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/regress/lib/libc/telldir/shortseek.c b/src/regress/lib/libc/telldir/shortseek.c new file mode 100644 index 0000000000..9b2141ab4c --- /dev/null +++ b/src/regress/lib/libc/telldir/shortseek.c | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | /* $OpenBSD: shortseek.c,v 1.1 2013/11/03 00:20:24 schwarze Exp $ */ | ||
| 2 | |||
| 3 | /* Written by Otto Moerbeek, 2006, Public domain. */ | ||
| 4 | /* Modified by Ingo Schwarze, 2013, Public domain. */ | ||
| 5 | |||
| 6 | #include <sys/types.h> | ||
| 7 | #include <dirent.h> | ||
| 8 | #include <err.h> | ||
| 9 | #include <limits.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | #include <string.h> | ||
| 12 | |||
| 13 | #include "utils.h" | ||
| 14 | |||
| 15 | #define NFILES 5 | ||
| 16 | |||
| 17 | static void | ||
| 18 | shortloop(DIR *dp, int iend, int iback) | ||
| 19 | { | ||
| 20 | struct dirent *f; | ||
| 21 | char fend[PATH_MAX], fback[PATH_MAX]; | ||
| 22 | long pos, t, remember = -1; | ||
| 23 | int i; | ||
| 24 | |||
| 25 | rewinddir(dp); | ||
| 26 | snprintf(fend, sizeof fend, "%d", iend); | ||
| 27 | snprintf(fback, sizeof fback, "%d", iback); | ||
| 28 | |||
| 29 | /* Scan to iend, remember where iback is. */ | ||
| 30 | |||
| 31 | for (;;) { | ||
| 32 | pos = telldir(dp); | ||
| 33 | f = readdir(dp); | ||
| 34 | if (f == NULL) | ||
| 35 | errx(1, "file %s not found", fend); | ||
| 36 | if (strcmp(fback, f->d_name) == 0) | ||
| 37 | remember = pos; | ||
| 38 | if (strcmp(fend, f->d_name) == 0) | ||
| 39 | break; | ||
| 40 | } | ||
| 41 | if (remember == -1) | ||
| 42 | errx(1, "file %s not found", fback); | ||
| 43 | |||
| 44 | /* Go back to iback, checking seekdir, telldir and readdir. */ | ||
| 45 | |||
| 46 | seekdir(dp, remember); | ||
| 47 | if ((t = telldir(dp)) != remember) | ||
| 48 | errx(1, "tell after seek %s %ld != %ld", fback, t, remember); | ||
| 49 | if ((t = telldir(dp)) != remember) | ||
| 50 | errx(1, "tell after tell %s %ld != %ld", fback, t, remember); | ||
| 51 | f = readdir(dp); | ||
| 52 | if (f == NULL) | ||
| 53 | errx(1, "readdir %s at %ld", fback, remember); | ||
| 54 | |||
| 55 | if (strcmp(f->d_name, fback)) | ||
| 56 | errx(1, "name mismatch: %s != %s", f->d_name, fback); | ||
| 57 | |||
| 58 | /* Check that readdir can iterate the remaining files. */ | ||
| 59 | |||
| 60 | for (i = iback + 1; i < NFILES; i++) { | ||
| 61 | f = readdir(dp); | ||
| 62 | if (f == NULL) | ||
| 63 | errx(1, "readdir %i failed", i); | ||
| 64 | } | ||
| 65 | |||
| 66 | /* Check that readdir stops at the right place. */ | ||
| 67 | |||
| 68 | f = readdir(dp); | ||
| 69 | if (f != NULL) | ||
| 70 | errx(1, "readdir %i returned %s", NFILES, f->d_name); | ||
| 71 | } | ||
| 72 | |||
| 73 | void | ||
| 74 | shortseek(void) | ||
| 75 | { | ||
| 76 | DIR *dp; | ||
| 77 | int iend, iback; | ||
| 78 | |||
| 79 | createfiles(NFILES); | ||
| 80 | |||
| 81 | dp = opendir("d"); | ||
| 82 | if (dp == NULL) | ||
| 83 | err(1, "shortseek: opendir"); | ||
| 84 | |||
| 85 | for (iend = 0; iend < NFILES; iend++) | ||
| 86 | for (iback = 0; iback <= iend; iback++) | ||
| 87 | shortloop(dp, iend, iback); | ||
| 88 | |||
| 89 | closedir(dp); | ||
| 90 | delfiles(); | ||
| 91 | } | ||
