Skip to content
Snippets Groups Projects
Commit 3b794a8c authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add -r option for opening the file to lock read-only

It makes a difference on *nix.
parent d9d86d6a
No related branches found
No related tags found
No related merge requests found
#include <stdlib.h>
#include <string.h>
#include "filewrap.h"
int main(int argc, char** argv)
{
int access = O_RDWR;
const char* path = argv[1];
if(argc < 4) {
fprintf(stderr, "usage: lockfile path offset length\n");
fprintf(stderr, "usage: lockfile [-r] path offset length\n");
printf("-r open file read-only instead of read/write\n");
return 0;
}
int argn = 1;
if(strcmp(argv[argn], "-r") == 0) {
access = O_RDONLY;
++argn;
}
const char* path = argv[argn++];
int file = open(path, access);
if(file < 0) {
puts("Open failure");
......@@ -18,7 +25,7 @@ int main(int argc, char** argv)
return -1;
}
int result = lock(file, atoi(argv[2]), atoi(argv[3]));
int result = lock(file, atoi(argv[argn]), atoi(argv[argn + 1]));
if(result != 0) {
puts("Lock failure");
perror(path);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment