Skip to contentSkip to conversation

NFS and C Language

DISCUSSION RLN 3 participants 4 messages Dec 9, 2005

Subsystems (2)
lore
Share & tools
lore

RLN

author
#0 lore raw

Hi!

I want to write simple C language program to read and write a file in = NFS file system.

Please give a link or advice to write a program using C language.

Also help for which calls I have to use.

Send instant messages to your online friends http://in.messenger.yahoo.com

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#1 lore raw

Le Vendredi 09 D=E9cembre 2005 12:12, RLN a =E9crit=A0:

Quoted 9 lines

Hi!

I want to write simple C language program to read and write a file in NFS file system.

Please give a link or advice to write a program using C language.

Also help for which calls I have to use.

You access NFS as other file systems...
Generic C howto about files manipulations works for NFS.

Vincent

_______________________
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char **argv){
    char path[]=3D"/path/to/the/testfile";
    char string[]=3D"hello world!";
    int fd;

    fd=3Dopen(path,O_WRONLY|O_CREAT);
    if(fd<0){
        perror("Open");
        return 0;
    }
    write(fd, string,strlen(string));
    return 0;
}

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#2 lore raw
Quoted 7 lines · Vincent Roqueta

Le Vendredi 09 D=E9cembre 2005 12:12, RLN a =E9crit :
=20

Quoted 4 lines

Hi!

I want to write simple C language program to read and write a file in N=

FS

Quoted 21 lines
Quoted 7 lines

file system.

Please give a link or advice to write a program using C language.

Also help for which calls I have to use.
=20

You access NFS as other file systems...
Generic C howto about files manipulations works for NFS.

Vincent

_______________________
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

Quoted 14 lines
int main(int argc, char **argv){
   char path[]=3D"/path/to/the/testfile";
   char string[]=3D"hello world!";
   int fd;

   fd=3Dopen(path,O_WRONLY|O_CREAT);
   if(fd<0){
       perror("Open");
       return 0;
   }
   write(fd, string,strlen(string));
   return 0;
}

Actually, this little program may or may not work on an NFS mounted file system. It contains a bug in that the open(2) system call takes three arguments and the third, the mode, is used when O_CREAT is specified and the file does not already exist. If the random bits, which the kernel uses because the mode was not actually specified, are wrong, like for example require mandatory locking, then the write(2) call may fail. Also, the write(2) call returns whether or not it succeeded and how much data was written. The close(2) call, which was not explicitly coded, but should have been, is also used to return whether any error occurred while trying to write data to the NFS server.

Thanx...

ps

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#3 lore raw

Le Vendredi 09 D=E9cembre 2005 15:10, Peter Staubach a =E9crit=A0:

Quoted 6 lines
Quoted 5 lines · Vincent Roqueta

Le Vendredi 09 D=E9cembre 2005 12:12, RLN a =E9crit :

Quoted 4 lines

Hi!

I want to write simple C language program to read and write a file in N=

=46S

Quoted 34 lines
Quoted 20 lines
Quoted 5 lines

file system.

Please give a link or advice to write a program using C language.

Also help for which calls I have to use.

You access NFS as other file systems...
Generic C howto about files manipulations works for NFS.

Vincent

_______________________
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

Quoted 13 lines
int main(int argc, char **argv){
   char path[]=3D"/path/to/the/testfile";
   char string[]=3D"hello world!";
   int fd;

   fd=3Dopen(path,O_WRONLY|O_CREAT);
   if(fd<0){
       perror("Open");
       return 0;
   }
   write(fd, string,strlen(string));
   return 0;
}
Quoted 10 lines

Actually, this little program may or may not work on an NFS mounted file system. It contains a bug in that the open(2) system call takes three arguments and the third, the mode, is used when O_CREAT is specified and the file does not already exist. If the random bits, which the kernel uses because the mode was not actually specified, are wrong, like for example require mandatory locking, then the write(2) call may fail. Also, the write(2) call returns whether or not it succeeded and how much data was written. The close(2) call, which was not explicitly coded, but should have been, is also used to return whether any error occurred while trying to write data to the NFS server.

Right...
You can add I return 0 even if the program fails...

I have taken a little less than 3 minutes to write this little example :) I hope C howto examples will be better :)

Vincent

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs