File: limit.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 -O3 -march=native -mtune=native -flto -o ./limit ./limit.c
  29 */
  30 
  31 #include <stdbool.h>
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <string.h>
  35 
  36 #ifdef _WIN32
  37 #include <fcntl.h>
  38 #include <windows.h>
  39 #endif
  40 
  41 #ifdef RED_ERRORS
  42 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  43 #ifdef __APPLE__
  44 #define ERROR_STYLE "\x1b[31m"
  45 #endif
  46 #define RESET_STYLE "\x1b[0m"
  47 #else
  48 #define ERROR_STYLE
  49 #define RESET_STYLE
  50 #endif
  51 
  52 #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n")
  53 
  54 #ifndef IBUF_SIZE
  55 #define IBUF_SIZE (32 * 1024)
  56 #endif
  57 
  58 const char* info = ""
  59 "limit [bytes...] [filenames...]\n"
  60 "\n"
  61 "Emit up to the first n bytes from all the named sources given: if no number\n"
  62 "is given, the default is 1024. The name `-` stands for the standard input.\n"
  63 "When no names are given, the standard input is used by default.\n"
  64 "";
  65 
  66 void handle_reader(FILE* w, FILE* r, size_t* count) {
  67     unsigned char buf[IBUF_SIZE];
  68 
  69     while (!feof(w) && *count > 0) {
  70         size_t len = fread(buf, sizeof(buf[0]), sizeof(buf), r);
  71         if (len < 1) {
  72             break;
  73         }
  74 
  75         if (*count <= len) {
  76             fwrite(buf, 1, *count, w);
  77             *count = 0;
  78             return;
  79         }
  80 
  81         fwrite(buf, 1, len, w);
  82         *count -= len;
  83     }
  84 }
  85 
  86 // handle_file handles data from the filename given; returns false only when
  87 // the file can't be opened
  88 bool handle_file(FILE* w, const char* path, size_t* n) {
  89     FILE* f = fopen(path, "rb");
  90     if (f == NULL) {
  91         fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path);
  92         return false;
  93     }
  94 
  95     handle_reader(w, f, n);
  96     fclose(f);
  97     return true;
  98 }
  99 
 100 // run returns the number of errors
 101 int run(char** args, size_t nargs, FILE* w, size_t* count) {
 102     size_t dashes = 0;
 103     for (size_t i = 0; i < nargs; i++) {
 104         if (args[i][0] == '-' && args[i][1] == 0) {
 105             dashes++;
 106         }
 107     }
 108 
 109     if (dashes > 1) {
 110         const char* m = "can't use the standard input (dash) more than once";
 111         fprintf(stderr, ERROR_LINE("%s"), m);
 112         return 1;
 113     }
 114 
 115     size_t errors = 0;
 116     for (size_t i = 0; i < nargs && !feof(w) && *count > 0; i++) {
 117         if (args[i][0] == '-' && args[i][1] == 0) {
 118             handle_reader(w, stdin, count);
 119             continue;
 120         }
 121 
 122         if (!handle_file(w, args[i], count)) {
 123             errors++;
 124         }
 125     }
 126 
 127     // use stdin when not given any filepaths
 128     if (nargs < 1) {
 129         handle_reader(w, stdin, count);
 130     }
 131 
 132     return errors;
 133 }
 134 
 135 int main(int argc, char** argv) {
 136 #ifdef _WIN32
 137     setmode(fileno(stdin), O_BINARY);
 138     // ensure output lines end in LF instead of CRLF on windows
 139     setmode(fileno(stdout), O_BINARY);
 140     setmode(fileno(stderr), O_BINARY);
 141 #endif
 142 
 143     if (argc > 1) {
 144         if (
 145             strcmp(argv[1], "-h") == 0 ||
 146             strcmp(argv[1], "-help") == 0 ||
 147             strcmp(argv[1], "--h") == 0 ||
 148             strcmp(argv[1], "--help") == 0
 149         ) {
 150             fprintf(stdout, "%s", info);
 151             return 0;
 152         }
 153     }
 154 
 155     size_t nargs = argc - 1;
 156     char** args = argv + 1;
 157 
 158     size_t count = 1;
 159     if (nargs > 0) {
 160         char* end;
 161         const ssize_t n = strtol(argv[1], &end, 10);
 162         if (*end == 0 && argv[1] != end) {
 163             count = n > 0 ? (ssize_t)n : 0;
 164             nargs--;
 165             args++;
 166         }
 167     }
 168 
 169     if (nargs > 0 && strcmp(args[0], "--") == 0) {
 170         nargs--;
 171         args++;
 172     }
 173 
 174     return run(args, nargs, stdout, &count) == 0 ? 0 : 1;
 175 }