char isReadOnly = 0;
FilemapOpenCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]){	int theErr = 0;	/*	first (second) parameter is the variable name,		second (third) parameter is the database path */	if (objc != 4) {		Tcl_WrongNumArgs(interp, 1, objv, "open filemapName path");		return TCL_ERROR;	}		do {		const char* thePath;		Tcl_Obj* theObject;		SFilemapObject* theFilemapObject;		int theLockFD;		struct flock theLock;		char isReadOnly = 0;		SNode* theRoot = NULL;			thePath = Tcl_GetString(objv[3]);						/* open the lock file */		{			char theLockPath[PATH_MAX];			theLockPath[sizeof(theLockPath) - 1] = 0;			(void) snprintf(				theLockPath, sizeof(theLockPath) - 1, "%s.lock", thePath);			theLockFD = open(theLockPath, O_RDWR | O_CREAT, 0664);			if (theLockFD >= 0)			{				/* Get a R/W lock on it (wait if required) */				theLock.l_type = F_WRLCK;				theLock.l_whence = SEEK_SET;				theLock.l_start = 0;				theLock.l_len = 0;				if (fcntl(theLockFD, F_SETLKW, &theLock) == -1)				{					theErr = errno;					break;				}			} else {				theErr = errno;				if (theErr == EACCES)				{					theErr = 0;					/* try again without R/W */					isReadOnly = 1;					theLockFD = open(theLockPath, O_RDONLY, 0);					if (theLockFD < 0)					{						theErr = errno;						break;					}					theLock.l_type = F_RDLCK;					theLock.l_whence = SEEK_SET;					theLock.l_start = 0;					theLock.l_len = 0;					if (fcntl(theLockFD, F_SETLKW, &theLock) == -1)					{						theErr = errno;						break;					}				} else {					break;				}			}		}				/* load the map from the file */		theErr = Load(thePath, &theRoot);		if (theErr != 0)		{			if (theRoot)			{				Free(&theRoot);			}						/* Close the lock */			(void) close(theLockFD);			break;		}				/* Create the object */		theObject = Tcl_NewObj();		theFilemapObject = (SFilemapObject*) ckalloc(sizeof(SFilemapObject));		theFilemapObject->fRefCount = 1;		(void) strncpy(			theFilemapObject->fFilemapPath,			thePath,			sizeof(theFilemapObject->fFilemapPath));		theFilemapObject->fLockFD = theLockFD;		theFilemapObject->fRoot = theRoot;		theFilemapObject->fIsReadOnly = isReadOnly;		theFilemapObject->fIsDirty = 0;		theObject->internalRep.otherValuePtr = (VOID*) theFilemapObject;		theObject->typePtr = &tclFilemapType;				/* Save it in global variable */		(void) Tcl_ObjSetVar2(					interp,					objv[2],					NULL,					theObject,					0);	} while (0);		return SetResultFromErrorCode(interp, theErr);}