diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-14 22:45:22 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-14 22:45:22 +0200 |
commit | 1a847e6f3f1727571d6c185de52b418cc5746f4f (patch) | |
tree | b92fdd17b1230ecbe89dd5a56de0bf73415b9276 | |
parent | 8a2d1b4928150c361413f4b294e016ee5011a017 (diff) | |
download | attocc-1a847e6f3f1727571d6c185de52b418cc5746f4f.tar attocc-1a847e6f3f1727571d6c185de52b418cc5746f4f.tar.bz2 attocc-1a847e6f3f1727571d6c185de52b418cc5746f4f.tar.zst |
can read file
-rw-r--r-- | attocc.c | 73 |
1 files changed, 57 insertions, 16 deletions
@@ -1,6 +1,8 @@ +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #define NUM_KEYWORDS 32 static char *KEYWORDS[NUM_KEYWORDS] = { @@ -82,23 +84,15 @@ struct token *tokenize(char *source) { if (remaining >= 2) { if (p[0] == '/' && p[1] == '/') { p += 2; - while (*p) { - if (*p++ == '\n') { - break; - } - } + char c; + while ((c = *p++) && c != '\n') + ; continue; } else if (p[0] == '/' && p[1] == '*') { p += 2; - char last = '\0'; - while (*p) { - if (*p == '/' && last == '*') { - p++; - break; - } - last = *p; - p++; - } + char c, d; + while ((d = c, c = *p++) && c == '/' && d == '*') + ; continue; } } @@ -112,7 +106,7 @@ struct token *tokenize(char *source) { tokens = realloc(tokens, sizeof(struct token) * num_tokens); if (!tokens) { - fprintf(stderr, "realloc failed"); + fprintf(stderr, "realloc failed\n"); return NULL; } struct token *new_token = &tokens[num_tokens - 1]; @@ -131,4 +125,51 @@ struct token *tokenize(char *source) { return tokens; } -int main() {} +int main(int argc, char **argv) { + if (argc < 3) { + fprintf(stderr, "USAGE:\n\tattocc <input> <output>\n"); + return 1; + } + char *input = argv[1]; + char *output = argv[2]; + + int input_fd = open(input, O_RDONLY | O_CLOEXEC); + if (input_fd < 0) { + perror("cannot open input"); + return 1; + } + + int output_fd = open(output, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0640); + if (output_fd < 0) { + perror("cannot open input"); + return 1; + } + + int source_len = 0; + char *source = NULL; + int size; + char buffer[4096]; + + while ((size = read(input_fd, &buffer, 4096))) { + if (size < 0) { + perror("cannot read source"); + return 1; + } + + source_len += size; + source = realloc(source, source_len + 1); + if (!source) { + fprintf(stderr, "malloc failed\n"); + return 1; + } + + for (int i = 0; i < size; i++) { + source[source_len - size + i] = buffer[i]; + } + } + source[source_len] = '\0'; + + printf("%s", source); + + return 0; +} |