-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.c
119 lines (104 loc) · 1.45 KB
/
utils.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "a.h"
Rectangle
boundsrect(Rectangle r)
{
return Rect(0, 0, Dx(r), Dy(r));
}
Image*
ealloccolor(ulong col)
{
Image *b;
b = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, col);
if(b == nil)
sysfatal("allocimage: %r");
return b;
}
void*
emalloc(ulong n)
{
void *p;
p = malloc(n);
if(p == nil)
sysfatal("malloc: %r");
return p;
}
void*
erealloc(void *p, ulong n)
{
void *t;
t = realloc(p, n);
if(t == nil)
sysfatal("realloc: %r");
return t;
}
char*
slurp(char *path)
{
int fd;
long r, n, s;
char *buf;
n = 0;
s = 8192;
buf = malloc(s);
if(buf == nil)
return nil;
fd = open(path, OREAD);
if(fd < 0)
return nil;
for(;;){
r = read(fd, buf + n, s - n);
if(r < 0)
return nil;
if(r == 0)
break;
n += r;
if(n == s){
s *= 1.5;
buf = realloc(buf, s);
if(buf == nil)
return nil;
}
}
buf[n] = 0;
close(fd);
return buf;
}
char*
homedir(void)
{
Biobuf *bp;
char *s;
bp = Bopen("/env/home", OREAD);
s = Brdstr(bp, 0, 0);
Bterm(bp);
if(s == nil)
s = strdup("/tmp");
return s;
}
char*
abspath(char *wd, char *p)
{
char *s;
if(p[0]=='/')
s = strdup(p);
else
s = smprint("%s/%s", wd, p);
cleanname(s);
return s;
}
int
wresize(int w, int h)
{
int fd, n;
char buf[255];
fd = open("/dev/wctl", OWRITE|OCEXEC);
if(fd < 0)
return -1;
n = snprint(buf, sizeof buf, "resize -dx %d -dy %d", w, h);
if(write(fd, buf, n) != n){
close(fd);
return -1;
}
close(fd);
return 0;
}