diff options
Diffstat (limited to 'jacc/dat.c')
-rw-r--r-- | jacc/dat.c | 320 |
1 files changed, 320 insertions, 0 deletions
diff --git a/jacc/dat.c b/jacc/dat.c new file mode 100644 index 0000000..053df30 --- /dev/null +++ b/jacc/dat.c @@ -0,0 +1,320 @@ +/* + * Copy me if you can. + * by 20h + */ + +#include <u.h> +#include <libc.h> +#include "dat.h" + +char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0}; +char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", + "Sep", "Oct", "Nov", "Dec", 0}; + +int +fileexists(char *path) +{ + uchar buf[STATMAX]; + int fd; + + fd = stat(path, buf, STATMAX); + if(fd < 0) + return 0; + + close(fd); + return 1; +} + +void +playmp3(char *file) +{ + char cmd[256]; + + if(!fileexists("/dev/audio")) + return; + + memset(cmd, 0, 256); + strncpy(cmd, file, 255); + if(getenv("home") != nil){ + snprint(cmd, 255, "%s/audio/%s", getenv("home"), file); + if(!fileexists(cmd)) + strncpy(cmd, file, 255); + } + if(!fileexists(cmd)) + return; + + if(fork() == 0) + execl("/bin/madplay", "madplay", "-Q", cmd, 0); + wait(); +} + +void * +reallocj(void *p, int s, short d) +{ + p = realloc(p, s); + if(p == nil) + sysfatal("realloc: %r"); + + if(d != 0) + memset(p, 0, s); + + return (void *)p; +} + +void +killproc(int p) +{ + char *e; + int fd; + + e = mallocz(65, 2); + snprint(e, 64, "/proc/%d/ctl", p); + + fd = open(e, OWRITE); + if(fd != -1){ + write(fd, "kill", 4); + close(fd); + } + free(e); +} + +char * +setwindowlbl(char *w) +{ + int s; + + s = open("/dev/label", OWRITE); + if(s < 0) + return nil; + + write(s, w, strlen(w)); + + close(s); + return w; +} + +char * +getwindowlbl(void) +{ + int s; + char *ret; + short i; + + s = open("/dev/label", OREAD); + if(s < 0) + return nil; + + i = 0; + ret = malloc(0); + while(realloc(ret, ++i) != nil && read(s, &ret[i - 1], 1) > 0 && + i < 513); + + ret[i - 1] = '\0'; + + close(s); + return ret; +} + +char * +mktmstmp(char bord, char bord_e) +{ + Tm *tim; + char *ret; + + ret = reallocj(nil, 32, 2); + tim = localtime(time(0)); + snprint(ret, 31, "%c%.2d:%.2d%c ", bord, tim->hour, tim->min, bord_e); + + return ret; +} + +char * +setchan(char *old, char *new) +{ + old = reallocj(old, strlen(new) + 1, 2); + strcpy(old, new); + + return old; +} + +char * +printjid(char *user, char *serv, char *reso) +{ + char *ret; + int i; + + if(user == nil || serv == nil) + return nil; + + i = strlen(user) + strlen(serv) + 3 + ((reso != nil) ? strlen(reso) : 0); + + ret = reallocj(nil, i, 2); + snprint(ret, i, "%s@%s%s%s", user, serv, (reso != nil) ? "/" : "\0", + ((reso != nil) ? reso : "")); + + return ret; +} + +char * +getarg(char *b, char n, char t) +{ + char *ret, delim; + + ret = nil; + + while(n-- > 0){ + if(*b == '\'' || *b == '\"'){ + delim = *b; + b++; + ret = strchr(b, delim); + } else + ret = strchr(b, ' '); + if(ret == nil) + return nil; + + b = ret + 1; + } + + if(t == 0) + b = strchr(ret + 1, ' '); + else + b = nil; + + if(b == nil) + ret = strdup(ret + 1); + else { + n = *b; + *b = '\0'; + ret = strdup(ret + 1); + *b = n; + } + + ret = filterhin(ret, 2); + + return ret; +} + +void +readwrite(int sock, int fd) +{ + char *b; + int l; + + l = 2048; + b = reallocj(nil, 2048, 2); + while(l == 2048){ + l = read(fd, b, 2048); + if(l > 0) + write(sock, b, l); + } + + free(b); + return; +} + +char * +getmonth(int m) +{ + if(m > 11 || m < 0) + return nil; + + return month[m]; +} + +char * +getday(int d) +{ + if(d > 6 || d < 0) + return nil; + + return wdays[d]; +} + +char * +filterhin(char *in, int a) +{ + char *ret, *z; + int l; + + l = 0; + ret = reallocj(nil, 1, 0); + z = in; + + while(*in != '\0'){ + switch(*in){ + case '<': + l += 4; + ret = reallocj(ret, l + 1, 0); + strncpy(&ret[l - 4], "<", 4); + break; + case '&': + l += 5; + ret = reallocj(ret, l + 1, 0); + strncpy(&ret[l - 5], "&", 5); + break; + case '>': + l += 4; + ret = reallocj(ret, l + 1, 0); + strncpy(&ret[l - 4], ">", 4); + break; + case '"': + l += 6; + ret = reallocj(ret, l + 1, 0); + strncpy(&ret[l - 6], """, 6); + break; + default: + ret = reallocj(ret, ++l + 1, 0); + ret[l - 1] = *in; + break; + } + in++; + } + ret[l] = '\0'; + + if(a != 0) + free(z); + + return ret; +} + +char * +filterzur(char *out) +{ + char *a, *b; + int changed; + + changed = 1; + + while(changed){ + changed = 0; + if((a = strstr(out, "<")) != nil) { + *a = '<'; + strcpy(a + 1, a + 4); + changed = 1; + } + if((a = strstr(out, ">")) != nil) { + *a = '>'; + strcpy(a + 1, a + 4); + changed = 1; + } + if((a = strstr(out, """)) != nil) { + *a = '"'; + strcpy(a + 1, a + 6); + changed = 1; + } + if((a = strstr(out, "&")) != nil) { + *a = '&'; + strcpy(a + 1, a + 5); + changed = 1; + } + if((a = strstr(out, "text/x-aolrtf;")) != nil) { + b = strchr(a, ':'); + if(b != nil) { + strcpy(a, b + 2); + changed = 1; + } + } + } + + return out; +} |