summaryrefslogtreecommitdiff
path: root/rccat.c
blob: 9a1d60e400689dfdb8d161a58a6062e1f28a4d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define BUF_SIZE 10

#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"

const char *colors[] = {KNRM, KRED, KGRN, KYEL, KMAG, KCYN};

int lastcolor = 0;
void printColors(char *text)
{

	if (text) {
		for (int i = 0; i < strlen(text); ++i) {
			int color = 0;
			do {
				color = rand() % 6;
			} while (color == lastcolor);
			lastcolor = color;
			printf("%s%c", colors[color], text[i]);
		}
	}
	printf("%s", KWHT);
}

int main(int argc, char *argv[])
{
	srand(time(NULL));
	char buffer[BUF_SIZE] = {""};
	
	for (int i = 0; i < argc; ++i) {
		if (strcmp(argv[i], "-") == 0 || argc == 1) {
			while (fgets(buffer, BUF_SIZE, stdin) != NULL) {
				printColors(buffer);
			}
			clearerr(stdin);
		} else if (i >= 1) {
			FILE *fp;
			fp = fopen(argv[i], "r");
			if (!(fp)) {
				fprintf(stderr, "rccat: %s: no such file or directory.\n", argv[i]);
			}
			size_t got;
			while ((got = fread(buffer, 1, BUF_SIZE -1, fp))) {
				buffer[got] = '\0';
					printColors(buffer);
				}
				if (ferror(fp))
					fprintf(stderr, "Error: can't read %s\n", argv[1]);
			fclose(fp);
		}
	}

	return EXIT_SUCCESS;
}