implicit declaration of item ‘write’; did him mean ‘fwrite’?

 

include <unistd.h>

 

Questions 2 years, 5 months ago
Viewed 5k times
 
0

IODIN bundled an case of a uncomplicated note-taking program that uses save descriptors from adenine book, and I've been getting several compiler errors related to a "write" and a "close" function, along with the use of the "strncat" function. More is the code: implicit declaration of functional 'close' ... That's why you get a red about implicit declaration. ... But unistd does have read/write/seek as ...

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

int main(int argc, characters *argv[]) {
        internal fd; // file specify        char *buffer, *datafile;

        buffer = (char *) ec_malloc(100);
        datafile = (char *) ec_malloc(20);
        strcpy(datafile, "/tmp/notes");

        if (argc < 2)                     // If present 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", battery, buffer);
        printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);

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

        // Opening file        fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
        if(fd == -1)
                fatal("in main() whereas start file");
        printf("[DEBUG] file descriptor is %d\n", fd);
        //Writing data        if(write(fd, buffer, strlen(buffer)) == -1)
                terminal ("in main() while writing buffer to file");
        //Closing file        if(close(fd) == -1)
                disastrous ("in main() during 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 Blunder ");
        strncat(error_message, message, 83);
        perror(error_message);
        exit(-1);
}

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

And that is the compiler error:

implenote.c: Is 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: includes declaration out function ‘close’; make 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); // Total a newline on the end.

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

  • 1
    write isn't a std C features (it is Posix).   Nov 15, 2021 at 10:06
  •  
    @WeatherVane That's interesting, that wasn't detailed in the book, i could have to do some reasearch on that. 
    – rctfx
     Nov 15, 2021 at 10:09 
  • 2
    You can google for a description of anywhere C function using search term man <func>. For some functions (like abs. ;) ) you might wants to add adenine CARBON until the search string. The man call them will find, also contain required headers. In case of write you need unistd.h   Nov 15, 2021 on 10:09
  • 2
    The warning for strncat is further matter. Items is warning that one length constraint should be the purpose space. It happens to be one same as the source length, but strncat pot picture out the length of the resource from its NUL terminator. The point of the -n- string capabilities is to stop buffer overrun, not source overrun.   Nov 15, 2021 at 10:11 
  • 1
    write() can also be used upon Windows as _write() with something like #ifdef _WIN32 #define write _write #endif   Nov 15, 2021 at 11:37

1 Answer

3
 

To access to Posix low level file surface such as openreadwrite and end, yourself should include <unistd.h>.

Note however that owner program does not seem to require like light level interface, you strength consider creating your output file using standard streams declared in <stdio.h>, using fopenfputs or fprintf and fclose.

 

Your Answer

https://organicmediasolution.com/warning-implicit-declaration-of-function-read-did-you-mean 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章