mine.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
short int aleph_recurs (char *one, char *two);
void *alphabetize (char **list, short int sz);
char *appendend (char *string);
int buflen (char *buffer);
char *checkline(char *line);
void chomp (char *line);
short int copytmp (char *tmp, char *perm);
void *ec_malloc (unsigned int bytes);
void fatal (int retv, char *messg);
int file_check (char *file, int field);
short int filetext (char *text, char *file);
void gap (short int secs, short int hundredths);
char *getbin (char *filename, int *ptr);
int intest (char *string);
char *linefile (char *file);
char *linein (FILE *stream);
short int loclsckt (char *file);
short int loclconn (char *socket, int fd);
char *regexp (char *string, char *rgxp);
char *returnline (char *file, char *match, int nofl);
int strexd (int EXPlen, char *string);
int togbool (int *boo);
char *EXDline;
struct matchspec {
int bgn;
int end;
} rgxp;
short int aleph_recurs (char *one, char *two) {
short int depth=0, dv, sw, retv=0;
while (1) { dv = depth; sw = 0;
if ((one[depth] == 0) && (two[depth] == 0)) return 0;
else if (one[depth] == 0) return 1;
else if (two[depth] == 0) return 2;
if (one[depth] >= 65 && one[depth] <=90) {one[depth]+=32;sw++;}
if (two[depth] >= 65 && two[depth] <=90) {two[depth]+=32;sw+=2;}
if (one[depth] == two[depth]) depth++;
else if (one[dv] < two[dv]) {
retv=1;
} else if (one[dv] > two[dv]) {
retv=2;
}
if (sw > 0) {
switch (sw) {
case 1:
one[dv]-=32;
break;
case 2:
two[dv]-=32;
break;
case 3:
two[dv]-=32;
one[dv]-=32;
break;
default:
puts("!aleph_recurs ERROR");
}
}
if (retv > 0) return retv;
} }
void *alphabetize (char **list, short int sz) {
short int i, sw, seq;
char *tmp;
while (1) {
sw = 0;
for (i=0; i<(sz-1); i++) {
seq = aleph_recurs(list[i],list[i+1]);
if (seq == 2) {
tmp = list[i+1];
list[i+1] = list[i];
list[i] = tmp;
sw = 1;
}
}
if (sw == 0) break;
}
}
char *appendend (char *string) {
int len = strlen(string);
char *copy;
if (string[len+1] == 0) return string;
copy=ec_malloc(len+2);
strcpy(copy,string);
copy[len+1] = 0;
return copy;
}
int buflen (char *buffer) {
int blen = strlen(buffer), NofL=0, i;
if (buffer == NULL) return 0;
for (i=0; i<=blen; i++)
if (((buffer[i] == 10)) || ((buffer[i] == 0) && (buffer[i-1] != 10))) NofL++;
return NofL;
}
char *checkline(char *line) {
short int len = strlen(line), i=1;
if (len == 0) return NULL;
if (line[0] == 32) {
while (1) {
if (line[i] == 32) {
i++;
continue;}
if (line[i] == 0) return NULL;
if (line[i] > 126) return NULL;
else break;
}
return line;
}
}
void chomp (char *line) {
size_t len = strlen(line);
if ((len>0) && (line[len-1]==10)) line[len-1]=0;
}
short int copytmp (char *tmp, char *perm) {
short int retv = 0;
char *line;
FILE *fstRO, *fstW;
if ((fstRO=fopen(tmp, "r")) == NULL) return -4;
if ((fstW=fopen(perm, "w")) == NULL) {
fclose(fstRO); return -5;}
while ((line=linein(fstRO)) != NULL) {fprintf(fstW,"%s",line);free(line);}
if ((fclose(fstRO)) != 0) retv = -1;
if ((fclose(fstW)) != 0) retv -= 2;
if (unlink(tmp)<0) retv -= 10;
return retv;
}
void *ec_malloc (unsigned int bytes) {
void *assign;
assign = malloc(bytes);
if (assign == NULL) fatal(-99, "ec_malloc got NULL");
return assign;
}
void fatal (int retv, char *messg) {
pid_t pid=getpid();
puts(messg);
if (retv>0) kill(pid,retv);
exit(retv);
}
int file_check (char *file, int field) {
int ftype;
struct stat info;
if (stat(file,&info)<0) {if (errno==2) return -2;
else return 0;}
ftype=(info.st_mode & S_IFMT);
return ftype;
}
short int filetext (char *text, char *file) {
FILE *FST_mine = fopen(file, "w");
if (FST_mine == NULL) return -1;
fprintf(FST_mine,"%s",text);
if (fclose(FST_mine) != 0) return 1;
return 0;
}
void gap (short int secs, short int hundredths) {
struct timespec interval;
interval.tv_sec=secs;
interval.tv_nsec=hundredths*10000;
nanosleep(&interval,NULL);
}
char *getbin (char *filename, int *ptr) {
char *buffer;
struct stat finfo;
FILE *fstRO = fopen(filename, "rb");
if (fstRO == NULL) return NULL;
stat(filename,&finfo);
*ptr=finfo.st_size;
if ((buffer=ec_malloc(finfo.st_size)) == NULL) {
fclose(fstRO); return NULL;}
if (fread(buffer,1,finfo.st_size,fstRO) != finfo.st_size) {
free(buffer);
fclose(fstRO); return NULL;}
fclose(fstRO); return buffer;
}
int intest (char *string) {
int i=0, retv=0;
if (!string) return -1;
while (string[i] != 0) {
if ((string[i] < 48) || (string[i] > 57)) retv++;
i++;}
return retv;
}
char *linefile (char *file) {
size_t len, mem=0;
char *cumul = NULL, *line = NULL;
FILE *FST_mine = fopen(file, "r");
if (FST_mine == NULL) return line;
while ((line = linein(FST_mine)) != NULL) {
len = strlen(line);
if (mem == 0) { mem = len+1;
cumul = ec_malloc(mem);
strcpy(cumul,line);
}
else { mem += len;
cumul = realloc(cumul,mem);
if (cumul == NULL) perror("realloc fail in mine.h:linefile()");
strcat(cumul,line);
}
free(line);
}
if (fclose(FST_mine) != 0) fatal(-40, "fclose fail in mine.h:linefile()");
return cumul;
}
char *linein (FILE *stream) {
size_t len=0;
char *line = NULL;
if((getline(&line,&len,stream)) == -1) return NULL;
return line;
}
short int loclconn (char *socket, int fd) {
size_t size;
struct sockaddr_un peer;
peer.sun_family=AF_LOCAL;
strcpy(peer.sun_path,socket);
size=SUN_LEN(&peer);
return connect(fd,(struct sockaddr*)&peer,size);
}
short int loclsckt (char *file) {
struct sockaddr_un name;
int sock;
socklen_t size;
if ((strlen(file)) > 107) return -2;
if ((sock=socket(PF_LOCAL,SOCK_STREAM,0)) < 0) return -1;
name.sun_family = AF_LOCAL;
strcpy(name.sun_path, file);
size=SUN_LEN(&name);
if ((bind(sock,(struct sockaddr*)&name,size) < 0)) {
close(sock); return -3;}
return sock;
}
char *regexp (char *string, char *patrn) {
short int i, w=0, len;
char *word = NULL;
regex_t rgT;
regmatch_t match;
regcomp(&rgT,patrn,REG_EXTENDED);
if ((regexec(&rgT,string,1,&match,0)) == 0) {
rgxp.bgn = (int)match.rm_so;
rgxp.end = (int)match.rm_eo;
len = rgxp.end-rgxp.bgn;
word=ec_malloc(len+1);
for (i=rgxp.bgn; i<rgxp.end; i++) {
word[w] = string[i];
w++; }
word[w]=0;
}
regfree(&rgT);
return word;
}
char *returnline (char *file, char *match, int nofl) {
short int len=strlen(match), i;
char *line, *retline, *ptr;
FILE *fstRO = fopen(file, "r");
if (fstRO == NULL) return NULL;
while ((line=linein(fstRO)) != NULL) {
if ((strncmp(line,match,len)) == 0) {
retline=ec_malloc(strlen(line)+1);
strcpy(retline,line);
free(line);
for (i=1; i<nofl; i++) {
if ((line=linein(fstRO)) == NULL) {free(line);break;}
len=strlen(retline);
ptr = realloc(retline, strlen(line)+len+1);
if (ptr) retline = ptr;
else fatal(-77,"realloc fail in mine.h: returnline()");
strcat(retline, line);
}
if ((fclose(fstRO)) != 0) perror("mine.h: in returnline() fclose fail");
return retline;}
free(line);
}
if ((fclose(fstRO)) != 0) perror("mine.h: in returnline() fclose fail");
return NULL;
}
int strexd (int EXPlen, char *string) {
char *ptr;
int len = strlen(string);
if (EXPlen == 0) {
EXDline = ec_malloc(len+1);
strcpy(EXDline,string);
return len;
} else { EXPlen += len;
ptr = realloc(EXDline,EXPlen+1);
if (ptr) EXDline=ptr;
else fatal(-98, "strexd realloc fail...");
strcat(EXDline,string);
return EXPlen;
}
}
int togbool (int *boo) {
if (*boo==0) *boo=1;
else *boo=0;
return *boo;
}