static NODE * do_fork(int nargs) { int ret = -1; NODE **aptr; NODE *tmp; if (do_lint && get_curfunc_arg_count() > 0) lintwarn("fork: called with too many arguments"); ret = fork(); if (ret < 0) update_ERRNO(); else if (ret == 0) { /* update PROCINFO in the child */ aptr = assoc_lookup(PROCINFO_node, tmp = make_string("pid", 3), FALSE); (*aptr)->numbr = (AWKNUM) getpid(); unref(tmp); aptr = assoc_lookup(PROCINFO_node, tmp = make_string("ppid", 4), FALSE); (*aptr)->numbr = (AWKNUM) getppid(); unref(tmp); } /* Set the return value */ return make_number((AWKNUM) ret); }
static int sort_names(const void *a,const void *b,void *c) { struct assoc *orders = (struct assoc *)c; int *aa,*bb; aa = assoc_lookup(orders,*(char **)a); bb = assoc_lookup(orders,*(char **)b); if(!aa) { return 1; } if(!bb) { return -1; } if(*aa<*bb) { return 1; } if(*aa>*bb) { return -1; } return 0; }
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 */ }
int rotate_log(struct rotator *rr,char *filename) { char *dir,*base,*oldname,*newname; DIR *dirh; struct dirent des,*de; struct array *names; struct assoc *changes,*orders; int order,i,n; dirbasename(filename,&dir,&base); log_debug(("Rotating in '%s'",dir)); dirh = opendir(dir); if(!dirh) { log_error(("Could not rotate log file")); return 1; } names = array_create(type_free,0); changes = assoc_create(0,0,type_free,0); orders = assoc_create(0,0,type_free,0); while(1) { if(readdir_r(dirh,&des,&de)) { log_error(("Problem reading directory during log rotation")); break; } if(!de) { break; } order = matching_file(rr,filename,de->d_name,&newname); if(order == MATCH_IGNORE) { continue; } if(order == MATCH_DELETE) { log_debug(("delete %s",de->d_name)); unlink(de->d_name); } else { oldname = strdup(de->d_name); array_insert(names,oldname); assoc_set(changes,oldname,newname); assoc_set(orders,oldname,make_int(order)); } } if(closedir(dirh)) { log_error(("Could not closedir during log rotation!")); /* But continue: what else to do? */ } array_sort(names,sort_names,orders); n = array_length(names); for(i=0;i<n;i++) { oldname = array_index(names,i); newname = assoc_lookup(changes,oldname); log_warn(("name=%s -> %s",oldname,newname)); rename_file(rr,dir,oldname,newname); } assoc_release(changes); assoc_release(orders); array_release(names); free(dir); free(base); return 0; }
static IOBUF * nextfile() { static int i = 1; static int files = 0; static IOBUF *curfile = NULL; char *arg; char *cp; int fd = -1; if (curfile != NULL && curfile->cnt != EOF) return curfile; for (; i < (int) (ARGC_node->lnode->numbr); i++) { arg = (*assoc_lookup(ARGV_node, tmp_number((AWKNUM) i)))->stptr; if (*arg == '\0') continue; cp = strchr(arg, '='); if (cp != NULL) { *cp++ = '\0'; variable(arg)->var_value = make_string(cp, strlen(cp)); *--cp = '='; /* restore original text of ARGV */ } else { files++; if (STREQ(arg, "-")) fd = 0; else fd = devopen(arg, "r"); if (fd == -1) fatal("cannot open file `%s' for reading (%s)", arg, strerror(errno)); /* NOTREACHED */ /* This is a kludge. */ deref = FILENAME_node->var_value; do_deref(); FILENAME_node->var_value = make_string(arg, strlen(arg)); FNR_node->var_value->numbr = 0.0; i++; break; } } if (files == 0) { files++; /* no args. -- use stdin */ /* FILENAME is init'ed to "-" */ /* FNR is init'ed to 0 */ fd = 0; } if (fd == -1) return NULL; return curfile = iop_alloc(fd); }