static void wifi_site_survey(const char *ifname, int print) { char *s = malloc(IW_SCAN_MAX_DATA); int ret; int socket_id; struct iwreq wrq; char *line, *start; iwpriv(ifname, "SiteSurvey", ""); sleep(5); memset(s, 0x00, IW_SCAN_MAX_DATA); strcpy(wrq.ifr_name, ifname); wrq.u.data.length = IW_SCAN_MAX_DATA; wrq.u.data.pointer = s; wrq.u.data.flags = 0; socket_id = socket(AF_INET, SOCK_DGRAM, 0); ret = ioctl(socket_id, RTPRIV_IOCTL_GSITESURVEY, &wrq); close(socket_id); if (ret != 0) goto out; if (wrq.u.data.length < 1) goto out; /* ioctl result starts with a newline, for some reason */ start = s; while (*start == '\n') start++; line = strtok((char *)start, "\n"); line = strtok(NULL, "\n"); survey_count = 0; while(line && (survey_count < 64)) { next_field(&line, st[survey_count].channel, sizeof(st->channel)); next_field(&line, st[survey_count].ssid, sizeof(st->ssid)); next_field(&line, st[survey_count].bssid, sizeof(st->bssid)); next_field(&line, st[survey_count].security, sizeof(st->security)); line = strtok(NULL, "\n"); st[survey_count].crypto = strstr(st[survey_count].security, "/"); if (st[survey_count].crypto) { *st[survey_count].crypto = '\0'; st[survey_count].crypto++; syslog(LOG_INFO, "Found network - %s %s %s %s\n", st[survey_count].channel, st[survey_count].ssid, st[survey_count].bssid, st[survey_count].security); } else { st[survey_count].crypto = ""; } survey_count++; } if (survey_count == 0 && !print) syslog(LOG_INFO, "No results"); out: free(s); }
static void process_i386_cpu_flag (FILE *table, char *flag, int macro, const char *comma, const char *indent) { char *str, *next, *last; bitfield flags [ARRAY_SIZE (cpu_flags)]; /* Copy the default cpu flags. */ memcpy (flags, cpu_flags, sizeof (cpu_flags)); if (strcasecmp (flag, "unknown") == 0) { unsigned int i; /* We turn on everything except for cpu64 in case of CPU_UNKNOWN_FLAGS. */ for (i = 0; i < ARRAY_SIZE (flags); i++) if (flags[i].position != Cpu64) flags[i].value = 1; } else if (strcmp (flag, "0")) { last = flag + strlen (flag); for (next = flag; next && next < last; ) { str = next_field (next, '|', &next); if (str) set_bitfield (str, flags, ARRAY_SIZE (flags)); } } output_cpu_flags (table, flags, ARRAY_SIZE (flags), macro, comma, indent); }
bool hfp_context_get_range(struct hfp_context *context, uint32_t *min, uint32_t *max) { uint32_t l, h; uint32_t start; start = context->offset; if (!hfp_context_get_number(context, &l)) goto failed; if (context->data[context->offset] != '-') goto failed; context->offset++; if (!hfp_context_get_number(context, &h)) goto failed; *min = l; *max = h; next_field(context); return true; failed: context->offset = start; return false; }
static void output_int64_size (int64_t size) { char buf[LONGEST_HUMAN_READABLE]; int hopts = human_round_to_nearest|human_autoscale|human_base_1024|human_SI; int r; next_field (); if (!csv) { if (!human) r = printf ("%10" PRIi64, size); else r = printf ("%10s", human_readable ((uintmax_t) size, buf, hopts, 1, 1)); } else { /* CSV is the same as non-CSV but we don't need to right-align. */ if (!human) r = printf ("%" PRIi64, size); else r = printf ("%s", human_readable ((uintmax_t) size, buf, hopts, 1, 1)); } if (r < 0) { perror ("printf"); exit (EXIT_FAILURE); } }
static int set_bitfield_from_cpu_flag_init (char *f, bitfield *array, int value, unsigned int size, int lineno) { char *str, *next, *last; unsigned int i; for (i = 0; i < ARRAY_SIZE (cpu_flag_init); i++) if (strcmp (cpu_flag_init[i].name, f) == 0) { /* Turn on selective bits. */ char *init = xstrdup (cpu_flag_init[i].init); last = init + strlen (init); for (next = init; next && next < last; ) { str = next_field (next, '|', &next, last); if (str) set_bitfield (str, array, 1, size, lineno); } free (init); return 0; } return -1; }
bool hfp_context_get_number(struct hfp_context *context, unsigned int *val) { unsigned int i; int tmp = 0; skip_whitespace(context); i = context->offset; while (context->data[i] >= '0' && context->data[i] <= '9') tmp = tmp * 10 + context->data[i++] - '0'; if (i == context->offset) return false; if (val) *val = tmp; context->offset = i; skip_whitespace(context); next_field(context); return true; }
static void output_int64_uid (int64_t i) { next_field (); /* csv doesn't need escaping */ if (printf ("%4" PRIi64, i) < 0) error (EXIT_FAILURE, errno, "printf"); }
static void output_int64_perms (int64_t i) { next_field (); /* csv doesn't need escaping */ if (printf ("%04" PRIo64, (uint64_t) i) < 0) error (EXIT_FAILURE, errno, "printf"); }
static int parse_line( char* in, M3u_Playlist::entry_t& entry ) { int result = 0; // file entry.file = in; entry.type = ""; in = parse_filename( in, entry ); // track entry.track = -1; entry.decimal_track = 0; in = parse_track( in, entry, &result ); // name entry.name = in; in = parse_name( in ); // time entry.length = -1; in = parse_time( in, &entry.length, &result ); // loop entry.intro = -1; entry.loop = -1; if ( *in == '-' ) { entry.loop = entry.length; in++; } else { in = parse_time_( in, &entry.loop ); if ( entry.loop >= 0 ) { entry.intro = entry.length - entry.loop; if ( *in == '-' ) // trailing '-' means that intro length was specified { in++; entry.intro = entry.loop; entry.loop = entry.length - entry.intro; } } } in = next_field( in, &result ); // fade entry.fade = -1; in = parse_time( in, &entry.fade, &result ); // repeat entry.repeat = -1; in = parse_int( in, &entry.repeat, &result ); return result; }
static void output_int64_uid (int64_t i) { next_field (); /* csv doesn't need escaping */ if (printf (csv ? "%" PRIi64 : "%4" PRIi64, i) < 0) { perror ("printf"); exit (EXIT_FAILURE); } }
static void output_int64_perms (int64_t i) { next_field (); /* csv doesn't need escaping */ if (printf ("%04" PRIo64, i) < 0) { perror ("printf"); exit (EXIT_FAILURE); } }
/* If we can check usage without entering kernel, do so. */ static int check_usage(int log, const char *modname) { FILE *module_list; char line[10240], name[64]; unsigned long size, refs; int scanned; module_list = fopen("/proc/modules", "r"); if (!module_list) { if (errno == ENOENT) /* /proc may not be mounted. */ return 0; fatal(log, "can't open /proc/modules: %s\n", strerror(errno)); } while (fgets(line, sizeof(line)-1, module_list) != NULL) { if (strchr(line, '\n') == NULL) { fatal(log, "V. v. long line broke rmmod.\n"); exit(1); } scanned = sscanf(line, "%s %lu %lu", name, &size, &refs); if (strcmp(name, modname) != 0) continue; if (scanned < 2) fatal(log, "Unknown format in /proc/modules: %s\n", line); if (scanned == 2) fatal(log, "Kernel does not have unload support.\n"); /* Hand it fields 3 onwards. */ if (refs != 0) mod_in_use(log, modname, next_field(next_field(next_field(line)))); goto out; } error(log, "Module %s does not exist in /proc/modules\n", modname); refs = 1; out: fclose(module_list); return (refs == 0) ? 0 : -EINVAL; }
static void output_string (const char *s) { next_field (); if (!csv) { print_no_quoting: if (printf ("%s", s) < 0) { perror ("printf"); exit (EXIT_FAILURE); } } else { /* Quote CSV string without requiring an external module. */ size_t i, len; int needs_quoting = 0; len = strlen (s); for (i = 0; i < len; ++i) { if (s[i] == ' ' || s[i] == '"' || s[i] == '\n' || s[i] == ',') { needs_quoting = 1; break; } } if (!needs_quoting) goto print_no_quoting; /* Quoting for CSV fields. */ if (putchar ('"') == EOF) { perror ("putchar"); exit (EXIT_FAILURE); } for (i = 0; i < len; ++i) { if (s[i] == '"') { if (putchar ('"') == EOF || putchar ('"') == EOF) { perror ("putchar"); exit (EXIT_FAILURE); } } else { if (putchar (s[i]) == EOF) { perror ("putchar"); exit (EXIT_FAILURE); } } } if (putchar ('"') == EOF) { perror ("putchar"); exit (EXIT_FAILURE); } } }
void hfp_context_skip_field(struct hfp_context *context) { const char *data = context->data; unsigned int offset = context->offset; while (data[offset] != '\0' && data[offset] != ',') offset++; context->offset = offset; next_field(context); }
static void output_string_link (const char *link) { if (csv) output_string (link); else { next_field (); if (printf ("-> %s", link) < 0) error (EXIT_FAILURE, errno, "printf"); } }
static bool next_field_uintmax(char** current, uintmax_t* result) { char* id_str = next_field(current); if ( !id_str ) return false; char* id_endptr; uintmax_t id_umax = strtoumax(id_str, &id_endptr, 10); if ( *id_endptr ) return false; *result = id_umax; return true; }
static void output_int64_dev (int64_t i) { dev_t dev = i; next_field (); /* csv doesn't need escaping */ if (printf ("%ju:%ju", (uintmax_t) major (dev), (uintmax_t) minor (dev)) < 0) error (EXIT_FAILURE, errno, "printf"); }
static void output_int64_dev (int64_t i) { dev_t dev = i; next_field (); /* csv doesn't need escaping */ if (printf ("%d:%d", major (dev), minor (dev)) < 0) { perror ("printf"); exit (EXIT_FAILURE); } }
bool hfp_context_close_container(struct hfp_context *context) { skip_whitespace(context); /* The list shall be followed by a right parenthesis (")" V250 5.7.3.1*/ if (context->data[context->offset] != ')') return false; context->offset++; next_field(context); return true; }
static void output_int64_time (int64_t i) { int r; next_field (); /* csv doesn't need escaping */ if (time_t_output) { switch (time_relative) { case 0: /* --time-t */ r = printf ("%10" PRIi64, i); break; case 1: /* --time-relative */ r = printf ("%8" PRIi64, now - i); break; case 2: /* --time-days */ default: r = printf ("%3" PRIi64, (now - i) / 86400); break; } } else { time_t t = (time_t) i; char buf[64]; struct tm *tm; tm = localtime (&t); if (tm == NULL) { perror ("localtime"); exit (EXIT_FAILURE); } if (strftime (buf, sizeof buf, "%F %T", tm) == 0) { perror ("strftime"); exit (EXIT_FAILURE); } r = printf ("%s", buf); } if (r < 0) { perror ("printf"); exit (EXIT_FAILURE); } }
static void process_i386_opcode_modifier (FILE *table, char *mod) { char *str, *next, *last; bitfield modifiers [ARRAY_SIZE (opcode_modifiers)]; /* Copy the default opcode modifier. */ memcpy (modifiers, opcode_modifiers, sizeof (modifiers)); if (strcmp (mod, "0")) { last = mod + strlen (mod); for (next = mod; next && next < last; ) { str = next_field (next, '|', &next); if (str) set_bitfield (str, modifiers, ARRAY_SIZE (modifiers)); } } output_opcode_modifier (table, modifiers, ARRAY_SIZE (modifiers)); }
bool hfp_context_get_string(struct hfp_context *context, char *buf, uint8_t len) { int i = 0; const char *data = context->data; unsigned int offset; skip_whitespace(context); if (data[context->offset] != '"') return false; offset = context->offset; offset++; while (data[offset] != '\0' && data[offset] != '"') { if (i == len) return false; buf[i++] = data[offset]; offset++; } if (i == len) return false; buf[i] = '\0'; if (data[offset] == '"') offset++; else return false; context->offset = offset; skip_whitespace(context); next_field(context); return true; }
void exec(const char* fn, const void* buf, size_t n) { (void)fn; if(n == dims[0]*dims[1]*dims[2]*sizeof(float)) { float rng[2]; range((const float*)buf, n/sizeof(float), &rng[0], &rng[1]); TRACE(nek, "[%zu] range: [%14.7g %14.7g]", rank(), rng[0], rng[1]); if(quant == NULL) { quant = calloc(dims[0]*dims[1]*dims[2], sizeof(uint16_t)); } if(fld != 4) { /* 4 is always blank, it seems. */ memset(quant, 0, dims[0]*dims[1]*dims[2]*sizeof(uint16_t)); quantize((const float*)buf, n/sizeof(float), rng[0], rng[1], quant); char fname[256]; snprintf(fname, 256, "%zu.fld%zu.png", ts, fld); writepng(fname, quant, dims[0], dims[1]); } next_field(); } }
static void process_i386_operand_type (FILE *table, char *op, int macro, const char *indent, int lineno) { char *str, *next, *last; bitfield types [ARRAY_SIZE (operand_types)]; /* Copy the default operand type. */ memcpy (types, operand_types, sizeof (types)); if (strcmp (op, "0")) { last = op + strlen (op); for (next = op; next && next < last; ) { str = next_field (next, '|', &next, last); if (str) set_bitfield (str, types, 1, ARRAY_SIZE (types), lineno); } } output_operand_type (table, types, ARRAY_SIZE (types), macro, indent); }
bool hfp_context_get_unquoted_string(struct hfp_context *context, char *buf, uint8_t len) { const char *data = context->data; unsigned int offset; int i = 0; char c; skip_whitespace(context); c = data[context->offset]; if (c == '"' || c == ')' || c == '(') return false; offset = context->offset; while (data[offset] != '\0' && data[offset] != ',' && data[offset] != ')') { if (i == len) return false; buf[i++] = data[offset]; offset++; } if (i == len) return false; buf[i] = '\0'; context->offset = offset; next_field(context); return true; }
static char* parse_track( char* in, M3u_Playlist::entry_t& entry, int* result ) { if ( *in == '$' ) { in++; int n = 0; while ( 1 ) { int h = from_hex_char( *in ); if ( h > 15 ) break; in++; n = n * 16 + h; entry.track = n; } } else { in = parse_int_( in, &entry.track ); if ( entry.track >= 0 ) entry.decimal_track = 1; } return next_field( in, result ); }
SEXP insnp_new(const SEXP Filenames, const SEXP Sample_id, const SEXP Snp_id, const SEXP Diploid, const SEXP Fields, const SEXP Codes, const SEXP Threshold, const SEXP Lower, const SEXP Sep, const SEXP Comment, const SEXP Skip, const SEXP Simplify, const SEXP Verbose, const SEXP In_order, const SEXP Every){ /* Process arguments */ if (TYPEOF(Verbose)!=LGLSXP) error("Argument type error: Verbose"); if (length(Verbose)>1) warning("Only first element of argument used: Verbose"); int verbose = *INTEGER(Verbose); if (TYPEOF(In_order)!=LGLSXP) error("Argument type error: In_order"); if (length(In_order)>1) warning("Only first element of argument used: In_order"); int in_order = *INTEGER(In_order); if (TYPEOF(Filenames)!=STRSXP) error("Argument type error: Filenames"); int Nfile = length(Filenames); int Nsample=0; if (TYPEOF(Sample_id)==STRSXP) Nsample = length(Sample_id); else if (TYPEOF(Sample_id)!=NILSXP) error("Argument type error: Sample_id"); int Nsnp=0; if (TYPEOF(Snp_id)==STRSXP) Nsnp = length(Snp_id); else if (TYPEOF(Snp_id)!=NILSXP) error("Argument type error: Snp_id"); /* file is 1 = sample, 2 = snp, 0 = irrelevant */ int file_is = 0; if (!Nsample) { if (Nsnp) { Nsample = Nfile; Rprintf("Each file is assumed to concern a single sample\n"); Rprintf("(Sample IDs are assumed to be included in filenames)\n"); file_is = 1; } else error("No sample or SNP IDs specified"); } else if (!Nsnp) { Nsnp = Nfile; Rprintf("Each file is assumed to concern a single SNP\n"); Rprintf("(SNP IDs are assumed to be included in filenames)\n"); file_is = 2; } /* If not in order, set up hash tables */ index_db sample_index = NULL; index_db snp_index = NULL; if (!in_order) { if (file_is != 1) sample_index = create_name_index(Sample_id); if (file_is != 2) snp_index = create_name_index(Snp_id); } int *diploid=NULL; if (TYPEOF(Diploid)==LGLSXP) { if (length(Diploid)!=Nsample) error("Argument length error: diploid argument"); diploid = LOGICAL(Diploid); } else if (TYPEOF(Diploid)!=NILSXP) error("Argument type error: diploid argument"); if (TYPEOF(Fields)!=INTSXP) error("Argument type error: Fields"); int *fields = INTEGER(Fields); int fsamp=0, fsnp=0, fgt=0, fa1=0, fa2=0, fconf=0; SEXP Fnames = getAttrib(Fields, R_NamesSymbol); if (TYPEOF(Fnames)==NILSXP) error("Argument error: Fields argument has no names"); int fmax = 0; int Nfield = length(Fields); for (int i=0; i<Nfield; i++) { const char *fname = CHAR(STRING_ELT(Fnames, i)); int fi = fields[i]; if (!strcmp(fname, "sample")) fsamp = fi; else if (!strcmp(fname, "snp")) fsnp = fi; else if (!strcmp(fname, "genotype")) fgt = fi; else if (!strcmp(fname, "allele1")) fa1 = fi; else if (!strcmp(fname, "allele2")) fa2 = fi; else if (!strcmp(fname, "confidence")) fconf = fi; else error("Unrecognized input field name: %s", fname); if (fi>fmax) fmax = fi; } if (verbose) { Rprintf("Reading one call per input line\n"); if (fsamp) { Rprintf(" Sample id is in field %d", fsamp); if (file_is==1) Rprintf(" (ignored)\n"); else Rprintf("\n"); } if (fsnp) { Rprintf(" SNP id is in field %d", fsnp); if (file_is==2) Rprintf(" (ignored)\n"); else Rprintf("\n"); } if (fgt) Rprintf(" Genotype is in field %d\n", fgt); if (fa1) Rprintf(" Allele 1 is in field %d\n", fa1); if (fa2) Rprintf(" Allele 2 is in field %d\n", fa2); if (fconf) Rprintf(" Confidence score is in field %d\n", fconf); } if (file_is==1) fsamp = 0; else if (file_is==2) fsnp = 0; /* Allele or genotype coding? */ int gcoding; if (fgt) { if (fa1 || fa2) error("Coding must be by genotype OR allele, not both"); gcoding = 1; } else { if (!(fa1 && fa2)) error("No genotype or allele field(s) specified"); if (!(fa1 && fa2)) error("Field positions for both alleles must be specified"); gcoding = 0; } int nuc = 0; if (TYPEOF(Codes)!=STRSXP) error("Argument type error: Codes"); if (length(Codes)==1) { SEXP Code = STRING_ELT(Codes, 0); const char *code = CHAR(Code); if (!strcmp(code, "nucleotide")) nuc = 1; else error("Unrecognized coding: %s", code); } else { int ncode = length(Codes); if (gcoding) { if (diploid) { if (ncode!=5) { if (ncode==3) warning("Genotype coding for X: haploid genotypes are assumed to be coded as homozygous"); else error("Genotype coding for X.snp: three or five genotype codes must be specified"); } } else { if (ncode!=3) error("Genotype coding: three genotype codes must be specified"); } } else { if (ncode!=2) error("Allele coding: two allele codes must be specified"); } } if (TYPEOF(Threshold)==NILSXP && !fconf) error("Argument type error: no threshold argument"); if (TYPEOF(Threshold)!=REALSXP) error("Argument type error: Threshold"); double threshold = *REAL(Threshold); if (fconf && threshold==NA_REAL) error("Confidence score is read but no threshold is set"); if (TYPEOF(Lower)!=LGLSXP) error("Argument type error: Lower"); if (length(Lower)>1) warning("Only first element of argument used: Lower"); int lower = *INTEGER(Lower); char sep = ' '; if (TYPEOF(Sep)==STRSXP) { if (length(Sep)>1) warning("Only first element of argument used: Sep"); const char *c = CHAR(STRING_ELT(Sep, 0)); if (strlen(c)>1) warning("Only first character used: Sep"); sep = c[0]; } else if (TYPEOF(Sep)!=NILSXP) error("Argument type error: Sep"); char comment = (char) 0; if (TYPEOF(Comment)==STRSXP) { if (length(Sep)>1) warning("Only first element of argument used: Comment"); const char *c = CHAR(STRING_ELT(Comment, 0)); if (strlen(c)>1) warning("Only first character used: Comment"); comment = c[0]; } else if (TYPEOF(Comment)!=NILSXP) error("Argument type error: Comment"); int skip = 0; if (TYPEOF(Skip)==INTSXP) { if (length(Skip)>1) warning("Only first element used: Skip"); skip = INTEGER(Skip)[0]; } else if (TYPEOF(Skip)!=NILSXP) error("Argument type error: Skip"); if (TYPEOF(Simplify)!=LGLSXP) error("Argument type error: Simplify"); if (length(Simplify)>2) error("Argument length error: Simplify"); int *simplify = INTEGER(Simplify); if (length(Simplify)==1) simplify[1] = simplify[0]; int every=0; if (TYPEOF(Every)==INTSXP) { if (length(Every)>1) warning("Only first element used: Every"); every = INTEGER(Every)[0]; } else if (TYPEOF(Every)!=NILSXP) error("Argument type error: Every"); /* Create output object and initialise to zero */ if (verbose) { if (diploid) Rprintf("Reading XSnpMatrix with %d rows and %d columns\n", Nsample, Nsnp); else Rprintf("Reading SnpMatrix with %d rows and %d columns\n", Nsample, Nsnp); } SEXP Result, Dimnames, Package, Class; PROTECT(Result = allocMatrix(RAWSXP, Nsample, Nsnp)); PROTECT(Dimnames = allocVector(VECSXP, 2)); if (simplify[0]) { SET_VECTOR_ELT(Dimnames, 0, simplify_names(file_is==1? Filenames: Sample_id)); } else { SET_VECTOR_ELT(Dimnames, 0, duplicate(file_is==1? Filenames: Sample_id)); } if (simplify[1]) { SET_VECTOR_ELT(Dimnames, 1, simplify_names(file_is==2? Filenames: Snp_id)); } else { SET_VECTOR_ELT(Dimnames, 1, duplicate(file_is==2? Filenames: Snp_id)); } setAttrib(Result, R_DimNamesSymbol, Dimnames); /* Class */ PROTECT(Class = allocVector(STRSXP, 1)); if (diploid) { R_do_slot_assign(Result, mkString("diploid"), Diploid); SET_STRING_ELT(Class, 0, mkChar("XSnpMatrix")); } else { SET_STRING_ELT(Class, 0, mkChar("SnpMatrix")); } PROTECT(Package = allocVector(STRSXP, 1)); SET_STRING_ELT(Package, 0, mkChar("snpStats")); setAttrib(Class, install("package"), Package); classgets(Result, Class); SET_S4_OBJECT(Result); unsigned char *result = RAW(Result); memset(result, 0x00, Nsample*Nsnp); /* Read in data */ char field[MAX_FLD]; int Naccept = 0, Nreject = 0, Nocall = 0, Nskipped = 0, Nxerror = 0; int i_this = 0, j_this = 0; const char *this_sample=NULL, *this_snp=NULL; if (fsamp) { this_sample = CHAR(STRING_ELT(Sample_id, 0)); } if (fsnp) { this_snp = CHAR(STRING_ELT(Snp_id, 0)); } if (verbose) { Rprintf(" Cumulative totals\n"); Rprintf(" -----------------------------------\n"); Rprintf(" File Line Accepted Rejected No call Skipped File name\n"); } /* slowest varying 0 = don't know, 1 = sample, 2 = snp */ int slowest = file_is, last=Nsample*Nsnp-1; int advance = 0, finished=0; for (int f=0; f<Nfile; f++) { /* Open input file */ const char *filename = CHAR(STRING_ELT(Filenames, f)); if (verbose) { int lfn = strlen(filename); if (lfn > 20) { Rprintf("%59s...%-17s\r", "", filename+lfn-17); } else Rprintf("%59s%-20s\r", "", filename); } gzFile infile = gzopen(filename, "rb"); if (!infile) { warning("Failure to open input file: %s", filename); continue; } int fterm = 2, line = 0, found_in_file = 0; /* Skip any header lines */ for (int i=0; i<skip; i++) { line++; if (skip_to_eol(infile)==3) error("End-of-file reached on line %d", line); } Nskipped += skip; /* Read data lines */ while (fterm!=3) { /* Read a line */ line++; if (verbose && every && !(line % every)) Rprintf("%8d %8d %10d %8d %8d %8d\r", f+1, line, Naccept, Nreject, Nocall, Nskipped); int genotype=0, allele1=0, allele2=0; /* wanted is coded as: 1 if this call is to be accepted 0 if it is not wanted (or a comment line) -1 if rejected due to insufficient confidence -2 coded as no-call */ int wanted = 1; char sampid[MAX_FLD], snpid[MAX_FLD]; char gtype1[MAX_FLD], gtype2[MAX_FLD]; char cscore[MAX_FLD]; sampid[0] = snpid[0] = cscore[0] = (char) 0; for (int r=1; (r<=fmax); r++) { fterm = next_field(infile, sep, comment, '_', field, MAX_FLD); if (!fterm) error("Field overflow: line %d, field %d", line, r); if ((fterm>1) && (r<fmax)) { if (r==1) { if(!field[0]) { /* Empty line or comment line */ wanted = 0; if (fterm==2) { Nskipped++; continue; } else break; } } error("Incomplete line: %d (last field read: %d = %s)", line, r, field); } /* Save fields */ if (r==fsamp) { strncpy(sampid, field, MAX_FLD-1); } else if (r==fsnp) { strncpy(snpid, field, MAX_FLD-1); } else if (r==fconf) { strncpy(cscore, field, MAX_FLD-1); } else if (r==fgt) strncpy(gtype1, field, MAX_FLD-1); else if (r==fa1) strncpy(gtype1, field, MAX_FLD-1); else if (r==fa2) strncpy(gtype2, field, MAX_FLD-1); else { /* skip field */ } } /* Matches: for (int r=1; (r<=fmax); r++) { */ if (!wanted) continue; /* Discard any further fields */ if (fterm<2) { fterm = skip_to_eol(infile); } /* Find next target and check matches */ int match_sample, match_snp; if (in_order) { /* Advance to next target read */ if (advance) { /* If unknown, determine sort order by seeing which indicator has changed */ if (!slowest) { if (strcmp(this_sample, sampid)) slowest = 2; /* sample fastest, SNP slowest */ else if (strcmp(this_snp, snpid)) slowest = 1; /* SNP fastest, sample slowest */ else error("Error in input file sort order"); } /* Now advance fastest varying indicator */ if (slowest==1) { j_this++; if (j_this==Nsnp) { j_this = 0; if (fsamp) { i_this++; if (i_this==Nsample) { finished = 1; break; } else this_sample = CHAR(STRING_ELT(Sample_id, i_this)); } } this_snp = CHAR(STRING_ELT(Snp_id, j_this)); } else { i_this++; if (i_this==Nsample) { i_this = 0; if (fsnp) { j_this++; if (j_this==Nsnp) { finished = 1; break; } else this_snp = CHAR(STRING_ELT(Snp_id, j_this)); } } this_sample = CHAR(STRING_ELT(Sample_id, i_this)); } } /* Does current line match current target? */ match_sample = file_is==1 || !strcmp(this_sample, sampid); match_snp = file_is==2 || !strcmp(this_snp, snpid); } else { /* Not in order */ if (file_is!=1) { if (this_sample && strcmp(this_sample, sampid)) i_this = index_lookup(sample_index, sampid); if (i_this >= 0) { this_sample = CHAR(STRING_ELT(Sample_id, i_this)); match_sample = 1; } else { this_sample = NULL; match_sample = 0; } } else { match_sample = 1; i_this = f; } if (file_is!=2) { if (this_snp && strcmp(this_snp, snpid)) j_this = index_lookup(snp_index, snpid); if (j_this >= 0) { this_snp = CHAR(STRING_ELT(Snp_id, j_this)); match_snp = 1; } else { this_snp = NULL; match_snp = 0; } } else { match_snp = 1; j_this = f; } } if (match_sample && match_snp) { /* Next target read found in file(s) */ found_in_file++; int ij_this = j_this*Nsample + i_this; finished = (ij_this==last); /* Check confidence score */ if (fconf) { double conf; if (sscanf(cscore, "%lf", &conf)!=1) error("Failure to read confidence score: line %d", line); if ((lower && conf<threshold) || (!lower && conf>threshold)) { wanted = -1; Nreject++; if (finished) break; else advance = 1; continue; } } /* Decode genotype */ int which; if (gcoding) { if (nuc) { switch (strlen(gtype1)) { case 0: allele1 = allele2 = 0; break; case 1: allele1 = allele2 = nucleotide(gtype1[0]); break; case 2: allele1 = nucleotide(gtype1[0]); allele2 = nucleotide(gtype1[1]); break; default: error("Nucleotide coded genotype should be 2 character string: line %d", line); } } else { which = str_inlist(Codes, gtype1); if (!which) genotype = 0; else if (which>3) genotype = 2*which - 7; else genotype = which; } } else { if (nuc) { allele1 = nucleotide(gtype1[0]); allele2 = nucleotide(gtype2[0]); } else { allele1 = str_inlist(Codes, gtype1); allele2 = str_inlist(Codes, gtype2); } } /* Successful read, store genotype in result[i_this, j_this] */ if (nuc || !gcoding) { if (allele2 < allele1) { genotype = allele2; allele2 = allele1; allele1 = genotype; } if (allele1 && allele2) genotype = allele1 + (allele2*(allele2-1))/2; else genotype = 0; } if (genotype) { if (diploid && !diploid[i_this] && (genotype==2)) Nxerror++; else { Naccept++; result[ij_this] = (unsigned char) genotype; } } else { wanted = -2; Nocall++; } /* Flag need to advance to next target */ advance = 1; } /* matches if (match_sample && match_snp) { */ else { Nskipped++; /* Flag no advance to next target */ advance = 0; } if (finished) break; } /* matches: while (fterm!=3) { */ if (file_is==1) i_this++; if (file_is==2) j_this++; if (verbose) { Rprintf("%8d %8d %10d %8d %8d %8d\r", f+1, line, Naccept, Nreject, Nocall, Nskipped); } if(!found_in_file) warning("No calls found in file %s", filename); gzclose(infile); if (finished) break; } /* Warnings */ if (in_order && !finished) warning("End of data reached before search completed"); if (Nxerror) warning("%d haploid genotypes were coded as heterozygous; set to NA", Nxerror); if (Nskipped) warning("%d lines of input file(s) were skipped", Nskipped); /* Report */ if (verbose) Rprintf("\n"); Rprintf("%d genotypes successfully read\n", Naccept); if (Nreject) Rprintf("%d genotypes were rejected due to low confidence\n", Nreject); if (Nocall) Rprintf("%d genotypes were not called\n", Nocall); if (Nsample*Nsnp > Naccept+Nxerror+Nreject+Nocall) Rprintf("%d genotypes could not be found on input file(s)\n", Nsample*Nsnp - Naccept - Nreject - Nxerror-Nocall); if (nuc) { if (verbose) Rprintf("Recasting and checking nucleotide coding\n"); int none_snps = recode_snp(result, Nsample, Nsnp); if (none_snps) { Rprintf("%d polymorphisms were not SNPs and have been set to NA ", none_snps); Rprintf("(see warnings for details)\n"); } } UNPROTECT(4); /* Destroy hash indexes */ if (sample_index) index_destroy(sample_index); if (snp_index) index_destroy(snp_index); return Result; }
static struct passwd *getpwcommon(void *context, const char *arg, int which) { char *p, **list; struct passwd *pw; /* Get the response and copy the first entry into a buffer. */ list = hesiod_resolve(context, arg, which ? "uid" : "passwd"); if (!list) return NULL; p = malloc(strlen(*list) + 1); if (!p) { hesiod_free_list(context, list); errno = ENOMEM; return NULL; } strcpy(p, *list); hesiod_free_list(context, list); /* Allocate memory for the result. */ pw = (struct passwd *) malloc(sizeof(struct passwd)); if (!pw) { free(p); errno = ENOMEM; return NULL; } /* Split up buf into fields. */ pw->pw_name = p; p = next_field(p); pw->pw_passwd = p; p = next_field(p); pw->pw_uid = atoi(p); p = next_field(p); pw->pw_gid = atoi(p); p = next_field(p); pw->pw_gecos = p; p = next_field(p); pw->pw_dir = p; p = next_field(p); pw->pw_shell = p; while (*p && *p != '\n') p++; *p = 0; #ifdef HAVE_PW_QUOTA pw->pw_quota = 0; #endif #ifdef HAVE_PW_COMMENT pw->pw_comment = ""; #endif #ifdef HAVE_PW_CLASS pw->pw_class = ""; #endif #ifdef HAVE_PW_CHANGE pw->pw_change = 0; #endif #ifdef HAVE_PW_EXPIRE pw->pw_expire = 0; #endif return pw; }
Code_t ZParseNotice(char *buffer, int len, ZNotice_t *notice) { char *ptr, *end; unsigned long temp; int maj, numfields, i; #ifndef __LINE__ #define __LINE__ -1 #endif #define BAD_PACKET(what) return _bad_packet(__LINE__, ptr, notice, what) (void) memset((char *)notice, 0, sizeof(ZNotice_t)); ptr = buffer; end = buffer+len; notice->z_packet = buffer; notice->z_version = ptr; if (strncmp(ptr, ZVERSIONHDR, sizeof(ZVERSIONHDR) - 1)) return (ZERR_VERS); ptr += sizeof(ZVERSIONHDR) - 1; if (!*ptr) BAD_PACKET("null version string"); maj = atoi(ptr); if (maj != ZVERSIONMAJOR) return (ZERR_VERS); ptr = next_field(ptr, end); if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD) BAD_PACKET("parsing num_hdr_fields"); numfields = temp; notice->z_num_hdr_fields = numfields; ptr = next_field(ptr, end); /*XXX 3 */ numfields -= 2; /* numfields, version, and checksum */ if (numfields < 0) BAD_PACKET("no header fields"); if (numfields && ptr < end) { if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD) BAD_PACKET("parsing kind"); notice->z_kind = (ZNotice_Kind_t)temp; numfields--; ptr = next_field(ptr, end); } else BAD_PACKET("missing kind"); if (numfields && ptr < end) { if (ZReadAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid, sizeof(ZUnique_Id_t)) == ZERR_BADFIELD) BAD_PACKET("parsing uid"); notice->z_time.tv_sec = ntohl((u_long) notice->z_uid.tv.tv_sec); notice->z_time.tv_usec = ntohl((u_long) notice->z_uid.tv.tv_usec); numfields--; ptr = next_field(ptr, end); } else BAD_PACKET("missing uid"); if (numfields && ptr < end) { if (ZReadAscii16(ptr, end-ptr, ¬ice->z_port) == ZERR_BADFIELD) BAD_PACKET("parsing port"); notice->z_port = htons(notice->z_port); numfields--; ptr = next_field(ptr, end); } else BAD_PACKET("missing port"); if (numfields && ptr < end) { if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD) BAD_PACKET("parsing auth"); notice->z_auth = temp; numfields--; ptr = next_field(ptr, end); } else BAD_PACKET("missing auth"); notice->z_checked_auth = ZAUTH_UNSET; if (numfields && ptr < end) { if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD) BAD_PACKET("parsing authenticator length"); notice->z_authent_len = temp; numfields--; ptr = next_field(ptr, end); } else BAD_PACKET("missing authenticator length"); if (numfields && ptr < end) { notice->z_ascii_authent = ptr; numfields--; ptr = next_field(ptr, end); } else BAD_PACKET("missing authenticator field"); if (numfields && ptr < end) { notice->z_class = ptr; numfields--; ptr = next_field(ptr, end); } else notice->z_class = ""; if (numfields && ptr < end) { notice->z_class_inst = ptr; numfields--; ptr = next_field(ptr, end); } else notice->z_class_inst = ""; if (numfields && ptr < end) { notice->z_opcode = ptr; numfields--; ptr = next_field(ptr, end); } else notice->z_opcode = ""; if (numfields && ptr < end) { notice->z_sender = ptr; numfields--; ptr = next_field(ptr, end); } else notice->z_sender = ""; if (numfields && ptr < end) { notice->z_recipient = ptr; numfields--; ptr = next_field(ptr, end); } else notice->z_recipient = ""; if (numfields && ptr < end) { notice->z_default_format = ptr; numfields--; ptr = next_field(ptr, end); } else notice->z_default_format = ""; if (numfields && ptr < end) { notice->z_ascii_checksum = ptr; if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD) notice->z_checksum = 0; else notice->z_checksum = temp; numfields--; ptr = next_field (ptr, end); } else { notice->z_ascii_checksum = ""; notice->z_checksum = 0; } if (numfields && ptr < end) { notice->z_multinotice = ptr; numfields--; ptr = next_field(ptr, end); } else notice->z_multinotice = ""; if (numfields && ptr < end) { if (ZReadAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid, sizeof(ZUnique_Id_t)) == ZERR_BADFIELD) BAD_PACKET("parsing multiuid"); notice->z_time.tv_sec = ntohl((u_long) notice->z_multiuid.tv.tv_sec); notice->z_time.tv_usec = ntohl((u_long) notice->z_multiuid.tv.tv_usec); numfields--; ptr = next_field(ptr, end); } else notice->z_multiuid = notice->z_uid; if (numfields && ptr < end) { /* we will take it on faith that ipv6 addresses are longer than ipv4 addresses */ unsigned char addrbuf[sizeof(notice->z_sender_sockaddr.ip6.sin6_addr)]; int alen; /* because we're paranoid about naughtily misformatted packets */ if (memchr(ptr, '\0', end - ptr) == NULL) BAD_PACKET("unterminated address field"); if (*ptr == 'Z') { if (ZReadZcode((unsigned char *)ptr, addrbuf, sizeof(addrbuf), &alen) == ZERR_BADFIELD) BAD_PACKET("parsing Zcode address"); } else { alen = sizeof(notice->z_sender_sockaddr.ip4.sin_addr); if (ZReadAscii(ptr, end - ptr, (unsigned char *)addrbuf, alen) == ZERR_BADFIELD) BAD_PACKET("parsing NetASCII address"); } if (alen == sizeof(notice->z_sender_sockaddr.ip6.sin6_addr)) { notice->z_sender_sockaddr.ip6.sin6_family = AF_INET6; memcpy(¬ice->z_sender_sockaddr.ip6.sin6_addr, addrbuf, alen); #ifdef HAVE_SOCKADDR_IN6_SIN6_LEN notice->z_sender_sockaddr.ip6.sin6_len = sizeof(notice->z_sender_sockaddr.ip6); #endif } else if (alen == sizeof(notice->z_sender_sockaddr.ip4.sin_addr)) { notice->z_sender_sockaddr.ip4.sin_family = AF_INET; memcpy(¬ice->z_sender_sockaddr.ip4.sin_addr, addrbuf, alen); #ifdef HAVE_SOCKADDR_IN_SIN_LEN notice->z_sender_sockaddr.ip4.sin_len = sizeof(notice->z_sender_sockaddr.ip4); #endif } else BAD_PACKET("address claims to be neither IPv4 or IPv6"); numfields--; ptr = next_field(ptr, end); } else { memset(¬ice->z_sender_sockaddr, 0, sizeof notice->z_sender_sockaddr); notice->z_sender_sockaddr.ip4.sin_family = AF_INET; notice->z_sender_sockaddr.ip4.sin_addr = notice->z_uid.zuid_addr; #ifdef HAVE_SOCKADDR_IN_SIN_LEN notice->z_sender_sockaddr.ip4.sin_len = sizeof(notice->z_sender_sockaddr.ip4); #endif } if (numfields && ptr < end) { if (ZReadAscii16(ptr, end-ptr, ¬ice->z_charset) == ZERR_BADFIELD) BAD_PACKET("parsing charset"); notice->z_charset = htons(notice->z_charset); numfields--; ptr = next_field(ptr, end); } else notice->z_charset = ZCHARSET_UNKNOWN; for (i=0;ptr < end && i<Z_MAXOTHERFIELDS && numfields;i++,numfields--) { notice->z_other_fields[i] = ptr; ptr = next_field(ptr, end); } notice->z_num_other_fields = i; for (i=0;ptr < end && numfields;numfields--) ptr = next_field(ptr, end); if (numfields || *(ptr - 1) != '\0') BAD_PACKET("end of headers"); notice->z_message = (void *)ptr; notice->z_message_len = len-(ptr-buffer); return (ZERR_NONE); }
static char* parse_time( char* in, int* out, int* result ) { return next_field( parse_time_( in, out ), result ); }