nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

readsubdir.c (1048B)


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