summaryrefslogtreecommitdiff
path: root/sys/src/cmd/sam/util.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/sam/util.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/sam/util.c')
-rwxr-xr-xsys/src/cmd/sam/util.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/sys/src/cmd/sam/util.c b/sys/src/cmd/sam/util.c
new file mode 100755
index 000000000..b25996894
--- /dev/null
+++ b/sys/src/cmd/sam/util.c
@@ -0,0 +1,54 @@
+#include "sam.h"
+
+void
+cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
+{
+ uchar *q;
+ Rune *s;
+ int j, w;
+
+ /*
+ * Always guaranteed that n bytes may be interpreted
+ * without worrying about partial runes. This may mean
+ * reading up to UTFmax-1 more bytes than n; the caller
+ * knows this. If n is a firm limit, the caller should
+ * set p[n] = 0.
+ */
+ q = (uchar*)p;
+ s = r;
+ for(j=0; j<n; j+=w){
+ if(*q < Runeself){
+ w = 1;
+ *s = *q++;
+ }else{
+ w = chartorune(s, (char*)q);
+ q += w;
+ }
+ if(*s)
+ s++;
+ else if(nulls)
+ *nulls = TRUE;
+ }
+ *nb = (char*)q-p;
+ *nr = s-r;
+}
+
+void*
+fbufalloc(void)
+{
+ return emalloc(BUFSIZE);
+}
+
+void
+fbuffree(void *f)
+{
+ free(f);
+}
+
+uint
+min(uint a, uint b)
+{
+ if(a < b)
+ return a;
+ return b;
+}