~moody/mpl

mpl/util.c -rw-r--r-- 1.2 KiB
7309e0f2 — Jacob Moody Archive and point to zuke 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>

#include "dat.h"
#include "fncs.h"

/* Malloc and memset to 0, quit gracefully on error */
void*
emalloc(vlong size)
{
	void *v = malloc(size);
	if(v == nil)
		quit("Out of memory");
	setmalloctag(v, getcallerpc(&size));
	v = memset(v, 0, size);
	return v;
}

/*
* Unmarshal LSB ordered buffer into unsigned int
* https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
*/
u64int
lebtoi(uchar *buf, int nbyte)
{
	int i;
	u64int out = 0;
	for(i=0;i<nbyte;i++)
		out = out | buf[i]<<(i*8);
	return out;
}

/* Unmarshal MSB ordered buffer into unsigned int */
u64int
bebtoi(uchar *buf, int nbyte)
{
	int i;
	int n = nbyte;
	u64int out = 0;
	for(i=0;i<nbyte;i++)
		out = out | buf[i]<<(--n*8);
	return out;
}

void
kill(int pid)
{
	int nfd;
	char *note = smprint( "/proc/%d/note", pid);
	nfd = open(note, OWRITE);
	if(nfd<0)
		sysfatal("proc doesn't exist");
	if(write(nfd, "kill", 4)!=4)
		sysfatal("could not write to note");
	close(nfd);
	free(note);
}

int
runecstrcmp(Rune *s1, Rune *s2)
{
	Rune c1, c2;

	for(;;) {
		c1 = tolowerrune(*s1++);
		c2 = tolowerrune(*s2++);
		if(c1 != c2) {
			if(c1 > c2)
				return 1;
			return -1;
		}
		if(c1 == 0)
			return 0;
	}
}