static void load_READLINE(NODE *symbol, void *data) { sdata_t *sd = (sdata_t *) data; NODE *file, *tmp; FILE *fp; static char linebuf[BUFSIZ]; int i; bool long_line = false; if (! sd->load_file) /* non-existent SYS["readline"] or already loaded */ return; file = sd->filename; force_string(file); if (file->stlen == 0) return; assoc_clear(symbol); if ((fp = fopen(file->stptr, "r" )) == NULL) { warning(_("READLINE (%s): %s"), file->stptr, strerror(errno)); return; } for (i = 1; fgets(linebuf, sizeof(linebuf), fp ) != NULL; i++) { NODE **lhs; size_t sz; sz = strlen(linebuf); if (sz > 0 && linebuf[sz - 1] == '\n') { linebuf[sz - 1] = '\0'; sz--; if (long_line) { long_line = false; i--; continue; } } else if (long_line) { i--; continue; } else { if (do_lint) lintwarn(_("file `%s' does not end in newline or line # `%d' is too long"), file->stptr, i); long_line = true; } tmp = make_number(i); lhs = assoc_lookup(symbol, tmp); unref(tmp); unref(*lhs); *lhs = make_string(linebuf, sz); } fclose(fp); sd->load_file = false; /* don't load this file again */ }
static NODE * install_array(const char *name) { NODE *r; r = lookup(name); if (r == NULL) r = install_symbol(estrdup(name, strlen(name)), Node_var_array); switch (r->type) { case Node_var_new: r = force_array(r, false); /* fall through */ case Node_var_array: assoc_clear(r); break; default: fatal(_("`%s' is not an array"), name); } return r; }
static NODE * do_reada(int nargs) { NODE *file, *array; int ret; int fd; uint32_t major; uint32_t minor; char magic_buf[30]; if (do_lint && get_curfunc_arg_count() > 2) lintwarn("reada: called with too many arguments"); /* directory is first arg, array to dump is second */ file = get_scalar_argument(0, FALSE); array = get_array_argument(1, FALSE); (void) force_string(file); fd = open(file->stptr, O_RDONLY); if (fd < 0) { goto done1; } memset(magic_buf, '\0', sizeof(magic_buf)); if (read(fd, magic_buf, strlen(MAGIC)) != strlen(MAGIC)) { goto done1; } if (strcmp(magic_buf, MAGIC) != 0) { goto done1; } if (read(fd, & major, sizeof(major)) != sizeof(major)) { goto done1; } major = ntohl(major); if (major != MAJOR) { goto done1; } if (read(fd, & minor, sizeof(minor)) != sizeof(minor)) { goto done1; } minor = ntohl(minor); if (minor != MINOR) { goto done1; } assoc_clear(array); ret = read_array(fd, array); if (ret == 0) goto done0; done1: ret = -1; update_ERRNO(); done0: close(fd); /* Set the return value */ return make_number((AWKNUM) ret); }