Skip to content

Commit

Permalink
Remove some global variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
magsilva committed May 11, 2015
1 parent 90a2b28 commit 203ee50
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 95 deletions.
2 changes: 1 addition & 1 deletion lib/disco.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ gravarq(FILE *fp, char buf[], int len)
{
int i = fwrite(buf, 1, len, fp);
if (i != len) {
fprintf(stderr, "Error saving data to file %s.%s (%d from %d bytes)", nomearq(fp), extarq(fp), i, len);
fprintf(stderr, "Error saving data to file %s%s (%d from %d bytes)", nomearq(fp), extarq(fp), i, len);
return ERRO;
}
return OK;
Expand Down
61 changes: 20 additions & 41 deletions tcase/lib/playinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,18 @@ pty_fork(int *ptrfdm, int pipe_out[], int pipe_err[])
}
}

int
playinput(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(),
int (*tr_err)(), long vtimer, long rtimer,
char *prog, char *argv[])
int playinput(int *ret_code, FILE *fp_in, int (*tr_in)(), FILE *fp_out, int (*tr_out)(), FILE *fp_err, int (*tr_err)(), long vtimer, char *prog, char *argv[])
{
int i, c, bytes, ret1, k, j;
struct itimerval t1;
long tt2, tt1;
struct rlimit resource_limit;
int fim_le;
int pipe_out[2], pipe_err[2], fdm;
pid_t pid, pipe_fork();
int i, c, bytes, ret1, k, j;
struct itimerval t1;
long tt2, tt1;
struct rlimit resource_limit;
int fim_le;
int pipe_out[2], pipe_err[2], fdm;
pid_t pid, pipe_fork();



pid = pty_fork(&fdm, pipe_out, pipe_err);
pid = pty_fork(&fdm, pipe_out, pipe_err);

if (pid < 0)
return ERRO;
Expand All @@ -204,14 +200,6 @@ pid_t pid, pipe_fork();
signal(SIGVTALRM, SIG_DFL);
setitimer(ITIMER_VIRTUAL, &t1, NULL);
}
if (rtimer != 0)
{
tick2timeval(rtimer, &t1.it_value);
t1.it_interval.tv_sec = t1.it_value.tv_sec ;
t1.it_interval.tv_usec = t1.it_value.tv_usec;
signal(SIGALRM, SIG_DFL);
setitimer(ITIMER_REAL, &t1, NULL);
}
execv(prog,argv );
return ERRO;
}
Expand All @@ -228,7 +216,7 @@ pid_t pid, pipe_fork();
bytes = read(pipe_out[0],buff,BUFFSIZE);
else bytes = 0;

if (tr_out != NULL && (k = tr_out(buff, bytes,pid)) != bytes)
if (tr_out != NULL && (k = tr_out(fp_out, buff, bytes,pid)) != bytes)
{
if (k != ERRO) k = OK;
goto fim;
Expand All @@ -240,7 +228,7 @@ pid_t pid, pipe_fork();
bytes = read(pipe_err[0],buff,BUFFSIZE);
else bytes = 0;

if (tr_err != NULL && (k = tr_err(buff, bytes, pid)) != bytes)
if (tr_err != NULL && (k = tr_err(fp_err, buff, bytes, pid)) != bytes)
{
if (k != ERRO) k = OK;
goto fim;
Expand Down Expand Up @@ -275,23 +263,14 @@ pid_t pid, pipe_fork();
}

}
/* sometimes, itimer doesn't work then this is a
guarantee for aborting looping mutants */
tt2 = gettimedad() - tt1;
if (rtimer != 0 && tt2 > rtimer)
{
kill(pid, SIGKILL);
k = 1; c = -1;
goto fim;
}
}
}
do {

if (ioctl(pipe_out[0],FIONREAD,&bytes) != -1 && bytes > 0)
bytes = read(pipe_out[0],buff,BUFFSIZE);
else bytes = 0;

if (tr_out != NULL && (k =tr_out(buff, bytes, pid)) != bytes)
if (tr_out != NULL && (k =tr_out(fp_out, buff, bytes, pid)) != bytes)
{
if (k != ERRO) k = OK;
goto fim;
Expand All @@ -304,7 +283,7 @@ pid_t pid, pipe_fork();
bytes = read(pipe_err[0],buff,BUFFSIZE);
else bytes = 0;

if (tr_err != NULL && (k = tr_err(buff, bytes, pid))!=bytes)
if (tr_err != NULL && (k = tr_err(fp_err, buff, bytes, pid))!=bytes)
{
if (k != ERRO) k = OK;
goto fim;
Expand Down Expand Up @@ -391,7 +370,7 @@ pipe_fork(int pipe_in[], int pipe_out[], int pipe_err[])
/**
* @param tr_out Handler function for data read from stdin. Its parameters are: byte buffer, size of byte buffer, pid of child process.
*/
int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr_err)(), long vtimer, char *prog, char *argv[])
int playbatch(int *ret_code, FILE *fp_in, int (*tr_in)(), FILE *fp_out, int (*tr_out)(), FILE *fp_err, int (*tr_err)(), long vtimer, char *prog, char *argv[])
{
int pid;
int pipe_out[2], pipe_err[2], pipe_in[2];
Expand Down Expand Up @@ -440,7 +419,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr
}
// Handle data
if (tr_out != NULL) {
result = tr_out(buff, bytes, pid);
result = tr_out(fp_out, buff, bytes, pid);
if (result == ERRO || result != bytes) {
result = ERRO;
goto fim;
Expand All @@ -460,15 +439,15 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr
}
// Handle data
if (tr_err != NULL) {
result = tr_err(buff, bytes, pid);
result = tr_err(fp_err, buff, bytes, pid);
if (result == ERRO || result != bytes) {
result == ERRO;
goto fim;
}
}

if ( ! fim_le) {
bytes = tr_in(buff, BUFFSIZE);
bytes = tr_in(fp_in, buff, BUFFSIZE);
if (bytes < 0) {
result = ERRO;
goto fim;
Expand Down Expand Up @@ -504,7 +483,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr
}
// Handle data
if (tr_out != NULL) {
result = tr_out(buff, bytes, pid);
result = tr_out(fp_out, buff, bytes, pid);
if (result == ERRO || result != bytes) {
result = ERRO;
goto fim;
Expand All @@ -527,7 +506,7 @@ int playbatch(int *ret_code, int input, int (*tr_in)(),int (*tr_out)(), int (*tr
}
// Handle data
if (tr_err != NULL) {
result = tr_err(buff, bytes, pid);
result = tr_err(fp_err, buff, bytes, pid);
if (result == ERRO || result != bytes) {
result == ERRO;
goto fim;
Expand Down
105 changes: 52 additions & 53 deletions tcase/lib/tcase_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,50 +77,45 @@ tcase_ex_bat(char *dir, char *test, char *direx, char *exec, char *param)
}


FILE *fpin, *fpout, *fperr;

int le_input(char *buff, int n)
int le_input(FILE *file, char *buff, int n)
{
int nread;

nread = fread( buff, 1, n, fpin);
if (nread <= 0)
{
if ( ferror(fpin))
{
msg("Error reading input file");
return ERRO;
int nread;
nread = fread( buff, 1, n, file);
if (nread <= 0) {
if (ferror(file)) {
msg("Error reading input file");
return ERRO;
}
}
if (feof(fpin))
return 0;
}
return nread;
if (feof(file)) {
return 0;
} else {
return nread;
}
}

int trata_out(char *buff, int n, pid_t pid)
int trata_out(FILE *file, char *buff, int n, pid_t pid)
{
int nread;
nread = gravarq(fpout, buff, n);
if (nread != OK)
return ERRO;
return n;
int nread;
nread = gravarq(file, buff, n);
if (nread != OK) {
return ERRO;
}
return n;
}

int trata_err(char *buff, int n, pid_t pid)
int trata_err(FILE *file, char *buff, int n, pid_t pid)
{
int nread;
nread = gravarq(fperr, buff, n);
if (nread != OK)
return ERRO;
return n;
int nread;
nread = gravarq(file, buff, n);
if (nread != OK) {
return ERRO;
}
return n;
}


exec_from_ascii(direx, exec, instrum, param, dir, teste,
reg, timeout, use_instrum, shel, interactive)
char *exec, *instrum, *param, *direx, *dir, *teste, *shel;
REG_TCASE *reg;
int timeout, use_instrum, interactive;
int
exec_from_ascii(char *direx, char *exec, char *instrum, char *param, char *dir, char *teste, REG_TCASE *reg, int timeout, int use_instrum, char *shel, int interactive)
{
long t1, tt1, t2, tt2;
int xret_cod, ret_cod, k;
Expand All @@ -131,30 +126,34 @@ char *p;
char inst_exec_fullname[PATH_MAX];
char test_fullname[PATH_MAX];
char **argv;
FILE *fpin, *fpout, *fperr;



if (tty_save(STDIN_FILENO) == ERRO)
return ERRO;

t1 = gettimechild();
tt1 = times(&xtms);

fpin = abrearq(dir, teste, SUFIXO_INPUT, 0);
if (fpin == NULL)
return ERRO;
fpout = criarq(dir, teste, SUFIXO_OUTPUT);
if (fpout == NULL)
{
fecharq(fpin);
return ERRO;
}
fperr = criarq(dir, teste, SUFIXO_ERRO);
if (fperr == NULL)
{
fecharq(fpin);
fecharq(fpout);
return ERRO;
}
monta_nome(exec_fullname, direx, exec, "");
fpin = abrearq(dir, teste, SUFIXO_INPUT, 0);
if (fpin == NULL) {
return ERRO;
}

fpout = criarq(dir, teste, SUFIXO_OUTPUT);
if (fpout == NULL) {
fecharq(fpin);
return ERRO;
}

fperr = criarq(dir, teste, SUFIXO_ERRO);
if (fperr == NULL) {
fecharq(fpin);
fecharq(fpout);
return ERRO;
}
monta_nome(exec_fullname, direx, exec, "");

if (use_instrum) {
monta_nome(inst_exec_fullname, direx, instrum, "");
Expand All @@ -167,10 +166,10 @@ char *p;

if (interactive) {
/* aloca_master(); */
k = playinput(&ret_cod, fileno(fpin), le_input, trata_out, trata_err, timeout*100, timeout*100, p, argv);
k = playinput(&ret_cod, fpin, le_input, fpout, trata_out, fperr, trata_err, timeout*100, p, argv);
/* libera_master(); */
} else {
k = playbatch(&ret_cod, fileno(fpin), le_input, trata_out, trata_err, timeout*100, p, argv);
k = playbatch(&ret_cod, fpin, le_input, fpout, trata_out, fperr, trata_err, timeout*100, p, argv);
}
free_array_str(argv);

Expand Down

0 comments on commit 203ee50

Please sign in to comment.