Append a record to the end of a text file: Difference between revisions

Added general comment.
(Added general comment.)
Line 2,038:
 
=={{header|Nim}}==
This solution defines a procedure "getlock" which try the get an exclusive (write) lock on the file. On Posix systems, the standard Posix advisory locking mechanism, which works at process level, is used. On other systems, especially Windows, the procedure does nothing and returns true as there is no need for such a mechanism.
 
 
Note that on Posix system, other programs may still write on the file if they do not check if an exclusive lock is already set on the file.
 
 
As several other solutions, we have chosen to parse the "passwd" lines even if it not required.
<lang Nim>import posix
import strutils
Line 2,125 ⟶ 2,132:
## Try to get an exclusive write lock on file "f". Return false is unsuccessful.
 
when defined(posix):
var flock = TFlock(l_type: cshort(F_WRLCK), l_whence: cshort(SEEK_SET), l_start: 0, l_len: 0)
result = f.getFileHandle().fcntl(F_SETLK, flock.addr) >= 0
else:
result = true
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
Anonymous user