summaryrefslogtreecommitdiffstats
path: root/usr.bin/comm/comm.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/comm/comm.c')
-rw-r--r--usr.bin/comm/comm.c45
1 files changed, 24 insertions, 21 deletions
diff --git a/usr.bin/comm/comm.c b/usr.bin/comm/comm.c
index ea92673..afda0e7 100644
--- a/usr.bin/comm/comm.c
+++ b/usr.bin/comm/comm.c
@@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <limits.h>
#include <locale.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -59,7 +60,8 @@ __FBSDID("$FreeBSD$");
#include <wchar.h>
#include <wctype.h>
-#define MAXLINELEN (LINE_MAX + 1)
+#define INITLINELEN (LINE_MAX + 1)
+#define MAXLINELEN ((SIZE_MAX / sizeof(wchar_t)) / 2)
const wchar_t *tabs[] = { L"", L"\t", L"\t\t" };
@@ -83,8 +85,8 @@ main(int argc, char *argv[])
flag1 = flag2 = flag3 = 1;
iflag = 0;
- line1len = MAXLINELEN;
- line2len = MAXLINELEN;
+ line1len = INITLINELEN;
+ line2len = INITLINELEN;
line1 = malloc(line1len * sizeof(*line1));
line2 = malloc(line2len * sizeof(*line2));
if (line1 == NULL || line2 == NULL)
@@ -163,7 +165,7 @@ main(int argc, char *argv[])
if (!comp) {
read1 = read2 = 1;
if (col3 != NULL)
- (void)printf("%ls%ls", col3, line1);
+ (void)printf("%ls%ls\n", col3, line1);
continue;
}
@@ -172,12 +174,12 @@ main(int argc, char *argv[])
read1 = 1;
read2 = 0;
if (col1 != NULL)
- (void)printf("%ls%ls", col1, line1);
+ (void)printf("%ls%ls\n", col1, line1);
} else {
read1 = 0;
read2 = 1;
if (col2 != NULL)
- (void)printf("%ls%ls", col2, line2);
+ (void)printf("%ls%ls\n", col2, line2);
}
}
exit(0);
@@ -190,19 +192,20 @@ getline(wchar_t *buf, size_t *buflen, FILE *fp)
wint_t ch;
bufpos = 0;
- do {
- if ((ch = getwc(fp)) != WEOF) {
- if (bufpos + 2 >= *buflen) {
- *buflen = *buflen * 2;
- buf = reallocf(buf, *buflen * sizeof(*buf));
- if (buf == NULL)
- return (NULL);
- }
- buf[bufpos++] = ch;
+ while ((ch = getwc(fp)) != WEOF && ch != '\n') {
+ if (bufpos + 1 >= *buflen) {
+ *buflen = *buflen * 2;
+ if (*buflen > MAXLINELEN)
+ errx(1,
+ "Maximum line buffer length (%zu) exceeded",
+ MAXLINELEN);
+ buf = reallocf(buf, *buflen * sizeof(*buf));
+ if (buf == NULL)
+ err(1, "reallocf");
}
- } while (ch != WEOF && ch != '\n');
- if (bufpos + 1 != *buflen)
- buf[bufpos] = '\0';
+ buf[bufpos++] = ch;
+ }
+ buf[bufpos] = '\0';
return (bufpos != 0 || ch == '\n' ? buf : NULL);
}
@@ -212,7 +215,7 @@ show(FILE *fp, const char *fn, const wchar_t *offset, wchar_t *buf, size_t *bufl
{
do {
- (void)printf("%ls%ls", offset, buf);
+ (void)printf("%ls%ls\n", offset, buf);
} while ((buf = getline(buf, buflen, fp)) != NULL);
if (ferror(fp))
err(1, "%s", fn);
@@ -254,13 +257,13 @@ wcsicoll(const wchar_t *s1, const wchar_t *s2)
new_l2_buflen = wcsicoll_l2_buflen;
while (new_l1_buflen < l1) {
if (new_l1_buflen == 0)
- new_l1_buflen = MAXLINELEN;
+ new_l1_buflen = INITLINELEN;
else
new_l1_buflen *= 2;
}
while (new_l2_buflen < l2) {
if (new_l2_buflen == 0)
- new_l2_buflen = MAXLINELEN;
+ new_l2_buflen = INITLINELEN;
else
new_l2_buflen *= 2;
}
OpenPOWER on IntegriCloud