]> git.lizzy.rs Git - plan9front.git/blob - acme/news/src/util.c
fill /acme
[plan9front.git] / acme / news / src / util.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <thread.h>
5 #include "win.h"
6
7 void*
8 emalloc(uint n)
9 {
10         void *p;
11
12         p = malloc(n);
13         if(p == nil)
14                 error("can't malloc: %r");
15         memset(p, 0, n);
16         return p;
17 }
18
19 char*
20 estrdup(char *s)
21 {
22         char *t;
23
24         t = emalloc(strlen(s)+1);
25         strcpy(t, s);
26         return t;
27 }
28
29 char*
30 estrstrdup(char *s, char *t)
31 {
32         char *u;
33
34         u = emalloc(strlen(s)+strlen(t)+1);
35         strcpy(u, s);
36         strcat(u, t);
37         return u;
38 }
39
40 char*
41 estrstrstrdup(char *r, char *s, char *t)
42 {
43         char *u;
44
45         u = emalloc(strlen(r)+strlen(s)+strlen(t)+1);
46         strcpy(u, r);
47         strcat(u, s);
48         strcat(u, t);
49         return u;
50 }
51
52 char*
53 eappend(char *s, char *sep, char *t)
54 {
55         char *u;
56
57         if(t == nil)
58                 u = estrstrdup(s, sep);
59         else{
60                 u = emalloc(strlen(s)+strlen(sep)+strlen(t)+1);
61                 strcpy(u, s);
62                 strcat(u, sep);
63                 strcat(u, t);
64         }
65         free(s);
66         return u;
67 }
68
69 char*
70 egrow(char *s, char *sep, char *t)
71 {
72         s = eappend(s, sep, t);
73         free(t);
74         return s;
75 }
76
77 void
78 error(char *fmt, ...)
79 {
80         va_list arg;
81         char buf[256];
82         Fmt f;
83
84         fmtfdinit(&f, 2, buf, sizeof buf);
85         fmtprint(&f, "%s: ", argv0);
86         va_start(arg, fmt);
87         fmtprint(&f, fmt, arg);
88         va_end(arg);
89         fmtprint(&f, "\n");
90         fmtfdflush(&f);
91         threadexitsall(fmt);
92 }
93
94 void
95 ctlprint(int fd, char *fmt, ...)
96 {
97         int n;
98         va_list arg;
99         char buf[256];
100
101         va_start(arg, fmt);
102         n = vfprint(fd, fmt, arg);
103         va_end(arg);
104         if(n < 0)
105                 error("control file write(%s) error: %r", buf);
106 }