0

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.

Daniel Baron
  • 34
  • 1
  • 5

2 Answers2

0

You forgot to include stdio.h, and a typo on S_IWUSR, and forgot an semi colon.

#include <stdlib.h>
#include <stdio.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;
}
user253751
  • 57,427
  • 7
  • 48
  • 90
Mark Nicolle
  • 307
  • 1
  • 11
0

Generally speaking, it's very helpful to include the exact messages that your compiler tells you.

In this case, my compiler would report:

cc     foo.c   -o foo
foo.c:8:5: warning: implicitly declaring library function 'printf' with type 'int (const char *, ...)'
    printf("Usage: %s <data to add to %s>\n", prog_name, filename);
    ^
foo.c:8:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'printf'
foo.c:35:63: error: use of undeclared identifier 'S_IWSUR'
    fd = open(datafile, O_WRONLY |O_CREAT | O_APPEND, S_IRUSR|S_IWSUR) 
                                                              ^
foo.c:44:5: warning: implicit declaration of function 'perror' is invalid in C99 [-Wimplicit-function-declaration]
    perror(error_message);
    ^
2 warnings and 1 error generated.

Reading this, we can find two errors:

  1. You need to include stdio.h for printf().
  2. S_IWSUR is misspelled. It should be S_IWUSR.
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • It didn't copy accross proper;y, i have included all those things yet it persists to not work. Im using the C/C++ compiler with visual basic and it says that the two flags are undefined. – Daniel Baron Feb 23 '15 at 00:26
  • @DanielBaron: Can you include the exact error messages that you are getting in your question? That is, can you copy it (or take a screenshot) and copy it directly word for word into your question? – Bill Lynch Feb 23 '15 at 00:28
  • 'error C2065: 'S_IRUSR' : undeclared identifier' and 'error C2065: 'S_IWUSR' : undeclared identifier' and this warning 'warning C4013: 'open' undefined assuming extern returning int' – Daniel Baron Feb 23 '15 at 00:32
  • @DanielBaron: What is your compiler (including version)? Presumably this is some version of visual studio? – Bill Lynch Feb 23 '15 at 00:32
  • Mircrosoft Visual Studio Express 2013 for windows desktop. Version 12.0.21005.1 REL and the Microsoft .NET Framework. Version 4.5.51209 – Daniel Baron Feb 23 '15 at 00:34
  • Additionally, could you post the complete output from the preprocessor for this file? http://stackoverflow.com/questions/8978997/how-can-i-see-the-output-of-the-visual-c-preprocessor Posting it to http://gist.github.com would be fine, as would inline in the question. – Bill Lynch Feb 23 '15 at 00:35
  • i think ive got the preprocessed file but its huge, its about 13,000 lines long most of it white space – Daniel Baron Feb 23 '15 at 00:45
  • @DanielBaron: Yep. That's what I would expect. And I'd love to be able to see the entire thing. – Bill Lynch Feb 23 '15 at 00:45