-
Variable Oddities :D
I have written the following code. It is part of an operating environment for DOS I'm developing, and it's meant to accertain weather the file in inputstx is described in a data file as restricted to the current user. The function ECIREAD does all the file reading from this file, and it works like this:-
Code:
char *eciread(char *locationinfile, char *anotherlocationinfile, int characterstoread, int offsetfromlastlocation, int mode)
this is it:-
Code:
int qfs(char *inputstx) /* Returns 0 for OK, 42 if file is restricted, and 43 if extention is restricted.*/
{
char *tmp, tmp3[700], tmp5[700], tmp6[700], *tmp7;
FILE *tmp4;
int tmp2;
tmp2=0;
tmp7=inputstx;
do
{
tmp3[tmp2]=NULL;
tmp2=tmp2+1;
}
while(tmp2<673);
tmp2=0;
strcpy(tmp3, "f=");
strcat(tmp3, tmp7);
tmp=eciread("[ACCESS]", tmp3, 700, 0, 1);
if(tmp!=NULL)
{
return(42);
}
do
{
tmp3[tmp2]=NULL;
tmp2=tmp2+1;
}
while(tmp2<sizeof(tmp3));
tmp2=0;
tmp=chkext(tmp7, ".", 0);
strcpy(tmp3, "x=");
strcat(tmp3, tmp);
tmp=eciread("[ACCESS]", tmp3, 700, 0, 1);
if(tmp!=NULL)
{
return(43);
}
do
{
tmp3[tmp2]=NULL;
tmp5[tmp2]=NULL;
tmp6[tmp2]=NULL;
tmp2=tmp2+1;
}
while(tmp2<sizeof(tmp3));
tmp2=0;
system("cd >~qfs.tmo");
tmp4=fopen("~qfs.tmo", "r");
do
{
tmp=fgets(tmp3, 2, tmp4);
if(strcmp(tmp, "\n")==0)
{
tmp2=32767;
}
else
{
strcat(tmp6, tmp);
}
}
while(tmp2!=32767);
fclose(tmp4);
tmp2=0;
do
{
tmp3[tmp2]=NULL;
tmp2=tmp2+1;
}
while(tmp2<sizeof(tmp3));
strcat(tmp6, "\\");
strcat(tmp6, tmp7);
strcpy(tmp3, "f=");
strcat(tmp3, tmp6);
strlwr(tmp3);
tmp=eciread("[ACCESS]", tmp3, 700, 0, 1);
if(tmp!=NULL)
{
return(42);
}
return(0);
}
When I run it, it changes makes other char * variables previously defiened before main() null values, and I can't tell why. Any ideas?
..I just figured out that if I make the first do...while loop go round 672 times instead of 700 it works up til the point of the next loop... And that doesnt help me at all am i missing something blindingly obvious?
-
just nitpicking here, but
Code:
while(tmp2!=32767);
what use does that have? the best it could do is cause an endless loop, unless you're using multiple threads in which case you wouldn't be using dos anyway.
-
tmp2
It's meant to trip when the string in 'tmp' becomes '\n' - I'm reading the data from the file but not the \n that's on the end. It's not very good style I know but I'm not a very good programmer! lol :D
I hope my 1st post actually meant sense - I read thru it and was confused by my own words :D
-
1. give all your variables more meaningful names - tmp1 tmp2 etc does not do anything for understanding.
2. Try splitting the function into a couple of smaller functions which perform something more specific. Long rambling code does not make for good reading, and it's harder to change.
-
ding!
I've fixed it! I had called ECIREAD in a previous function to obtain the variable that cept becoming corrupt, and found that every time I called ECIREAD from this function is changed what was in the variable that was being corrupted..... I'll avoid that in future! Thanks people!:rolleyes: