static int do_redi_left(t_pipe *list_pipe, int i, char **env) { unsigned int j; char *buffer; char *tmp; restore_after_fork(); if (list_pipe->cmd[i]->is_redi_left == 1) return (do_simple_left_redi(list_pipe, i)); j = 0; buffer = NULL; tmp = NULL; while (is_good_string_redi(buffer, list_pipe->cmd[i]->redi_left) != 1) { my_putstr("> "); tmp = chose_read(env, 0); if ((buffer = realloc_it(buffer, tmp)) == NULL) return (-1); j += 1; } buffer = rm_good_string(buffer); return (make_two_redi_left(list_pipe, i, buffer)); }
struct json_metadata* get_json_metadata(const char* filename) { struct json_metadata* result = malloc(sizeof(struct json_metadata)); if (result == NULL) { fprintf(stderr, "%s: %d: malloc failed: %s\n", __FILE__, __LINE__, strerror(errno)); return 0; } int r; int eof_expected = 0; char *js = NULL; size_t jslen = 0; char buf[BUFSIZ]; FILE* fd = NULL; jsmn_parser p; jsmntok_t *tok = NULL; size_t tokcount = 10; /* Prepare parser */ jsmn_init(&p); /* Allocate some tokens as a start */ tok = malloc(sizeof(*tok) * tokcount); if (tok == NULL) { fprintf(stderr, "malloc(): error:%s\n", strerror(errno)); goto errexit; } fd = fopen(filename, "rb"); if (fd == NULL) { fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno)); goto errexit; } for (;;) { /* Read another chunk */ r = fread(buf, 1, sizeof(buf), fd); if (r < 0) { fprintf(stderr, "fread(): %s\n", strerror(errno)); goto errexit; } if (r == 0) { if (eof_expected != 0) { break; } else { fprintf(stderr, "fread(): unexpected EOF\n"); goto errexit; } } js = realloc_it(js, jslen + r + 1); if (js == NULL) { goto errexit; } strncpy(js + jslen, buf, r); jslen = jslen + r; again: r = jsmn_parse(&p, js, jslen, tok, tokcount); if (r < 0) { if (r == JSMN_ERROR_NOMEM) { tokcount = tokcount * 2; tok = realloc_it(tok, sizeof(*tok) * tokcount); if (tok == NULL) { goto errexit; } goto again; } } else { eof_expected = 1; } } fclose(fd); result->tok = tok; result->js = js; return result; errexit: fprintf(stderr, "error during json metadata parsing\n"); if (fd) { fclose(fd); fd = NULL; } if (tok) { free(tok); tok = NULL; } if (js) { free(js); js = NULL; } if (result) { free(result); result = NULL; } return NULL; }
int main() { int r; int eof_expected = 0; char *js = NULL; size_t jslen = 0; char buf[BUFSIZ]; jsmn_parser p; jsmntok_t *tok; size_t tokcount = 2; /* Prepare parser */ jsmn_init(&p); /* Allocate some tokens as a start */ tok = malloc(sizeof(*tok) * tokcount); if (tok == NULL) { fprintf(stderr, "malloc(): errno=%d\n", errno); return(3); } for (;;) { /* Read another chunk */ r = fread(buf, 1, sizeof(buf), stdin); if (r < 0) { fprintf(stderr, "fread(): %d, errno=%d\n", r, errno); return 1; } if (r == 0) { if (eof_expected != 0) { return 0; } else { fprintf(stderr, "fread(): unexpected EOF\n"); return 2; } } js = realloc_it(js, jslen + r + 1); if (js == NULL) { return 3; } } strncpy(js + jslen, buf, r); jslen = jslen + r; again: r = jsmn_parse(&p, js, jslen, tok, tokcount); if (r < 0) { if (r == JSMN_ERROR_NOMEM) { tokcount = tokcount * 2; tok = realloc_it(tok, sizeof(*tok) * tokcount); if (tok == NULL) { return 3; } goto again; } } else { dump(js, tok, p.toknext, 0); eof_expected = 1; } return(0); }