File: teletype.c
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 You can build this command-line app by running
  27 
  28 cc -Wall -s -O2 -o ./teletype ./teletype.c
  29 */
  30 
  31 #include <stdbool.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <time.h>
  35 
  36 #ifdef _WIN32
  37 #include <windows.h>
  38 #endif
  39 
  40 // info is the multi-line help message
  41 const char* info = ""
  42 "teletype [options...] [filenames...]\n"
  43 "\n"
  44 "Simulate the cadence of old-fashioned teletype machines by delaying output.\n"
  45 "\n"
  46 "\n"
  47 "Options\n"
  48 "\n"
  49 "    -h, --h            show this help message\n"
  50 "    -help, --help      aliases for option -h\n"
  51 "";
  52 
  53 // handle_reader shows all bytes read from the source given as colored hex
  54 // values, showing offsets and ASCII symbols on the sides of each output line
  55 void handle_reader(FILE* w, FILE* src) {
  56     struct timespec delay;
  57     delay.tv_sec = 0;
  58     // delay.tv_nsec = 0;
  59 
  60     while (!feof(w)) {
  61         int c = getc(src);
  62         if (c < 0) {
  63             break;
  64         }
  65 
  66         delay.tv_nsec = (c == '\n') ? 750 * 1e6 : 15 * 1e6;
  67         nanosleep(&delay, NULL);
  68         putc(c, w);
  69     }
  70 }
  71 
  72 // handle_file handles data from the filename given; returns false only when
  73 // the file can't be opened
  74 bool handle_file(FILE* w, const char* path) {
  75     FILE* f = fopen(path, "rb");
  76     if (f == NULL) {
  77         // ensure currently-buffered/deferred output shows up right now: not
  78         // doing so may scramble results in the common case where stdout and
  79         // stderr are the same, thus confusing users
  80         putc('\n', w);
  81 
  82         fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", path);
  83         return false;
  84     }
  85 
  86     handle_reader(w, f);
  87     fclose(f);
  88     return true;
  89 }
  90 
  91 // is_help_option simplifies control-flow for func run
  92 bool is_help_option(const char* s) {
  93     return (s[0] == '-') && (
  94         strcmp(s, "-h") == 0 ||
  95         strcmp(s, "-help") == 0 ||
  96         strcmp(s, "--h") == 0 ||
  97         strcmp(s, "--help") == 0
  98     );
  99 }
 100 
 101 // run returns the number of errors
 102 int run(int argc, char** argv, FILE* w) {
 103     size_t files = 0;
 104     size_t errors = 0;
 105 
 106     // handle all filenames/options given
 107     for (size_t i = 1; i < argc && !feof(w); i++) {
 108         // a `-` filename stands for the standard input
 109         if (argv[i][0] == '-' && argv[i][1] == 0) {
 110             handle_reader(w, stdin);
 111             continue;
 112         }
 113 
 114         if (is_help_option(argv[i])) {
 115             // help option quits the app right away
 116             fprintf(stderr, "%s", info);
 117             return 0;
 118         }
 119 
 120         if (!handle_file(w, argv[i])) {
 121             errors++;
 122         }
 123         files++;
 124     }
 125 
 126     // no filenames means use stdin as the only input
 127     if (files == 0) {
 128         handle_reader(w, stdin);
 129     }
 130 
 131     return errors;
 132 }
 133 
 134 int main(int argc, char** argv) {
 135 #ifdef _WIN32
 136     setmode(fileno(stdin), O_BINARY);
 137     // ensure output lines end in LF instead of CRLF on windows
 138     setmode(fileno(stdout), O_BINARY);
 139     setmode(fileno(stderr), O_BINARY);
 140 #endif
 141 
 142     // disable buffering for stdout
 143     setvbuf(stdout, NULL, _IONBF, 0);
 144 
 145     return run(argc, argv, stdout) == 0 ? 0 : 1;
 146 }