Home > Net >  implicit declaration of function ‘write’; did you mean ‘fwrite’?
implicit declaration of function ‘write’; did you mean ‘fwrite’?

Time:11-16

I compiled an example of a simple note-taking program that uses file descriptors from a book, and I've been getting some compiler errors related to a "write" and a "close" function, along with the use of the "strncat" function. Here is the code:

#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 *);             // A function for fatar errors
void *ec_malloc(unsigned int);  // An error-checked malloc() wrapper

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)                     // If there aren't command-line arguments,
                usage(argv[0], datafile); // display usage message and exit.

        strcpy(buffer, argv[1]); // Copy into buffer.

        printf("[DEBUG] buffer   @ %p: \'%s\'\n", buffer, buffer);
        printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);

        strncat(buffer, "\n", 1); // Add a newline on the end.

        // Opening file
        fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
        if(fd == -1)
                fatal("in main() while opening file");
        printf("[DEBUG] file descriptor is %d\n", fd);
        //Writing data
        if(write(fd, buffer, strlen(buffer)) == -1)
                fatal ("in main() while writing buffer to file");
        //Closing file
        if(close(fd) == -1)
                fatal ("in main() while closing file");

        printf("Note has been saved.\n");
        free(buffer);
        free(datafile);
}

// A function to display and error message and then exit
void fatal(char *message) {
        char error_message[100];

        strcpy(error_message, "[!!] Fatal Error ");
        strncat(error_message, message, 83);
        perror(error_message);
        exit(-1);
}

// An error-checked malloc() wrapper function
void *ec_malloc(unsigned int size) {
        void *ptr;
        ptr = malloc(size);
        if(ptr == NULL) {
                fatal("in ec_malloc() on memory allocation");
                exit(-1);
        }
        return ptr;
}

And this is the compiler error:

implenote.c: In function ‘main’:
simplenote.c:39:5: warning: implicit declaration of function ‘write’; did you mean ‘fwrite’? [-Wimplicit-function-declaration]
   39 |  if(write(fd, buffer, strlen(buffer)) == -1)
      |     ^~~~~
      |     fwrite
simplenote.c:42:5: warning: implicit declaration of function ‘close’; did you mean ‘pclose’? [-Wimplicit-function-declaration]
   42 |  if(close(fd) == -1)
      |     ^~~~~
      |     pclose
simplenote.c:31:2: warning: ‘strncat’ specified bound 1 equals source length [-Wstringop-overflow=]
   31 |  strncat(buffer, "\n", 1); // Add a newline on the end.

The code was copied from the book without any alteration, i would like to know why this is happening and what i can do to make the code run. Note that the book (Hacking: The Art of Exploitation, Second Edition) is a bit dated and was released in 2008.

CodePudding user response:

To access the Posix low level file interface such as open, read, write and close, you should include <unistd.h>.

Note however that your program does not seem to require such low level interface, you might consider creating your output file using standard streams declared in <stdio.h>, using fopen, fputs or fprintf and fclose.

  • Related