operator* for dirit

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