88 lines
1.4 KiB
C
88 lines
1.4 KiB
C
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <ctype.h>
|
|
|
|
unsigned int d_flag;
|
|
char eyes[] = {'U', 'O'};
|
|
|
|
void OwO(char *buf, off_t len) {
|
|
if (d_flag)
|
|
return;
|
|
|
|
for (int i = 0; i < len / 10; i++) {
|
|
size_t i = rand() % len;
|
|
|
|
if (i + 3 < len && !isspace(buf[i]) && !isspace(buf[i + 1]) && !isspace(buf[i + 2])) {
|
|
char eye = eyes[rand() % sizeof(eyes)];
|
|
|
|
buf[i] = eye;
|
|
buf[i + 1] = 'w';
|
|
buf[i + 2] = eye;
|
|
}
|
|
}
|
|
}
|
|
|
|
int cat(const char *path) {
|
|
int fd = STDIN_FILENO;
|
|
|
|
if (strcmp(path, "-"))
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
fprintf(stderr, "cutecat: %s: %s\n", path, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
char buf[513];
|
|
off_t len = 0;
|
|
while ((len = read(fd, buf, sizeof(buf))) > 0) {
|
|
OwO(buf, len);
|
|
if (write(STDOUT_FILENO, buf, len) != len) {
|
|
fprintf(stderr, "cutecat: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (strcmp(path, "-"))
|
|
close(fd);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "d")) != -1) {
|
|
switch (opt) {
|
|
case 'd':
|
|
d_flag = 1;
|
|
break;
|
|
|
|
default:
|
|
printf("cutecat [file1 file2...]\n\t[-d Default mode]\n");
|
|
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;
|
|
}
|
|
}
|