2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
48 static wchar_t **wmonths;
49 static unsigned char **cmonths;
51 /* initialise months */
54 initialise_months(void)
56 const nl_item item[12] = { ABMON_1, ABMON_2, ABMON_3, ABMON_4,
57 ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10,
62 if (MB_CUR_MAX == 1) {
63 if (cmonths == NULL) {
66 cmonths = sort_malloc(sizeof(unsigned char*) * 12);
67 for (int i = 0; i < 12; i++) {
69 tmp = (unsigned char *) nl_langinfo(item[i]);
71 printf("month[%d]=%s\n", i, tmp);
76 for (unsigned int j = 0; j < len; j++)
83 if (wmonths == NULL) {
86 wmonths = sort_malloc(sizeof(wchar_t *) * 12);
87 for (int i = 0; i < 12; i++) {
89 tmp = (unsigned char *) nl_langinfo(item[i]);
91 printf("month[%d]=%s\n", i, tmp);
95 m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
96 if (mbstowcs(m, (char*)tmp, len) ==
102 for (unsigned int j = 0; j < len; j++)
103 m[j] = towupper(m[j]);
111 * Compare two wide-character strings
114 wide_str_coll(const wchar_t *s1, const wchar_t *s2)
119 ret = wcscoll(s1, s2);
120 if (errno == EILSEQ) {
122 ret = wcscmp(s1, s2);
124 for (size_t i = 0; ; ++i) {
128 return ((c2 == L'\0') ? 0 : -1);
133 return ((int)(c1 - c2));
140 /* counterparts of wcs functions */
143 bwsprintf(FILE *f, struct bwstring *bws, const char *prefix, const char *suffix)
147 fprintf(f, "%s%s%s", prefix, bws->data.cstr, suffix);
149 fprintf(f, "%s%S%s", prefix, bws->data.wstr, suffix);
152 const void* bwsrawdata(const struct bwstring *bws)
155 return (&(bws->data));
158 size_t bwsrawlen(const struct bwstring *bws)
161 return ((MB_CUR_MAX == 1) ? bws->len : SIZEOF_WCHAR_STRING(bws->len));
165 bws_memsize(const struct bwstring *bws)
168 return ((MB_CUR_MAX == 1) ? (bws->len + 2 + sizeof(struct bwstring)) :
169 (SIZEOF_WCHAR_STRING(bws->len + 1) + sizeof(struct bwstring)));
173 bws_setlen(struct bwstring *bws, size_t newlen)
176 if (bws && newlen != bws->len && newlen <= bws->len) {
179 bws->data.cstr[newlen] = '\0';
181 bws->data.wstr[newlen] = L'\0';
186 * Allocate a new binary string of specified size
191 struct bwstring *ret;
194 ret = sort_malloc(sizeof(struct bwstring) + 1 + sz);
196 ret = sort_malloc(sizeof(struct bwstring) +
197 SIZEOF_WCHAR_STRING(sz + 1));
201 ret->data.cstr[ret->len] = '\0';
203 ret->data.wstr[ret->len] = L'\0';
209 * Create a copy of binary string.
210 * New string size equals the length of the old string.
213 bwsdup(const struct bwstring *s)
219 struct bwstring *ret = bwsalloc(s->len);
222 memcpy(ret->data.cstr, s->data.cstr, (s->len));
224 memcpy(ret->data.wstr, s->data.wstr,
225 SIZEOF_WCHAR_STRING(s->len));
232 * Create a new binary string from a wide character buffer.
235 bwssbdup(const wchar_t *str, size_t len)
239 return ((len == 0) ? bwsalloc(0) : NULL);
241 struct bwstring *ret;
246 for (size_t i = 0; i < len; ++i)
247 ret->data.cstr[i] = (unsigned char) str[i];
249 memcpy(ret->data.wstr, str, SIZEOF_WCHAR_STRING(len));
256 * Create a new binary string from a raw binary buffer.
259 bwscsbdup(const unsigned char *str, size_t len)
261 struct bwstring *ret;
267 memcpy(ret->data.cstr, str, len);
271 size_t charlen, chars, cptr;
275 s = (const char *) str;
277 memset(&mbs, 0, sizeof(mbs));
280 size_t n = MB_CUR_MAX;
284 charlen = mbrlen(s + cptr, n, &mbs);
291 ret->data.wstr[chars++] =
292 (unsigned char) s[cptr];
296 n = mbrtowc(ret->data.wstr + (chars++),
297 s + cptr, charlen, &mbs);
298 if ((n == (size_t)-1) || (n == (size_t)-2))
300 err(2, "mbrtowc error");
306 ret->data.wstr[ret->len] = L'\0';
313 * De-allocate object memory
316 bwsfree(const struct bwstring *s)
324 * Copy content of src binary string to dst.
325 * If the capacity of the dst string is not sufficient,
326 * then the data is truncated.
329 bwscpy(struct bwstring *dst, const struct bwstring *src)
331 size_t nums = src->len;
337 if (MB_CUR_MAX == 1) {
338 memcpy(dst->data.cstr, src->data.cstr, nums);
339 dst->data.cstr[dst->len] = '\0';
341 memcpy(dst->data.wstr, src->data.wstr,
342 SIZEOF_WCHAR_STRING(nums + 1));
343 dst->data.wstr[dst->len] = L'\0';
350 * Copy content of src binary string to dst,
351 * with specified number of symbols to be copied.
352 * If the capacity of the dst string is not sufficient,
353 * then the data is truncated.
356 bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size)
358 size_t nums = src->len;
366 if (MB_CUR_MAX == 1) {
367 memcpy(dst->data.cstr, src->data.cstr, nums);
368 dst->data.cstr[dst->len] = '\0';
370 memcpy(dst->data.wstr, src->data.wstr,
371 SIZEOF_WCHAR_STRING(nums + 1));
372 dst->data.wstr[dst->len] = L'\0';
379 * Copy content of src binary string to dst,
380 * with specified number of symbols to be copied.
381 * An offset value can be specified, from the start of src string.
382 * If the capacity of the dst string is not sufficient,
383 * then the data is truncated.
386 bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset,
390 if (offset >= src->len) {
391 dst->data.wstr[0] = 0;
394 size_t nums = src->len - offset;
401 if (MB_CUR_MAX == 1) {
402 memcpy(dst->data.cstr, src->data.cstr + offset,
404 dst->data.cstr[dst->len] = '\0';
406 memcpy(dst->data.wstr, src->data.wstr + offset,
407 SIZEOF_WCHAR_STRING(nums));
408 dst->data.wstr[dst->len] = L'\0';
415 * Write binary string to the file.
416 * The output is ended either with '\n' (nl == true)
417 * or '\0' (nl == false).
420 bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended)
423 if (MB_CUR_MAX == 1) {
424 size_t len = bws->len;
427 bws->data.cstr[len] = '\n';
429 if (fwrite(bws->data.cstr, len + 1, 1, f) < 1)
432 bws->data.cstr[len] = '\0';
433 } else if (fwrite(bws->data.cstr, len + 1, 1, f) < 1)
442 eols = zero_ended ? btowc('\0') : btowc('\n');
444 while (printed < BWSLEN(bws)) {
445 const wchar_t *s = bws->data.wstr + printed;
450 nums = fwprintf(f, L"%lc", *s);
458 nums = fwprintf(f, L"%ls", s);
465 fwprintf(f, L"%lc", eols);
466 return (printed + 1);
471 * Allocate and read a binary string from file.
472 * The strings are nl-ended or zero-ended, depending on the sort setting.
475 bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb)
479 eols = zero_ended ? btowc('\0') : btowc('\n');
481 if (!zero_ended && (MB_CUR_MAX > 1)) {
484 ret = fgetwln(f, len);
492 if (ret[*len - 1] == (wchar_t)eols)
495 return (bwssbdup(ret, *len));
497 } else if (!zero_ended && (MB_CUR_MAX == 1)) {
500 ret = fgetln(f, len);
508 if (ret[*len - 1] == '\n')
511 return (bwscsbdup((unsigned char*)ret, *len));
519 if (2 >= rb->fgetwln_z_buffer_size) {
520 rb->fgetwln_z_buffer_size += 256;
521 rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
522 sizeof(wchar_t) * rb->fgetwln_z_buffer_size);
524 rb->fgetwln_z_buffer[*len] = 0;
540 if (*len + 1 >= rb->fgetwln_z_buffer_size) {
541 rb->fgetwln_z_buffer_size += 256;
542 rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
543 SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
546 rb->fgetwln_z_buffer[*len] = c;
547 rb->fgetwln_z_buffer[++(*len)] = 0;
563 if (*len + 1 >= rb->fgetwln_z_buffer_size) {
564 rb->fgetwln_z_buffer_size += 256;
565 rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
566 SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
569 rb->fgetwln_z_buffer[*len] = c;
570 rb->fgetwln_z_buffer[++(*len)] = 0;
574 /* we do not count the last 0 */
575 return (bwssbdup(rb->fgetwln_z_buffer, *len));
580 bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2,
581 size_t offset, size_t len)
583 size_t cmp_len, len1, len2;
590 if (len1 <= offset) {
591 return ((len2 <= offset) ? 0 : -1);
607 if (MB_CUR_MAX == 1) {
608 const unsigned char *s1, *s2;
610 s1 = bws1->data.cstr + offset;
611 s2 = bws2->data.cstr + offset;
613 res = memcmp(s1, s2, cmp_len);
616 const wchar_t *s1, *s2;
618 s1 = bws1->data.wstr + offset;
619 s2 = bws2->data.wstr + offset;
621 res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len));
627 if (len1 < cmp_len && len1 < len2)
629 else if (len2 < cmp_len && len2 < len1)
637 bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
639 size_t len1, len2, cmp_len;
653 res = bwsncmp(bws1, bws2, offset, cmp_len);
658 else if (len2 < len1)
666 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len)
671 for (i = 0; i < len; ++i) {
672 c1 = bws_get_iter_value(iter1);
673 c2 = bws_get_iter_value(iter2);
676 iter1 = bws_iterator_inc(iter1, 1);
677 iter2 = bws_iterator_inc(iter2, 1);
684 bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
692 return ((len2 <= offset) ? 0 : -1);
700 if (MB_CUR_MAX == 1) {
701 const unsigned char *s1, *s2;
703 s1 = bws1->data.cstr + offset;
704 s2 = bws2->data.cstr + offset;
710 res = memcmp(s1, s2, len2);
713 } else if (len1 < len2) {
714 res = memcmp(s1, s2, len1);
718 res = memcmp(s1, s2, len1);
733 /* goto next non-zero part: */
734 while ((i < maxlen) &&
744 err(2, "bwscoll error 01");
747 } else if (s2[i] == 0)
750 res = strcoll((const char*)(s1 + i), (const char*)(s2 + i));
754 while ((i < maxlen) &&
767 } else if (s2[i] == 0)
771 err(2, "bwscoll error 02");
776 else if (len1 > len2)
782 const wchar_t *s1, *s2;
786 s1 = bws1->data.wstr + offset;
787 s2 = bws2->data.wstr + offset;
797 /* goto next non-zero part: */
798 while ((i < maxlen) &&
808 err(2, "bwscoll error 1");
811 } else if (s2[i] == 0)
814 res = wide_str_coll(s1 + i, s2 + i);
818 while ((i < maxlen) && s1[i] && s2[i])
830 } else if (s2[i] == 0)
834 err(2, "bwscoll error 2");
839 else if (len1 > len2)
849 * Correction of the system API
852 bwstod(struct bwstring *s0, bool *empty)
856 if (MB_CUR_MAX == 1) {
857 unsigned char *end, *s;
864 while (isblank(*s) && s < end)
872 ret = strtod((char*)s, &ep);
873 if ((unsigned char*) ep == s) {
878 wchar_t *end, *ep, *s;
884 while (iswblank(*s) && s < end)
892 ret = wcstod(s, &ep);
904 * A helper function for monthcoll. If a line matches
905 * a month name, it returns (number of the month - 1),
906 * while if there is no match, it just return -1.
910 bws_month_score(const struct bwstring *s0)
913 if (MB_CUR_MAX == 1) {
914 const unsigned char *end, *s;
919 while (isblank(*s) && s < end)
922 for (int i = 11; i >= 0; --i) {
924 (s == (unsigned char*)strstr((const char*)s, (char*)(cmonths[i]))))
929 const wchar_t *end, *s;
934 while (iswblank(*s) && s < end)
937 for (int i = 11; i >= 0; --i) {
938 if (wmonths[i] && (s == wcsstr(s, wmonths[i])))
947 * Rips out leading blanks (-b).
950 ignore_leading_blanks(struct bwstring *str)
953 if (MB_CUR_MAX == 1) {
954 unsigned char *dst, *end, *src;
956 src = str->data.cstr;
958 end = src + str->len;
960 while (src < end && isblank(*src))
966 newlen = BWSLEN(str) - (src - dst);
973 bws_setlen(str, newlen);
976 wchar_t *dst, *end, *src;
978 src = str->data.wstr;
980 end = src + str->len;
982 while (src < end && iswblank(*src))
987 size_t newlen = BWSLEN(str) - (src - dst);
994 bws_setlen(str, newlen);
1002 * Rips out nonprinting characters (-i).
1005 ignore_nonprinting(struct bwstring *str)
1007 size_t newlen = str->len;
1009 if (MB_CUR_MAX == 1) {
1010 unsigned char *dst, *end, *src;
1013 src = str->data.cstr;
1015 end = src + str->len;
1029 wchar_t *dst, *end, *src;
1032 src = str->data.wstr;
1034 end = src + str->len;
1048 bws_setlen(str, newlen);
1054 * Rips out any characters that are not alphanumeric characters
1058 dictionary_order(struct bwstring *str)
1060 size_t newlen = str->len;
1062 if (MB_CUR_MAX == 1) {
1063 unsigned char *dst, *end, *src;
1066 src = str->data.cstr;
1068 end = src + str->len;
1072 if (isalnum(c) || isblank(c)) {
1082 wchar_t *dst, *end, *src;
1085 src = str->data.wstr;
1087 end = src + str->len;
1091 if (iswalnum(c) || iswblank(c)) {
1101 bws_setlen(str, newlen);
1107 * Converts string to lower case(-f).
1110 ignore_case(struct bwstring *str)
1113 if (MB_CUR_MAX == 1) {
1114 unsigned char *end, *s;
1138 bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos)
1141 if (MB_CUR_MAX == 1)
1142 warnx("%s:%zu: disorder: %s", fn, pos + 1, s->data.cstr);
1144 warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->data.wstr);