static awk_value_t * do_writea(int nargs, awk_value_t *result) { awk_value_t filename, array; int fd = -1; uint32_t major = MAJOR; uint32_t minor = MINOR; assert(result != NULL); make_number(0.0, result); if (do_lint && nargs > 2) lintwarn(ext_id, _("writea: called with too many arguments")); if (nargs < 2) goto out; /* directory is first arg, array to dump is second */ if (! get_argument(0, AWK_STRING, & filename)) { fprintf(stderr, _("do_writea: argument 0 is not a string\n")); errno = EINVAL; goto done1; } if (! get_argument(1, AWK_ARRAY, & array)) { fprintf(stderr, _("do_writea: argument 1 is not an array\n")); errno = EINVAL; goto done1; } /* open the file, if error, set ERRNO and return */ fd = creat(filename.str_value.str, 0600); if (fd < 0) goto done1; if (write(fd, MAGIC, strlen(MAGIC)) != strlen(MAGIC)) goto done1; major = htonl(major); if (write(fd, & major, sizeof(major)) != sizeof(major)) goto done1; minor = htonl(minor); if (write(fd, & minor, sizeof(minor)) != sizeof(minor)) goto done1; if (write_array(fd, array.array_cookie)) { make_number(1.0, result); goto done0; } done1: update_ERRNO_int(errno); unlink(filename.str_value.str); done0: close(fd); out: return result; }
static awk_bool_t dir_take_control_of(awk_input_buf_t *iobuf) { DIR *dp; open_directory_t *the_dir; size_t size; errno = 0; #ifdef HAVE_FDOPENDIR dp = fdopendir(iobuf->fd); #else dp = opendir(iobuf->name); if (dp != NULL) iobuf->fd = dirfd(dp); #endif if (dp == NULL) { warning(ext_id, _("dir_take_control_of: opendir/fdopendir failed: %s"), strerror(errno)); update_ERRNO_int(errno); return awk_false; } emalloc(the_dir, open_directory_t *, sizeof(open_directory_t), "dir_take_control_of"); the_dir->dp = dp; size = sizeof(struct dirent) + 21 /* max digits in inode */ + 2 /* slashes */; emalloc(the_dir->buf, char *, size, "dir_take_control_of"); iobuf->opaque = the_dir; iobuf->get_record = dir_get_record; iobuf->close_func = dir_close; return awk_true; }
static void api_update_ERRNO_int(awk_ext_id_t id, int errno_val) { (void) id; update_ERRNO_int(errno_val); }
/* BEGIN { ERRNO = "" ret = test_errno() printf("test_errno() returned %d, ERRNO = %s\n", ret, ERRNO) print "" } */ static awk_value_t * test_errno(int nargs, awk_value_t *result) { assert(result != NULL); make_number(0.0, result); if (nargs != 0) { printf("test_errno: nargs not right (%d should be 0)\n", nargs); goto out; } update_ERRNO_int(ECHILD); make_number(1.0, result); out: return result; }
static awk_value_t * do_chdir(int nargs, awk_value_t *result) { awk_value_t newdir; int ret = -1; assert(result != NULL); if (do_lint && nargs != 1) lintwarn(ext_id, _("chdir: called with incorrect number of arguments, expecting 1")); if (get_argument(0, AWK_STRING, & newdir)) { ret = chdir(newdir.str_value.str); if (ret < 0) update_ERRNO_int(errno); } return make_number(ret, result); }
static awk_value_t * do_stat(int nargs, awk_value_t *result) { awk_value_t file_param, array_param; char *name; awk_array_t array; int ret; struct stat sbuf; assert(result != NULL); if (do_lint && nargs != 2) { lintwarn(ext_id, _("stat: called with wrong number of arguments")); return make_number(-1, result); } /* file is first arg, array to hold results is second */ if ( ! get_argument(0, AWK_STRING, & file_param) || ! get_argument(1, AWK_ARRAY, & array_param)) { warning(ext_id, _("stat: bad parameters")); return make_number(-1, result); } name = file_param.str_value.str; array = array_param.array_cookie; /* lstat the file, if error, set ERRNO and return */ ret = lstat(name, & sbuf); if (ret < 0) { update_ERRNO_int(errno); return make_number(ret, result); } ret = fill_stat_array(name, array, & sbuf); return make_number(ret, result); }
static awk_value_t * do_reada(int nargs, awk_value_t *result) { awk_value_t filename, array; int fd = -1; uint32_t major; uint32_t minor; char magic_buf[30]; assert(result != NULL); make_number(0.0, result); if (do_lint && nargs > 2) lintwarn(ext_id, _("reada: called with too many arguments")); if (nargs < 2) goto out; /* directory is first arg, array to read is second */ if (! get_argument(0, AWK_STRING, & filename)) { fprintf(stderr, _("do_reada: argument 0 is not a string\n")); errno = EINVAL; goto done1; } if (! get_argument(1, AWK_ARRAY, & array)) { fprintf(stderr, _("do_reada: argument 1 is not an array\n")); errno = EINVAL; goto done1; } fd = open(filename.str_value.str, O_RDONLY); if (fd < 0) goto done1; memset(magic_buf, '\0', sizeof(magic_buf)); if (read(fd, magic_buf, strlen(MAGIC)) != strlen(MAGIC)) { errno = EBADF; goto done1; } if (strcmp(magic_buf, MAGIC) != 0) { errno = EBADF; goto done1; } if (read(fd, & major, sizeof(major)) != sizeof(major)) { errno = EBADF; goto done1; } major = ntohl(major); if (major != MAJOR) { errno = EBADF; goto done1; } if (read(fd, & minor, sizeof(minor)) != sizeof(minor)) { /* read() sets errno */ goto done1; } minor = ntohl(minor); if (minor != MINOR) { errno = EBADF; goto done1; } if (! clear_array(array.array_cookie)) { errno = ENOMEM; fprintf(stderr, _("do_reada: clear_array failed\n")); goto done1; } if (read_array(fd, array.array_cookie)) { make_number(1.0, result); goto done0; } done1: update_ERRNO_int(errno); done0: close(fd); out: return result; }
static awk_value_t * do_fts(int nargs, awk_value_t *result) { awk_value_t pathlist, flagval, dest; awk_flat_array_t *path_array = NULL; char **pathvector = NULL; FTS *heirarchy; int flags; size_t i, count; int ret = -1; static const int mask = ( FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR | FTS_PHYSICAL | FTS_SEEDOT | FTS_XDEV); assert(result != NULL); fts_errors = 0; /* ensure a fresh start */ if (do_lint && nargs != 3) lintwarn(ext_id, _("fts: called with incorrect number of arguments, expecting 3")); if (! get_argument(0, AWK_ARRAY, & pathlist)) { warning(ext_id, _("fts: bad first parameter")); update_ERRNO_int(EINVAL); goto out; } if (! get_argument(1, AWK_NUMBER, & flagval)) { warning(ext_id, _("fts: bad second parameter")); update_ERRNO_int(EINVAL); goto out; } if (! get_argument(2, AWK_ARRAY, & dest)) { warning(ext_id, _("fts: bad third parameter")); update_ERRNO_int(EINVAL); goto out; } /* flatten pathlist */ if (! flatten_array(pathlist.array_cookie, & path_array)) { warning(ext_id, _("fts: could not flatten array\n")); goto out; } /* check the flags first, before the array flattening */ /* get flags */ flags = flagval.num_value; /* enforce physical or logical but not both, and not no_stat */ if ((flags & (FTS_PHYSICAL|FTS_LOGICAL)) == 0 || (flags & (FTS_PHYSICAL|FTS_LOGICAL)) == (FTS_PHYSICAL|FTS_LOGICAL)) { update_ERRNO_int(EINVAL); goto out; } if ((flags & FTS_NOSTAT) != 0) { flags &= ~FTS_NOSTAT; if (do_lint) lintwarn(ext_id, _("fts: ignoring sneaky FTS_NOSTAT flag. nyah, nyah, nyah.")); } flags &= mask; /* turn off anything else */ /* make pathvector */ count = path_array->count + 1; emalloc(pathvector, char **, count * sizeof(char *), "do_fts"); memset(pathvector, 0, count * sizeof(char *)); /* fill it in */ count--; /* ignore final NULL at end of vector */ for (i = 0; i < count; i++) pathvector[i] = path_array->elements[i].value.str_value.str; /* clear dest array */ if (! clear_array(dest.array_cookie)) { warning(ext_id, _("fts: clear_array failed\n")); goto out; } /* let's do it! */ if ((heirarchy = fts_open(pathvector, flags, NULL)) != NULL) { process(heirarchy, dest.array_cookie, (flags & FTS_SEEDOT) != 0); fts_close(heirarchy); if (fts_errors == 0) ret = 0; } else update_ERRNO_int(errno); out: if (pathvector != NULL) free(pathvector); if (path_array != NULL) (void) release_flattened_array(pathlist.array_cookie, path_array); return make_number(ret, result); }