diff --git a/usr.bin/sort/bwstring.c b/usr.bin/sort/bwstring.c --- a/usr.bin/sort/bwstring.c +++ b/usr.bin/sort/bwstring.c @@ -43,63 +43,94 @@ bool byte_sort; -static wchar_t **wmonths; -static char **cmonths; +struct wmonth { + wchar_t *mon; + wchar_t *ab; + wchar_t *alt; +}; + +struct cmonth { + char *mon; + char *ab; + char *alt; +}; + +static struct wmonth *wmonths; +static struct cmonth *cmonths; /* initialise months */ void initialise_months(void) { - const nl_item item[12] = { ABMON_1, ABMON_2, ABMON_3, ABMON_4, + const nl_item mon_item[12] = { MON_1, MON_2, MON_3, MON_4, + MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, + MON_11, MON_12 }; + const nl_item ab_item[12] = { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 }; + const nl_item alt_item[12] = { ALTMON_1, ALTMON_2, ALTMON_3, ALTMON_4, + ALTMON_5, ALTMON_6, ALTMON_7, ALTMON_8, ALTMON_9, ALTMON_10, + ALTMON_11, ALTMON_12 }; char *tmp; size_t len; + /* + * Handle all possible month formats: abbrevation, full month name, + * standalone month name (without case ending). + */ if (mb_cur_max == 1) { if (cmonths == NULL) { - char *m; + struct cmonth m; - cmonths = sort_malloc(sizeof(char*) * 12); + cmonths = sort_malloc(sizeof(struct cmonth) * 12); for (int i = 0; i < 12; i++) { - cmonths[i] = NULL; - tmp = nl_langinfo(item[i]); - if (debug_sort) - printf("month[%d]=%s\n", i, tmp); - if (*tmp == '\0') - continue; - m = sort_strdup(tmp); - len = strlen(tmp); - for (unsigned int j = 0; j < len; j++) - m[j] = toupper(m[j]); - cmonths[i] = m; +#define POPULATE(field) do { \ + tmp = nl_langinfo(field##_item[i]); \ + if (debug_sort) \ + printf("month[%d]=%s\n", i, tmp); \ + if (*tmp == '\0') \ + continue; \ + m.field = sort_strdup(tmp); \ + len = strlen(tmp); \ + for (unsigned int j = 0; j < len; j++) \ + m.field[j] = toupper(m.field[j]); \ + cmonths[i].field = m.field; \ +} while (0) + POPULATE(mon); + POPULATE(ab); + POPULATE(alt); +#undef POPULATE } } } else { if (wmonths == NULL) { - wchar_t *m; + struct wmonth m; - wmonths = sort_malloc(sizeof(wchar_t *) * 12); + wmonths = sort_malloc(sizeof(struct wmonth) * 12); for (int i = 0; i < 12; i++) { - wmonths[i] = NULL; - tmp = nl_langinfo(item[i]); - if (debug_sort) - printf("month[%d]=%s\n", i, tmp); - if (*tmp == '\0') - continue; - len = strlen(tmp); - m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1)); - if (mbstowcs(m, tmp, len) == - ((size_t) - 1)) { - sort_free(m); - continue; - } - m[len] = L'\0'; - for (unsigned int j = 0; j < len; j++) - m[j] = towupper(m[j]); - wmonths[i] = m; +#define POPULATE(field) do { \ + tmp = nl_langinfo(field##_item[i]); \ + if (debug_sort) \ + printf("month[%d]=%s\n", i, tmp); \ + if (*tmp == '\0') \ + continue; \ + len = strlen(tmp); \ + m.field = sort_malloc(SIZEOF_WCHAR_STRING(len + 1)); \ + if (mbstowcs(m.field, tmp, len) == ((size_t) - 1)) { \ + sort_free(m.field); \ + continue; \ + } \ + m.field[len] = L'\0'; \ + for (unsigned int j = 0; j < len; j++) \ + m.field[j] = towupper(m.field[j]); \ + wmonths[i].field = m.field; \ +} while (0) + POPULATE(mon); + POPULATE(ab); + POPULATE(alt); +#undef POPULATE } } } @@ -754,8 +785,11 @@ ++s; for (int i = 11; i >= 0; --i) { - if (cmonths[i] && - (s == strstr(s, cmonths[i]))) + if (cmonths[i].mon && (s == strstr(s, cmonths[i].mon))) + return (i); + if (cmonths[i].ab && (s == strstr(s, cmonths[i].ab))) + return (i); + if (cmonths[i].alt && (s == strstr(s, cmonths[i].alt))) return (i); } @@ -769,7 +803,11 @@ ++s; for (int i = 11; i >= 0; --i) { - if (wmonths[i] && (s == wcsstr(s, wmonths[i]))) + if (wmonths[i].ab && (s == wcsstr(s, wmonths[i].ab))) + return (i); + if (wmonths[i].mon && (s == wcsstr(s, wmonths[i].mon))) + return (i); + if (wmonths[i].alt && (s == wcsstr(s, wmonths[i].alt))) return (i); } }