A C Program a Day

2019-10-07 - Sort by

Here's a small program which is like sort, but it sorts by a specific column, which is passed in as an argument.

Today's procedure is sort.

Downloads



#include "procs-2019-10-07.c"

static int sortcmp(void *a, void *b) {
	const char *s = *(const char **)a;
	const char *t = *(const char **)b;
	do {
		if (*t - *s != 0)
			return *t - *s;
		s++, t++;
	} while (s[-1] != 0 && t[-1] != 0);
	return 0;
}

int main(int argc, char **argv) {
	long col;
	char buffer[256];
	ssize_t bytesread;
	char *text;
	long nlines = 0;
	*(char *)sbrk(1) = '\n'; /* put a newline before text */
	text = sbrk(0);
	if (argc < 2)
		col = 0;
	else {
		char *p = argv[1];
		col = atol(&p);
		if (p == argv[1] || *p != 0) {
			const char *err = "Please enter a number.";
			write(stderr, err, (size_t)strlen(err));
			return exitfailure;
		}
	}

	while ((bytesread = read(stdin, buffer, sizeof buffer)) > 0) {
		char *part = sbrk(bytesread);
		long i;
		for (i = 0; i < (long)bytesread; i++) {
			part[i] = buffer[i];
			if (buffer[i] == '\n')
				nlines++;
		}
	}

	if (bytesread < 0) {
		const char *err = "Error reading from stdin.";
		write(stderr, err, (size_t)strlen(err));
		return exitfailure;
	}
	
	*(char *)sbrk(1) = 0; /* null-terminate */
	{
		char **lines; /* points to the correct column in the line */
		char *p = text;
		long i;
		sbrk((long)(sizeof *lines - ((size_t)sbrk(0)) % sizeof *lines)); /* alignment */
		lines = sbrk(nlines * (long)sizeof *lines);
		for (i = 0; i < nlines; i++) {
			long c = 1;
			lines[i] = "";
			while (*p != '\n' && *p != 0) {
				if (*p=='\t' || *p==' ') {
					c++;
					p++;
					continue;
				}
				if (c == col) {
					lines[i] = p;
					while (*p != '\n' && *p != 0) p++;
					break;
				}
				p++;
			}
			p++;
		}
		sort(lines, nlines, sizeof *lines, sortcmp);
		for (i = 0; i < nlines; i++) {
			char *p = lines[i];
			long len;
			while (*p != '\n') p--;
			p++;
			for (len = 0; p[len] && p[len] != '\n'; len++);
			write(stdout, p, (size_t)len);
			write(stdout, "\n", 1);
		}
	}
	return 0;
}
		

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to <http://unlicense.org/>