aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/smugfs/util.c
diff options
context:
space:
mode:
authorRuss Cox <rsc@swtch.com>2008-08-03 07:42:27 -0700
committerRuss Cox <rsc@swtch.com>2008-08-03 07:42:27 -0700
commit18824b586835525594cde126fbc90b8281d5af8b (patch)
treee8b69c05eda4543b6d2f3d30777abe6109b48b7f /src/cmd/smugfs/util.c
parent3d36f4437348227c5bad62587dc12b5fd4a3e95e (diff)
downloadplan9port-18824b586835525594cde126fbc90b8281d5af8b.tar.gz
plan9port-18824b586835525594cde126fbc90b8281d5af8b.tar.bz2
plan9port-18824b586835525594cde126fbc90b8281d5af8b.zip
smugfs(4): new program
Diffstat (limited to 'src/cmd/smugfs/util.c')
-rw-r--r--src/cmd/smugfs/util.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/cmd/smugfs/util.c b/src/cmd/smugfs/util.c
new file mode 100644
index 00000000..b4a649d8
--- /dev/null
+++ b/src/cmd/smugfs/util.c
@@ -0,0 +1,81 @@
+#include "a.h"
+
+void*
+emalloc(int n)
+{
+ void *v;
+
+ v = mallocz(n, 1);
+ if(v == nil)
+ sysfatal("out of memory");
+ return v;
+}
+
+void*
+erealloc(void *v, int n)
+{
+ v = realloc(v, n);
+ if(v == nil)
+ sysfatal("out of memory");
+ return v;
+}
+
+char*
+estrdup(char *s)
+{
+ s = strdup(s);
+ if(s == nil)
+ sysfatal("out of memory");
+ return s;
+}
+
+int
+timefmt(Fmt *f)
+{
+ Tm tm;
+ vlong ms;
+
+ ms = nsec()/1000000;
+
+ tm = *localtime(ms/1000);
+ fmtprint(f, "%02d:%02d:%02d.%03d",
+ tm.hour, tm.min, tm.sec,
+ (int)(ms%1000));
+ return 0;
+}
+
+int
+writen(int fd, void *buf, int n)
+{
+ long m, tot;
+
+ for(tot=0; tot<n; tot+=m){
+ m = n - tot;
+ if(m > 8192)
+ m = 8192;
+ if(write(fd, (uchar*)buf+tot, m) != m)
+ break;
+ }
+ return tot;
+}
+
+int
+urlencodefmt(Fmt *fmt)
+{
+ int x;
+ char *s;
+
+ s = va_arg(fmt->args, char*);
+ for(; *s; s++){
+ x = (uchar)*s;
+ if(x == ' ')
+ fmtrune(fmt, '+');
+ else if(('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9')
+ || strchr("$-_.+!*'()", x)){
+ fmtrune(fmt, x);
+ }else
+ fmtprint(fmt, "%%%02ux", x);
+ }
+ return 0;
+}
+