operator* for dirit

上级 acebdcb7
......@@ -46,16 +46,17 @@ du(int fd)
wq_for<dirit>(di,
[](dirit &i)->bool { return !i.end(); },
[&](dirit &i)->void
[&](const char *name)->void
{
char buf[BSIZ];
i.name(buf, BSIZ);
if (!strcmp(buf, ".") || !strcmp(buf, ".."))
if (!strcmp(name, ".") || !strcmp(name, "..")) {
free((void*)name);
return;
}
int nfd = openat(fd, buf, 0);
int nfd = openat(fd, name, 0);
if (nfd >= 0)
size += du(nfd); // should go into work queue
free((void*)name);
});
}
......@@ -66,7 +67,6 @@ du(int fd)
int
main(int ac, char **av)
{
extern void initwq(void);
//initwq();
printf("%d\n", du(open(".", 0)));
......
......@@ -9,8 +9,14 @@ public:
return *this;
}
const char * operator*() {
char *buf = (char*)malloc(256);
return name(buf, 256);
}
bool end() const { return end_; }
private:
char *name(char *buf, size_t n) const {
n = MIN(DIRSIZ+1, n);
memmove(buf, de_.name, n-1);
......@@ -18,7 +24,6 @@ public:
return buf;
}
private:
void refill(void) {
int r;
......
......@@ -8,11 +8,17 @@ struct work {
char data[];
};
void initwq(void);
struct work * allocwork(void);
void freework(struct work *w);
int wq_push(struct work *w);
template <typename IT, typename BODY>
static inline void
wq_for(IT &init, bool (*cond)(IT &it), BODY body) {
wq_for(IT &init, bool (*cond)(IT &it), BODY body)
{
// XXX(sbw) should be able to coarsen loop
for (IT &it = init; cond(it); ++it) {
body(it);
body(*it);
}
}
#include <sys/types.h>
#include <stdlib.h>
#include <dirent.h>
class dirit {
......@@ -9,20 +10,29 @@ public:
refill();
}
~dirit() {
closedir(d_);
}
dirit& operator ++() {
refill();
return *this;
}
const char * operator*() {
char *buf = (char*)malloc(256);
return name(buf, 256);
}
bool end() const { return end_; }
private:
char *name(char *buf, size_t n) const {
strncpy(buf, ent_->d_name, n-1);
buf[n-1] = 0;
return buf;
}
private:
void refill(void) {
struct dirent *result;
int r = readdir_r(d_, ent_, &result);
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论