summaryrefslogtreecommitdiff
path: root/sys/src/cmd/tcs
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@gmx.de>2012-12-20 17:00:08 +0100
committercinap_lenrek <cinap_lenrek@gmx.de>2012-12-20 17:00:08 +0100
commitdb71faf2d7df886c32cb2f7ac0c99982b9a5b7ea (patch)
treed2cc8593b539536f00969ea60b6180babcaa893c /sys/src/cmd/tcs
parent03024cd814eaf06cb239d88a09923e4c8f82e6d8 (diff)
tcs: dont get confused on odd read count for utf-16
Diffstat (limited to 'sys/src/cmd/tcs')
-rw-r--r--sys/src/cmd/tcs/tcs.c64
1 files changed, 29 insertions, 35 deletions
diff --git a/sys/src/cmd/tcs/tcs.c b/sys/src/cmd/tcs/tcs.c
index aa8c7df15..4922890d0 100644
--- a/sys/src/cmd/tcs/tcs.c
+++ b/sys/src/cmd/tcs/tcs.c
@@ -206,6 +206,25 @@ swab2(char *b, int n)
}
}
+static int
+cread(int fd, void *buf, int len, int mod)
+{
+ char *off = buf;
+
+ len -= (len % mod);
+Again:
+ len = read(fd, off, len);
+ if(len <= 0)
+ return len;
+ off += len;
+ len = off - (char*)buf;
+ if((len % mod) != 0){
+ len = mod - (len % mod);
+ goto Again;
+ }
+ return len;
+}
+
void
unicode_in(int fd, long *notused, struct convert *out)
{
@@ -214,7 +233,7 @@ unicode_in(int fd, long *notused, struct convert *out)
int swabme;
USED(notused);
- if(read(fd, (char *)buf, 2) != 2)
+ if(cread(fd, (char *)buf, 2, 2) != 2)
return;
ninput += 2;
switch(buf[0])
@@ -228,19 +247,10 @@ unicode_in(int fd, long *notused, struct convert *out)
swabme = 1;
break;
}
- while((n = read(fd, (char *)buf, 2*N)) > 0){
+ while((n = cread(fd, (char *)buf, 2*N, 2)) > 0){
ninput += n;
if(swabme)
swab2((char *)buf, n);
- if(n&1){
- if(squawk)
- EPR "%s: odd byte count in %s\n", argv0, file);
- nerrors++;
- if(clean)
- n--;
- else
- buf[n++/2] = Runeerror;
- }
OUT(out, buf, n/2);
}
OUT(out, buf, 0);
@@ -254,24 +264,16 @@ unicode_in_be(int fd, long *notused, struct convert *out)
uchar *p;
USED(notused);
- while((n = read(fd, (char *)buf, 2*N)) > 0){
+ while((n = cread(fd, (char *)buf, 2*N, 2)) > 0){
ninput += n;
+ n /= 2;
p = (uchar*)buf;
- for(i=0; i<n/2; i++){
+ for(i=0; i<n; i++){
r = *p++<<8;
r |= *p++;
buf[i] = r;
}
- if(n&1){
- if(squawk)
- EPR "%s: odd byte count in %s\n", argv0, file);
- nerrors++;
- if(clean)
- n--;
- else
- buf[n++/2] = Runeerror;
- }
- OUT(out, buf, n/2);
+ OUT(out, buf, n);
}
OUT(out, buf, 0);
}
@@ -284,24 +286,16 @@ unicode_in_le(int fd, long *notused, struct convert *out)
uchar *p;
USED(notused);
- while((n = read(fd, (char *)buf, 2*N)) > 0){
+ while((n = cread(fd, (char *)buf, 2*N, 2)) > 0){
ninput += n;
+ n /= 2;
p = (uchar*)buf;
- for(i=0; i<n/2; i++){
+ for(i=0; i<n; i++){
r = *p++;
r |= *p++<<8;
buf[i] = r;
}
- if(n&1){
- if(squawk)
- EPR "%s: odd byte count in %s\n", argv0, file);
- nerrors++;
- if(clean)
- n--;
- else
- buf[n++/2] = Runeerror;
- }
- OUT(out, buf, n/2);
+ OUT(out, buf, n);
}
OUT(out, buf, 0);
}