uint16_t frame_rx(uint8_t packet[], uint16_t length, uint16_t src_addr, uint16_t freeze_time_1w, uint16_t freeze_time_0w) { // get other node's time from message uint16_t other_time_1w = (uint16_t)(packet[2] | packet[3] << 8); uint16_t other_time_0w = (uint16_t)(packet[4] | packet[5] << 8); // apply local offset add_offset(offset, &freeze_time_1w, &freeze_time_0w); // remove transmission delay add_offset(-35, &freeze_time_1w, &freeze_time_0w); // check packet purpose switch (packet[1]) { case 's': adjust_skew(freeze_time_1w, freeze_time_0w, other_time_1w, other_time_0w); case 'o': increase_offset(freeze_time_1w, freeze_time_0w, other_time_1w, other_time_0w); printf("RUN, %u, %u, ", src_addr, node_addr); printf("%u, %u, ", other_time_1w, other_time_0w); printf("%u, %u\n", freeze_time_1w, freeze_time_0w); case 'n': sync_algorithm = packet[1]; break; case 'm': printf("MON, %u, %u, ", src_addr, node_addr); printf("%u, %u, ", other_time_1w, other_time_0w); printf("%u, %u, %i\n", freeze_time_1w, freeze_time_0w, freeze_time_0w-other_time_0w); } return 1; }
/** * Copies source- into destination array, * starting from the given offset. * * This is the BACKWARD version. * * @param p0 the destination array * @param p1 the source array * @param p2 the abstraction * @param p3 the count * @param p4 the destination index * @param p5 the source index */ void copy_array_backward(void* p0, void* p1, void* p2, void* p3, void* p4, void* p5) { // CAUTION! These null pointer comparisons are IMPORTANT, in order to // avoid a system crash if one or both of the two arrays are null! // All other copying functions are based on this copier function, // so that checking for null pointer right here suffices. if (p1 != *NULL_POINTER_MEMORY_MODEL) { if (p0 != *NULL_POINTER_MEMORY_MODEL) { log_terminated_message((void*) DEBUG_LEVEL_LOG_MODEL, (void*) L"Copy array backward."); // The destination array, source array. // CAUTION! They HAVE TO BE initialised with p0 and p1, // since an offset is added below. void* d = p0; void* s = p1; // Add offset. add_offset((void*) &d, p2, p4); add_offset((void*) &s, p2, p5); copy_array_elements_backward(d, s, p2, p3); } else { log_terminated_message((void*) ERROR_LEVEL_LOG_MODEL, (void*) L"Could not copy array backward. The destination array is null."); } } else { log_terminated_message((void*) ERROR_LEVEL_LOG_MODEL, (void*) L"Could not copy array backward. The source array is null."); } }
/** * gom_command_builder_build_select: * @builder: (in): A #GomCommandBuilder. * * Builds a #GomCommand that will select all the rows matching the current * query params. * * Returns: (transfer full): A #GomCommand. */ GomCommand * gom_command_builder_build_select (GomCommandBuilder *builder) { GomCommandBuilderPrivate *priv; GomResourceClass *klass; GomCommand *command; GString *str; g_return_val_if_fail(GOM_IS_COMMAND_BUILDER(builder), NULL); priv = builder->priv; klass = g_type_class_ref(priv->resource_type); str = g_string_new("SELECT "); add_fields(str, klass); add_from(str, klass); add_joins(str, klass); add_m2m(str, klass, priv->m2m_table, priv->m2m_type); add_where(str, priv->m2m_type, priv->m2m_table, priv->filter); add_limit(str, priv->limit); add_offset(str, priv->offset); command = g_object_new(GOM_TYPE_COMMAND, "adapter", priv->adapter, "sql", str->str, NULL); bind_params(command, priv->filter); g_type_class_unref(klass); g_string_free(str, TRUE); return command; }
static struct wipe_desc * get_offset_from_probe(struct wipe_desc *wp, blkid_probe pr, int zap) { const char *off, *type, *usage, *mag; size_t len; if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) == 0 && blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL) == 0 && blkid_probe_lookup_value(pr, "SBMAGIC", &mag, &len) == 0 && blkid_probe_lookup_value(pr, "USAGE", &usage, NULL) == 0) { loff_t offset = strtoll(off, NULL, 10); const char *p; wp = add_offset(wp, offset, zap); if (!wp) return NULL; wp->usage = xstrdup(usage); wp->type = xstrdup(type); wp->magic = xmalloc(len); memcpy(wp->magic, mag, len); wp->len = len; if (blkid_probe_lookup_value(pr, "LABEL", &p, NULL) == 0) wp->label = xstrdup(p); if (blkid_probe_lookup_value(pr, "UUID", &p, NULL) == 0) wp->uuid = xstrdup(p); } return wp; }
assignment_node *assignment_set::add_child(assignment_node *current, const vector<pair<string, span>>& assignment, size_t offset) { current->children.emplace_back(new assignment_node(current)); current = current->children.back().get(); current->assignment = assignment; add_offset(current->assignment, offset); return current; }
void post(state *s,int U_Bit,int L_Bit,uint32_t inst){ int I_Bit = bitCheck(inst,25); uint32_t offset = getOp2(s,inst,!I_Bit); uint32_t rd = getRD(inst); uint32_t rn = getRN(inst); uint32_t rnValue = getContents(s,rn); uint32_t rdValue = getContents(s,rd); uint32_t memLoc = getContents(s,rn); uint32_t memValue; if(rn == 15){ memLoc = s-> PC; memLoc += offset; } int type = typeofAddress(memLoc,s->Special_Memory); if(memLoc > 16380 && type == NOT_GPIO){ printf("Error: Out of bounds memory access at address 0x%08x\n",memLoc); return; } if(type != 0 && L_Bit == 0){ handleGPIOTypes(type); return; }else if(type != 0 && L_Bit == 1){ memValue = s->Special_Memory[type-1]; handleGPIOTypes(type); }else{ memValue = (memLoc%4 == 0) ? s-> ARM_mem[memLoc/4] : getUnallignedWord(memLoc,s); } if(U_Bit == 1){ switch(L_Bit){ case(0) : writeMem(s,rdValue,memLoc); add_offset(s,rn,offset); break; case(1) : writeReg(s,rd,memValue); add_offset(s,rn,offset); break; default : perror("Error in post"); break; } }else{ switch(L_Bit){ case(0) : writeMem(s,rdValue,memLoc); subtract_offset(s,rn,offset); break; case(1) : writeReg(s,rd,memValue); add_offset(s,rn,offset); break; default : perror("Error in post"); break; } } }
static struct wipe_desc * get_desc_for_probe(struct wipe_desc *wp, blkid_probe pr) { const char *off, *type, *mag, *p, *usage = NULL; size_t len; loff_t offset; int rc, ispt = 0; /* superblocks */ if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) == 0) { rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL); if (!rc) rc = blkid_probe_lookup_value(pr, "SBMAGIC", &mag, &len); if (rc) return wp; /* partitions */ } else if (blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL) == 0) { rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL); if (!rc) rc = blkid_probe_lookup_value(pr, "PTMAGIC", &mag, &len); if (rc) return wp; usage = N_("partition table"); ispt = 1; } else return wp; if (type_pattern && !match_fstype(type, type_pattern)) return wp; offset = strtoll(off, NULL, 10); wp = add_offset(wp, offset, 0); if (!wp) return NULL; if (usage || blkid_probe_lookup_value(pr, "USAGE", &usage, NULL) == 0) wp->usage = xstrdup(usage); wp->type = xstrdup(type); wp->on_disk = 1; wp->is_parttable = ispt ? 1 : 0; wp->magic = xmalloc(len); memcpy(wp->magic, mag, len); wp->len = len; if (blkid_probe_lookup_value(pr, "LABEL", &p, NULL) == 0) wp->label = xstrdup(p); if (blkid_probe_lookup_value(pr, "UUID", &p, NULL) == 0) wp->uuid = xstrdup(p); return wp; }
static struct wipe_desc * clone_offset(struct wipe_desc *wp0) { struct wipe_desc *wp = NULL; while(wp0) { wp = add_offset(wp, wp0->offset, wp0->zap); wp0 = wp0->next; } return wp; }
int main(int argc, char **argv) { char *action; if (argc < 2) main_usage(argv[0]); action = argv[1]; if (carmen_strcasecmp(action, "-h") == 0 || carmen_strcasecmp(action, "--help") == 0) main_usage(argv[0]); if (carmen_strcasecmp(action, "help") == 0) { help(argc, argv); return 0; } if (carmen_strcasecmp(action, "toppm") == 0) toppm(argc, argv); #ifndef NO_GRAPHICS else if (carmen_strcasecmp(action, "tomap") == 0) tomap(argc, argv); #endif else if (carmen_strcasecmp(action, "rotate") == 0) rotate(argc, argv); else if (carmen_strcasecmp(action, "minimize") == 0) minimize(argc, argv); else if (carmen_strcasecmp(action, "add_place") == 0) add_place(argc, argv); else if (carmen_strcasecmp(action, "add_offset") == 0) add_offset(argc, argv); else if (carmen_strcasecmp(action, "strip") == 0) strip(argc, argv); else if (carmen_strcasecmp(action, "info") == 0) info(argc, argv); else { carmen_warn("\nUnrecognized action %s\n", argv[1]); main_usage(argv[0]); } return 0; }
void send_time(char mode) { uint16_t ctime_1w; uint16_t ctime_0w; uint8_t mymessage[6]; mymessage[0] = 0; // unused mymessage[1] = mode; ctime_1w = time_1w; ctime_0w = timerA_time(); add_offset(offset, &ctime_1w, &ctime_0w); mymessage[2] = ctime_1w & 0xff; mymessage[3] = ctime_1w >> 8; mymessage[4] = ctime_0w & 0xff; mymessage[5] = ctime_0w >> 8; mac_send_once(mymessage, 6, MAC_BROADCAST); }
/* * main: * Drive the sucker. There are two main modes -- either we store * the seek pointers, if the table is to be sorted or randomized, * or we write the pointer directly to the file, if we are to stay * in file order. If the former, we allocate and re-allocate in * CHUNKSIZE blocks; if the latter, we just write each pointer, * and then seek back to the beginning to write in the table. */ int main(int ac, char *av[]) { char *sp, dc; FILE *inf, *outf; int32_t last_off, length, pos; int32_t *p; int first, cnt; char *nsp; STR *fp; static char string[257]; getargs(ac, av); /* evalute arguments */ dc = Delimch; if ((inf = fopen(Infile, "r")) == NULL) err(1, "%s", Infile); if ((outf = fopen(Outfile, "w")) == NULL) err(1, "%s", Outfile); if (!STORING_PTRS) (void) fseek(outf, sizeof Tbl, SEEK_SET); /* * Write the strings onto the file */ Tbl.str_longlen = 0; Tbl.str_shortlen = (unsigned int) 0xffffffff; Tbl.str_delim = dc; Tbl.str_version = VERSION; first = Oflag; add_offset(outf, ftell(inf)); last_off = 0; do { sp = fgets(string, sizeof(string), inf); if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) { pos = ftell(inf); length = pos - last_off - (sp ? strlen(sp) : 0); last_off = pos; if (!length) continue; add_offset(outf, pos); if (Tbl.str_longlen < (u_int32_t)length) Tbl.str_longlen = length; if (Tbl.str_shortlen > (u_int32_t)length) Tbl.str_shortlen = length; first = Oflag; } else if (first) { for (nsp = sp; !isalnum(*nsp); nsp++) continue; ALLOC(Firstch, Num_pts); fp = &Firstch[Num_pts - 1]; if (Iflag && isupper(*nsp)) fp->first = tolower(*nsp); else fp->first = *nsp; fp->pos = Seekpts[Num_pts - 1]; first = FALSE; } } while (sp != NULL); /* * write the tables in */ (void) fclose(inf); Tbl.str_numstr = Num_pts - 1; if (Tbl.str_numstr == 0) Tbl.str_shortlen = 0; if (Oflag) do_order(); else if (Rflag) randomize(); if (Xflag) Tbl.str_flags |= STR_ROTATED; if (!Sflag) { printf("\"%s\" created\n", Outfile); if (Tbl.str_numstr == 1) puts("There was 1 string"); else printf("There were %u strings\n", Tbl.str_numstr); printf("Longest string: %lu byte%s\n", (unsigned long) Tbl.str_longlen, Tbl.str_longlen == 1 ? "" : "s"); printf("Shortest string: %lu byte%s\n", (unsigned long) Tbl.str_shortlen, Tbl.str_shortlen == 1 ? "" : "s"); } (void) fseek(outf, 0, SEEK_SET); Tbl.str_version = htonl(Tbl.str_version); Tbl.str_numstr = htonl(Tbl.str_numstr); Tbl.str_longlen = htonl(Tbl.str_longlen); Tbl.str_shortlen = htonl(Tbl.str_shortlen); Tbl.str_flags = htonl(Tbl.str_flags); (void) fwrite(&Tbl.str_version, sizeof(Tbl.str_version), 1, outf); (void) fwrite(&Tbl.str_numstr, sizeof(Tbl.str_numstr), 1, outf); (void) fwrite(&Tbl.str_longlen, sizeof(Tbl.str_longlen), 1, outf); (void) fwrite(&Tbl.str_shortlen, sizeof(Tbl.str_shortlen), 1, outf); (void) fwrite(&Tbl.str_flags, sizeof(Tbl.str_flags), 1, outf); (void) fwrite( Tbl.stuff, sizeof(Tbl.stuff), 1, outf); if (STORING_PTRS) for (p = Seekpts, cnt = Num_pts; cnt--; ++p) { *p = htonl(*p); (void) fwrite(p, sizeof(*p), 1, outf); } if (fclose(outf)) err(1, "fclose `%s'", Outfile); exit(0); }
int main(int argc, char **argv) { struct wipe_desc *wp0 = NULL, *wp; int c, all = 0, has_offset = 0, noact = 0, quiet = 0; int mode = WP_MODE_PRETTY; static const struct option longopts[] = { { "all", 0, 0, 'a' }, { "help", 0, 0, 'h' }, { "no-act", 0, 0, 'n' }, { "offset", 1, 0, 'o' }, { "parsable", 0, 0, 'p' }, { "quiet", 0, 0, 'q' }, { "types", 1, 0, 't' }, { "version", 0, 0, 'V' }, { NULL, 0, 0, 0 } }; static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ { 'a','o' }, { 0 } }; int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "ahno:pqt:V", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); switch(c) { case 'a': all++; break; case 'h': usage(stdout); break; case 'n': noact++; break; case 'o': wp0 = add_offset(wp0, strtosize_or_err(optarg, _("invalid offset argument")), 1); has_offset++; break; case 'p': mode = WP_MODE_PARSABLE; break; case 'q': quiet++; break; case 't': type_pattern = optarg; break; case 'V': printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING); return EXIT_SUCCESS; default: usage(stderr); break; } } if (optind == argc) usage(stderr); if (!all && !has_offset) { /* * Print only */ while (optind < argc) { wp0 = read_offsets(NULL, argv[optind++]); if (wp0) print_all(wp0, mode); free_wipe(wp0); } } else { /* * Erase */ while (optind < argc) { wp = clone_offset(wp0); wp = do_wipe(wp, argv[optind++], noact, all, quiet); free_wipe(wp); } } return EXIT_SUCCESS; }
/* * main: * Drive the sucker. There are two main modes -- either we store * the seek pointers, if the table is to be sorted or randomized, * or we write the pointer directly to the file, if we are to stay * in file order. If the former, we allocate and re-allocate in * CHUNKSIZE blocks; if the latter, we just write each pointer, * and then seek back to the beginning to write in the table. */ int main(int ac, char *av[]) { char *sp, *nsp, dc; FILE *inf, *outf; off_t last_off, pos, *p; size_t length; int first; uint32_t cnt; STR *fp; static char string[257]; setlocale(LC_ALL, ""); getargs(ac, av); /* evalute arguments */ dc = Delimch; if ((inf = fopen(Infile, "r")) == NULL) { perror(Infile); exit(1); } if ((outf = fopen(Outfile, "w")) == NULL) { perror(Outfile); exit(1); } if (!STORING_PTRS) fseek(outf, (long)sizeof(Tbl), SEEK_SET); /* * Write the strings onto the file */ Tbl.str_longlen = 0; Tbl.str_shortlen = 0xffffffff; Tbl.str_delim = dc; Tbl.str_version = VERSION; first = Oflag; add_offset(outf, ftello(inf)); last_off = 0; do { sp = fgets(string, 256, inf); if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) { pos = ftello(inf); length = (size_t)(pos - last_off) - (sp != NULL ? strlen(sp) : 0); last_off = pos; if (length == 0) continue; add_offset(outf, pos); if ((size_t)Tbl.str_longlen < length) Tbl.str_longlen = length; if ((size_t)Tbl.str_shortlen > length) Tbl.str_shortlen = length; first = Oflag; } else if (first) { for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++) continue; ALLOC(Firstch, Num_pts); fp = &Firstch[Num_pts - 1]; if (Iflag && isupper((unsigned char)*nsp)) fp->first = tolower((unsigned char)*nsp); else fp->first = *nsp; fp->pos = Seekpts[Num_pts - 1]; first = false; } } while (sp != NULL); /* * write the tables in */ fclose(inf); Tbl.str_numstr = Num_pts - 1; if (Cflag) Tbl.str_flags |= STR_COMMENTS; if (Oflag) do_order(); else if (Rflag) randomize(); if (Xflag) Tbl.str_flags |= STR_ROTATED; if (!Sflag) { printf("\"%s\" created\n", Outfile); if (Num_pts == 2) puts("There was 1 string"); else printf("There were %u strings\n", Num_pts - 1); printf("Longest string: %u byte%s\n", Tbl.str_longlen, Tbl.str_longlen == 1 ? "" : "s"); printf("Shortest string: %u byte%s\n", Tbl.str_shortlen, Tbl.str_shortlen == 1 ? "" : "s"); } rewind(outf); Tbl.str_version = htobe32(Tbl.str_version); Tbl.str_numstr = htobe32(Tbl.str_numstr); Tbl.str_longlen = htobe32(Tbl.str_longlen); Tbl.str_shortlen = htobe32(Tbl.str_shortlen); Tbl.str_flags = htobe32(Tbl.str_flags); fwrite((char *)&Tbl, sizeof(Tbl), 1, outf); if (STORING_PTRS) { for (p = Seekpts, cnt = Num_pts; cnt--; ++p) *p = htobe64(*p); fwrite(Seekpts, sizeof(*Seekpts), (size_t)Num_pts, outf); } fclose(outf); exit(0); }
int main(int argc, char **argv) { struct wipe_desc *wp0 = NULL, *wp; int c, has_offset = 0, flags = 0; int mode = WP_MODE_PRETTY; static const struct option longopts[] = { { "all", 0, 0, 'a' }, { "backup", 0, 0, 'b' }, { "force", 0, 0, 'f' }, { "help", 0, 0, 'h' }, { "no-act", 0, 0, 'n' }, { "offset", 1, 0, 'o' }, { "parsable", 0, 0, 'p' }, { "quiet", 0, 0, 'q' }, { "types", 1, 0, 't' }, { "version", 0, 0, 'V' }, { NULL, 0, 0, 0 } }; static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ { 'a','o' }, { 0 } }; int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "afhno:pqt:V", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); switch(c) { case 'a': flags |= WP_FL_ALL; break; case 'b': flags |= WP_FL_BACKUP; break; case 'f': flags |= WP_FL_FORCE; break; case 'h': usage(stdout); break; case 'n': flags |= WP_FL_NOACT; break; case 'o': wp0 = add_offset(wp0, strtosize_or_err(optarg, _("invalid offset argument")), 1); has_offset++; break; case 'p': mode = WP_MODE_PARSABLE; break; case 'q': flags |= WP_FL_QUIET; break; case 't': type_pattern = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; default: usage(stderr); break; } } if (optind == argc) usage(stderr); if ((flags & WP_FL_BACKUP) && !((flags & WP_FL_ALL) || has_offset)) warnx(_("The --backup option is meaningless in this context")); if (!(flags & WP_FL_ALL) && !has_offset) { /* * Print only */ while (optind < argc) { wp0 = read_offsets(NULL, argv[optind++]); if (wp0) print_all(wp0, mode); free_wipe(wp0); } } else { /* * Erase */ while (optind < argc) { wp = clone_offset(wp0); wp = do_wipe(wp, argv[optind++], flags); free_wipe(wp); } } return EXIT_SUCCESS; }
void VertexInfo::add_point(int offset) { add_offset(points_offsets, offset); }
void VertexInfo::add_vector(int offset, bool orthogonal) { add_offset(vectors_offsets, offset); vectors_orthogonality.push_back(orthogonal); }
void start() { // open the two files FILE * file_1 = fopen(FILE_NAME_1, "r"); FILE * file_2 = fopen(FILE_NAME_2, "r"); if (file_1 == NULL || file_2 == NULL) { printf("Invalid file name(s)."); return; } stream_t strm_1, strm_2; // stream 1 and stream 2 strm_1.satelitesOK = 0; strm_2.satelitesOK = 0; /* Synchronise the streams */ sync_satelites(file_1, file_2, &strm_1, &strm_2); sync_time_gps(file_1, file_2, &strm_1, &strm_2); /* Declare the head of the linked list*/ node_t * head = NULL; /* initialise the offset values*/ long lat_offset = 0; long lng_offset = 0; /* infinite loop, will break only if one of the stream ends */ while (1) { if (strm_1.satelitesOK) { // stream 1 satellite one was ok , //add the its location to the list add_element(&head, strm_1.location); if (strm_2.satelitesOK) { // if both streams are ok, calculate the offset get_offset(&lat_offset, &lng_offset, strm_1.location, strm_2.location); } else { // stream 2 failed, fix its location add_offset(lat_offset, lng_offset, strm_1.location, &strm_2.location); } } else if (strm_2.satelitesOK) { //stream 2 add to good location to the list add_element(&head, strm_2.location); // stream 1 failed , fix its location, add_offset(lat_offset, lng_offset, strm_2.location, &strm_1.location); } // status integer to see what we have read int line_read = 0; // read from stream 1 until new // coordinates and time fix is obtained while (line_read != GPS_TIME && line_read != _EOF) { line_read = read_line(file_1, &strm_1); } if (line_read == _EOF) { break; // stream 1 ended } line_read = 0; // read from stream 2 until new //coordinates and time fix is obtained while (line_read != GPS_TIME && line_read != _EOF) { line_read = read_line(file_2, &strm_2); } if (line_read == _EOF) { break; // stream 2 ended } } // output the data in the linked list to a file out_to_file(head); fclose(file_1); fclose(file_2); }
int main(int argc, char **argv) { struct wipe_desc *wp = NULL; int c, all = 0, has_offset = 0, noact = 0, mode = 0; const char *fname; struct option longopts[] = { { "all", 0, 0, 'a' }, { "help", 0, 0, 'h' }, { "no-act", 0, 0, 'n' }, { "offset", 1, 0, 'o' }, { "parsable", 0, 0, 'p' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); while ((c = getopt_long(argc, argv, "ahno:p", longopts, NULL)) != -1) { switch(c) { case 'a': all++; break; case 'h': usage(stdout); break; case 'n': noact++; break; case 'o': wp = add_offset(wp, strtoll_offset(optarg), 1); has_offset++; break; case 'p': mode = WP_MODE_PARSABLE; break; default: usage(stderr); break; } } if (wp && all) errx(EXIT_FAILURE, _("--offset and --all are mutually exclusive")); if (optind == argc) usage(stderr); fname = argv[optind++]; wp = read_offsets(wp, fname, all); if (wp) { if (has_offset || all) do_wipe(wp, fname, noact); else print_all(wp, mode); free_wipe(wp); } return EXIT_SUCCESS; }
/* * main: * Drive the sucker. There are two main modes -- either we store * the seek pointers, if the table is to be sorted or randomized, * or we write the pointer directly to the file, if we are to stay * in file order. If the former, we allocate and re-allocate in * CHUNKSIZE blocks; if the latter, we just write each pointer, * and then seek back to the beginning to write in the table. */ int main(int ac, char **av) { register unsigned char *sp; register FILE *inf, *outf; register int32_t last_off, length, pos, *p; register int first, cnt; register char *nsp; register STR *fp; static char string[257]; getargs(ac, av); /* evalute arguments */ if ((inf = fopen(Infile, "r")) == NULL) { perror(Infile); exit(1); } if ((outf = fopen(Outfile, "w")) == NULL) { perror(Outfile); exit(1); } if (!STORING_PTRS) (void) fseek(outf, sizeof Tbl, 0); /* * Write the strings onto the file */ Tbl.str_longlen = 0; Tbl.str_shortlen = (unsigned int) 0xffffffff; Tbl.str_delim = Delimch; Tbl.str_version = VERSION; first = Oflag; add_offset(outf, ftell(inf)); last_off = 0; do { sp = (unsigned char*)fgets(string, 256, inf); if (sp == NULL || STR_ENDSTRING(sp, Tbl)) { pos = ftell(inf); length = pos - last_off - (sp ? strlen((const char*)sp) : 0); if (!length) /* Here's where we go back and fix things, if the * 'fortune' just read was the null string. * We had to make the assignment of last_off slightly * redundant to achieve this. */ { if (pos - last_off == 2) fix_last_offset(outf, pos); last_off = pos; continue; } last_off = pos; add_offset(outf, pos); if (Tbl.str_longlen < length) Tbl.str_longlen = length; if (Tbl.str_shortlen > length) Tbl.str_shortlen = length; first = Oflag; } else if (first) { for (nsp = (char*)sp; !isalnum(*nsp); nsp++) continue; ALLOC(Firstch, Num_pts); fp = &Firstch[Num_pts - 1]; if (Iflag && isupper(*nsp)) fp->first = tolower(*nsp); else fp->first = *nsp; fp->pos = Seekpts[Num_pts - 1]; first = FALSE; } } while (sp != NULL); /* * write the tables in */ fclose(inf); if (Oflag) do_order(); else if (Rflag) randomize(); if (Xflag) Tbl.str_flags |= STR_ROTATED; if (!Sflag) { printf("\"%s\" created\n", Outfile); if (Num_pts == 1) puts("There was no string"); else { if (Num_pts == 2) puts("There was 1 string"); else printf("There were %ld strings\n", Num_pts - 1); printf("Longest string: %u byte%s\n", Tbl.str_longlen, Tbl.str_longlen == 1 ? "" : "s"); printf("Shortest string: %u byte%s\n", Tbl.str_shortlen, Tbl.str_shortlen == 1 ? "" : "s"); } } fseek(outf, (off_t) 0, 0); Tbl.str_version = htonl(Tbl.str_version); Tbl.str_numstr = htonl(Num_pts - 1); /* Look, Ma! After using the variable three times, let's store * something in it! */ Tbl.str_longlen = htonl(Tbl.str_longlen); Tbl.str_shortlen = htonl(Tbl.str_shortlen); Tbl.str_flags = htonl(Tbl.str_flags); fwrite(&Tbl.str_version, sizeof Tbl.str_version, 1, outf); fwrite(&Tbl.str_numstr, sizeof Tbl.str_numstr, 1, outf); fwrite(&Tbl.str_longlen, sizeof Tbl.str_longlen, 1, outf); fwrite(&Tbl.str_shortlen, sizeof Tbl.str_shortlen, 1, outf); fwrite(&Tbl.str_flags, sizeof Tbl.str_flags, 1, outf); fwrite( Tbl.stuff, sizeof Tbl.stuff, 1, outf); if (STORING_PTRS) { for (p = Seekpts, cnt = Num_pts; cnt--; ++p) { *p = htonl(*p); fwrite(p, sizeof *p, 1, outf); } } fclose(outf); exit(0); }