static int get_wait(const char *arg) { if (strcasecmp(arg, STR_NONE) == 0) return ASYNC_CLOSING_WAIT_NONE; if (strcasecmp(arg, STR_INFINITE) == 0) return ASYNC_CLOSING_WAIT_INF; return get_numeric(arg); }
static void serial_set(char **arg, int opts) { struct serial_struct serinfo; int cmd; const char *word; int fd; fd = serial_open(*arg++, /*quiet:*/ false); if (fd < 0) exit(201); serial_ctl(fd, CTL_GET, &serinfo); if (opts & OPT_ZERO) serinfo.flags = 0; while (*arg) { int invert; word = *arg++; invert = (*word == '^'); word += invert; cmd = find_cmd(word); if (*arg == NULL && cmd_need_arg(cmd)) bb_error_msg_and_die(bb_msg_requires_arg, word); if (invert && !cmd_is_flag(cmd)) bb_error_msg_and_die("can't invert %s", word); switch (cmd) { case CMD_SPD_NORMAL: case CMD_SPD_HI: case CMD_SPD_VHI: case CMD_SPD_SHI: case CMD_SPD_WARP: case CMD_SPD_CUST: serinfo.flags &= ~ASYNC_SPD_MASK; /* fallthrough */ case CMD_FLAG_SAK: case CMD_FLAG_FOURPORT: case CMD_FLAG_NUP_NOTIFY: case CMD_FLAG_SKIP_TEST: case CMD_FLAG_AUTO_IRQ: case CMD_FLAG_SPLIT_TERMIOS: case CMD_FLAG_SESSION_LOCKOUT: case CMD_FLAG_PGRP_LOCKOUT: case CMD_FLAG_CALLOUT_NOHUP: case CMD_FLAG_LOW_LATENCY: if (invert) serinfo.flags &= ~setbits[cmd]; else serinfo.flags |= setbits[cmd]; break; case CMD_PORT: serinfo.port = get_numeric(*arg++); break; case CMD_IRQ: serinfo.irq = get_numeric(*arg++); break; case CMD_DIVISOR: serinfo.custom_divisor = get_numeric(*arg++); break; case CMD_UART: serinfo.type = get_uart(*arg++); break; case CMD_BASE: serinfo.baud_base = get_numeric(*arg++); break; case CMD_DELAY: serinfo.close_delay = get_numeric(*arg++); break; case CMD_WAIT: serinfo.closing_wait = get_wait(*arg++); break; case CMD_AUTOCONFIG: serial_ctl(fd, CTL_SET | CTL_CONFIG | CTL_GET, &serinfo); break; default: assert(0); } } serial_ctl(fd, CTL_SET | CTL_CLOSE, &serinfo); }
int main(int argc, char **argv){ unsigned num_mf = argc-1; // Allocate/open all files for(unsigned i=1; i<argc; i++){ char word[1024]; meanfiles[i-1] = alloc_meanfile(argv[i]); meanfile *mf = meanfiles[i-1]; while(read_word(mf->fd, word) == 1){ if(is_numeric(word)){ add_num(mf, get_numeric(word)); } } } // Ensure that all of the files have the same number of numbers unsigned g_numcount = meanfiles[0]->numcount; for(unsigned i=1; i<num_mf; i++){ meanfile *mf = meanfiles[i]; if(mf->numcount != g_numcount){ fprintf(stderr, "These files have different structures!\n"); exit(2); } } double *meannums = malloc(sizeof(double) * g_numcount); double *minnums = malloc(sizeof(double) * g_numcount); double *maxnums = malloc(sizeof(double) * g_numcount); for(unsigned i=0; i<g_numcount; i++){ // We'll take the arithmetic mean. double sum = 0; double g_min = meanfiles[0]->nums[i]; double g_max = meanfiles[0]->nums[i]; for(unsigned j=0; j<num_mf; j++){ sum += meanfiles[j]->nums[i]; g_min = min(g_min, meanfiles[j]->nums[i]); g_max = max(g_max, meanfiles[j]->nums[i]); } double mean = sum / num_mf; meannums[i] = mean; minnums[i] = g_min; maxnums[i] = g_max; } // Using the first file as a model, spit back out the averaged file. rewind(meanfiles[0]->fd); char word[1024]; char line[1024*1024]; unsigned current_num = 0; while(fgets(line, sizeof(line), meanfiles[0]->fd)){ int first = 1; char* token; const char seps[6] = " \n\r\t"; token = strtok(line, seps); while(token != NULL){ char tmp[strlen(token)]; strcpy(tmp, token); int had_comma = tmp[strlen(tmp)-1] == ','; if(had_comma) tmp[strlen(tmp)-1] = '\0'; int is_num = is_numeric(tmp); if(!first) printf(" "); if(is_num){ char *mr_str = getenv("MEAN_REDUCTION"); if(mr_str && strcmp(mr_str, "min")==0){ printf("%0.3f", minnums[current_num++]); }else if(mr_str && strcmp(mr_str, "max")==0){ printf("%0.3f", maxnums[current_num++]); }else{ printf("%0.3f", meannums[current_num++]); } }else{ printf("%s", tmp); } if(had_comma) printf(","); token = strtok(NULL, seps); first = 0; } printf("\n"); } return 0; }