Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't this file lock code work?

Status
Not open for further replies.

SuaveRick

Programmer
Apr 12, 2004
142
CA
Can anyone solve this? I have a websphere state table calling this function to check if one of these files are locked (I test with 'locki' most of the time) but everytime it says the file is locked. The file IS EMPTY, I made sure of that, when you CAT locki there is nothing printed at all. Maybe someone else has some ideas:

int isLocked(char privCode[1]){
FILE *in;
char fileName[38];
int result;
result=0;
fprintf(stderr, "\nthe value of result before we check lock: %d", result);
fflush(stderr);
if (privCode == "H")
strcpy(fileName, "/home/dtuser/rick/SMR/Export/lockh");
else
strcpy(fileName, "/home/dtuser/rick/SMR/Export/locki");

if ((in = fopen(fileName,"r")) == NULL)
result = 0; /*0 is not locked*/
else
result = 1; /*1 is locked*/
fclose(in);
strcpy(fileName, "");
return(result);
 

At a quick glance ... your only checking to see if the file exists ... if it does, your setting result to 1 indicating a lock (by having the file specified present). It has nothing to do with the contents of file.

Lord Blood
 
Ok, that makes sense to me. Is this not the correct way to check then? Going with this logic, I'd have to delete the file when it's no longer in use. Would there be a better way to do this? This is just kind of like a flag to show if there is a user in the system, if there is then I want this file to be 'locked' so if another person comes in to do the same thing a guy #1 he can't until that file is 'unlocked'.
 
a) i prefer 'stat()' or 'access()'
b) pay a little more attention, i know systems they core
dump trying to close a NOT opened fd:

if ((in = fopen(fileName,"r")) == NULL)
result = 0; /*0 is not locked*/
else
result = 1; /*1 is locked*/
fclose(in);

write:
if ((in = fopen(fileName,"r")) == NULL) return(0);
fclose(in);
return(1);
or enclose the 'fclose' in the else statement

:) guggach
 
Thanks guggach, I have it working great now!! You guys helped me out lots.

Suave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top