/* * ls.c * Implementation of SUSv3 XCU ls utility * Copyright © 2007 Rich Felker * Licensed under the terms of the GNU General Public License, v2 or later */ #include #include #include #include #include #include #include #include #include #include static size_t mbswidth(const char *s) { size_t l, w=0; int v; wchar_t wc; mbstate_t mbs = { 0 }; for (w=0; *s; s+=l, w+=v) if ((l=mbrtowc(&wc, s, MB_LEN_MAX, &mbs))+2<2 || (v=wcwidth(wc)) < 0) return -1; return w; } static void my_perror(char *prog, char *msg) { char *err = strerror(errno); write(2, prog, strlen(prog)); write(2, ": ", 2); write(2, msg, strlen(msg)); write(2, ": ", 2); write(2, err, strlen(err)); write(2, "\n", 1); } static int cmp(const void *a, const void *b) { return strcoll(*(const char **)a, *(const char **)b); } int main(int argc, char *argv[]) { struct stat st; int (*xstat)(const char *, struct stat *) = stat; if (!setlocale(LC_ALL, "")) my_perror(argv[0], "setlocale"); while ((b=getopt(argc,argv,"CFHLRacdfgilmnoprstux1"))!=EOF) switch(b) { default: return 1; } if (optind == argc) { } for (i=optind; i