I'm writing a C Linux program for college, using low-level file I/O (I have to use fcntl, basically). I need to lock 8 bytes past the end of the file, to append some new data. Trying this as below returns an Invalid argument error.
struct flock field_lock;
field_lock.l_type = F_WRLCK;
field_lock.l_whence = SEEK_CUR;
field_lock.l_start = 0;
field_lock.l_len = 2 * sizeof(int);
// ...
lseek(stocks_fd, 0, SEEK_END);
// ...
fcntl(stocks_fd, F_SETLKW, &field_lock);
Any idea how else I could achieve this, or what I'm doing wrong?
edit 1: https://gist.github.com/limelier/5a7ba8ab166a1f586a3c4feec355b83b Entire program with an improvement applied to the EOF locks and writing logic, as recommended in the comments below. Same problem still exists, though, and a sample output has been appended.