comm(1) (was: Re: uniq truncates lines > 2048 bytes)
Mario Hoerich
lists at MHoerich.de
Fri Jan 28 18:08:50 PST 2005
# Kris Kennaway:
> On Wed, Jan 26, 2005 at 09:10:47AM +1100, Tim Robbins wrote:
> >
> > I was planning on going through all text-processing utilities in the base
> > system some time and either fixing line length problems or documenting them,
> > similar to what I did with multibyte character support. I may make a start
> > at that today.
>
> If someone could fix comm(1) that would be a big help for me, because
> I have a local hack I have to carry around in all of my local package
> source trees.
Does this patch help?
I haven't made any contributions before, so I just hope I'm
not making a fool of myself with this...
HTH,
Mario
Index: comm.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/comm/comm.c,v
retrieving revision 1.21
diff -u -r1.21 comm.c
--- comm.c 2 Jul 2004 22:48:29 -0000 1.21
+++ comm.c 29 Jan 2005 01:30:13 -0000
@@ -66,6 +66,8 @@
FILE *file(const char *);
void show(FILE *, const char *, const wchar_t *, wchar_t *);
int wcsicoll(const wchar_t *, const wchar_t *);
+wchar_t *fgetwla(FILE *);
+
static void usage(void);
int
@@ -75,7 +77,7 @@
int ch, flag1, flag2, flag3, iflag;
FILE *fp1, *fp2;
const wchar_t *col1, *col2, *col3;
- wchar_t line1[MAXLINELEN], line2[MAXLINELEN];
+ wchar_t *line1, *line2;
const wchar_t **p;
flag1 = flag2 = flag3 = 1;
@@ -123,12 +125,14 @@
for (read1 = read2 = 1;;) {
/* read next line, check for EOF */
if (read1) {
- file1done = !fgetws(line1, MAXLINELEN, fp1);
+ line1 = fgetwla(fp1);
+ file1done = (line1 == NULL);
if (file1done && ferror(fp1))
err(1, "%s", argv[0]);
}
if (read2) {
- file2done = !fgetws(line2, MAXLINELEN, fp2);
+ line2 = fgetwla(fp2);
+ file2done = (line2 == NULL);
if (file2done && ferror(fp2))
err(1, "%s", argv[1]);
}
@@ -170,6 +174,10 @@
if (col2)
(void)printf("%ls%ls", col2, line2);
}
+ if (read1)
+ free(line1);
+ if (read2)
+ free(line2);
}
exit(0);
}
@@ -177,10 +185,15 @@
void
show(FILE *fp, const char *fn, const wchar_t *offset, wchar_t *buf)
{
+ wchar_t *line;
+
+ line = buf;
do {
- (void)printf("%ls%ls", offset, buf);
- } while (fgetws(buf, MAXLINELEN, fp));
+ (void)printf("%ls%ls", offset, line);
+ free(line);
+ line = fgetwla(fp);
+ } while (line != NULL);
if (ferror(fp))
err(1, "%s", fn);
}
@@ -208,7 +221,13 @@
int
wcsicoll(const wchar_t *s1, const wchar_t *s2)
{
- wchar_t *p, line1[MAXLINELEN], line2[MAXLINELEN];
+ wchar_t *p, *line1, *line2;
+ int comp;
+
+ if( (line1 = malloc(wcslen(s1) + 1)) == NULL)
+ err(1, "%s", __func__);
+ if( (line2 = malloc(wcslen(s2) + 1)) == NULL)
+ err(1, "%s", __func__);
for (p = line1; *s1; s1++)
*p++ = towlower(*s1);
@@ -216,5 +235,28 @@
for (p = line2; *s2; s2++)
*p++ = towlower(*s2);
*p = '\0';
- return (wcscoll(line1, line2));
+
+ comp = wcscoll(line1, line2);
+ free(line1);
+ free(line2);
+
+ return comp;
+}
+
+wchar_t *
+fgetwla(FILE *fp)
+{
+ wchar_t *line, *wtmp;
+ size_t len;
+
+ if ( (wtmp=fgetwln(fp, &len)) == NULL)
+ return NULL;
+
+ if ( (line=malloc((len + 1) * sizeof(wchar_t))) == NULL)
+ err(1, "%s", __func__);
+
+ wcsncpy(line, wtmp, len);
+ line[len] = L'\0';
+
+ return line;
}
--
More information about the freebsd-current
mailing list