wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

clientutil.c (1186B)


      1 /* Copyright ©2009-2010 Kris Maglione <maglione.k at Gmail>
      2  * See LICENSE file for license details.
      3  */
      4 #define IXP_NO_P9_
      5 #define IXP_P9_STRUCTS
      6 #define CLIENTEXTERN
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <ixp.h>
     10 #include <stuff/clientutil.h>
     11 #include <stuff/util.h>
     12 
     13 char*
     14 readctl(char *ctlname, char *key) {
     15 	static char	ctlfile[128];
     16 	static char 	ctl[1024];
     17 	static char*	ectl;
     18 	IxpCFid *fid;
     19 	char *s, *p;
     20 	int nkey, n;
     21 
     22 	if(strcmp(ctlname, ctlfile)) {
     23 		strncpy(ctlfile, ctlname, sizeof ctlfile);
     24 		fid = ixp_open(client, ctlfile, OREAD);
     25 		n = ixp_read(fid, ctl, sizeof ctl - 1);
     26 		ectl = ctl + n;
     27 		ixp_close(fid);
     28 	}
     29 
     30 	nkey = strlen(key);
     31 	p = ctl - 1;
     32 	do {
     33 		p++;
     34 		if(!strncmp(p, key, nkey)) {
     35 			p += nkey;
     36 			s = strchr(p, '\n');
     37 			n = (s ? s : ectl) - p;
     38 			s = freelater(emalloc(n + 1));
     39 			s[n] = '\0';
     40 			return memcpy(s, p, n);
     41 		}
     42 	} while((p = strchr(p, '\n')));
     43 	return "";
     44 }
     45 
     46 void
     47 client_init(char* address) {
     48 	IXP_ASSERT_VERSION;
     49 
     50 	if(address == nil)
     51 		address = getenv("WMII_ADDRESS");
     52 	if(address && *address)
     53 		client = ixp_mount(address);
     54 	else
     55 		client = ixp_nsmount("wmii");
     56 	if(client == nil)
     57 		fatal("can't mount wmii filesystem: %r\n");
     58 }