Back to index

wims  3.65+svn20090927
Functions
basic.c File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void error (char *s)
void * xmalloc (size_t n)
unsigned long int texint (void *bp, int l)
long int texintsigned (void *bp, int l)
char * find_word_start (char *p)
char * find_word_end (char *p)
int search_list (void *list, int items, size_t item_size, const char *str)
int getfile (char *fname, unsigned char **buf)
int execredirected (char *cmdf, char *inf, char *outf, char *errf, char *arg[])
int call_sh (char *s,...)
void mkdirs (char *s)

Function Documentation

int call_sh ( char *  s,
  ... 
)

Definition at line 148 of file basic.c.

{
    va_list vp;
    char buf[MAX_LINELEN+1];
    char *parmbuf[4];
    int t;

    va_start(vp,s);
    vsnprintf(buf,sizeof(buf),s,vp);
    va_end(vp);
    parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL;
    wrapexec=0;
    t=execredirected("sh",NULL,NULL,NULL,parmbuf);
    sync(); return t;
}

Here is the call graph for this function:

void error ( char *  s)

Definition at line 20 of file basic.c.

{
    fprintf(stderr,"%s: %s\n",progname, s);
    exit(1);
}
int execredirected ( char *  cmdf,
char *  inf,
char *  outf,
char *  errf,
char *  arg[] 
)

Definition at line 121 of file basic.c.

{
    pid_t pid;
    int status;

    fflush(NULL);    /* flush all output streams before forking
                      * otherwise they will be doubled */
    pid=fork(); if(pid==-1) return -1;
    if(!pid) {       /* child */
       if(inf!=NULL) freopen(inf,"r",stdin);
       if(outf!=NULL) freopen(outf,"w",stdout);
       if(errf!=NULL) freopen(errf,"w",stderr);
       if(wrapexec) {
           setreuid(getuid(),getuid());setregid(getgid(),getgid());
       }
       if(strchr(cmdf,'/')) execv(cmdf,arg);
       else execvp(cmdf,arg);
       exit(127);
    }
    else {    /* parent */
       status=0; waitpid(pid,&status,0);
       return WEXITSTATUS(status);
    }
}
char* find_word_end ( char *  p)

Definition at line 68 of file basic.c.

{
    int i;
    for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
    return p;
}
char* find_word_start ( char *  p)

Definition at line 60 of file basic.c.

{
    int i;
    for(i=0; isspace(*p); p++,i++);
    return p;
}
int getfile ( char *  fname,
unsigned char **  buf 
)

Definition at line 103 of file basic.c.

{
    FILE *f;
    long l, l2;
    
    f=fopen(fname,"r"); if(f==NULL) return -1;
    fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
    if(l>FILE_LENGTH_LIMIT || l<=0) {
       fclose(f); return -1;
    }
    *buf=xmalloc(l+16);
    l2=fread(*buf,1,l,f); fclose(f);
    if(l!=l2) {
       free(*buf); return -1;
    }
    return l;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void mkdirs ( char *  s)

Definition at line 165 of file basic.c.

{
    struct stat st;
    char *buf;
    if(stat(s,&st)==-1) {
       if(strrchr(s,'/')!=NULL) {
           buf=xmalloc(strlen(s)+1);
           strcpy(buf,s); *strrchr(buf,'/')=0;
           mkdirs(buf); free(buf);
       }
       mkdir(s,-1);
    }
}

Here is the call graph for this function:

int search_list ( void *  list,
int  items,
size_t  item_size,
const char *  str 
)

Definition at line 77 of file basic.c.

{
    int i1,i2,j,k;
    char **p;
    char c;
    
    if(items<=0) return -1;
    j=0; c=*str;
    p=list;
    k=**p-c; if(k==0) k=strcmp(*p,str);
    if(k==0) return k; if(k>0) return -1;
    p=list+(items-1)*item_size; 
    k=**p-c; if(k==0) k=strcmp(*p,str);
    if(k==0) return items-1; if(k<0) return -1;
    for(i1=0,i2=items-1;i2>i1+1;) {
       j=i1+(i2-i1)/2;
       p=list+(j*item_size);
       k=**p-c; if(k==0) k=strcmp(*p,str);
       if(k==0) return j;
       if(k>0) {i2=j; continue;}
       if(k<0) {i1=j; continue;}   
    }
    return -1;
}
unsigned long int texint ( void *  bp,
int  l 
)

Definition at line 34 of file basic.c.

{
    unsigned long int o, t;
    unsigned char c, *p;
    int i;
    o=t=0; p=bp;
    for(i=0;i<l;i++) {
       c=p[i]; t=c; o<<=8; o+=t;
    }
    return o;
}

Here is the caller graph for this function:

long int texintsigned ( void *  bp,
int  l 
)

Definition at line 46 of file basic.c.

{
    long int o, t;
    signed char c, *p;
    int i;
    if(l<=0) return 0;
    p=bp; o=*p;
    for(i=1;i<l;i++) {
       c=p[i]; t=c; o<<=8; o|=t&255;
    }
    return o;
}

Here is the caller graph for this function:

void* xmalloc ( size_t  n)

Definition at line 26 of file basic.c.

{
    void *p;
    p=malloc(n);
    if(p==NULL) error("Malloc failure.");
    return p;
}

Here is the call graph for this function:

Here is the caller graph for this function: