static int do_mount(struct ENT *ent) { char *argv[8]; pid_t pid; int status; argv[0] = UNCONST("mount"); argv[1] = UNCONST("-o"); argv[2] = ENT_OPTS(*ent); argv[3] = UNCONST("-t"); argv[4] = ENT_TYPE(*ent); argv[5] = ENT_BLOCKDEVICE(*ent); argv[6] = ENT_FILE(*ent); argv[7] = NULL; switch (pid = vfork()) { case -1: eerrorx("%s: vfork: %s", applet, strerror(errno)); /* NOTREACHED */ case 0: execvp(argv[0], argv); eerror("%s: execv: %s", applet, strerror(errno)); _exit(EXIT_FAILURE); /* NOTREACHED */ default: waitpid(pid, &status, 0); if (WIFEXITED(status)) return WEXITSTATUS(status); else return -1; /* NOTREACHED */ } }
void swap_states(const Eigen::MatrixBase<Derived> &temp_const,const Eigen::MatrixBase<Derived> &mat_const, const Eigen::MatrixBase<Derived2> &indicies) { UNCONST(Derived,mat_const,mat); UNCONST(Derived,temp_const,temp); for(int i=0; i<indicies.rows(); i++) { temp.col(i) = mat.col(indicies(i)); } mat = temp; }
int open_socket(struct interface *iface, int protocol) { int s; union sockunion { struct sockaddr sa; struct sockaddr_in sin; struct sockaddr_ll sll; struct sockaddr_storage ss; } su; struct sock_fprog pf; int *fd; if ((s = socket(PF_PACKET, SOCK_DGRAM, htons(protocol))) == -1) return -1; memset(&su, 0, sizeof(su)); su.sll.sll_family = PF_PACKET; su.sll.sll_protocol = htons(protocol); /*if (!(su.sll.sll_ifindex = if_nametoindex(iface->name))) { errno = ENOENT; goto eexit; }*/ /* Install the DHCP filter */ memset(&pf, 0, sizeof(pf)); if (protocol == ETHERTYPE_ARP) { pf.filter = UNCONST(arp_bpf_filter); pf.len = arp_bpf_filter_len; } else { pf.filter = UNCONST(dhcp_bpf_filter); pf.len = dhcp_bpf_filter_len; } if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) != 0) goto eexit; if (set_cloexec(s) == -1) goto eexit; if (set_nonblock(s) == -1) goto eexit; if (bind(s, &su.sa, sizeof(su)) == -1) goto eexit; if (protocol == ETHERTYPE_ARP) fd = &iface->arp_fd; else fd = &iface->raw_fd; if (*fd != -1) close(*fd); *fd = s; return s; eexit: close(s); return -1; }
void init_output_dist(const Eigen::MatrixBase<Derived> &outputDist_const,const Eigen::MatrixBase<Derived2> &normalization_const) { UNCONST(Derived,outputDist_const,outputDist); UNCONST(Derived2,normalization_const,normalization); initMatrix(outputDist); //std::cout << outputDist << "\n\n"; normalization.setZero(); for(int i=0; i<outputDist.rows(); i++) { normalization += outputDist.row(i); } for(int i=0; i<outputDist.rows(); i++) { outputDist.row(i) = (outputDist.row(i).array()/normalization.array()).matrix(); } }
void * ascii_alloc(char *outopts) { struct termp *p; const char *toks[2]; char *v; if (NULL == (p = term_alloc(TERMENC_ASCII))) return(NULL); p->type = TERMTYPE_CHAR; p->letter = ascii_letter; p->begin = ascii_begin; p->end = ascii_end; p->endline = ascii_endline; p->advance = ascii_advance; toks[0] = "width"; toks[1] = NULL; while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case (0): p->defrmargin = (size_t)atoi(v); break; default: break; } /* Enforce a lower boundary. */ if (p->defrmargin < 58) p->defrmargin = 58; return(p); }
/* * Str_FindSubstring -- See if a string contains a particular substring. * * Input: * string String to search. * substring Substring to find in string. * * Results: If string contains substring, the return value is the location of * the first matching instance of substring in string. If string doesn't * contain substring, the return value is NULL. Matching is done on an exact * character-for-character basis with no wildcards or special characters. * * Side effects: None. */ char * Str_FindSubstring(const char *string, const char *substring) { const char *a, *b; /* * First scan quickly through the two strings looking for a single- * character match. When it's found, then compare the rest of the * substring. */ for (b = substring; *string != 0; string += 1) { if (*string != *b) continue; a = string; for (;;) { if (*b == 0) return UNCONST(string); if (*a++ != *b++) break; } b = substring; } return NULL; }
static void * ml_alloc(char *outopts, enum htmltype type) { struct html *h; const char *toks[4]; char *v; toks[0] = "style"; toks[1] = "man"; toks[2] = "includes"; toks[3] = NULL; h = mandoc_calloc(1, sizeof(struct html)); h->type = type; h->tags.head = NULL; h->symtab = mchars_alloc(); while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case (0): h->style = v; break; case (1): h->base_man = v; break; case (2): h->base_includes = v; break; default: break; } return(h); }
static int const_execvp(const char *file, const char *const *argv) { #define UNCONST(a) ((void *)(unsigned long)(const void *)(a)) return ::execvp(file, (char* const*)(UNCONST(argv))); #undef UNCONST }
static void dev_free (struct device *dev) { if (!dev) return; if (dev->sane.name) free (UNCONST(dev->sane.name)); if (dev->sane.vendor) free (UNCONST(dev->sane.vendor)); if (dev->sane.model) free (UNCONST(dev->sane.model)); if (dev->sane.type) free (UNCONST(dev->sane.type)); if (dev->data) free(dev->data); memset (dev, 0, sizeof (*dev)); free (dev); }
void handle_interface(int action, const char *ifname) { struct if_head *ifs; struct interface *ifp, *ifn, *ifl = NULL; const char * const argv[] = { ifname }; int i; if (action == -1) { ifp = find_interface(ifname); if (ifp != NULL) { ifp->options->options |= DHCPCD_DEPARTED; stop_interface(ifp); } return; } /* If running off an interface list, check it's in it. */ if (ifc) { for (i = 0; i < ifc; i++) if (strcmp(ifv[i], ifname) == 0) break; if (i >= ifc) return; } ifs = discover_interfaces(-1, UNCONST(argv)); TAILQ_FOREACH_SAFE(ifp, ifs, next, ifn) { if (strcmp(ifp->name, ifname) != 0) continue; /* Check if we already have the interface */ ifl = find_interface(ifp->name); if (ifl) { /* The flags and hwaddr could have changed */ ifl->flags = ifp->flags; ifl->hwlen = ifp->hwlen; if (ifp->hwlen != 0) memcpy(ifl->hwaddr, ifp->hwaddr, ifl->hwlen); } else { TAILQ_REMOVE(ifs, ifp, next); TAILQ_INSERT_TAIL(ifaces, ifp, next); } if (action == 1) { init_state(ifp, margc, margv); start_interface(ifp); } } /* Free our discovered list */ while ((ifp = TAILQ_FIRST(ifs))) { TAILQ_REMOVE(ifs, ifp, next); free_interface(ifp); } free(ifs); }
void base_layer<dType>::initMatrix(const Eigen::MatrixBase<Derived> &input_const) { UNCONST(Derived,input_const,input); dType lower = -1.0; //Lower bound for uniform dist dType upper = 1.0; //Upper bound for uniform dist boost::uniform_real<> distribution(lower,upper); for(int j=0; j<input.cols(); j++) { for(int i=0; i<input.rows(); i++) { input(i,j) = distribution(gen); } } }
void initMatrix(const Eigen::MatrixBase<Derived> &input_const) { UNCONST(Derived,input_const,input); precision lower = 0.0; //Lower bound for uniform dist precision upper = 5.0; //Upper bound for uniform dist boost::uniform_real<> distribution(lower,upper); for(int j=0; j<input.cols(); j++) { for(int i=0; i<input.rows(); i++) { input(i,j) = distribution(gen); } } }
enum mandoclevel mparse_readmem(struct mparse *curp, const void *buf, size_t len, const char *file) { struct buf blk; blk.buf = UNCONST(buf); blk.sz = len; mparse_parse_buffer(curp, blk, file); return(curp->file_status); }
/*- *----------------------------------------------------------------------- * Str_SYSVMatch -- * Check word against pattern for a match (% is wild), * * Input: * word Word to examine * pattern Pattern to examine against * len Number of characters to substitute * * Results: * Returns the beginning position of a match or null. The number * of characters matched is returned in len. * * Side Effects: * None * *----------------------------------------------------------------------- */ char * Str_SYSVMatch(const char *word, const char *pattern, int *len) { const char *p = pattern; const char *w = word; const char *m; if (*p == '\0') { /* Null pattern is the whole string */ *len = strlen(w); return UNCONST(w); } if ((m = strchr(p, '%')) != NULL) { /* check that the prefix matches */ for (; p != m && *w && *w == *p; w++, p++) continue; if (p != m) return NULL; /* No match */ if (*++p == '\0') { /* No more pattern, return the rest of the string */ *len = strlen(w); return UNCONST(w); } } m = w; /* Find a matching tail */ do if (strcmp(p, w) == 0) { *len = w - m; return UNCONST(m); } while (*w++ != '\0'); return NULL; }
static char * strskipwhite(const char *s) { if (s == NULL) return NULL; while (*s == ' ' || *s == '\t') { if (*s == '\0') return NULL; s++; } return UNCONST(s); }
static void setFilename(const char *Filename, int OwnsFilename) { /* Check if this is a new filename and therefore needs truncation. */ int NewFile = !__llvm_profile_CurrentFilename || (Filename && strcmp(Filename, __llvm_profile_CurrentFilename)); if (__llvm_profile_OwnsFilename) free(UNCONST(__llvm_profile_CurrentFilename)); __llvm_profile_CurrentFilename = Filename; __llvm_profile_OwnsFilename = OwnsFilename; /* If not a new file, append to support profiling multiple shared objects. */ if (NewFile) truncateCurrentFile(); }
/*- *----------------------------------------------------------------------- * Arch_LibOODate -- * Decide if a node with the OP_LIB attribute is out-of-date. Called * from Make_OODate to make its life easier. * * There are several ways for a library to be out-of-date that are * not available to ordinary files. In addition, there are ways * that are open to regular files that are not available to * libraries. A library that is only used as a source is never * considered out-of-date by itself. This does not preclude the * library's modification time from making its parent be out-of-date. * A library will be considered out-of-date for any of these reasons, * given that it is a target on a dependency line somewhere: * Its modification time is less than that of one of its * sources (gn->mtime < gn->cmgn->mtime). * Its modification time is greater than the time at which the * make began (i.e. it's been modified in the course * of the make, probably by archiving). * The modification time of one of its sources is greater than * the one of its RANLIBMAG member (i.e. its table of contents * is out-of-date). We don't compare of the archive time * vs. TOC time because they can be too close. In my * opinion we should not bother with the TOC at all since * this is used by 'ar' rules that affect the data contents * of the archive, not by ranlib rules, which affect the * TOC. * * Input: * gn The library's graph node * * Results: * TRUE if the library is out-of-date. FALSE otherwise. * * Side Effects: * The library will be hashed if it hasn't been already. * *----------------------------------------------------------------------- */ Boolean Arch_LibOODate(GNode *gn) { Boolean oodate; if (gn->type & OP_PHONY) { oodate = TRUE; } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) { oodate = FALSE; } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) || (gn->mtime > now) || (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) { oodate = TRUE; } else { #ifdef RANLIBMAG struct ar_hdr *arhPtr; /* Header for __.SYMDEF */ int modTimeTOC; /* The table-of-contents's mod time */ arhPtr = ArchStatMember(gn->path, UNCONST(RANLIBMAG), FALSE); if (arhPtr != NULL) { modTimeTOC = (int)strtol(arhPtr->AR_DATE, NULL, 10); if (DEBUG(ARCH) || DEBUG(MAKE)) { fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC)); } oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC); } else { /* * A library w/o a table of contents is out-of-date */ if (DEBUG(ARCH) || DEBUG(MAKE)) { fprintf(debug_file, "No t.o.c...."); } oodate = TRUE; } #else oodate = FALSE; #endif } return (oodate); }
Arch_TouchLib(GNode *gn) #endif { #ifdef RANLIBMAG FILE * arch; /* Stream open to archive */ struct ar_hdr arh; /* Header describing table of contents */ struct utimbuf times; /* Times for utime() call */ arch = ArchFindMember(gn->path, UNCONST(RANLIBMAG), &arh, "r+"); snprintf(arh.AR_DATE, sizeof(arh.AR_DATE), "%-12ld", (long) now); if (arch != NULL) { (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); fclose(arch); times.actime = times.modtime = now; utime(gn->path, ×); } #endif }
void base_layer<dType>::check_gradient(dType epsilon,const Eigen::MatrixBase<Derived2> ¶meter_const,const Eigen::MatrixBase<Derived> &grad) { UNCONST(Derived2, parameter_const, parameter); for(int i=0; i<grad.rows(); i++) { for(int j=0; j<grad.cols(); j++) { dType loss = 0; parameter(i,j)+= epsilon; loss = model->getError(false); parameter(i,j)+= -2*epsilon; loss-= model->getError(false); parameter(i,j)+= epsilon; if( (std::abs(grad(i,j) - loss/(2.0*epsilon))) > 1/1000.0 || (std::abs(grad(i,j) - loss/(2*epsilon))/(std::abs(grad(i,j)) + std::abs(loss/(2*epsilon)))) > 1/1000.0 ) { std::cout << "Gradient for gradient check: " << loss/(2*epsilon) << "\n"; std::cout << "My gradient: " << grad(i,j) << "\n"; std::cout << "Gradient difference: " << std::abs(grad(i,j) - loss/(2*epsilon)) << "\n"; std::cout << "Gradient difference (Equation 2): " << std::abs(grad(i,j) - loss/(2.0*epsilon))/(std::abs(grad(i,j)) + std::abs(loss/(2*epsilon)) ) << "\n\n"; } } } }
/* Find the end pointer of a string. */ static char * strend(const char *s) { s = strskipwhite(s); if (s == NULL) return NULL; if (*s != '"') return strchr(s, ' '); s++; for (; *s != '"' ; s++) { if (*s == '\0') return NULL; if (*s == '\\') { if (*(++s) == '\0') return NULL; } } return UNCONST(++s); }
pid_t fork_chdir_exec(const char *dir, const char *cmd, const char * const *argv, int *out) { static const char chdir_err[] = "Cannot change directory to \""; static const char dup2_err[] = "Cannot reassign stdout of child"; pid_t child; int output_pipe[2]; if (pipe(output_pipe) == -1) err(1, "Cannot create pipe for output"); if ((child = vfork()) != 0) { if (child == -1) { (void)close(output_pipe[0]); (void)close(output_pipe[1]); } else { *out = output_pipe[0]; (void)close(output_pipe[1]); } return child; } (void)close(output_pipe[0]); if (chdir(dir) == -1) { (void)write(STDERR_FILENO, chdir_err, sizeof(chdir_err) - 1); (void)write(STDERR_FILENO, dir, strlen(dir)); (void)write(STDERR_FILENO, "\".\n", 3); _exit(1); } if (output_pipe[1] != STDOUT_FILENO) { if (dup2(output_pipe[1], STDOUT_FILENO) == -1) { (void)write(STDERR_FILENO, dup2_err, sizeof(dup2_err) - 1); _exit(1); } (void)close(output_pipe[1]); } (void)execvp(cmd, UNCONST(argv)); _exit(1); /* NOTREACHED */ }
static void reset_options(struct device *dev) { dev->val[OPT_RESOLUTION].w = 150; dev->val[OPT_MODE].s = string_match(scan_modes, SANE_VALUE_SCAN_MODE_COLOR); /* if docs loaded in adf use it as default source, flatbed oterwise */ dev->val[OPT_SOURCE].s = UNCONST(doc_sources[(dev->doc_loaded)? 1 : 0]); dev->val[OPT_THRESHOLD].w = SANE_FIX(50); /* this is reported maximum window size, will be fixed later */ dev->win_x_range.min = SANE_FIX(0); dev->win_x_range.max = SANE_FIX((double)dev->max_win_width / PNT_PER_MM); dev->win_x_range.quant = SANE_FIX(1); dev->win_y_range.min = SANE_FIX(0); dev->win_y_range.max = SANE_FIX((double)dev->max_win_len / PNT_PER_MM); dev->win_y_range.quant = SANE_FIX(1); dev->val[OPT_SCAN_TL_X].w = dev->win_x_range.min; dev->val[OPT_SCAN_TL_Y].w = dev->win_y_range.min; dev->val[OPT_SCAN_BR_X].w = dev->win_x_range.max; dev->val[OPT_SCAN_BR_Y].w = dev->win_y_range.max; }
static void * ml_alloc(char *outopts, enum htmltype type) { struct html *h; const char *toks[4]; char *v; toks[0] = "style"; toks[1] = "man"; toks[2] = "includes"; toks[3] = NULL; h = calloc(1, sizeof(struct html)); if (NULL == h) { perror(NULL); exit((int)MANDOCLEVEL_SYSERR); } h->type = type; h->tags.head = NULL; h->symtab = chars_init(CHARS_HTML); while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case (0): h->style = v; break; case (1): h->base_man = v; break; case (2): h->base_includes = v; break; default: break; } return(h); }
void * html_alloc(const struct mchars *mchars, char *outopts) { struct html *h; const char *toks[5]; char *v; toks[0] = "style"; toks[1] = "man"; toks[2] = "includes"; toks[3] = "fragment"; toks[4] = NULL; h = mandoc_calloc(1, sizeof(struct html)); h->tags.head = NULL; h->symtab = mchars; while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case 0: h->style = v; break; case 1: h->base_man = v; break; case 2: h->base_includes = v; break; case 3: h->oflags |= HTML_FRAGMENT; break; default: break; } return(h); }
ssize_t if_sendrawpacket(const struct interface *ifp, int protocol, const void *data, size_t len) { struct iovec iov[2]; struct ether_header hw; int fd; const struct dhcp_state *state; memset(&hw, 0, ETHER_HDR_LEN); memset(&hw.ether_dhost, 0xff, ETHER_ADDR_LEN); hw.ether_type = htons(protocol); iov[0].iov_base = &hw; iov[0].iov_len = ETHER_HDR_LEN; iov[1].iov_base = UNCONST(data); iov[1].iov_len = len; state = D_CSTATE(ifp); if (protocol == ETHERTYPE_ARP) fd = state->arp_fd; else fd = state->raw_fd; return writev(fd, iov, 2); }
static struct termp * ascii_init(enum termenc enc, char *outopts) { const char *toks[4]; char *v; struct termp *p; p = mandoc_calloc(1, sizeof(struct termp)); p->tabwidth = 5; p->defrmargin = 78; p->begin = ascii_begin; p->end = ascii_end; p->hspan = ascii_hspan; p->type = TERMTYPE_CHAR; p->enc = TERMENC_ASCII; p->advance = ascii_advance; p->endline = ascii_endline; p->letter = ascii_letter; p->width = ascii_width; #ifdef USE_WCHAR if (TERMENC_ASCII != enc) { v = TERMENC_LOCALE == enc ? setlocale(LC_ALL, "") : setlocale(LC_CTYPE, "en_US.UTF-8"); if (NULL != v && MB_CUR_MAX > 1) { p->enc = enc; p->advance = locale_advance; p->endline = locale_endline; p->letter = locale_letter; p->width = locale_width; } } #endif toks[0] = "indent"; toks[1] = "width"; toks[2] = "mdoc"; toks[3] = NULL; while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case (0): p->defindent = (size_t)atoi(v); break; case (1): p->defrmargin = (size_t)atoi(v); break; case (2): /* * Temporary, undocumented mode * to imitate mdoc(7) output style. */ p->mdocstyle = 1; p->defindent = 5; break; default: break; } /* Enforce a lower boundary. */ if (p->defrmargin < 58) p->defrmargin = 58; return(p); }
int if_openrawsocket(struct interface *ifp, int protocol) { struct dhcp_state *state; int fd = -1; struct ifreq ifr; int ibuf_len = 0; size_t buf_len; struct bpf_version pv; struct bpf_program pf; #ifdef BIOCIMMEDIATE int flags; #endif #ifdef _PATH_BPF fd = open(_PATH_BPF, O_RDWR | O_CLOEXEC | O_NONBLOCK); #else char device[32]; int n = 0; do { snprintf(device, sizeof(device), "/dev/bpf%d", n++); fd = open(device, O_RDWR | O_CLOEXEC | O_NONBLOCK); } while (fd == -1 && errno == EBUSY); #endif if (fd == -1) return -1; state = D_STATE(ifp); memset(&pv, 0, sizeof(pv)); if (ioctl(fd, BIOCVERSION, &pv) == -1) goto eexit; if (pv.bv_major != BPF_MAJOR_VERSION || pv.bv_minor < BPF_MINOR_VERSION) { syslog(LOG_ERR, "BPF version mismatch - recompile"); goto eexit; } memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name)); if (ioctl(fd, BIOCSETIF, &ifr) == -1) goto eexit; /* Get the required BPF buffer length from the kernel. */ if (ioctl(fd, BIOCGBLEN, &ibuf_len) == -1) goto eexit; buf_len = (size_t)ibuf_len; if (state->buffer_size != buf_len) { free(state->buffer); state->buffer = malloc(buf_len); if (state->buffer == NULL) goto eexit; state->buffer_size = buf_len; state->buffer_len = state->buffer_pos = 0; } #ifdef BIOCIMMEDIATE flags = 1; if (ioctl(fd, BIOCIMMEDIATE, &flags) == -1) goto eexit; #endif /* Install the DHCP filter */ memset(&pf, 0, sizeof(pf)); if (protocol == ETHERTYPE_ARP) { pf.bf_insns = UNCONST(arp_bpf_filter); pf.bf_len = arp_bpf_filter_len; } else { pf.bf_insns = UNCONST(dhcp_bpf_filter); pf.bf_len = dhcp_bpf_filter_len; } if (ioctl(fd, BIOCSETF, &pf) == -1) goto eexit; return fd; eexit: free(state->buffer); state->buffer = NULL; close(fd); return -1; }
/*- *----------------------------------------------------------------------- * CompatRunCommand -- * Execute the next command for a target. If the command returns an * error, the node's made field is set to ERROR and creation stops. * * Input: * cmdp Command to execute * gnp Node from which the command came * * Results: * 0 if the command succeeded, 1 if an error occurred. * * Side Effects: * The node's 'made' field may be set to ERROR. * *----------------------------------------------------------------------- */ int CompatRunCommand(void *cmdp, void *gnp) { char *cmdStart; /* Start of expanded command */ char *cp, *bp; Boolean silent, /* Don't print command */ doIt; /* Execute even if -n */ volatile Boolean errCheck; /* Check errors */ WAIT_T reason; /* Reason for child's death */ int status; /* Description of child's death */ pid_t cpid; /* Child actually found */ pid_t retstat; /* Result of wait */ LstNode cmdNode; /* Node where current command is located */ const char ** volatile av; /* Argument vector for thing to exec */ char ** volatile mav;/* Copy of the argument vector for freeing */ int argc; /* Number of arguments in av or 0 if not * dynamically allocated */ Boolean local; /* TRUE if command should be executed * locally */ Boolean useShell; /* TRUE if command should be executed * using a shell */ char * volatile cmd = (char *)cmdp; GNode *gn = (GNode *)gnp; silent = gn->type & OP_SILENT; errCheck = !(gn->type & OP_IGNORE); doIt = FALSE; cmdNode = Lst_Member(gn->commands, cmd); cmdStart = Var_Subst(NULL, cmd, gn, FALSE); /* * brk_string will return an argv with a NULL in av[0], thus causing * execvp to choke and die horribly. Besides, how can we execute a null * command? In any case, we warn the user that the command expanded to * nothing (is this the right thing to do?). */ if (*cmdStart == '\0') { free(cmdStart); Error("%s expands to empty string", cmd); return(0); } cmd = cmdStart; Lst_Replace(cmdNode, cmdStart); if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { (void)Lst_AtEnd(ENDNode->commands, cmdStart); return(0); } if (strcmp(cmdStart, "...") == 0) { gn->type |= OP_SAVE_CMDS; return(0); } while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) { switch (*cmd) { case '@': silent = DEBUG(LOUD) ? FALSE : TRUE; break; case '-': errCheck = FALSE; break; case '+': doIt = TRUE; if (!meta[0]) /* we came here from jobs */ Compat_Init(); break; } cmd++; } while (isspace((unsigned char)*cmd)) cmd++; #if !defined(MAKE_NATIVE) /* * In a non-native build, the host environment might be weird enough * that it's necessary to go through a shell to get the correct * behaviour. Or perhaps the shell has been replaced with something * that does extra logging, and that should not be bypassed. */ useShell = TRUE; #else /* * Search for meta characters in the command. If there are no meta * characters, there's no need to execute a shell to execute the * command. */ for (cp = cmd; !meta[(unsigned char)*cp]; cp++) { continue; } useShell = (*cp != '\0'); #endif /* * Print the command before echoing if we're not supposed to be quiet for * this one. We also print the command if -n given. */ if (!silent || NoExecute(gn)) { printf("%s\n", cmd); fflush(stdout); } /* * If we're not supposed to execute any commands, this is as far as * we go... */ if (!doIt && NoExecute(gn)) { return (0); } if (DEBUG(JOB)) fprintf(debug_file, "Execute: '%s'\n", cmd); again: if (useShell) { /* * We need to pass the command off to the shell, typically * because the command contains a "meta" character. */ static const char *shargv[4]; shargv[0] = shellPath; /* * The following work for any of the builtin shell specs. */ if (DEBUG(SHELL)) shargv[1] = "-xc"; else shargv[1] = "-c"; shargv[2] = cmd; shargv[3] = NULL; av = shargv; argc = 0; bp = NULL; mav = NULL; } else { /* * No meta-characters, so no need to exec a shell. Break the command * into words to form an argument vector we can execute. */ mav = brk_string(cmd, &argc, TRUE, &bp); if (mav == NULL) { useShell = 1; goto again; } av = (const char **)mav; } local = TRUE; #ifdef USE_META if (useMeta) { meta_compat_start(); } #endif /* * Fork and execute the single command. If the fork fails, we abort. */ cpid = vFork(); if (cpid < 0) { Fatal("Could not fork"); } if (cpid == 0) { Check_Cwd(av); Var_ExportVars(); #ifdef USE_META if (useMeta) { meta_compat_child(); } #endif if (local) (void)execvp(av[0], (char *const *)UNCONST(av)); else (void)execv(av[0], (char *const *)UNCONST(av)); execError("exec", av[0]); _exit(1); } if (mav) free(mav); if (bp) free(bp); Lst_Replace(cmdNode, NULL); #ifdef USE_META if (useMeta) { meta_compat_parent(); } #endif /* * The child is off and running. Now all we can do is wait... */ while (1) { while ((retstat = wait(&reason)) != cpid) { if (retstat > 0) JobReapChild(retstat, reason, FALSE); /* not ours? */ if (retstat == -1 && errno != EINTR) { break; } } if (retstat > -1) { if (WIFSTOPPED(reason)) { status = WSTOPSIG(reason); /* stopped */ } else if (WIFEXITED(reason)) { status = WEXITSTATUS(reason); /* exited */ #if defined(USE_META) && defined(USE_FILEMON_ONCE) if (useMeta) { meta_cmd_finish(NULL); } #endif if (status != 0) { if (DEBUG(ERROR)) { fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ", gn->name); for (cp = cmd; *cp; ) { if (isspace((unsigned char)*cp)) { fprintf(debug_file, " "); while (isspace((unsigned char)*cp)) cp++; } else { fprintf(debug_file, "%c", *cp); cp++; } } fprintf(debug_file, "\n"); } printf("*** Error code %d", status); } } else { status = WTERMSIG(reason); /* signaled */ printf("*** Signal %d", status); } if (!WIFEXITED(reason) || (status != 0)) { if (errCheck) { #ifdef USE_META if (useMeta) { meta_job_error(NULL, gn, 0, status); } #endif gn->made = ERROR; if (keepgoing) { /* * Abort the current target, but let others * continue. */ printf(" (continuing)\n"); } } else { /* * Continue executing commands for this target. * If we return 0, this will happen... */ printf(" (ignored)\n"); status = 0; } } break; } else { Fatal("error in wait: %d: %s", retstat, strerror(errno)); /*NOTREACHED*/ } } free(cmdStart); return (status); }
static void write_single_job(int fd, const char *begin_entry, struct scan_job *job) { struct pkgname_hash *entry; const char *end_entry, *end_line; char *pkgname; int skip_current; struct iovec output[5]; const int iovcnt = 5; ssize_t expected; for (; begin_entry != NULL && begin_entry[0] != '\0';) { pkgname = pkgname_dup(begin_entry); if (pkgname == NULL) { warnx("pbulk-index output not recognized for %s", job->pkg_location); break; } if (pkgname_in_hash(pkgname)) { warnx("Duplicate package: %s", pkgname); skip_current = 1; } else { skip_current = 0; } end_entry = pbulk_item_end(begin_entry); if (end_entry == NULL) { free(pkgname); warnx("pbulk-index output not recognized for %s", job->pkg_location); break; } if (skip_current == 0) { end_line = strchr(begin_entry, '\n'); if (end_line == NULL || end_line > end_entry) errx(254, "internal error"); output[0].iov_base = UNCONST(begin_entry); expected = output[0].iov_len = end_line + 1 - begin_entry; output[1].iov_base = UNCONST("PKG_LOCATION="); output[1].iov_len = strlen(output[1].iov_base); expected += output[1].iov_len; output[2].iov_base = job->pkg_location; output[2].iov_len = strlen(output[2].iov_base); expected += output[2].iov_len; output[3].iov_base = UNCONST("\n"); output[3].iov_len = 1; expected += output[3].iov_len; output[4].iov_base = UNCONST(end_line + 1); output[4].iov_len = end_entry - end_line - 1; expected += output[4].iov_len; if (writev(fd, output, iovcnt) != expected) err(1, "writev failed"); entry = xmalloc(sizeof(*entry)); entry->next = pkgname_hash[HASH_ITEM(pkgname)]; entry->pkgname = pkgname; pkgname_hash[HASH_ITEM(pkgname)] = entry; } else { free(pkgname); } begin_entry = end_entry; } }
static struct termp * pspdf_alloc(char *outopts) { struct termp *p; unsigned int pagex, pagey; size_t marginx, marginy, lineheight; const char *toks[2]; const char *pp; char *v; p = mandoc_calloc(1, sizeof(struct termp)); p->enc = TERMENC_ASCII; p->ps = mandoc_calloc(1, sizeof(struct termp_ps)); p->advance = ps_advance; p->begin = ps_begin; p->end = ps_end; p->endline = ps_endline; p->hspan = ps_hspan; p->letter = ps_letter; p->width = ps_width; toks[0] = "paper"; toks[1] = NULL; pp = NULL; while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case (0): pp = v; break; default: break; } /* Default to US letter (millimetres). */ pagex = 216; pagey = 279; /* * The ISO-269 paper sizes can be calculated automatically, but * it would require bringing in -lm for pow() and I'd rather not * do that. So just do it the easy way for now. Since this * only happens once, I'm not terribly concerned. */ if (pp && strcasecmp(pp, "letter")) { if (0 == strcasecmp(pp, "a3")) { pagex = 297; pagey = 420; } else if (0 == strcasecmp(pp, "a4")) { pagex = 210; pagey = 297; } else if (0 == strcasecmp(pp, "a5")) { pagex = 148; pagey = 210; } else if (0 == strcasecmp(pp, "legal")) { pagex = 216; pagey = 356; } else if (2 != sscanf(pp, "%ux%u", &pagex, &pagey)) fprintf(stderr, "%s: Unknown paper\n", pp); } /* * This MUST be defined before any PNT2AFM or AFM2PNT * calculations occur. */ p->ps->scale = 11; /* Remember millimetres -> AFM units. */ pagex = PNT2AFM(p, ((double)pagex * 2.834)); pagey = PNT2AFM(p, ((double)pagey * 2.834)); /* Margins are 1/9 the page x and y. */ marginx = /* LINTED */ (size_t)((double)pagex / 9.0); marginy = /* LINTED */ (size_t)((double)pagey / 9.0); /* Line-height is 1.4em. */ lineheight = PNT2AFM(p, ((double)p->ps->scale * 1.4)); p->ps->width = (size_t)pagex; p->ps->height = (size_t)pagey; p->ps->header = pagey - (marginy / 2) - (lineheight / 2); p->ps->top = pagey - marginy; p->ps->footer = (marginy / 2) - (lineheight / 2); p->ps->bottom = marginy; p->ps->left = marginx; p->ps->lineheight = lineheight; p->defrmargin = pagex - (marginx * 2); return(p); }