I just want to get the read() system call to do it's thing! LOL It's now 3:30 AM in the morning, and as I'm the stubborn type I don't like to go to bed with unsolved code
However I submit humbly now to anyone who can show me what my dumb mistake is. The program imitates a daemon process that so far checks to see if a specific file exists, if not then it creates it. It then locks the file and attempts to read the file while locked. If my variable fileLength is any other value than 0, the program will lock and you have to CTRL-C out. If anyone wants to help a newbie learn the ways of Unix Network Programming, please enlighten me. Here is the code, It's commented well and easy to understand, everything works except read(), which I want to read the whole file, beginning to end. Thanks to anyone who cares
:
//Program: fileinfodaemon
// By Matt Johnson
/************** INCLUDES *******************/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h> //We need this for finding out statistics
//of files
#include <unistd.h> //We need this for our locking functions
/******************************************/
/************ DEFINES ********************/
#define FILENAME "shared2.txt"
#define MAXBUFF (1024)
/******************************************/
/******** VARIABLE DECLARATION ****************/
int fd; //File Descriptor
int loopFlag = 0; //Loop condition for while loop
int n; //Index for buff[] array
int bytesRead = 0;
struct stat fileBuffer; //buffer that holds file statistics
unsigned long fileLength = 0; // determines the length of the file
char *buff = NULL;
/*********************************************/
int main()
{
/* This opens whatever named file appears under the FILENAME define. If that file does not exist, The program creates that file. */
//If fd == -1, then the file doesn't exist
if( fd = open( FILENAME, O_RDWR ) == -1 )
{
printf("Shared file does not exist: FD = %d\n", fd);
/* Since the file does not exist, we must create it, and supply the individual and group read write access rights to the file. */
fd = creat( FILENAME, 0664 );
printf("creating %s :FD = %d\n", FILENAME, fd );
}//end if
/* If we've made it this far the file exist and it is opened */
printf("File has been opened\n");
stat ( FILENAME, &fileBuffer );
while ( loopFlag == 0 )
{
sleep( 3 );
/* Enough time has passed for us to check the shared file
to see if any requests have been written to it. We need to
lock the shared file before we attempt to open it, and
unlock the file after we are done reading from it. */
//The file needs to be locked from beginning to end.
//lseek ensures that the file is locked from the beginning.
lseek( fd, 0, 0 ); /* Rewind file */
if( lockf( fd, F_LOCK, 0 ) == 0 )
{
printf("Shared file successfully locked.\n");
//The file has been successfully locked, now it must be read.
/*The commented line below is what I want to use, but it
locks up the program once read() is called*/
//fileLength = fileBuffer.st_size;
buff = (char *) calloc( MAXBUFF + 1, sizeof( char ));
printf("FD: %d \n", fd); //debugging
//ERROR OCCURS HERE if fileLength is anything other than 0
bytesRead = read( fd, buff, fileLength );
printf("file has been read. Bytes read: %d\n", bytesRead);
}//end if
}//end while
}//end main