File: dirfix.c

package info (click to toggle)
bincompat 1.1.0-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 152 kB
  • ctags: 97
  • sloc: ansic: 1,291; makefile: 78; asm: 74; sh: 2
file content (99 lines) | stat: -rw-r--r-- 2,373 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stddef.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>

struct kernel_dirent
{
  long int d_ino;
  long d_off;
  unsigned short int d_reclen;
  char d_name[256];
};

static void
l2k(struct kernel_dirent *d, const struct dirent *s)
{
  d->d_ino = s->d_fileno;
  d->d_off = s->d_off;
  d->d_reclen = (s->d_reclen
		 - sizeof(struct dirent) + sizeof (struct kernel_dirent));
  memcpy(d->d_name, s->d_name, 256);
}

struct kernel_dirent *readdA0 (DIR *dirp)
{
  struct dirent *s, ss;
  static struct kernel_dirent kd;
  if (readdir_r(dirp, &ss, &s) != 0)
    return NULL;
  if (s == NULL)
    return NULL;
  l2k(&kd, s);
  return &kd;
}

int readdA0_r (DIR *dirp, struct kernel_dirent *entry,
	       struct kernel_dirent **result)
{
  int rr;
  struct dirent e, *r;
  rr = readdir_r (dirp, &e, &r);
  if (rr != 0)
    return rr;
  l2k(entry, &e);
  *result = entry;
  return 0;
}

int scandA0 (const char *dir,
	     struct kernel_dirent ***namelist,
	     int (*selector) (struct kernel_dirent *),
	     int (*cmp) (const void * a, const void * b))
{
  int mycmp (const void *ax, const void *bx)
    {
      struct kernel_dirent ka, kb, *kap = &ka, *kbp = &kb;
      l2k(kap, *(struct dirent **)ax);
      l2k(kbp, *(struct dirent **)bx);
      return cmp(&kap, &kbp);
    }
  int mysel (const struct dirent *d)
    {
      struct kernel_dirent kd;
      l2k(&kd, d);
      return selector(&kd);
    }
  int rr, i;
  struct dirent **names;
  rr = scandir(dir, &names, selector == NULL ? NULL : mysel,
	       cmp == NULL ? NULL : mycmp);
  if (rr == -1)
    return -1;
  *namelist = malloc(sizeof(**namelist) * rr);
  for (i = 0; i < rr; i++)
    {
      namelist[0][i] = malloc(sizeof(struct kernel_dirent));
      l2k(namelist[0][i],names[i]);
      free(names[i]);
    }
  free(names);
  return rr;
}

/* Function to compare two `struct dirent's alphabetically.  */
int alphasoA1 (struct kernel_dirent *a, struct kernel_dirent *b)
{
  return strcmp(a->d_name, b->d_name);
}

/* Read directory entries from FD into BUF, reading at most NBYTES.
   Reading starts at offset *BASEP, and *BASEP is updated with the new
   position after reading.  Returns the number of bytes read; zero when at
   end of directory; or -1 for errors.  */
ssize_t getdA0entries (int fd, char *buf,
		    size_t nbytes, off_t *basep)
{
  abort();
}