mblaze/msort.c

340 lines
6.0 KiB
C
Raw Normal View History

2016-07-20 10:19:50 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2016-07-26 16:17:31 +00:00
#include <strings.h>
2016-07-20 10:19:50 +00:00
#include <time.h>
#include <unistd.h>
#include "blaze822.h"
struct mail {
char *file;
long idx;
char *from;
char *subj;
time_t date;
time_t mtime;
off_t size;
2016-07-20 10:19:50 +00:00
};
struct mail *mails;
ssize_t mailalloc = 1024;
int idx;
2016-07-20 10:19:50 +00:00
int rflag;
static int (*sortorders[16])(const void *, const void *);
int order_idx;
int mystrverscmp(const char *, const char *);
2016-07-20 10:19:50 +00:00
char *
fetch_subj(char *file)
{
struct message *msg = blaze822(file);
if (!msg)
return " (error)";
char *v = blaze822_hdr(msg, "subject");
if (!v) {
blaze822_free(msg);
return " (no subject)";
}
char *ov = 0;
char *s;
while (v != ov) {
while (*v == ' ')
v++;
if (strncasecmp(v, "Re:", 3) == 0)
v += 3;
if (strncasecmp(v, "Aw:", 3) == 0)
v += 3;
if (strncasecmp(v, "Sv:", 3) == 0)
v += 3;
if (strncasecmp(v, "Wg:", 3) == 0)
v += 3;
if (strncasecmp(v, "Fwd:", 4) == 0)
v += 4;
// XXX skip [prefix]?
ov = v;
}
s = strdup(v);
blaze822_free(msg);
return s;
}
char *
fetch_from(char *file)
{
char *from = " (unknown)";
struct message *msg = blaze822(file);
if (!msg)
return " (error)";
char *v = blaze822_hdr(msg, "from");
if (v) {
char *disp, *addr;
blaze822_addr(v, &disp, &addr);
if (disp)
2016-07-20 10:19:50 +00:00
from = strdup(disp);
else if (addr)
2016-07-20 10:19:50 +00:00
from = strdup(addr);
}
blaze822_free(msg);
return from;
}
time_t
fetch_date(char *file)
{
time_t t = -1;
struct message *msg = blaze822(file);
if (!msg)
return -1;
char *v = blaze822_hdr(msg, "date");
if (v)
t = blaze822_date(v);
blaze822_free(msg);
return t;
}
time_t
fetch_mtime(char *file)
{
struct stat st;
if (stat(file, &st) < 0)
return -1;
return st.st_mtime;
}
off_t
fetch_size(char *file)
{
struct stat st;
if (stat(file, &st) < 0)
return 0;
return st.st_size;
}
int
subjorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
if (!ia->subj)
ia->subj = fetch_subj(ia->file);
if (!ib->subj)
ib->subj = fetch_subj(ib->file);
2016-07-20 10:19:50 +00:00
return strcasecmp(ia->subj, ib->subj);
2016-07-20 10:19:50 +00:00
}
int
fromorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
if (!ia->from)
ia->from = fetch_from(ia->file);
if (!ib->from)
ib->from = fetch_from(ib->file);
2016-07-20 10:19:50 +00:00
return strcasecmp(ia->from, ib->from);
2016-07-20 10:19:50 +00:00
}
int
sizeorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
if (!ia->size)
ia->size = fetch_size(ia->file);
if (!ib->size)
ib->size = fetch_size(ib->file);
2016-07-20 10:19:50 +00:00
if (ia->size > ib->size)
return 1;
else if (ia->size < ib->size)
return -1;
return 0;
2016-07-20 10:19:50 +00:00
}
int
mtimeorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
if (!ia->mtime)
ia->mtime = fetch_mtime(ia->file);
if (!ib->mtime)
ib->mtime = fetch_mtime(ib->file);
2016-07-20 10:19:50 +00:00
if (ia->mtime > ib->mtime)
return 1;
else if (ia->mtime < ib->mtime)
return -1;
return 0;
2016-07-20 10:19:50 +00:00
}
int
dateorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
if (!ia->date)
ia->date = fetch_date(ia->file);
if (!ib->date)
ib->date = fetch_date(ib->file);
2016-07-20 10:19:50 +00:00
if (ia->date > ib->date)
return 1;
else if (ia->date < ib->date)
return -1;
return 0;
2016-07-20 10:19:50 +00:00
}
int
fileorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
2016-07-26 19:52:30 +00:00
return mystrverscmp(ia->file, ib->file);
2016-07-20 10:19:50 +00:00
}
2016-08-06 16:27:01 +00:00
int
unreadorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
char *fa = strstr(ia->file, ":2,");
char *fb = strstr(ib->file, ":2,");
int unreada = fa ? !strchr(fa, 'S') : 0;
int unreadb = fb ? !strchr(fb, 'S') : 0;
return unreada - unreadb;
}
int
flaggedorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
char *fa = strstr(ia->file, ":2,");
char *fb = strstr(ib->file, ":2,");
int unreada = fa ? !!strchr(fa, 'F') : 0;
int unreadb = fb ? !!strchr(fb, 'F') : 0;
return unreadb - unreada;
}
2016-07-20 10:19:50 +00:00
int
idxorder(const void *a, const void *b)
{
struct mail *ia = (struct mail *)a;
struct mail *ib = (struct mail *)b;
2016-07-26 19:52:30 +00:00
if (ia->idx > ib->idx)
return 1;
else if (ia->idx < ib->idx)
return -1;
else
return 0;
2016-07-20 10:19:50 +00:00
}
void
add(char *file)
{
if (idx >= mailalloc) {
mailalloc *= 2;
if (mailalloc < 0)
exit(-1);
mails = realloc(mails, sizeof (struct mail) * mailalloc);
2016-07-29 11:10:25 +00:00
if (!mails)
exit(-1);
2016-07-20 10:19:50 +00:00
memset(mails+mailalloc/2, 0, sizeof (struct mail) * mailalloc/2);
}
if (!mails)
exit(-1);
mails[idx].file = strdup(file);
mails[idx].idx = idx;
idx++;
}
int
order(const void *a, const void *b)
{
int i, r;
for (i = 0, r = 0; i < order_idx; i++) {
r = (sortorders[i])(a, b);
if (r != 0)
return r;
}
return idxorder(a, b);
}
void
addorder(int (*sortorder)(const void *, const void *))
{
2016-07-25 18:55:58 +00:00
if (order_idx < (int)(sizeof sortorders / sizeof sortorders[0]))
sortorders[order_idx++] = sortorder;
}
2016-07-20 10:19:50 +00:00
int
main(int argc, char *argv[])
{
int c, i;
2016-08-06 16:27:01 +00:00
while ((c = getopt(argc, argv, "fdsFMSUIr")) != -1)
2016-07-20 10:19:50 +00:00
switch(c) {
case 'f': addorder(fromorder); break;
case 'd': addorder(dateorder); break;
case 's': addorder(subjorder); break;
case 'F': addorder(fileorder); break;
case 'M': addorder(mtimeorder); break;
case 'S': addorder(sizeorder); break;
2016-08-06 16:27:01 +00:00
case 'U': addorder(unreadorder); break;
case 'I': addorder(flaggedorder); break;
2016-07-20 10:19:50 +00:00
case 'r': rflag = !rflag; break;
default:
2016-07-26 19:58:10 +00:00
fprintf(stderr,
2016-08-06 16:27:01 +00:00
"Usage: msort [-r] [-fdsFMSUI] [msgs...]\n");
2016-07-20 10:19:50 +00:00
exit(1);
}
mails = calloc(sizeof (struct mail), mailalloc);
if (!mails)
exit(-1);
2016-07-22 22:28:44 +00:00
if (argc == optind && isatty(0))
blaze822_loop1(":", add);
else
blaze822_loop(argc-optind, argv+optind, add);
2016-07-20 10:19:50 +00:00
qsort(mails, idx, sizeof (struct mail), order);
2016-07-20 10:19:50 +00:00
if (rflag)
for (i = idx-1; i >= 0; i--)
printf("%s\n", mails[i].file);
else
for (i = 0; i < idx; i++)
printf("%s\n", mails[i].file);
return 0;
}