I have recently started to learn C, however, I am writing a small sample/practice piece of code and it happens to show errors. I uses a file descriptor and some of the flags in the 'open' command arn't working even thought i appear to of included the correct header files. Its probably a simple problem I've over looked.
The problem comes where 'S_IRUSR' and 'S_IWUSR' appear to be undefined. I not written any more code past this so i will post all I've got.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
void usage(char *prog_name, char *filename)
{
printf("Usage: %s <data to add to %s>\n", prog_name, filename);
exit(0);
}
void fatal(char *);
void *ec_malloc(unsigned int);
int main(int argc, char *argv[])
{
int fd; //file descriptor
char *buffer, *datafile;
buffer = (char *) ec_malloc(100);
datafile = (char *)ec_malloc(20);
strcpy(datafile, "/tmp/notes");
if (argc > 2)
usage(argv[0], datafile);
strcpy(buffer, argv[1]);
printf("[DEBUG] buffer\t @ %p: \'%s\'\n", buffer, buffer);
printf("[DEBUG] datafile\t @ %p: \'%s\'\n", datafile, datafile);
strncat(buffer, "\n", 1);
//opening file - this line of code is causing the problem.
fd = open(datafile, O_WRONLY |O_CREAT | O_APPEND, S_IRUSR|S_IWUSR)
}
void fatal(char *message)
{
char error_message[100];
strcpy(error_message, "[!!] Fatal Error ");
strncat(error_message, message, 83);
perror(error_message);
exit(-1);
}
void *ec_malloc(unsigned int size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
Like I say I don't believe there to be any typos in there and as far as I am aware the correct headers are there but correct me if I am wrong. Thanks for any help.