summaryrefslogtreecommitdiff
path: root/rccat.c
diff options
context:
space:
mode:
Diffstat (limited to 'rccat.c')
-rw-r--r--rccat.c38
1 files changed, 36 insertions, 2 deletions
diff --git a/rccat.c b/rccat.c
index 9a1d60e..1c31e38 100644
--- a/rccat.c
+++ b/rccat.c
@@ -1,11 +1,17 @@
+/* ~\~ language=C filename=rccat.c */
+/* ~\~ begin <<rccat.md|rccat.c>>[0] */
+/* ~\~ begin <<rccat.md|includes>>[0] */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
+/* ~\~ end */
+/* ~\~ begin <<rccat.md|constants>>[0] */
#define BUF_SIZE 10
+/* ~\~ begin <<rccat.md|colors>>[0] */
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
@@ -15,12 +21,21 @@
#define KWHT "\x1B[37m"
const char *colors[] = {KNRM, KRED, KGRN, KYEL, KMAG, KCYN};
+/* ~\~ end */
+/* ~\~ end */
+/* ~\~ begin <<rccat.md|colors-function>>[0] */
int lastcolor = 0;
void printColors(char *text)
{
-
+ /* ~\~ begin <<rccat.md|colors-notty>>[0] */
+ if (!(isatty(STDOUT_FILENO))) {
+ printf("%s", text);
+ return;
+ }
+ /* ~\~ end */
if (text) {
+ /* ~\~ begin <<rccat.md|colors-normal>>[0] */
for (int i = 0; i < strlen(text); ++i) {
int color = 0;
do {
@@ -29,27 +44,39 @@ void printColors(char *text)
lastcolor = color;
printf("%s%c", colors[color], text[i]);
}
+ /* ~\~ end */
}
+ /* ~\~ begin <<rccat.md|colors-reset>>[0] */
printf("%s", KWHT);
+ /* ~\~ end */
}
+/* ~\~ end */
+/* ~\~ begin <<rccat.md|main-function>>[0] */
int main(int argc, char *argv[])
{
+ /* ~\~ begin <<rccat.md|main-body>>[0] */
srand(time(NULL));
char buffer[BUF_SIZE] = {""};
-
+
for (int i = 0; i < argc; ++i) {
+ /* ~\~ begin <<rccat.md|parse-arg>>[0] */
if (strcmp(argv[i], "-") == 0 || argc == 1) {
+ /* ~\~ begin <<rccat.md|print-from-stdin>>[0] */
while (fgets(buffer, BUF_SIZE, stdin) != NULL) {
printColors(buffer);
}
clearerr(stdin);
+ /* ~\~ end */
} else if (i >= 1) {
+ /* ~\~ begin <<rccat.md|open-file>>[0] */
FILE *fp;
fp = fopen(argv[i], "r");
if (!(fp)) {
fprintf(stderr, "rccat: %s: no such file or directory.\n", argv[i]);
}
+ /* ~\~ end */
+ /* ~\~ begin <<rccat.md|read-file>>[0] */
size_t got;
while ((got = fread(buffer, 1, BUF_SIZE -1, fp))) {
buffer[got] = '\0';
@@ -57,9 +84,16 @@ int main(int argc, char *argv[])
}
if (ferror(fp))
fprintf(stderr, "Error: can't read %s\n", argv[1]);
+ /* ~\~ end */
+ /* ~\~ begin <<rccat.md|close-file>>[0] */
fclose(fp);
+ /* ~\~ end */
}
+ /* ~\~ end */
}
+ /* ~\~ end */
return EXIT_SUCCESS;
}
+/* ~\~ end */
+/* ~\~ end */