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 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(char*) * 12);
67 for (int i = 0; i < 12; i++) {
69 tmp = 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 = 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, 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;
589 if (len1 <= offset) {
590 return ((len2 <= offset) ? 0 : -1);
606 if (MB_CUR_MAX == 1) {
607 const unsigned char *s1, *s2;
609 s1 = bws1->data.cstr + offset;
610 s2 = bws2->data.cstr + offset;
612 res = memcmp(s1, s2, cmp_len);
615 const wchar_t *s1, *s2;
617 s1 = bws1->data.wstr + offset;
618 s2 = bws2->data.wstr + offset;
620 res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len));
626 if (len1 < cmp_len && len1 < len2)
628 else if (len2 < cmp_len && len2 < len1)
636 bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
638 size_t len1, len2, cmp_len;
652 res = bwsncmp(bws1, bws2, offset, cmp_len);
657 else if (len2 < len1)
665 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len)
670 for (i = 0; i < len; ++i) {
671 c1 = bws_get_iter_value(iter1);
672 c2 = bws_get_iter_value(iter2);
675 iter1 = bws_iterator_inc(iter1, 1);
676 iter2 = bws_iterator_inc(iter2, 1);
683 bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
691 return ((len2 <= offset) ? 0 : -1);
699 if (MB_CUR_MAX == 1) {
700 const unsigned char *s1, *s2;
702 s1 = bws1->data.cstr + offset;
703 s2 = bws2->data.cstr + offset;
709 res = memcmp(s1, s2, len2);
712 } else if (len1 < len2) {
713 res = memcmp(s1, s2, len1);
717 res = memcmp(s1, s2, len1);
732 /* goto next non-zero part: */
733 while ((i < maxlen) &&
743 err(2, "bwscoll error 01");
746 } else if (s2[i] == 0)
749 res = strcoll((const char*)(s1 + i), (const char*)(s2 + i));
753 while ((i < maxlen) &&
766 } else if (s2[i] == 0)
770 err(2, "bwscoll error 02");
775 else if (len1 > len2)
781 const wchar_t *s1, *s2;
785 s1 = bws1->data.wstr + offset;
786 s2 = bws2->data.wstr + offset;
796 /* goto next non-zero part: */
797 while ((i < maxlen) &&
807 err(2, "bwscoll error 1");
810 } else if (s2[i] == 0)
813 res = wide_str_coll(s1 + i, s2 + i);
817 while ((i < maxlen) && s1[i] && s2[i])
829 } else if (s2[i] == 0)
833 err(2, "bwscoll error 2");
838 else if (len1 > len2)
848 * Correction of the system API
851 bwstod(struct bwstring *s0, bool *empty)
855 if (MB_CUR_MAX == 1) {
856 unsigned char *end, *s;
863 while (isblank(*s) && s < end)
871 ret = strtod((char*)s, &ep);
872 if ((unsigned char*) ep == s) {
877 wchar_t *end, *ep, *s;
883 while (iswblank(*s) && s < end)
891 ret = wcstod(s, &ep);
903 * A helper function for monthcoll. If a line matches
904 * a month name, it returns (number of the month - 1),
905 * while if there is no match, it just return -1.
909 bws_month_score(const struct bwstring *s0)
912 if (MB_CUR_MAX == 1) {
913 const unsigned char *end, *s;
918 while (isblank(*s) && s < end)
921 for (int i = 11; i >= 0; --i) {
923 (s == (unsigned char*)strstr((const char*)s, (char*)(cmonths[i]))))
928 const wchar_t *end, *s;
933 while (iswblank(*s) && s < end)
936 for (int i = 11; i >= 0; --i) {
937 if (wmonths[i] && (s == wcsstr(s, wmonths[i])))
946 * Rips out leading blanks (-b).
949 ignore_leading_blanks(struct bwstring *str)
952 if (MB_CUR_MAX == 1) {
953 unsigned char *dst, *end, *src;
955 src = str->data.cstr;
957 end = src + str->len;
959 while (src < end && isblank(*src))
965 newlen = BWSLEN(str) - (src - dst);
972 bws_setlen(str, newlen);
975 wchar_t *dst, *end, *src;
977 src = str->data.wstr;
979 end = src + str->len;
981 while (src < end && iswblank(*src))
986 size_t newlen = BWSLEN(str) - (src - dst);
993 bws_setlen(str, newlen);
1001 * Rips out nonprinting characters (-i).
1004 ignore_nonprinting(struct bwstring *str)
1006 size_t newlen = str->len;
1008 if (MB_CUR_MAX == 1) {
1009 unsigned char *dst, *end, *src;
1012 src = str->data.cstr;
1014 end = src + str->len;
1028 wchar_t *dst, *end, *src;
1031 src = str->data.wstr;
1033 end = src + str->len;
1047 bws_setlen(str, newlen);
1053 * Rips out any characters that are not alphanumeric characters
1057 dictionary_order(struct bwstring *str)
1059 size_t newlen = str->len;
1061 if (MB_CUR_MAX == 1) {
1062 unsigned char *dst, *end, *src;
1065 src = str->data.cstr;
1067 end = src + str->len;
1071 if (isalnum(c) || isblank(c)) {
1081 wchar_t *dst, *end, *src;
1084 src = str->data.wstr;
1086 end = src + str->len;
1090 if (iswalnum(c) || iswblank(c)) {
1100 bws_setlen(str, newlen);
1106 * Converts string to lower case(-f).
1109 ignore_case(struct bwstring *str)
1112 if (MB_CUR_MAX == 1) {
1113 unsigned char *end, *s;
1137 bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos)
1140 if (MB_CUR_MAX == 1)
1141 warnx("%s:%zu: disorder: %s", fn, pos + 1, s->data.cstr);
1143 warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->data.wstr);