jacc

Jabber/XMPP client for Plan 9
git clone git://r-36.net/jacc
Log | Files | Refs | LICENSE

dat.c (1228B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  */
      5 
      6 #include <u.h>
      7 #include <libc.h>
      8 #include "dat.h"
      9 
     10 void *
     11 reallocj(void *p, int s, short d)
     12 {
     13 	p = realloc(p, s);
     14 	if(p == nil)
     15 		sysfatal("realloc: %r");
     16 
     17 	if(d != 0)
     18 		memset(p, 0, s);
     19 
     20 	return (void *)p;
     21 }
     22 
     23 char *
     24 setwindowlbl(char *w)
     25 {
     26 	int s;
     27 
     28 	s = open("/dev/label", OWRITE);
     29 	if(s < 0)
     30 		return nil;
     31 
     32 	write(s, w, strlen(w));
     33 
     34 	close(s);
     35 	return w;
     36 }
     37 
     38 char *
     39 getwindowlbl(void)
     40 {
     41 	int s;
     42 	char *ret;
     43 	short i;
     44 
     45 	s = open("/dev/label", OREAD);
     46 	if(s < 0)
     47 		return nil;
     48 
     49 	i = 0;
     50 	ret = malloc(0);
     51 	while(realloc(ret, ++i) != nil && read(s, &ret[i - 1], 1) > 0 &&
     52 			i < 513);
     53 
     54 	ret[i - 1] = '\0';
     55 
     56 	close(s);
     57 	return ret;
     58 }
     59 
     60 char *
     61 mktmstmp(char bord, char bord_e)
     62 {
     63 	Tm *tim;
     64 	char *ret;
     65 
     66 	ret = reallocj(nil, 32, 2);
     67 	tim = localtime(time(0));
     68 	snprint(ret, 31, "%c%.2d:%.2d%c ", bord, tim->hour, tim->min, bord_e);
     69 
     70 	return ret;
     71 }
     72 
     73 char *
     74 printjid(char *user, char *serv, char *reso)
     75 {
     76 	char *ret;
     77 	int i;
     78 
     79 	if(user == nil || serv == nil)
     80 		return nil;
     81 
     82 	i = strlen(user) + strlen(serv) + 3 + ((reso != nil) ? strlen(reso) : 0);
     83 
     84 	ret = reallocj(nil, i, 2);
     85 	snprint(ret, i, "%s@%s%s%s", user, serv, (reso != nil) ? "/" : "\0",
     86 				((reso != nil) ? reso : ""));
     87 
     88 	return ret;
     89 }