cutecat.c/cutecat.c

112 lines
1.9 KiB
C
Raw Permalink Normal View History

2024-01-08 09:28:03 +00:00
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
2024-01-12 16:55:46 +00:00
#include <math.h>
#define FREQ 220
2024-01-08 09:28:03 +00:00
unsigned int d_flag;
2024-01-12 16:55:46 +00:00
unsigned int r_flag;
2024-01-08 09:28:03 +00:00
void OwO(char *buf, off_t len) {
if (d_flag)
return;
2024-01-12 16:55:46 +00:00
char eyes[] = {'U', 'O'};
for (int i = 0; i < len; i++) {
2024-01-08 09:28:03 +00:00
size_t i = rand() % len;
2024-01-08 09:37:28 +00:00
if (i + 3 < len && !isspace(buf[i]) && !isspace(buf[i + 1]) && !isspace(buf[i + 2])) {
2024-01-08 09:28:03 +00:00
char eye = eyes[rand() % sizeof(eyes)];
buf[i] = eye;
buf[i + 1] = 'w';
buf[i + 2] = eye;
}
}
}
2024-01-12 16:55:46 +00:00
void PrintRainbow(char c, off_t i) {
2024-01-12 17:14:58 +00:00
unsigned int red = sin(FREQ * i) * 127 + 128;
unsigned int blue = sin(FREQ * i + 2 * M_PI / 3) * 127 + 128;
unsigned int green = sin(FREQ * i + 4 * M_PI / 3) * 127 + 128;
2024-01-12 16:55:46 +00:00
2024-01-12 17:14:58 +00:00
printf("\033[38;2;%u;%u;%um%c\033[0m", red, green, blue, c);
2024-01-12 16:55:46 +00:00
}
2024-01-08 09:28:03 +00:00
int cat(const char *path) {
int fd = STDIN_FILENO;
if (strcmp(path, "-"))
fd = open(path, O_RDONLY);
if (fd < 0) {
2024-01-08 09:29:33 +00:00
fprintf(stderr, "cutecat: %s: %s\n", path, strerror(errno));
2024-01-08 09:28:03 +00:00
return 1;
}
2024-01-12 16:55:46 +00:00
char buf[1025];
2024-01-08 09:28:03 +00:00
off_t len = 0;
2024-01-13 15:35:40 +00:00
off_t n_line = 10;
2024-01-12 16:55:46 +00:00
2024-01-08 09:28:03 +00:00
while ((len = read(fd, buf, sizeof(buf))) > 0) {
OwO(buf, len);
2024-01-12 16:55:46 +00:00
for (off_t i = 0; i < len; i++) {
2024-01-12 17:14:58 +00:00
if (buf[i] == '\n')
n_line++;
2024-01-12 16:55:46 +00:00
if (!isspace(buf[i]) && !r_flag)
2024-01-13 15:35:40 +00:00
PrintRainbow(buf[i], n_line + i / 10);
2024-01-12 16:55:46 +00:00
else
putchar(buf[i]);
2024-01-08 09:28:03 +00:00
}
}
if (strcmp(path, "-"))
close(fd);
return 0;
}
int main(int argc, char **argv) {
int opt;
2024-01-12 16:55:46 +00:00
while ((opt = getopt(argc, argv, "dr")) != -1) {
2024-01-08 09:28:03 +00:00
switch (opt) {
case 'd':
d_flag = 1;
break;
2024-01-12 16:55:46 +00:00
case 'r':
r_flag = 1;
break;
2024-01-08 09:28:03 +00:00
default:
2024-01-12 16:55:46 +00:00
printf("cutecat [file1 file2...]\n\t[-d Default mode]\n\t[-r Print without colorise text]\n");
2024-01-08 09:28:03 +00:00
return 0;
}
}
argv += optind;
argc -= optind;
if (!d_flag)
srand(getpid());
if (argc == 0)
return cat("-");
else {
int ret = 0;
for (int i = 0; i < argc; i++)
if (cat(argv[i]))
ret = 1;
return ret;
}
}