-
-
Save nucular/8dc154011a7e2aa7e70447aa7e6ddf4c to your computer and use it in GitHub Desktop.
Inmake, rewritten in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc inmake.c -o inmake | |
// ^ 100% self-hosted | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#define COMMAND_SIZE 255 | |
static const char *usage = | |
"usage: inmake FILE\n" | |
"\n" | |
"A tool that runs the first (or second, if a shebang is detected) line of\n" | |
"a file, with all characters up to the last whitespace stripped, as a shell\n" | |
"command.\n" | |
"\n" | |
"2016 by nucular, Public Domain under the Unlicense\n"; | |
int main(int argc, char **argv) { | |
const char *path; | |
FILE *file; | |
char c; | |
char *p; | |
char command[COMMAND_SIZE + 1]; // +1 for terminator | |
// "Parse" arguments | |
if (argc != 2) | |
{ | |
printf("%s", usage); | |
return 1; | |
} | |
path = argv[1]; | |
// Open file | |
file = fopen(path, "r"); | |
if (file == NULL) { | |
printf("inmake: %s\n", strerror(errno)); | |
return -errno; | |
} | |
// Check for a shebang | |
if ((char)fgetc(file) == '#' && (char)fgetc(file) == '!') { | |
// Scan first line | |
do { c = (char)fgetc(file); } while (c != '\n' && c != EOF); | |
} else { | |
// Make sure first two characters get scanned | |
rewind(file); | |
} | |
// Scan non-whitespaces up to the first whitespace | |
do { c = (char)fgetc(file); } while ((c != ' ' && c != '\t') && c != EOF); | |
// Scan whitespaces up to the first non-whitespace | |
do { c = (char)fgetc(file); } while ((c == ' ' || c == '\t') && c != EOF); | |
// Newline or EOF reached | |
if (c == '\n' || c == EOF) { | |
fclose(file); | |
printf("inmake: no command line found\n"); | |
return -1; | |
} | |
// Scan and store command | |
p = command; | |
do { | |
*(p++) = c; | |
c = (char)fgetc(file); | |
} while (c != '\n' && c != EOF && (p - command) < COMMAND_SIZE); | |
*(p++) = 0; | |
// Close file | |
fclose(file); | |
printf("inmake: %s\n", command); | |
system(command); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment