File locking text file in linux using C

File locking text file in linux using C

Post by Neur » Tue, 28 Mar 2000 04:00:00



Hi,

I'm relatively new to programming, I'm learning C.
I'm working with text files for CGI currently, and
I heard about file locking, it sounds like something
that I could use.  Does anyone have a snippet
code of this function? I saw an example for it
in a dos based program, but the library in unix
does not exist (or is different, I don't know what),
so I got a an error.  I have no idea how to write it
from scractch so any help will be greatly
appreciated, thank you!

-neuron

 
 
 

File locking text file in linux using C

Post by Fredrik Roube » Tue, 28 Mar 2000 04:00:00




> I'm relatively new to programming, I'm learning C.  I'm working with
> text files for CGI currently, and I heard about file locking, it sounds
> like something that I could use.  Does anyone have a snippet code of
> this function? I saw an example for it in a dos based program, but the
> library in unix does not exist (or is different, I don't know what), so
> I got a an error.  I have no idea how to write it from scractch so any
> help will be greatly appreciated, thank you!

This code snippet is the function for reading and updating the visitor
counter in a CGI:

#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define COUNTER "/var/run/counter"

int update_counter(void)
{
        int     fd, counter;
        FILE    *file;

        if ((fd = open(COUNTER, O_RDWR)) == -1)
                return -1;

        if (flock(fd, LOCK_EX) == -1)
                return -1;

        if ((file = fdopen(fd, "r+")) == NULL)
                return -1;

        fscanf(file, "%i", &counter);
        counter ++;
        rewind(file);
        fprintf(file, "%i\n", counter);

        fclose(file);
        flock(fd, LOCK_UN);
        close(fd);

        return counter;

Quote:}

The use of fdopen() is to make it possible to use fscanf(), rewind() and
fprintf() so although not necessary I think it makes things easier.

H?lsningar // Fredrik Roubert

--


http://www.efd.lth.se/~d95fr/         SE-222 40 Lund

 
 
 

File locking text file in linux using C

Post by Neur » Wed, 29 Mar 2000 04:00:00


Fredrik, Thank you! It's EXACTLY what I was looking for!
Great!

-Neuron
http://www.geeko.com/
Hardware Overclockers Search Engine and News




>> I'm relatively new to programming, I'm learning C.  I'm working with
>> text files for CGI currently, and I heard about file locking, it sounds
>> like something that I could use.  Does anyone have a snippet code of
>> this function? I saw an example for it in a dos based program, but the
>> library in unix does not exist (or is different, I don't know what), so
>> I got a an error.  I have no idea how to write it from scractch so any
>> help will be greatly appreciated, thank you!
>This code snippet is the function for reading and updating the visitor
>counter in a CGI:
>#include <stdio.h>
>#include <sys/file.h>
>#include <sys/types.h>
>#include <sys/stat.h>
>#include <fcntl.h>
>#include <unistd.h>
>#define COUNTER "/var/run/counter"
>int update_counter(void)
>{
>    int     fd, counter;
>    FILE    *file;
>    if ((fd = open(COUNTER, O_RDWR)) == -1)
>            return -1;
>    if (flock(fd, LOCK_EX) == -1)
>            return -1;
>    if ((file = fdopen(fd, "r+")) == NULL)
>            return -1;
>    fscanf(file, "%i", &counter);
>    counter ++;
>    rewind(file);
>    fprintf(file, "%i\n", counter);
>    fclose(file);
>    flock(fd, LOCK_UN);
>    close(fd);
>    return counter;
>}
>The use of fdopen() is to make it possible to use fscanf(), rewind() and
>fprintf() so although not necessary I think it makes things easier.
>H?lsningar // Fredrik Roubert
>--


>http://www.efd.lth.se/~d95fr/         SE-222 40 Lund