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(char* s) {
  93     return (s[0] == '-') && (
  94         strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 ||
  95         strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0
  96     );
  97 }
  98 
  99 // run returns the number of errors
 100 int run(int argc, char** argv, FILE* w) {
 101     size_t files = 0;
 102     size_t errors = 0;
 103 
 104     // handle all filenames/options given
 105     for (size_t i = 1; i < argc && !feof(w); i++) {
 106         // a `-` filename stands for the standard input
 107         if (argv[i][0] == '-' && argv[i][1] == 0) {
 108             handle_reader(w, stdin);
 109             continue;
 110         }
 111 
 112         if (is_help_option(argv[i])) {
 113             // help option quits the app right away
 114             fprintf(stderr, "%s", info);
 115             return 0;
 116         }
 117 
 118         if (!handle_file(w, argv[i])) {
 119             errors++;
 120         }
 121         files++;
 122     }
 123 
 124     // no filenames means use stdin as the only input
 125     if (files == 0) {
 126         handle_reader(w, stdin);
 127     }
 128 
 129     return errors;
 130 }
 131 
 132 int main(int argc, char** argv) {
 133 #ifdef _WIN32
 134     setmode(fileno(stdin), O_BINARY);
 135     // ensure output lines end in LF instead of CRLF on windows
 136     setmode(fileno(stdout), O_BINARY);
 137     setmode(fileno(stderr), O_BINARY);
 138 #endif
 139 
 140     // disable buffering for stdout
 141     setvbuf(stdout, NULL, _IONBF, 0);
 142 
 143     return run(argc, argv, stdout) == 0 ? 0 : 1;
 144 }