static char * readline(char **line, size_t *linesize, size_t *length, FILE *fp) { int c; size_t n = 0; if (*line == NULL || *linesize < LSIZE + n + 1) *line = srealloc(*line, *linesize = LSIZE + n + 1); for (;;) { if (n >= *linesize - LSIZE / 2) *line = srealloc(*line, *linesize += LSIZE); c = getc(fp); if (c != EOF) { (*line)[n++] = c; (*line)[n] = '\0'; if (c == '\n') break; } else { if (n > 0) break; else return NULL; } } *length = n; return *line; }
void *xrealloc(void *ptr,int size,int ID) { struct mem_block *mem; if (ptr==NULL) return xcalloc(size,ID); mem=(void*)((char*)(ptr)-sizeof(struct mem_block)); rem_block(mem); #ifdef DEBUG mem=srealloc(mem,size+sizeof(struct mem_block)+32,ID); #else mem=srealloc(mem,size+sizeof(struct mem_block),ID); #endif if (!mem) return NULL; mem->size=size; mem->ID=ID; #ifdef DEBUG memcpy(mem->magic, "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",32); memcpy(mem->content+size,"abcdefghijklmnopqrstuvwxyz678901",32); #endif add_block(mem); return mem->content; }
static void addcmd(const char *s) { if (a_cnt >= a_maxarg - 4096) { a_maxarg += 8192; a_vec = srealloc(a_vec, a_maxarg * sizeof *a_vec); } if (iflag && contains(s, iflag)) { if (replcnt >= replsiz) { #ifdef WEIRD_LIMITS if (replcnt >= 5) { fprintf(stderr, "%s: too many args with %s\n", progname, iflag); exit(1); #endif /* WEIRD_LIMITS */ replpos = srealloc(replpos, (replsiz += 5) * sizeof *replpos); } replpos[replcnt++] = a_cnt; } a_vec[a_cnt++] = s; a_asz += strlen(s) + 1; } static void endcmd(void) { a_agg = a_cnt; a_maxsize = sysconf(_SC_ARG_MAX) - envsz() - 2048 - a_asz; if (nflag || sflag) { long newsize = sflag ? atol(sflag) : #ifdef WEIRD_LIMITS LINE_MAX; #else /* !WEIRD_LIMITS */ a_maxsize; #endif /* !WEIRD_LIMITS */ if (sflag && (newsize <= a_asz || newsize > a_maxsize)) fprintf(stderr, "%s: 0 < max-cmd-line-size <= %ld: -s%s\n", progname, a_maxsize, sflag); if (newsize < a_maxsize) a_maxsize = newsize; } if (sflag) a_spc = smalloc(a_maxsize); else { while ((a_spc = malloc(a_maxsize)) == NULL) { a_maxsize /= 2; if (a_maxsize < LINE_MAX) { a_spc = smalloc(a_maxsize); break; } } } a_cur = a_agg; if (nflag && nflag < a_maxarg + a_cnt) { nflag += a_cnt; a_maxarg = nflag; } }
static int new_return_error(struct rtrn** ret, const char* err, int arg) { int r=-2; int i=0; const char** tmpv; int* tmparg; while ((*ret)->errsv[i++]); (*ret)->errsc=i; tmpv = srealloc((*ret)->errsv, (sizeof *(*ret)->errsv)*(i+1)); if (tmpv) { r++; (*ret)->errsv=tmpv; (*ret)->errsv[i-1]=err; (*ret)->errsv[i]=NULL; } tmparg = srealloc((*ret)->errsarg, (sizeof *(*ret)->errsarg)*i); if (tmparg) { r++; (*ret)->errsarg=tmparg; (*ret)->errsarg[i-1]=arg; } return r; }
static char *printer_add_enum(int param, char *buffer, int offset, int *nprinters_ptr) { DWORD needed, nprinters; buffer = srealloc(buffer, offset+512); /* * Exploratory call to EnumPrinters to determine how much space * we'll need for the output. Discard the return value since it * will almost certainly be a failure due to lack of space. */ EnumPrinters(param, NULL, ENUM_LEVEL, buffer+offset, 512, &needed, &nprinters); if (needed < 512) needed = 512; buffer = srealloc(buffer, offset+needed); if (EnumPrinters(param, NULL, ENUM_LEVEL, buffer+offset, needed, &needed, &nprinters) == 0) return NULL; *nprinters_ptr += nprinters; return buffer; }
unsigned vector_grow(DV_Vector* dv) { unsigned old_max = dv->size_hint; dv->size_hint *= 2; dv->data = srealloc(dv->data, dv->chunk_size * dv->size_hint); dv->indices = srealloc(dv->indices, sizeof(unsigned) * dv->size_hint); memset(dv->indices+old_max, 0, sizeof(unsigned)*old_max); return dv->size_hint - old_max; }
void mat_resize(Matrix *m, int nrows, int ncols) { int i; if (!(nrows >= 0 && ncols >= 0)) die("ERROR mat_resize: nrows=%i ncols=%i\n", nrows, ncols); for (i = nrows; i < m->nrows; i++) sfree(m->data[i]); m->data = srealloc(m->data, nrows * sizeof(void*)); for (i = 0; i < nrows; i++) m->data[i] = srealloc(m->data[i], ncols * sizeof(double)); m->nrows = nrows; m->ncols = ncols; }
/* read in a tree from a file. Return character string representing tree */ SEXP rph_tree_read(SEXP filename) { FILE *infile; char c; SEXP result; char *currStr, **strvec; int i, pos=0, currLen=10000, numparen=0, numtrees_alloc=1000, numtrees=0; infile = phast_fopen(CHARACTER_VALUE(filename), "r"); currStr = smalloc((currLen+2)*sizeof(char)); strvec = smalloc(numtrees_alloc*sizeof(char*)); while (1) { pos=0; numparen=0; while (';'!=(c=fgetc(infile))) { if (c==EOF) { if (pos==0) break; die("unexpected EOF in tree file. Trees should be terminated by \";\""); } if (isspace(c)) continue; if (c=='(') numparen++; if (c==')') numparen--; if (numparen < 0) die("bad tree in tree file"); if (pos==currLen) { currLen += 10000; currStr = srealloc(currStr, (currLen+2)*sizeof(char)); } currStr[pos++] = c; } if (pos > 0) { if (numparen != 0) die("unbalanced parenthesis in tree file"); currStr[pos++]=';'; currStr[pos]='\0'; if (numtrees == numtrees_alloc) { numtrees_alloc += 1000; strvec = srealloc(strvec, numtrees_alloc*sizeof(char*)); } strvec[numtrees] = smalloc((strlen(currStr)+1)*sizeof(char)); strcpy(strvec[numtrees], currStr); numtrees++; } else break; } phast_fclose(infile); PROTECT(result = NEW_CHARACTER(numtrees)); for (i=0; i<numtrees; i++) SET_STRING_ELT(result, i, mkChar(strvec[i])); UNPROTECT(1); return result; }
static int give_value(const char* val, struct option* opt) { int r=0; unsigned short i=0; if (opt->takes_value == 1) { free(opt->value); opt->value = smalloc((sizeof *opt->value) * (strlen(val)+1)); if (opt->value) strcpy(opt->value, val); else r=-1; } else { char** tmp; while (opt->valuev[i++]); tmp = srealloc(opt->valuev, (sizeof *opt->valuev)*(i+1)); if (tmp) { opt->valuev=tmp; (opt->valuev)[i-1] = smalloc((sizeof *(opt->valuev)[i-1]) * (strlen(val)+1)); if ((opt->valuev)[i-1]) strcpy((opt->valuev)[i-1], val); else r=-1; (opt->valuev)[i]=NULL; opt->valuec=i; } else r=-1; } return r; }
static bool opensex_read_next_row(database_handle_t *hdl) { int c = 0; unsigned int n = 0; opensex_t *rs = (opensex_t *)hdl->priv; while ((c = getc(rs->f)) != EOF && c != '\n') { rs->buf[n++] = c; if (n == rs->bufsize) { rs->bufsize *= 2; rs->buf = srealloc(rs->buf, rs->bufsize); } } rs->buf[n] = '\0'; rs->token = rs->buf; if (c == EOF && ferror(rs->f)) { slog(LG_ERROR, "opensex-read-next-row: error at %s line %d: %s", hdl->file, hdl->line, strerror(errno)); slog(LG_ERROR, "opensex-read-next-row: exiting to avoid data loss"); exit(EXIT_FAILURE); } if (c == EOF && n == 0) return false; hdl->line++; hdl->token = 0; return true; }
int split_buf(char *buf, char ***argv, int colon_special) { int argvsize = 8; int argc; char *s; *argv = scalloc(sizeof(char *) * argvsize, 1); argc = 0; while (*buf) { if (argc == argvsize) { argvsize += 8; *argv = srealloc(*argv, sizeof(char *) * argvsize); } if (*buf == ':') { (*argv)[argc++] = buf + 1; buf = ""; } else { s = strpbrk(buf, " "); if (s) { *s++ = 0; while (*s == ' ') s++; } else { s = buf + strlen(buf); } (*argv)[argc++] = buf; buf = s; } } return argc; }
void add_file(unsigned long long mult, char *name) { unsigned int i; file_t *file = NULL; char *buffer = NULL; // find matching entry in file_list for (i=0; i < file_list_len; i++) { file = &(file_list[i]); if (strcmp(file->name, name) == 0) { // is mult a multiple of file->mult? if (mult % file->mult == 0) return; // is file->mult a multiple of mult? if (file->mult % mult == 0) { file->mult = mult; return; } buffer = file->buffer; } } // no buffer for file found? if (!buffer) buffer = (char *) smalloc(buflen); // add new entry file_list_len++; file_list = (file_t *) srealloc((void *) file_list, sizeof(file_t) * file_list_len); file = &(file_list[file_list_len - 1]); file->mult = mult; file->name = (char *) smalloc(strlen(name) + 1); strcpy(file->name, name); file->buffer = buffer; }
static void xmlrpc_command_success_nodata(sourceinfo_t *si, const char *message) { connection_t *cptr; struct httpddata *hd; char *p; const char *q; cptr = si->connection; hd = cptr->userdata; if (hd->sent_reply) return; if (hd->replybuf != NULL) { hd->replybuf = srealloc(hd->replybuf, strlen(hd->replybuf) + strlen(message) + 2); p = hd->replybuf + strlen(hd->replybuf); *p++ = '\n'; } else { hd->replybuf = smalloc(strlen(message) + 1); p = hd->replybuf; } q = message; while (*q != '\0') if (*q > '\0' && *q < ' ') q++; else *p++ = *q++; *p = '\0'; }
inline int FindTimerSpace() { for (int i = 0; i<numTimers; i++) { if (timers[i].type == KILLED_TIMER) return i; } if (!srealloc(timers, sizeof(ScriptTimer) * (numTimers+1))) return -1; return numTimers ++; }
void string::set_length(int i) { assert(i >= 0); if (i > sz) ptr = srealloc(ptr, sz, len, i, &sz); len = i; }
void addtest(void *elem) { int i, j; void *retval, *realret; if (arraysize < arraylen+1) { arraysize = arraylen+1+256; array = (array == NULL ? smalloc(arraysize*sizeof(*array)) : srealloc(array, arraysize*sizeof(*array))); } i = 0; while (i < arraylen && cmp(elem, array[i]) > 0) i++; /* now i points to the first element >= elem */ if (i < arraylen && !cmp(elem, array[i])) retval = array[i]; /* expect that returned not elem */ else { retval = elem; /* expect elem returned (success) */ for (j = arraylen; j > i; j--) array[j] = array[j-1]; array[i] = elem; /* add elem to array */ arraylen++; } realret = add234(tree, elem); if (realret != retval) { error("add: retval was %p expected %p", realret, retval); } verify(); }
static int sftp_cmd_lcd(struct sftp_command *cmd) { char *currdir; int len; if (cmd->nwords < 2) { printf("lcd: expects a local directory name\n"); return 0; } if (!SetCurrentDirectory(cmd->words[1])) { LPVOID message; int i; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&message, 0, NULL); i = strcspn((char *)message, "\n"); printf("lcd: unable to change directory: %.*s\n", i, (LPCTSTR)message); LocalFree(message); return 0; } currdir = smalloc(256); len = GetCurrentDirectory(256, currdir); if (len > 256) currdir = srealloc(currdir, len); GetCurrentDirectory(len, currdir); printf("New local directory is %s\n", currdir); sfree(currdir); return 1; }
static void opl_append(struct oplist *o, enum optype type) { if (o->len == o->size) { o->size += o->size >> 1; o->ops = srealloc(o->ops, o->size * sizeof *o->ops); }
static void xmlrpc_command_success_nodata(sourceinfo_t *si, const char *message) { connection_t *cptr; struct httpddata *hd; char *newmessage; char *p; newmessage = xmlrpc_normalizeBuffer(message); cptr = si->connection; hd = cptr->userdata; if (hd->sent_reply) { free(newmessage); return; } if (hd->replybuf != NULL) { hd->replybuf = srealloc(hd->replybuf, strlen(hd->replybuf) + strlen(newmessage) + 2); p = hd->replybuf + strlen(hd->replybuf); *p++ = '\n'; } else { hd->replybuf = smalloc(strlen(newmessage) + 1); p = hd->replybuf; } strcpy(p, newmessage); free(newmessage); }
int find_side_bndy ( struct vtx_data **graph, /* array of vtx data for graph */ int nvtxs, /* number of vertices in graph */ int *assignment, /* processor each vertex gets assigned to */ int side, /* side to take vertices from */ int new_val, /* assignment value for boundary vtxs */ int **pbndy_list /* returned list, end with zero */ ) { int *edges; /* loops through edge list */ int *bndy_list; /* returned list, end with zero */ int list_length; /* returned number of vtxs on boundary */ int set, set2; /* set a vertex is in */ int i, j; /* loop counters */ if (*pbndy_list != NULL) { /* Contains list of all vertices on boundary. */ bndy_list = *pbndy_list; i = list_length = 0; while (bndy_list[i] != 0) { if (assignment[bndy_list[i]] == side) { bndy_list[list_length++] = bndy_list[i]; } ++i; } } else { bndy_list = smalloc((nvtxs + 1) * sizeof(int)); list_length = 0; for (i = 1; i <= nvtxs; i++) { set = assignment[i]; if (set == side) { edges = graph[i]->edges; for (j = graph[i]->nedges - 1; j; j--) { set2 = assignment[*(++edges)]; if (set2 != set) { bndy_list[list_length++] = i; break; } } } } } bndy_list[list_length] = 0; for (i = 0; i < list_length; i++) { assignment[bndy_list[i]] = (int) new_val; } /* Shrink out unnecessary space */ *pbndy_list = srealloc(bndy_list, (list_length + 1) * sizeof(int)); return (list_length); }
/* debug realloc: srealloc() with memory management */ void *d_realloc( void *oldptr, size_t newsize ) { void *p = srealloc( oldptr, newsize ); clog( CL_DEBUG, "srealloc(%p,%d) -> %p", oldptr, newsize, p ); return p; }
static void setlevel(int level, struct getdb *db, int fd) { if (level >= maxlevel) levels = srealloc(levels, (maxlevel=level+16) * sizeof *levels); levels[level].l_db = db; levels[level].l_fd = fd; }
/* conditioned_on must be an array of integer lists; specifically, the ith element must be the list of state numbers on which the ith state is conditioned. */ Unspooler *cm_create_unspooler(int nstates_spooled, List **conditioned_on) { UnspoolNode *n; int i, j; Stack *s; Unspooler *unsp; int *mark; int capacity; unsp = (Unspooler*)smalloc(sizeof(Unspooler)); unsp->nstates_spooled = nstates_spooled; unsp->nstates_unspooled = 0; unsp->spooled_to_unspooled = (UnspoolNode**)smalloc(nstates_spooled * sizeof(UnspoolNode*)); capacity = nstates_spooled * nstates_spooled; unsp->unspooled_to_spooled = (int*)smalloc(capacity * sizeof(int)); mark = (int*)smalloc(nstates_spooled * sizeof(int)); s = stk_new_ptr(nstates_spooled); for (i = 0; i < nstates_spooled; i++) { /* erase marks (used to detect cycles) */ for (j = 0; j < nstates_spooled; j++) mark[j] = 0; unsp->spooled_to_unspooled[i] = cm_new_unspool_node(i); stk_push_ptr(s, unsp->spooled_to_unspooled[i]); while ((n = (UnspoolNode*)stk_pop_ptr(s)) != NULL) { if (conditioned_on[n->oldstate] == NULL || lst_size(conditioned_on[n->oldstate]) == 0) { n->newstate = unsp->nstates_unspooled++; /* mapping to spooled space */ if (n->newstate >= capacity) { capacity *= 2; unsp->unspooled_to_spooled = (int*)srealloc(unsp->unspooled_to_spooled, capacity * sizeof(int)); } unsp->unspooled_to_spooled[n->newstate] = i; } else { for (j = 0; j < lst_size(conditioned_on[n->oldstate]); j++) { int oldstate = lst_get_int(conditioned_on[n->oldstate], j); UnspoolNode *m; if (mark[oldstate] == 1) die("ERROR: cycle in 'conditioned_on' dependencies.\n"); mark[oldstate] = 1; m = cm_new_unspool_node(oldstate); lst_push_ptr(n->children, m); stk_push_ptr(s, m); } } } } stk_free(s); sfree(mark); return unsp; }
void headpreheader(struct hthead *head, const char *name, const char *val) { head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1)); memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders); head->noheaders++; head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2); head->headers[0][0] = sstrdup(name); head->headers[0][1] = sstrdup(val); }
static void setppi(int f, long j, const char *p) { if (j >= ppisize[f]) { ppisize[f] = j + 1; ppibuf[f] = srealloc(ppibuf[f], ppisize[f] * sizeof *ppibuf[f]); } ppibuf[f][j] = p; }
/* Reallocate a category map to allow for the specified size. */ void cm_realloc(CategoryMap *cm, int new_ncats) { int i; int old_ncats = cm->ncats; cm->ncats = new_ncats; cm->ranges = srealloc(cm->ranges, (cm->ncats + 1) * sizeof(CategoryRange*)); cm->conditioned_on = srealloc(cm->conditioned_on, (cm->ncats + 1) * sizeof(List*)); cm->labelling_precedence = srealloc(cm->labelling_precedence, (cm->ncats + 1) * sizeof(int)); cm->fill_precedence = srealloc(cm->fill_precedence, (cm->ncats + 1) * sizeof(int)); for (i = old_ncats+1; i <= cm->ncats; i++) { cm->ranges[i] = NULL; cm->conditioned_on[i] = NULL; cm->labelling_precedence[i] = -1; cm->fill_precedence[i] = -1; } }
void string::append(const char *p, int n) { if (n > 0) { int newlen = len + n; if (newlen > sz) ptr = srealloc(ptr, sz, len, newlen, &sz); memcpy(ptr + len, p, n); len = newlen; } }
void headappheader(struct hthead *head, const char *name, const char *val) { int i; i = head->noheaders++; head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders); head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2); head->headers[i][0] = sstrdup(name); head->headers[i][1] = sstrdup(val); }
static int read_qseq_line(FILE *f, char *fname, char *fields[11]) { static const char *spaces = " \t\n"; static char *buffer = NULL; static size_t bufsz = 0; size_t s; char *b; char *res; int i; if (NULL == buffer) { bufsz = 1024; buffer = smalloc(bufsz); } b = buffer; s = bufsz; b[s - 1] = '*'; while (NULL != (res = fgets(b, s, f))) { if (b[s - 1] == 0 && b[s - 2] != '\n') { bufsz *= 2; buffer = srealloc(buffer, bufsz); b = buffer + s - 1; s = bufsz - (b - buffer); b[s - 1] = '*'; } else { break; } } if (NULL == res) { if (ferror(f)) { printf("Error reading %s: %s\n", fname, strerror(errno)); return -1; } else { /* eof */ return 1; } } /* (*line_no)++; */ b = buffer; s = 0; for (i = 0; i < 11; i++) { fields[i] = buffer + s; s += strcspn(buffer + s, spaces); if (buffer[s] == '\0') { printf("Error reading qseq file: not enough fields found\n"); return -1; } buffer[s++] = '\0'; s += strspn(buffer + s, spaces); } return 0; }
/* Get administrators of the groups */ static int get_groupadmins(void) { FILE *f; int line = 0; char buffer[IOBUF_SIZE], *colpos, *grouppos, *endname, *adminpos; if (!(f = fopen(adminsfile, "r"))) { errstr(_("Cannot open file with group administrators: %s\n"), strerror(errno)); return -1; } while (fgets(buffer, IOBUF_SIZE, f)) { line++; if (buffer[0] == ';' || buffer[0] == '#') continue; /* Skip initial spaces */ for (colpos = buffer; isspace(*colpos); colpos++); if (!*colpos) /* Empty line? */ continue; /* Find splitting colon */ for (grouppos = colpos; *colpos && *colpos != ':'; colpos++); if (!*colpos || grouppos == colpos) { errstr(_("Parse error at line %d. Cannot find end of group name.\n"), line); continue; } /* Cut trailing spaces */ for (endname = colpos-1; isspace(*endname); endname--); *(++endname) = 0; /* Skip initial spaces at admins name */ for (colpos++; isspace(*colpos); colpos++); if (!*colpos) { errstr(_("Parse error at line %d. Cannot find administrators name.\n"), line); continue; } /* Go through admins name */ for (adminpos = colpos; !isspace(*colpos); colpos++); if (*colpos) { /* Some characters after name? */ *colpos = 0; /* Skip trailing spaces */ for (colpos++; isspace(*colpos); colpos++); if (*colpos) { errstr(_("Parse error at line %d. Trailing characters after administrators name.\n"), line); continue; } } if (adminscnt >= adminsalloc) adminstable = srealloc(adminstable, sizeof(struct adminstable)*(adminsalloc+=ADMIN_TAB_ALLOC)); adminstable[adminscnt].grpname = sstrdup(grouppos); adminstable[adminscnt++].adminname = sstrdup(adminpos); } fclose(f); qsort(adminstable, adminscnt, sizeof(struct adminstable), admin_cmp); return 0; }