
v20-qd-1213615422ousmjic at riemann
Jun 9, 2008, 4:25 AM
Post #1 of 1
(247 views)
Permalink
|
This is small qmail-qstat written in C. Nikola /* qmail-qstat.c diet -Os gcc -nostdinc -O2 -s -o qmail-qstat qmail-qstat.c -Wall -W gcc -O2 -s -o qmail-qstat qmail-qstat.c -Wall -W permition: -rwx--s--x 1 root qmail qmail-qstat http://riemann.fmi.uni-sofia.bg/programs/ */ #define QQ "/var/qmail/queue" #define SCAN_DIR_buffer_len 4096 #include <unistd.h> #define __USE_GNU #include <fcntl.h> #include <dirent.h> unsigned long mesg; typedef struct buffer { char *x; /* actual buffer space */ unsigned int p; /* current position */ unsigned int n; /* string postition */ unsigned int a; /* allocated buffer size */ int fd; } buffer; #ifdef __dietlibc__ #define get_de int res = getdents(d->fd, (struct dirent *)d->x, d->a) #else #define get_de off_t x; int res = getdirentries(d->fd, d->x, d->a, &x) #endif static struct dirent* read_dir(buffer *d) { struct dirent *de = (struct dirent*)(d->x + d->p); if (d->p >= d->n || (d->p += de->d_reclen) >= d->n) { get_de; if (res<=0) return 0; d->n=res; d->p=0; } return (struct dirent*)(d->x+d->p); } static void scan_dir() { struct dirent *d; char buf_space[SCAN_DIR_buffer_len]; int fd = open(".", O_RDONLY | O_DIRECTORY); buffer b = { buf_space, 0, 0, sizeof(buf_space)-1, fd }; if (fd < 0) return; while ((d = read_dir(&b))) { if (d->d_name[0] == '.') continue; if (chdir(d->d_name)==0) { scan_dir(); fchdir(fd); continue; } else ++mesg; } close(fd); } static unsigned int fmt_ulong(char *s, unsigned long u) { unsigned int len; unsigned long q; len = 1; q = u; while (q > 9) { ++len; q /= 10; } if (s) { s += len; do { *--s = '0' + (u % 10); u /= 10; } while(u); /* handles u == 0 */ } return len; } static unsigned int fmt_str(char *s, char *t) { /* s,t must be not 0 */ unsigned int len=0; char ch; while ((ch = t[len])) s[len++] = ch; return len; } static void ch_dir(char *dir, char *tmp) { if (chdir(dir)) { char *p=tmp; p += fmt_str(p, "qmail-qstat: chdir "); p += fmt_str(p, dir); p += fmt_str(p, " error\n"); write(2,tmp, p-tmp); _exit(100); } } static int fmt_dir(char *pos, char *s) { char *p = pos; mesg = 0; scan_dir(); if (mesg) { p += fmt_str(p, s); p += fmt_ulong(p, mesg); *p++ = '\n'; } return p - pos; } int main() { char tmp[256]; char *p=tmp; ch_dir(QQ "/mess", tmp); p += fmt_dir(p, "qmail: messages in queue: "); ch_dir(QQ "/todo", tmp); p += fmt_dir(p, "qmail: messages in queue but not yet preprocessed: "); if (p>tmp) write(1,tmp, p-tmp); return 0; }
|