summaryrefslogblamecommitdiff
path: root/src/ls.c
blob: defee1e4dbb2657c280afd125c550684ddbef461 (plain) (tree)















































































                                                                               
/*
 * 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 <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include <unistd.h>
#include <limits.h>
#include <locale.h>
#include <errno.h>

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<argc; ) {
		if (xstat(argv[i], &st)) {
			my_perror(argv[0], argv[i]);
			memmove(&argv[i], &argv[i+1], argc-i-1);
			argc--;
		} else if (S_ISDIR(st.st_mode) && !opt_d) {
			char *tmp = argv[i];
			argv[i] = argv[--firstdir];
			argv[fisrtdir] = tmp;
			if (i == firstdir) break;
		} else i++;
	}
	qsort(argv+optind, firstdir-optind, sizeof(char *), cmp);
	qsort(argv+firstdir, argc-firstdir, sizeof(char *), cmp);
}