void updateTable(int *het, int depth, uchar *seq, Tables **tables, int count) { int index = 0; int start = het[5] - depth + 1; for (int i = 0; i < 6; ++i) { index <<= 1; index += (seq[het[i] - start] - '0'); } uchar *target = myalloc(depth - 5, uchar); for (int i = 0, j = 0; j < depth; ++j) { if ((j + start) == het[i]){ i++; } else { target[j - i] = seq[j]; } } target[depth - 6] = '\0'; int i; for (i = 0; i < (*tables)->num; ++i) { if (!strcmp(target, (*tables)->array[i].id)) { break; } } if (i != (*tables)->num) { (*tables)->array[i].data[index] = count; free(target); } else { if ((*tables)->num == (*tables)->cap) { (*tables) = resizeTables((*tables)); } (*tables)->array[(*tables)->num].data = myalloc(64, int); memset((*tables)->array[(*tables)->num].data, 0, 64 * sizeof(int)); (*tables)->array[(*tables)->num].data[index] = count; (*tables)->array[(*tables)->num].id = target; (*tables)->num++; } }
void pbwtBuildReverse (PBWT *p) { int i, j, M = p->M ; uchar *x = myalloc (M, uchar) ; PbwtCursor *uF ; if (p->aFend) uF = pbwtCursorCreate (p, TRUE, FALSE) ; else { uF = pbwtCursorCreate (p, TRUE, TRUE) ; for (i = 0 ; i < p->N ; ++i) /* first run forwards to the end */ pbwtCursorForwardsRead (uF) ; pbwtCursorToAFend (uF, p) ; } /* use p->aFend also to start the reverse cursor - this gives better performance */ if (!p->aRstart) p->aRstart = myalloc (M, int) ; memcpy (p->aRstart, uF->a, M * sizeof(int)) ; p->zz = arrayReCreate (p->zz, arrayMax(p->yz), uchar) ; PbwtCursor *uR = pbwtCursorCreate (p, FALSE, TRUE) ; /* will pick up aRstart */ for (i = p->N ; i-- ; ) { pbwtCursorReadBackwards (uF) ; for (j = 0 ; j < M ; ++j) x[uF->a[j]] = uF->y[j] ; for (j = 0 ; j < M ; ++j) uR->y[j] = x[uR->a[j]] ; pbwtCursorWriteForwards (uR) ; } /* save uR->a, which is the lexicographic order of the sequences */ if (!p->aRend) p->aRend = myalloc (M, int) ; memcpy (p->aRend, uR->a, M * sizeof(int)) ; fprintf (logFile, "built reverse PBWT - size %ld\n", arrayMax(p->zz)) ; if (isCheck) /* print out the reversed haplotypes */ { FILE *fp = fopen ("rev.haps","w") ; Array tz = p->yz ; p->yz = p->zz ; int* ta = p->aFstart ; p->aFstart = p->aRstart ; pbwtWriteHaplotypes (fp, p) ; p->yz = tz ; p->aFstart = ta ; } free (x) ; pbwtCursorDestroy (uF) ; pbwtCursorDestroy (uR) ; }
void ELinitialize(void) { int i ; freeinit(&hfl, sizeof(Halfedge)) ; ELhashsize = 2 * sqrt_nsites ; ELhash = (Halfedge **)myalloc( sizeof(*ELhash) * ELhashsize) ; for (i = 0 ; i < ELhashsize ; i++) { ELhash[i] = (Halfedge *)NULL ; } ELleftend = HEcreate((Edge *)NULL, 0) ; ELrightend = HEcreate((Edge *)NULL, 0) ; ELleftend->ELleft = (Halfedge *)NULL ; ELleftend->ELright = ELrightend ; ELrightend->ELleft = ELleftend ; ELrightend->ELright = (Halfedge *)NULL ; ELhash[0] = ELleftend ; ELhash[ELhashsize-1] = ELrightend ; }
void lowMsg(char *message) { int size; int pos = sendQ.lowSize++; if(pos >= Q_LOWSIZE) { /* max sendQ exceeded. */ sendQ.lowSize--; fprintf(out, "Max sendQ exceeded (low).\n"); return; } size = strlen(message) + 1; sendQ.low[pos].ts = (int)time(0); sendQ.low[pos].message = myalloc(size * sizeof(char)); memcpy(sendQ.low[pos].message, message, size); return; }
RAW *ben_enc(BEN * node) { RAW *raw = raw_init(); /* Calculate size of ben data */ raw->size = ben_enc_size(node); if (raw->size <= 0) { raw_free(raw); return NULL; } /* Encode ben object */ raw->code = (UCHAR *) myalloc(raw->size * sizeof(UCHAR)); raw->p = ben_enc_rec(node, raw->code); if (raw->p == NULL || (LONG) (raw->p - raw->code) != raw->size) { raw_free(raw); return NULL; } return raw; }
static int h_users(int argc, unsigned char **argv){ int j; unsigned char *arg; struct passwords *pwl = NULL; for (j = 1; j<argc; j++) { if(!(pwl = myalloc(sizeof(struct passwords)))) { fprintf(stderr, "No memory for PWL entry, line %d\n", linenum); return(1); } memset(pwl, 0, sizeof(struct passwords)); arg = (unsigned char *)strchr((char *)argv[j], ':'); if(!arg||!arg[1]||!arg[2]||arg[3]!=':') { pwl->user = (unsigned char *)mystrdup((char *)argv[j]); pwl->pwtype = SYS; } else { *arg = 0; pwl->user = (unsigned char *)mystrdup((char *)argv[j]); if((arg[1] == 'C' && arg[2] == 'L' && (pwl->pwtype = CL)) || (arg[1] == 'C' && arg[2] == 'R' && (pwl->pwtype = CR)) || (arg[1] == 'N' && arg[2] == 'T' && (pwl->pwtype = NT)) || (arg[1] == 'L' && arg[2] == 'M' && (pwl->pwtype = LM))){ pwl->password = (unsigned char *)mystrdup((char *)arg+4); } else { pwl->password = (unsigned char *) mystrdup((char *)arg + 1); pwl->pwtype = UN; } } pthread_mutex_lock(&pwl_mutex); pwl->next = conf.pwl; conf.pwl = pwl; pthread_mutex_unlock(&pwl_mutex); } return 0; }
PBWT *pbwtSubSites (PBWT *pOld, double fmin, double frac) { int M = pOld->M ; PBWT *pNew = pbwtCreate (M, 0) ; int i, j, k, thresh = M*(1-fmin) ; double bit = 0.0 ; uchar *x ; PbwtCursor *uOld = pbwtCursorCreate (pOld, TRUE, TRUE) ; PbwtCursor *uNew = pbwtCursorCreate (pNew, TRUE, TRUE) ; if (!pOld || !pOld->yz) die ("subsites without an existing pbwt") ; if (fmin < 0 || fmin >= 1 || frac <= 0 || frac > 1) die ("fmin %f, frac %f for subsites out of range\n", fmin, frac) ; x = myalloc (M, uchar) ; if (pOld->sites) pNew->sites = arrayCreate (4096, Site) ; for (i = 0 ; i < pOld->N ; ++i) { if ((uOld->c < thresh) && ((bit += frac) > 1.0)) { for (j = 0 ; j < M ; ++j) x[uOld->a[j]] = uOld->y[j] ; for (j = 0 ; j < M ; ++j) uNew->y[j] = x[uNew->a[j]] ; pbwtCursorWriteForwards (uNew) ; if (pOld->sites) array(pNew->sites, pNew->N, Site) = arr(pOld->sites, i, Site) ; ++pNew->N ; bit -= 1.0 ; } pbwtCursorForwardsRead (uOld) ; } pbwtCursorToAFend (uNew, pNew) ; fprintf (logFile, "subsites with fmin %f, frac %f leaves %d sites\n", fmin, frac, pNew->N) ; pNew->chrom = pOld->chrom ; pOld->chrom = 0 ; pNew->samples = pOld->samples ; pOld->samples = 0 ; pNew->missingOffset = pOld->missingOffset ; pOld->missingOffset = 0 ; pNew->zMissing = pOld->zMissing ; pOld->zMissing = 0 ; pbwtDestroy (pOld) ; pbwtCursorDestroy (uOld) ; pbwtCursorDestroy (uNew) ; free(x) ; return pNew ; }
void cache_put( UCHAR *session_id, int type ) { ITEM *item_sk = NULL; struct obj_key *sk = NULL; if( hash_exists( _main->cache->hash, session_id, SHA_DIGEST_LENGTH) ) { return; } sk = (struct obj_key *) myalloc( sizeof(struct obj_key), "cache_put" ); /* Session id */ memcpy( sk->session_id, session_id, SHA_DIGEST_LENGTH ); /* Multicast or Unicast */ sk->type = type; /* Availability */ sk->time = time_add_1_min(); item_sk = list_put( _main->cache->list, sk ); hash_put( _main->cache->hash, sk->session_id, SHA_DIGEST_LENGTH, item_sk ); }
bool VoronoiDiagramGenerator::ELinitialize() { int i; freeinit(&hfl, sizeof **ELhash); ELhashsize = 2 * sqrt_nsites; ELhash = (struct Halfedge **) myalloc ( sizeof *ELhash * ELhashsize); if(ELhash == 0) return false; for(i=0; i<ELhashsize; i +=1) ELhash[i] = (struct Halfedge *)NULL; ELleftend = HEcreate( (struct Edge *)NULL, 0); ELrightend = HEcreate( (struct Edge *)NULL, 0); ELleftend -> ELleft = (struct Halfedge *)NULL; ELleftend -> ELright = ELrightend; ELrightend -> ELleft = ELleftend; ELrightend -> ELright = (struct Halfedge *)NULL; ELhash[0] = ELleftend; ELhash[ELhashsize-1] = ELrightend; return true; }
void stringBufferSort(stringBuffer* L){ char** string; struct node_in_list_of_strings* p; int a; if (L==NULL) DieWithError("Function stringBufferSort: Non-NULL parameter is expected.\n"); if (L->length <= 1) return; else { string = myalloc(L->length * sizeof(char *)); for (p = L->first_node, a = 0; p!=NULL; p=p->next, a++) string[a] = p->value; qsort(string, L->length, sizeof(char*),strcmp_targets); for (p = L->first_node, a = 0; p!=NULL; p=p->next, a++) p->value = string[a]; free(string); return; } }
static int h_auth(int argc, unsigned char **argv){ struct auth *au, * newau; freeauth(conf.authfuncs); conf.authfuncs = NULL; if(!conf.bandlimfunc)conf.bandlimfunc = bandlimitfunc; for(argc--; argc; argc--){ for(au = authfuncs; au; au=au->next){ if(!strcmp((char *)argv[argc], au->desc)){ newau = myalloc(sizeof(struct auth)); newau->next = conf.authfuncs; conf.authfuncs = newau; conf.authfuncs->desc = au->desc; conf.authfuncs->authenticate = au->authenticate; conf.authfuncs->authorize = au->authorize; break; } } if(!au) return 1; } conf.authfunc = doauth; return 0; }
node_t *push_to_list_tail(list_t *list, void *addr) { node_t *new_node = myalloc(sizeof (node_t)); new_node->addr = addr; new_node->next = NULL; if (list->tail == NULL) { new_node->prev = NULL; list->tail = new_node; /* Atomic assigment */ list->head = new_node; } else { new_node->prev = list->tail; list->tail = new_node; /* Atomic assigment */ new_node->prev->next = new_node; } (list->len)++; return new_node; }
/* * add_one_elt * * Add one element to a (possibly empty) eary struct. */ void add_one_elt(char *eltname, eary * eary) { if (eary->alloc == 0) { eary->alloc = 8; eary->array = (char **) myalloc(8 * sizeof(char *)); } else if (eary->num >= eary->alloc) { eary->alloc *= 2; eary->array = (char **) realloc(eary->array, eary->alloc * sizeof(char *)); if (!eary->array) { fprintf(stderr, "out of memory"); exit(1); } } eary->array[eary->num] = mystrdup(eltname); eary->num++; }
BEN *ben_init(int type) { BEN *node = (BEN *) myalloc(sizeof(BEN)); node->t = type; switch (type) { case BEN_STR: node->v.s = NULL; break; case BEN_INT: node->v.i = 0; break; case BEN_DICT: node->v.d = list_init(); break; case BEN_LIST: node->v.l = list_init(); break; } return node; }
int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec){ int len; int bufsize; if(!param->srvbuf){ bufsize = BUFSIZE; if(param->ndatfilterssrv > 0 && bufsize < 32768) bufsize = 32768; if(!(param->srvbuf = myalloc(bufsize))) return 0; param->srvbufsize = bufsize; param->srvoffset = param->srvinbuf = 0; } if(param->srvinbuf && param->srvoffset < param->srvinbuf){ return (int)param->srvbuf[param->srvoffset++]; } param->srvoffset = param->srvinbuf = 0; if ((len = sockrecvfrom(param->remsock, ¶m->sins, param->srvbuf, param->srvbufsize, timeosec*1000 + timeousec))<=0) return EOF; param->srvinbuf = len; param->srvoffset = 1; param->nreads++; param->statssrv += len; return (int)*param->srvbuf; }
void VoronoiDiagramGenerator::insertVertexAddress(long vertexNum, struct Site* address) { if(vertices == 0) { vertices = (struct Site **) myalloc(4000*sizeof( *vertices)); sizeOfVertices = 4000; for(int i = sizeOfVertices - 4000; i < sizeOfVertices; i++) { vertices[i] = 0; } } //if the site address being entered is past the end of the array, then grow the array while(vertexNum > sizeOfVertices - 1) { vertices = (Site**)realloc(vertices,(sizeOfVertices+4000)*sizeof(Site*)); sizeOfVertices += 4000; for(int i = sizeOfVertices - 4000; i < sizeOfVertices; i++) { vertices[i] = 0; } } vertices[vertexNum] = address; }
char * VoronoiDiagramGenerator::getfree(struct VoronoiDiagramGenerator::Freelist *fl) { int i; struct Freenode *t; if(fl->head == (struct Freenode *) NULL) { t = (struct Freenode *) myalloc(sqrt_nsites * fl->nodesize); if(t == 0) return 0; currentMemoryBlock->next = new FreeNodeArrayList; currentMemoryBlock = currentMemoryBlock->next; currentMemoryBlock->memory = t; currentMemoryBlock->next = 0; for(i=0; i<sqrt_nsites; i+=1) makefree((struct Freenode *)((char *)t+i*fl->nodesize), fl); }; t = fl -> head; fl -> head = (fl -> head) -> nextfree; return((char *)t); }
char* create_script_file_path_from_executable() { # define PYTHON_SCRIPT_SUFFIX "-script.py" long prefix_len; char* script_file_path; const char* ext_ptr; char filename[MAX_PATH]; int filename_size; filename_size = GetModuleFileNameA(NULL, filename, MAX_PATH); filename[filename_size]='\x00'; ext_ptr = strrchr(filename, '.'); if (ext_ptr == NULL) { /* No extension, so use the entire path. */ ext_ptr = filename + strlen(filename); } prefix_len = ext_ptr - filename; script_file_path = (char*) myalloc("script file name", prefix_len + strlen(PYTHON_SCRIPT_SUFFIX) + 1); strncpy(script_file_path, filename, prefix_len); script_file_path[prefix_len] = '\x00'; strcat(script_file_path, PYTHON_SCRIPT_SUFFIX); return script_file_path; }
PBWT *pbwtSubRange (PBWT *pOld, int start, int end) { int M = pOld->M ; PBWT *pNew = pbwtCreate (M, 0) ; int i, j, k ; uchar *x ; PbwtCursor *uOld = pbwtCursorCreate (pOld, TRUE, TRUE) ; PbwtCursor *uNew = pbwtCursorCreate (pNew, TRUE, TRUE) ; if (!pOld || !pOld->yz) die ("subrange without an existing pbwt") ; if (start < 0 || end > pOld->N || end <= start) die ("subrange invalid start %d, end %d", start, end) ; x = myalloc (M, uchar) ; if (pOld->sites) pNew->sites = arrayCreate (4096, Site) ; for (i = 0 ; i < end ; ++i) { if (i >= start) { for (j = 0 ; j < M ; ++j) x[uOld->a[j]] = uOld->y[j] ; for (j = 0 ; j < M ; ++j) uNew->y[j] = x[uNew->a[j]] ; pbwtCursorWriteForwards (uNew) ; if (pOld->sites) array(pNew->sites, pNew->N, Site) = arr(pOld->sites, i, Site) ; ++pNew->N ; } pbwtCursorForwardsRead (uOld) ; } pbwtCursorToAFend (uNew, pNew) ; pNew->chrom = pOld->chrom ; pOld->chrom = 0 ; pNew->samples = pOld->samples ; pOld->samples = 0 ; pNew->missingOffset = pOld->missingOffset ; pOld->missingOffset = 0 ; pNew->zMissing = pOld->zMissing ; pOld->zMissing = 0 ; pbwtDestroy (pOld) ; pbwtCursorDestroy (uOld) ; pbwtCursorDestroy (uNew) ; free(x) ; return pNew ; }
static int addchar(inchr c) { int smushamount,row,k; char *templine; getletter(c); smushamount = smushamt(); if (outlinelen+currcharwidth-smushamount>outlinelenlimit ||inchrlinelen+1>inchrlinelenlimit) { return 0; } templine = (char*)myalloc(sizeof(char)*(outlinelenlimit+1)); for (row=0;row<charheight;row++) { if (right2left) { strcpy(templine,currchar[row]); for (k=0;k<smushamount;k++) { templine[currcharwidth-smushamount+k] = smushem(templine[currcharwidth-smushamount+k],outputline[row][k]); } strcat(templine,outputline[row]+smushamount); strcpy(outputline[row],templine); } else { for (k=0;k<smushamount;k++) { outputline[row][outlinelen-smushamount+k] = smushem(outputline[row][outlinelen-smushamount+k],currchar[row][k]); } strcat(outputline[row],currchar[row]+smushamount); } } myfree(templine); outlinelen = MYSTRLEN(outputline[0]); inchrline[inchrlinelen++] = c; return 1; }
struct ace * make_ace (int argc, unsigned char ** argv){ struct ace * acl; unsigned char *arg; struct iplist *ipl=NULL; struct portlist *portl=NULL; struct userlist *userl=NULL; struct hostname *hostnamel=NULL; int res; acl = myalloc(sizeof(struct ace)); if(!acl) return acl; memset(acl, 0, sizeof(struct ace)); if(argc > 0 && strcmp("*", (char *)argv[0])) { arg = argv[0]; arg = (unsigned char *)strtok((char *)arg, ","); do { if(!acl->users) { acl->users = userl = myalloc(sizeof(struct userlist)); } else { userl->next = myalloc(sizeof(struct userlist)); userl = userl -> next; } if(!userl) { fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(NULL); } memset(userl, 0, sizeof(struct userlist)); userl->user=(unsigned char*)mystrdup((char *)arg); } while((arg = (unsigned char *)strtok((char *)NULL, ","))); } if(argc > 1 && strcmp("*", (char *)argv[1])) { arg = (unsigned char *)strtok((char *)argv[1], ","); do { if(!acl->src) { acl->src = ipl = myalloc(sizeof(struct iplist)); } else { ipl->next = myalloc(sizeof(struct iplist)); ipl = ipl -> next; } if(!ipl) { fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(NULL); } memset(ipl, 0, sizeof(struct iplist)); if (scanipl(arg, ipl)) { fprintf(stderr, "Invalid IP, IP range or CIDR, line %d\n", linenum); return(NULL); } } while((arg = (unsigned char *)strtok((char *)NULL, ","))); } if(argc > 2 && strcmp("*", (char *)argv[2])) { arg = (unsigned char *)strtok((char *)argv[2], ","); do { int arglen; unsigned char *pattern; arglen = (int)strlen((char *)arg); if(arglen > 0 && (arg[arglen-1] < '0' || arg[arglen-1] > '9')){ if(!acl->dstnames) { acl->dstnames = hostnamel = myalloc(sizeof(struct hostname)); } else { hostnamel->next = myalloc(sizeof(struct hostname)); hostnamel = hostnamel -> next; } if(!hostnamel){ fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(NULL); } memset(hostnamel, 0, sizeof(struct hostname)); hostnamel->matchtype = 3; pattern = arg; if(pattern[arglen-1] == '*'){ arglen --; pattern[arglen] = 0; hostnamel->matchtype ^= MATCHEND; } if(pattern[0] == '*'){ pattern++; arglen--; hostnamel->matchtype ^= MATCHBEGIN; } hostnamel->name = (unsigned char *) mystrdup( (char *)pattern); if(!hostnamel->name) { fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(NULL); } } else { if(!acl->dst) { acl->dst = ipl = myalloc(sizeof(struct iplist)); } else { ipl->next = myalloc(sizeof(struct iplist)); ipl = ipl -> next; } if(!ipl) { fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(NULL); } memset(ipl, 0, sizeof(struct iplist)); if (scanipl(arg, ipl)) { fprintf(stderr, "Invalid IP, IP range or CIDR, line %d\n", linenum); return(NULL); } } }while((arg = (unsigned char *)strtok((char *)NULL, ","))); } if(argc > 3 && strcmp("*", (char *)argv[3])) { arg = (unsigned char *)strtok((char *)argv[3], ","); do { if(!acl->ports) { acl->ports = portl = myalloc(sizeof(struct portlist)); } else { portl->next = myalloc(sizeof(struct portlist)); portl = portl -> next; } if(!portl) { fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(NULL); } memset(portl, 0, sizeof(struct portlist)); res = sscanf((char *)arg, "%hu-%hu", &portl->startport, &portl->endport); if(res < 1) { fprintf(stderr, "Invalid port or port range, line %d\n", linenum); return(NULL); } if (res == 1) portl->endport = portl->startport; } while((arg = (unsigned char *)strtok((char *)NULL, ","))); } if(argc > 4 && strcmp("*", (char *)argv[4])) { arg = (unsigned char *)strtok((char *)argv[4], ","); do { if(!strcmp((char *)arg, "CONNECT")){ acl->operation |= CONNECT; } else if(!strcmp((char *)arg, "BIND")){ acl->operation |= BIND; } else if(!strcmp((char *)arg, "UDPASSOC")){ acl->operation |= UDPASSOC; } else if(!strcmp((char *)arg, "ICMPASSOC")){ acl->operation |= ICMPASSOC; } else if(!strcmp((char *)arg, "HTTP_GET")){ acl->operation |= HTTP_GET; } else if(!strcmp((char *)arg, "HTTP_PUT")){ acl->operation |= HTTP_PUT; } else if(!strcmp((char *)arg, "HTTP_POST")){ acl->operation |= HTTP_POST; } else if(!strcmp((char *)arg, "HTTP_HEAD")){ acl->operation |= HTTP_HEAD; } else if(!strcmp((char *)arg, "HTTP_OTHER")){ acl->operation |= HTTP_OTHER; } else if(!strcmp((char *)arg, "HTTP_CONNECT")){ acl->operation |= HTTP_CONNECT; } else if(!strcmp((char *)arg, "HTTP")){ acl->operation |= HTTP; } else if(!strcmp((char *)arg, "HTTPS")){ acl->operation |= HTTPS; } else if(!strcmp((char *)arg, "FTP_GET")){ acl->operation |= FTP_GET; } else if(!strcmp((char *)arg, "FTP_PUT")){ acl->operation |= FTP_PUT; } else if(!strcmp((char *)arg, "FTP_LIST")){ acl->operation |= FTP_LIST; } else if(!strcmp((char *)arg, "FTP_DATA")){ acl->operation |= FTP_DATA; } else if(!strcmp((char *)arg, "FTP")){ acl->operation |= FTP; } else if(!strcmp((char *)arg, "ADMIN")){ acl->operation |= ADMIN; } else if(!strcmp((char *)arg, "DNSRESOLVE")){ acl->operation |= DNSRESOLVE; } else if(!strcmp((char *)arg, "ICQ")){ acl->operation |= IM_ICQ; } else { fprintf(stderr, "Unknown operation type: %s line %d\n", arg, linenum); return(NULL); } } while((arg = (unsigned char *)strtok((char *)NULL, ","))); } if(argc > 5){ for(arg = argv[5]; *arg;){ int val, val1; if(!isnumber(*arg)){ arg++; continue; } val1 = val = (*arg - '0'); arg++; if(*arg == '-' && isnumber(*(arg+1)) && (*(arg+1) - '0') > val) { val1 = (*(arg+1) - '0'); arg+=2; } for(; val<=val1; val++) acl->wdays |= (1 << (val % 7)); } } if(argc > 6){ for(arg = argv[6]; strlen((char *)arg) >= 17 && isdigit(arg[0]) && isdigit(arg[1]) && isdigit(arg[3]) && isdigit(arg[4]) && isdigit(arg[6]) && isdigit(arg[7]) && isdigit(arg[9]) && isdigit(arg[10]) && isdigit(arg[12]) && isdigit(arg[13]) && isdigit(arg[15]) && isdigit(arg[16]) ; arg+=18){ int t1, t2; struct period *sp; t1 = (arg[0] - '0') * 10 + (arg[1] - '0'); t1 = (t1 * 60) + (arg[3] - '0') * 10 + (arg[4] - '0'); t1 = (t1 * 60) + (arg[6] - '0') * 10 + (arg[7] - '0'); t2 = (arg[9] - '0') * 10 + (arg[10] - '0'); t2 = (t2 * 60) + (arg[12] - '0') * 10 + (arg[13] - '0'); t2 = (t2 * 60) + (arg[15] - '0') * 10 + (arg[16] - '0'); if(t2 < t1) break; sp = myalloc(sizeof(struct period)); if(sp){ sp->fromtime = t1; sp->totime = t2; sp->next = acl->periods; acl->periods = sp; } if(arg[17]!=',') break; } } if (argc > 7){ acl->weight = atoi((char *)argv[7]); } return acl; }
char *mkstr_stat(char *s){ return strcpy((char *) myalloc(adp_statmem, (strlen(s)+1) * sizeof(char)), s); }
void main(int argc, char **argv) { int i; ARGBEGIN { # ifdef DEBUG case 'd': debug++; break; case 'y': yydebug = TRUE; break; # endif case 't': case 'T': Binit(&fout, 1, OWRITE); errorf= 2; foutopen = 1; break; case 'v': case 'V': report = 1; break; case 'n': case 'N': report = 0; break; case '9': nine = 1; break; default: warning("Unknown option %c", ARGC()); } ARGEND sargc = argc; sargv = argv; if (argc > 0){ yyfile = argv[fptr++]; fin = Bopen(yyfile, OREAD); if(fin == 0) error ("%s - can't open file: %r", yyfile); sargc--; sargv++; } else { yyfile = "/fd/0"; fin = myalloc(sizeof(Biobuf), 1); if(fin == 0) exits("core"); Binit(fin, 0, OREAD); } if(Bgetc(fin) == Beof) /* no input */ exits(0); Bseek(fin, 0, 0); gch(); /* may be gotten: def, subs, sname, stchar, ccl, dchar */ get1core(); /* may be gotten: name, left, right, nullstr, parent, ptr */ strcpy((char*)sp, "INITIAL"); sname[0] = sp; sp += strlen("INITIAL") + 1; sname[1] = 0; if(yyparse()) exits("error"); /* error return code */ /* may be disposed of: def, subs, dchar */ free1core(); /* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */ get2core(); ptail(); mkmatch(); # ifdef DEBUG if(debug) pccl(); # endif sect = ENDSECTION; if(tptr>0)cfoll(tptr-1); # ifdef DEBUG if(debug)pfoll(); # endif cgoto(); # ifdef DEBUG if(debug){ print("Print %d states:\n",stnum+1); for(i=0;i<=stnum;i++)stprt(i); } # endif /* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, stchar, sname */ /* may be gotten: verify, advance, stoff */ free2core(); get3core(); layout(); /* may be disposed of: verify, advance, stoff, nexts, nchar, gotof, atable, ccpackflg, sfall */ # ifdef DEBUG free3core(); # endif fother = Bopen(cname,OREAD); if(fother == 0) error("Lex driver missing, file %s: %r",cname); while ( (i=Bgetc(fother)) != Beof) Bputc(&fout, i); Bterm(fother); Bterm(&fout); if( # ifdef DEBUG debug || # endif report == 1)statistics(); if (fin) Bterm(fin); exits(0); /* success return code */ }
void * sockschild(struct clientparam* param) { int res; unsigned i=0; SOCKET s; unsigned size; SASIZETYPE sasize; unsigned char * buf=NULL; unsigned char c; unsigned char command=0; struct pollfd fds[3]; int ver=0; int havepass = 0; struct sockaddr_in sin; int len; param->req.sin_addr.s_addr = 0; param->service = S_SOCKS; if(!(buf = myalloc(BUFSIZE))) {RETURN(21);} memset(buf, 0, BUFSIZE); if ((ver = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_L], 0)) != 5 && ver != 4) { RETURN(401); } /* version */ param->service = ver; if(ver == 5){ if ((i = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(441);} /* nmethods */ for (; i; i--) { if ((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(442);} if (res == 2 && !param->srv->nouser) { havepass = res; } } buf[0] = 5; buf[1] = havepass; if(socksend(param->clisock, buf, 2, conf.timeouts[STRING_S])!=2){RETURN(402);} if (havepass) { if (((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_L], 0))) != 1) { RETURN(412); } if ((i = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(443);} if (i && (unsigned)(res = sockgetlinebuf(param, CLIENT, buf, i, 0, conf.timeouts[STRING_S])) != i){RETURN(444);}; buf[i] = 0; if(!param->username)param->username = (unsigned char *)mystrdup((char *)buf); if ((i = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(445);} if (i && (unsigned)(res = sockgetlinebuf(param, CLIENT, buf, i, 0, conf.timeouts[STRING_S])) != i){RETURN(446);}; buf[i] = 0; if(!param->password)param->password = (unsigned char *)mystrdup((char *)buf); buf[0] = 1; buf[1] = 0; if(socksend(param->clisock, buf, 2, conf.timeouts[STRING_S])!=2){RETURN(402);} } if ((c = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_L], 0)) != 5) { RETURN(421); } /* version */ } if( (command = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) < 1 || command > 3){command = 0; RETURN(407);} /* command */ if(ver == 5){ if (sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0) == EOF) {RETURN(447);} /* reserved */ c = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0); /* atype */ } else { if ((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(448);} buf[0] = (unsigned char) res; if ((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(449);} buf[1] = (unsigned char) res; param->sins.sin_port = param->req.sin_port = *(unsigned short*)buf; c = 1; } switch(c) { case 1: for (i = 0; i<4; i++){ if ((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(450);} buf[i] = (unsigned char)res; } param->sins.sin_addr.s_addr = param->req.sin_addr.s_addr = *(unsigned long *)buf; if(command==1 && !param->req.sin_addr.s_addr) { RETURN(422); } myinet_ntoa(param->sins.sin_addr, (char *)buf); break; case 3: if ((size = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(451);} /* nmethods */ for (i=0; i<size; i++){ /* size < 256 */ if ((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(452);} buf[i] = (unsigned char)res; } buf[i] = 0; param->sins.sin_addr.s_addr = param->req.sin_addr.s_addr = getip(buf); if(command==1 && !param->req.sin_addr.s_addr) { RETURN(100); } break; default: RETURN(998); } if(param->hostname)myfree(param->hostname); param->hostname = (unsigned char *)mystrdup((char *)buf); if (ver == 5) { if ((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(453);} buf[0] = (unsigned char) res; if ((res = sockgetcharcli(param, conf.timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(454);} buf[1] = (unsigned char) res; param->sins.sin_port = param->req.sin_port = *(unsigned short*)buf; } else { sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, 0, conf.timeouts[STRING_S]); buf[127] = 0; if(!param->srv->nouser && *buf && !param->username)param->username = (unsigned char *)mystrdup((char *)buf); if(param->sins.sin_addr.s_addr && ntohl(param->sins.sin_addr.s_addr)<256){ param->service = S_SOCKS45; sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, 0, conf.timeouts[STRING_S]); buf[127] = 0; if(param->hostname)myfree(param->hostname); param->hostname = (unsigned char *)mystrdup((char *)buf); param->sins.sin_addr.s_addr = param->req.sin_addr.s_addr = getip(buf); } } if(command == 1 && !param->req.sin_port) {RETURN(424);} param->sins.sin_family = AF_INET; switch(command) { case 1: param->operation = CONNECT; break; case 2: param->sins.sin_addr.s_addr = param->extip; param->sins.sin_port = param->extport?param->extport:param->req.sin_port; if ((param->remsock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {RETURN (11);} param->operation = BIND; break; case 3: param->sins.sin_port = param->extport?param->extport:param->req.sin_port; param->sins.sin_addr.s_addr = param->extip; if ((param->remsock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {RETURN (11);} param->operation = UDPASSOC; break; default: RETURN(997); } if((res = (*param->srv->authfunc)(param))) {RETURN(res);} if(command > 1) { if(bind(param->remsock,(struct sockaddr *)¶m->sins,sizeof(param->sins))) { param->sins.sin_port = 0; if(bind(param->remsock,(struct sockaddr *)¶m->sins,sizeof(param->sins)))RETURN (12); #if SOCKSTRACE > 0 fprintf(stderr, "%s:%hu binded to communicate with server\n", inet_ntoa(param->sins.sin_addr), ntohs(param->sins.sin_port) ); fflush(stderr); #endif } sasize = sizeof(struct sockaddr_in); getsockname(param->remsock, (struct sockaddr *)¶m->sins, &sasize); if(command == 3) { param->ctrlsock = param->clisock; param->clisock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(param->clisock == INVALID_SOCKET) {RETURN(11);} sin.sin_family = AF_INET; sin.sin_addr.s_addr = param->srv->intip; sin.sin_port = htons(0); if(bind(param->clisock,(struct sockaddr *)&sin,sizeof(struct sockaddr_in))) {RETURN (12);} #if SOCKSTRACE > 0 fprintf(stderr, "%s:%hu binded to communicate with client\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port) ); fflush(stderr); #endif } } param->res = 0; CLEANRET: if(param->clisock != INVALID_SOCKET){ sasize = sizeof(struct sockaddr_in); if(command != 3) getsockname(param->remsock, (struct sockaddr *)&sin, &sasize); else getsockname(param->clisock, (struct sockaddr *)&sin, &sasize); #if SOCKSTRACE > 0 fprintf(stderr, "Sending confirmation to client with code %d for %s with %s:%hu\n", param->res, commands[command], inet_ntoa(sin.sin_addr), ntohs(sin.sin_port) ); fflush(stderr); #endif if(ver == 5){ buf[0] = 5; buf[1] = param->res%10; buf[2] = 0; buf[3] = 1; memcpy(buf+4, &sin.sin_addr.s_addr, 4); memcpy(buf+8, &sin.sin_port, 2); socksend((command == 3)?param->ctrlsock:param->clisock, buf, 10, conf.timeouts[STRING_S]); } else{ buf[0] = 0; buf[1] = 90 + (param->res%10); memcpy(buf+2, &sin.sin_port, 2); memcpy(buf+4, &sin.sin_addr.s_addr, 4); socksend(param->clisock, buf, 8, conf.timeouts[STRING_S]); } if (param->res == 0) { switch(command) { case 1: if(param->redirectfunc){ if(buf)myfree(buf); return (*param->redirectfunc)(param); } param->res = sockmap(param, conf.timeouts[CONNECTION_L]); break; case 2: listen (param->remsock, 1); fds[0].fd = param->remsock; fds[1].fd = param->clisock; fds[0].events = fds[1].events = POLLIN; res = poll(fds, 2, conf.timeouts[(param->req.sin_addr.s_addr)?CONNECTION_S:CONNECTION_L] * 1000); if (res < 1 || fds[1].revents) { res = 460; break; } sasize = sizeof(param->sins); s = accept(param->remsock, (struct sockaddr *)¶m->sins, &sasize); closesocket(param->remsock); param->remsock = s; if(s == INVALID_SOCKET) { param->res = 462; break; } if(param->req.sin_addr.s_addr && param->req.sin_addr.s_addr != param->sins.sin_addr.s_addr) { param->res = 470; break; } #if SOCKSTRACE > 0 fprintf(stderr, "Sending incoming connection to client with code %d for %s with %s:%hu\n", param->res, commands[command], inet_ntoa(param->sins.sin_addr), ntohs(param->sins.sin_port) ); fflush(stderr); #endif if(ver == 5){ memcpy (buf+4, ¶m->sins.sin_addr, 4); memcpy (buf+8, ¶m->sins.sin_port, 2); socksend(param->clisock, buf, 10, conf.timeouts[STRING_S]); } else { memcpy (buf+2, ¶m->sins.sin_port, 2); memcpy (buf+4, ¶m->sins.sin_addr, 4); socksend(param->clisock, buf, 8, conf.timeouts[STRING_S]); } param->res = sockmap(param, conf.timeouts[CONNECTION_S]); break; case 3: param->sins.sin_addr.s_addr = param->req.sin_addr.s_addr; param->sins.sin_port = param->req.sin_port; myfree(buf); if(!(buf = myalloc(LARGEBUFSIZE))) {RETURN(21);} for(;;){ fds[0].fd = param->remsock; fds[1].fd = param->clisock; fds[2].fd = param->ctrlsock; fds[2].events = fds[1].events = fds[0].events = POLLIN; res = poll(fds, 3, conf.timeouts[CONNECTION_L]*1000); if(res <= 0) { param->res = 463; break; } if (fds[2].revents) { param->res = 0; break; } if (fds[1].revents) { sasize = sizeof(struct sockaddr_in); if((len = recvfrom(param->clisock, buf, 65535, 0, (struct sockaddr *)&sin, &sasize)) <= 10) { param->res = 464; break; } if(sin.sin_addr.s_addr != param->sinc.sin_addr.s_addr){ param->res = 465; break; } if(buf[0] || buf[1] || buf[2]) { param->res = 466; break; } switch(buf[3]) { case 1: i = 8; memcpy(¶m->sins.sin_addr.s_addr, buf+4, 4); break; case 3: size = buf[4]; for (i=4; size; i++, size--){ buf[i] = buf[i+1]; } buf[i++] = 0; param->sins.sin_addr.s_addr = getip(buf+4); break; default: RETURN(996); } memcpy(¶m->sins.sin_port, buf+i, 2); i+=2; sasize = sizeof(param->sins); if(len > (int)i){ if(socksendto(param->remsock, ¶m->sins, buf+i, len - i, conf.timeouts[SINGLEBYTE_L]*1000) <= 0){ param->res = 467; break; } param->statscli+=(len - i); param->nwrites++; #if SOCKSTRACE > 1 fprintf(stderr, "UDP packet relayed from client to %s:%hu size %d, header %d\n", inet_ntoa(param->sins.sin_addr), ntohs(param->sins.sin_port), (len - i), i ); fprintf(stderr, "client address is assumed to be %s:%hu\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port) ); fflush(stderr); #endif } } if (fds[0].revents) { struct sockaddr_in tsin; sasize = sizeof(tsin); buf[0]=buf[1]=buf[2]=0; buf[3]=1; if((len = recvfrom(param->remsock, buf+10, 65535 - 10, 0, (struct sockaddr *)&tsin, &sasize)) <= 0) { param->res = 468; break; } param->statssrv+=len; param->nreads++; memcpy(buf+4, &tsin.sin_addr.s_addr, 4); memcpy(buf+8, &tsin.sin_port, 2); sasize = sizeof(param->sins); if(socksendto(param->clisock, &sin, buf, len + 10, conf.timeouts[SINGLEBYTE_L]*1000) <=0){ param->res = 469; break; } #if SOCKSTRACE > 1 fprintf(stderr, "UDP packet relayed to client from %s:%hu size %d\n", inet_ntoa(tsin.sin_addr), ntohs(tsin.sin_port), len ); fflush(stderr); #endif } } break; default: param->res = 417; break; } } } if(command > 3) command = 0; if(buf){ sprintf((char *)buf, "%s ", commands[command]); if(param->hostname){ sprintf((char *)buf + strlen((char *)buf), "%.265s", param->hostname); } else myinet_ntoa(param->req.sin_addr, (char *)buf+strlen((char *)buf)); sprintf((char *)buf+strlen((char *)buf), ":%hu", ntohs(param->req.sin_port)); (*param->srv->logfunc)(param, buf); myfree(buf); } freeparam(param); return (NULL); }
static int h_ace(int argc, unsigned char **argv){ int res = 0; int offset = 0; struct ace *acl = NULL; struct bandlim * nbl; struct trafcount * tl; if(!strcmp((char *)argv[0], "allow")){ res = ALLOW; } else if(!strcmp((char *)argv[0], "deny")){ res = DENY; } else if(!strcmp((char *)argv[0], "redirect")){ res = REDIRECT; offset = 2; } else if(!strcmp((char *)argv[0], "bandlimin")||!strcmp((char *)argv[0], "bandlimout")){ res = BANDLIM; offset = 1; } else if(!strcmp((char *)argv[0], "nobandlimin")||!strcmp((char *)argv[0], "nobandlimout")){ res = NOBANDLIM; } else if(!strcmp((char *)argv[0], "countin")){ res = COUNTIN; offset = 3; } else if(!strcmp((char *)argv[0], "nocountin")){ res = NOCOUNTIN; } else if(!strcmp((char *)argv[0], "countout")){ res = COUNTOUT; offset = 3; } else if(!strcmp((char *)argv[0], "nocountout")){ res = NOCOUNTOUT; } acl = make_ace(argc - (offset+1), argv + (offset + 1)); if(!acl) { fprintf(stderr, "Unable to parse ACL entry, line %d\n", linenum); return(1); } acl->action = res; switch(acl->action){ case REDIRECT: acl->chains = myalloc(sizeof(struct chain)); memset(acl->chains, 0, sizeof(struct chain)); if(!acl->chains) { fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); return(2); } acl->chains->type = R_HTTP; if(!getip46(46, argv[1], (struct sockaddr *)&acl->chains->addr)) return 5; *SAPORT(&acl->chains->addr) = htons((unsigned short)atoi((char *)argv[2])); acl->chains->weight = 1000; acl->chains->extuser = NULL; acl->chains->extpass = NULL; acl->chains->next = NULL; case ALLOW: case DENY: if(!conf.acl){ conf.acl = acl; } else { struct ace * acei; for(acei = conf.acl; acei->next; acei = acei->next); acei->next = acl; } break; case BANDLIM: case NOBANDLIM: nbl = myalloc(sizeof(struct bandlim)); if(!nbl) { fprintf(stderr, "No memory to create band limit filter\n"); return(3); } memset(nbl, 0, sizeof(struct bandlim)); nbl->ace = acl; if(acl->action == BANDLIM) { sscanf((char *)argv[1], "%u", &nbl->rate); if(nbl->rate < 300) { fprintf(stderr, "Wrong bandwidth specified, line %d\n", linenum); return(4); } } pthread_mutex_lock(&bandlim_mutex); if(!strcmp((char *)argv[0], "bandlimin") || !strcmp((char *)argv[0], "nobandlimin")){ if(!conf.bandlimiter){ conf.bandlimiter = nbl; } else { struct bandlim * bli; for(bli = conf.bandlimiter; bli->next; bli = bli->next); bli->next = nbl; } } else { if(!conf.bandlimiterout){ conf.bandlimiterout = nbl; } else { struct bandlim * bli; for(bli = conf.bandlimiterout; bli->next; bli = bli->next); bli->next = nbl; } } pthread_mutex_unlock(&bandlim_mutex); break; case COUNTIN: case NOCOUNTIN: case COUNTOUT: case NOCOUNTOUT: tl = myalloc(sizeof(struct trafcount)); if(!tl) { fprintf(stderr, "No memory to create traffic limit filter\n"); return(5); } memset(tl, 0, sizeof(struct trafcount)); tl->ace = acl; if((acl->action == COUNTIN)||(acl->action == COUNTOUT)) { unsigned long lim; tl->comment = ( char *)argv[1]; while(isdigit(*tl->comment))tl->comment++; if(*tl->comment== '/')tl->comment++; tl->comment = mystrdup(tl->comment); sscanf((char *)argv[1], "%u", &tl->number); sscanf((char *)argv[3], "%lu", &lim); tl->type = getrotate(*argv[2]); tl->traflim64 = ((uint64_t)lim)*(1024*1024); if(!tl->traflim64) { fprintf(stderr, "Wrong traffic limit specified, line %d\n", linenum); return(6); } if(tl->number != 0 && conf.counterd >= 0) { lseek(conf.counterd, sizeof(struct counter_header) + (tl->number - 1) * sizeof(struct counter_record), SEEK_SET); memset(&crecord, 0, sizeof(struct counter_record)); read(conf.counterd, &crecord, sizeof(struct counter_record)); tl->traf64 = crecord.traf64; tl->cleared = crecord.cleared; tl->updated = crecord.updated; #ifdef _MAX__TIME64_T if(tl->cleared >= _MAX__TIME64_T || tl->updated >= _MAX__TIME64_T){ fprintf(stderr, "Invalid or corrupted counter file. Use countersutil utility to convert from older version\n"); return(6); } #endif } } pthread_mutex_lock(&tc_mutex); if(!conf.trafcounter){ conf.trafcounter = tl; } else { struct trafcount * ntl; for(ntl = conf.trafcounter; ntl->next; ntl = ntl->next); ntl->next = tl; } pthread_mutex_unlock(&tc_mutex); } return 0; }
static void libPP_init(tsequence *seq){ libPP_hlp = (char *) myalloc(adp_statmem, (seq->length+4) * sizeof(char)); dots_hlp = mkstr_stat(libPP_repeat(0, seq->length, '.')); }
int main(int argc, char **argv) { struct options *my_opts; PGconn *pgconn; my_opts = (struct options *) myalloc(sizeof(struct options)); my_opts->oids = (eary *) myalloc(sizeof(eary)); my_opts->tables = (eary *) myalloc(sizeof(eary)); my_opts->filenodes = (eary *) myalloc(sizeof(eary)); my_opts->oids->num = my_opts->oids->alloc = 0; my_opts->tables->num = my_opts->tables->alloc = 0; my_opts->filenodes->num = my_opts->filenodes->alloc = 0; /* parse the opts */ get_opts(argc, argv, my_opts); if (my_opts->dbname == NULL) { my_opts->dbname = "postgres"; my_opts->nodb = true; } pgconn = sql_conn(my_opts); /* display only tablespaces */ if (my_opts->tablespaces) { if (!my_opts->quiet) printf("All tablespaces:\n"); sql_exec_dumpalltbspc(pgconn, my_opts); PQfinish(pgconn); exit(0); } /* display the given elements in the database */ if (my_opts->oids->num > 0 || my_opts->tables->num > 0 || my_opts->filenodes->num > 0) { if (!my_opts->quiet) printf("From database \"%s\":\n", my_opts->dbname); sql_exec_searchtables(pgconn, my_opts); PQfinish(pgconn); exit(0); } /* no elements given; dump the given database */ if (my_opts->dbname && !my_opts->nodb) { if (!my_opts->quiet) printf("From database \"%s\":\n", my_opts->dbname); sql_exec_dumpalltables(pgconn, my_opts); PQfinish(pgconn); exit(0); } /* no database either; dump all databases */ if (!my_opts->quiet) printf("All databases:\n"); sql_exec_dumpalldbs(pgconn, my_opts); PQfinish(pgconn); return 0; }
/* * Show oid, filenode, name, schema and tablespace for each of the * given objects in the current database. */ void sql_exec_searchtables(PGconn *conn, struct options * opts) { char *todo; char *qualifiers, *ptr; char *comma_oids, *comma_filenodes, *comma_tables; bool written = false; char *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" "; /* get tables qualifiers, whether names, filenodes, or OIDs */ comma_oids = get_comma_elts(opts->oids); comma_tables = get_comma_elts(opts->tables); comma_filenodes = get_comma_elts(opts->filenodes); /* 80 extra chars for SQL expression */ qualifiers = (char *) myalloc(strlen(comma_oids) + strlen(comma_tables) + strlen(comma_filenodes) + 80); ptr = qualifiers; if (opts->oids->num > 0) { ptr += sprintf(ptr, "c.oid IN (%s)", comma_oids); written = true; } if (opts->filenodes->num > 0) { if (written) ptr += sprintf(ptr, " OR "); ptr += sprintf(ptr, "pg_catalog.pg_relation_filenode(c.oid) IN (%s)", comma_filenodes); written = true; } if (opts->tables->num > 0) { if (written) ptr += sprintf(ptr, " OR "); sprintf(ptr, "c.relname ~~ ANY (ARRAY[%s])", comma_tables); } free(comma_oids); free(comma_tables); free(comma_filenodes); /* now build the query */ todo = (char *) myalloc(650 + strlen(qualifiers)); snprintf(todo, 650 + strlen(qualifiers), "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n" "FROM pg_catalog.pg_class c \n" " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n" " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),\n" " pg_catalog.pg_tablespace t \n" "WHERE relkind IN ('r', 'i', 'S', 't') AND \n" " t.oid = CASE\n" " WHEN reltablespace <> 0 THEN reltablespace\n" " ELSE dattablespace\n" " END AND \n" " (%s) \n" "ORDER BY relname\n", opts->extended ? addfields : "", qualifiers); free(qualifiers); sql_exec(conn, todo, opts->quiet); }
/* * Actual code to make call to the database and print the output data. */ int sql_exec(PGconn *conn, const char *todo, bool quiet) { PGresult *res; int nfields; int nrows; int i, j, l; int *length; char *pad; /* make the call */ res = PQexec(conn, todo); /* check and deal with errors */ if (!res || PQresultStatus(res) > 2) { fprintf(stderr, "oid2name: query failed: %s\n", PQerrorMessage(conn)); fprintf(stderr, "oid2name: query was: %s\n", todo); PQclear(res); PQfinish(conn); exit(-1); } /* get the number of fields */ nrows = PQntuples(res); nfields = PQnfields(res); /* for each field, get the needed width */ length = (int *) myalloc(sizeof(int) * nfields); for (j = 0; j < nfields; j++) length[j] = strlen(PQfname(res, j)); for (i = 0; i < nrows; i++) { for (j = 0; j < nfields; j++) { l = strlen(PQgetvalue(res, i, j)); if (l > length[j]) length[j] = strlen(PQgetvalue(res, i, j)); } } /* print a header */ if (!quiet) { for (j = 0, l = 0; j < nfields; j++) { fprintf(stdout, "%*s", length[j] + 2, PQfname(res, j)); l += length[j] + 2; } fprintf(stdout, "\n"); pad = (char *) myalloc(l + 1); MemSet(pad, '-', l); pad[l] = '\0'; fprintf(stdout, "%s\n", pad); free(pad); } /* for each row, dump the information */ for (i = 0; i < nrows; i++) { for (j = 0; j < nfields; j++) fprintf(stdout, "%*s", length[j] + 2, PQgetvalue(res, i, j)); fprintf(stdout, "\n"); } /* cleanup */ PQclear(res); free(length); return 0; }
/* calculate the number of leading spaces for sequence output; example: <leading spaces>gucugcaugacugacugacugacuguagcugcaugcaugcaugcacugaugca (-20.4) ....(((.....))).........((((.....))))................ */ void fs_init_leading_space(char energy, char shrepProb, char dbString, char shapeString, char prob, char rank){ tformat_string *itr; int pos, rpos; char dbinside; char *t, *s; s = t = (char *) myalloc(adp_statmem, (FORMAT_LENGTH+100) * sizeof(char)); leading_space_db = (char *) myalloc(adp_statmem, 100 * sizeof(char)); dbinside = 0; itr = format_string_struct; while (itr) { switch(itr->type) { case FORMAT_ENERGY: if (energy) sprintf(s, itr->string, -10.0); break; case FORMAT_SHREPPROB: if (shrep_prob_show && shrepProb) sprintf(s, itr->string, 0.5); break; case FORMAT_DBSTRING: if (dbString) sprintf(s, "{"); if (!dbinside) { sprintf(leading_space_db, itr->string, "{"); fs_remove_color_commands(leading_space_db); pos = 0; while(leading_space_db[pos]) { if (leading_space_db[pos]=='{') { leading_space_db[pos]=0; break; } if ((leading_space_db[pos]=='\t') || (leading_space_db[pos]=='\n')) pos++; else leading_space_db[pos++]=' '; } } dbinside = 1; break; case FORMAT_SHAPESTRING: if (shapeString) sprintf(s, itr->string, "[][]"); break; case FORMAT_PROB: if (prob) sprintf(s, itr->string, 0.5); break; case FORMAT_RANK: if (rank) sprintf(s, itr->string, 1); break; case FORMAT_VERB: sprintf(s, "%s", itr->string); break; } s = s + strlen(s); itr = itr->next; } fs_remove_color_commands(t); s[0] = 0; pos=0; rpos=0; while(t[pos]) { if (t[pos]=='{') { t[pos]=0; break; } if ((t[pos]=='\t') || (t[pos]=='\n')) pos++; else t[pos++]=' '; } if (!dbString || !dbinside) t[0]=0; leading_space = t; }