commit f9cbb7067dc12d42f9788732593827f419405334
parent 44ef5da31145804c0959bf5357bdd94f4f63419b
Author: phoebos <ben@bvnf.space>
Date: Sat, 26 Mar 2022 19:34:05 +0000
ls, sort: respect locales
Diffstat:
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/ls.c b/ls.c
@@ -3,6 +3,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <locale.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
@@ -215,8 +216,12 @@ sort(const void *s1, const void *s2) {
ret = e2->size - e1->size;
if (flags & FLAG_t)
ret = e2->tim.tv_sec - e1->tim.tv_sec;
- if (ret == 0)
- ret = strcmp(e1->name, e2->name);
+ if (ret == 0) {
+ errno = 0;
+ ret = strcoll(e1->name, e2->name);
+ if (errno != 0)
+ ret = strcmp(e1->name, e2->name);
+ }
if (flags & FLAG_r)
ret *= -1;
return ret;
@@ -413,6 +418,7 @@ int
main(int argc, char **argv) {
int c, ret_val;
flags = ret_val = 0;
+ setlocale(LC_ALL, "");
while ((c = getopt(argc, argv, "1AFSacfgilmnopqrtu")) != -1) {
switch (c) {
diff --git a/sort.c b/sort.c
@@ -1,6 +1,7 @@
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <limits.h>
+#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -14,7 +15,7 @@ int flags;
int
alphacompare(const void *s1, const void *s2) {
- int ret = strcmp(*(const char **) s1, *(const char **) s2);
+ int ret = strcoll(*(const char **) s1, *(const char **) s2);
if (flags & FLAG_r)
ret *= -1;
return ret;
@@ -74,6 +75,7 @@ sort(FILE *f) {
int
main(int argc, char **argv) {
+ setlocale(LC_ALL, "");
int c;
int ret = 0;
flags = 0;