File: dirent.c

package info (click to toggle)
gccxml 0.9.0%2Bcvs20100501-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 79,132 kB
  • ctags: 73,371
  • sloc: ansic: 751,436; cpp: 34,175; asm: 26,833; sh: 5,077; makefile: 4,696; lex: 589; awk: 566; perl: 334; yacc: 271; pascal: 86; python: 29
file content (58 lines) | stat: -rw-r--r-- 919 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <dirent.h>
#include <io.h>
typedef struct DIR_s
{
  int first;
  struct dirent dir;
  long handle;
} DIR;

DIR* opendir(const char* dirname)
{
  struct _finddata_t data;
  long handle;
  char buf[1024];
  strcpy(buf, dirname);
  if(buf[strlen(buf)-1] != '\\')
    {
    strcat(buf, "\\");
    }
  strcat(buf, "*.*");
  if((handle = _findfirst(buf, &data)) >= 0)
    {
    DIR* d = (DIR*)malloc(sizeof(DIR));
    d->handle = handle;
    d->first = 1;
    strcpy(d->dir.d_name, data.name);
    return d;
    }
  return 0;
}

struct dirent* readdir(DIR* d)
{
  if(d)
    {
    struct _finddata_t data;
    if(d->first)
      {
      d->first = 0;
      return &d->dir;
      }
    else if(_findnext(d->handle, &data) == 0)
      {
      strcpy(d->dir.d_name, data.name);
      return &d->dir;
      }
    }
  return 0;
}

int closedir(DIR* d)
{
  if(d)
    {
    return _findclose(d->handle);
    }
  return 0;
}