| 1 | #include "readsubdir.h" |
| 2 | #include "fmt.h" |
| 3 | #include "scan.h" |
| 4 | #include "str.h" |
| 5 | #include "auto_split.h" |
| 6 | |
| 7 | void readsubdir_init(rs,name,pause) |
| 8 | readsubdir *rs; |
| 9 | char *name; |
| 10 | void (*pause)(); |
| 11 | { |
| 12 | rs->name = name; |
| 13 | rs->pause = pause; |
| 14 | rs->dir = 0; |
| 15 | rs->pos = 0; |
| 16 | } |
| 17 | |
| 18 | static char namepos[FMT_ULONG + 4 + READSUBDIR_NAMELEN]; |
| 19 | |
| 20 | int readsubdir_next(rs,id) |
| 21 | readsubdir *rs; |
| 22 | unsigned long *id; |
| 23 | { |
| 24 | direntry *d; |
| 25 | unsigned int len; |
| 26 | |
| 27 | if (!rs->dir) |
| 28 | { |
| 29 | if (rs->pos >= auto_split) return 0; |
| 30 | if (str_len(rs->name) > READSUBDIR_NAMELEN) { rs->pos++; return -1; } |
| 31 | len = 0; |
| 32 | len += fmt_str(namepos + len,rs->name); |
| 33 | namepos[len++] = '/'; |
| 34 | len += fmt_ulong(namepos + len,(unsigned long) rs->pos); |
| 35 | namepos[len] = 0; |
| 36 | while (!(rs->dir = opendir(namepos))) rs->pause(namepos); |
| 37 | rs->pos++; |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | d = readdir(rs->dir); |
| 42 | if (!d) { closedir(rs->dir); rs->dir = 0; return -1; } |
| 43 | |
| 44 | if (str_equal(d->d_name,".")) return -1; |
| 45 | if (str_equal(d->d_name,"..")) return -1; |
| 46 | len = scan_ulong(d->d_name,id); |
| 47 | if (!len || d->d_name[len]) return -2; |
| 48 | return 1; |
| 49 | } |