/* ANY CLAIM OF COPYRIGHT ON THIS FILE IS WAIVED. * * this POSIX.1-2001 C99 program takes any amount of filesystem path arguments. * the contents of the files are converted to newline-separated C string * literals which are written to standard output. */ #include #include #include #include #include #include int main(int buffer_index, char **argv) { uint32_t buffer[32767]; unsigned char *restrict input; size_t input_index, input_length; next_file: if (!*++argv) return 0; buffer_index = open(*argv, O_RDONLY | O_NOCTTY); if ( -1 == buffer_index || -1 == fstat(buffer_index, (struct stat *)buffer) ) { fail_open: perror(*argv); return 1; } input_length = ((struct stat *)buffer)->st_size; input = mmap(NULL, input_length, PROT_READ, MAP_SHARED, buffer_index, 0); close(buffer_index); if ( MAP_FAILED == input || 0 != posix_madvise(input, input_length, POSIX_MADV_SEQUENTIAL) ) goto fail_open; buffer[0] = 0x22202020; /* hex: " \"" */ buffer_index = 1; input_index = 0; do { /* string bits: * constant 11111000111110001111110011111111 * 0xf8f8fcff (~0x7070300) * set 00110000001100000011000001011100 * 0x3030305c * * map bits to fields: (num << 24) | (num << 13) | (num << 2) * num * 0x1002004 */ buffer[buffer_index++] = ((input[input_index] * 0x1002004) & 0x7070300) | 0x3030305c; if (buffer_index > (int)(sizeof(buffer) / sizeof(*buffer))) { write(1, buffer, buffer_index * sizeof(*buffer)); buffer_index = 0; } } while (++input_index < input_length); buffer[buffer_index] = 0xa202022; /* hex: "\" \n" */ write(1, buffer, (buffer_index + 1) * sizeof(*buffer)); munmap(input, input_length); goto next_file; }