From 41974ae5265fbc23a06f276f9e008d5dad020e0b Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Thu, 30 Aug 2012 16:16:25 -0700 Subject: Rename files for C++ conversion. Does not compile. After updating, you can remove these files, as shown in 'git status': Untracked files: (use "git add ..." to include in what will be committed) src/map/magic-interpreter-lexer.c src/map/magic-interpreter-parser.c src/map/magic-interpreter-parser.h --- src/common/lock.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/common/lock.cpp (limited to 'src/common/lock.cpp') diff --git a/src/common/lock.cpp b/src/common/lock.cpp new file mode 100644 index 0000000..2ba9a0a --- /dev/null +++ b/src/common/lock.cpp @@ -0,0 +1,36 @@ +#include +#include +#include "lock.hpp" +#include "socket.hpp" + +/// Protected file writing +/// (Until the file is closed, it keeps the old file) + +// Start writing a tmpfile +FILE *lock_fopen (const char *filename, int *info) +{ + char newfile[512]; + FILE *fp; + int no = getpid (); + + // Get a filename that doesn't already exist + do + { + sprintf (newfile, "%s_%d.tmp", filename, no++); + } + while ((fp = fopen_ (newfile, "r")) && (fclose_ (fp), 1)); + *info = --no; + return fopen_ (newfile, "w"); +} + +// Delete the old file and rename the new file +void lock_fclose (FILE * fp, const char *filename, int *info) +{ + char newfile[512]; + if (fp) + { + fclose_ (fp); + sprintf (newfile, "%s_%d.tmp", filename, *info); + rename (newfile, filename); + } +} -- cgit v1.2.3-60-g2f50