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 
 
posted @   a318013800  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2023-04-25 调试 Flutter 应用
2023-04-25 编译android13
2023-04-25 android内核源码下载和编译 ,下载工具bootimg-tools.git解压并替换内核
2023-04-25 自己动手调试Android源码(超简单)
2023-04-25 Android Studio导入Android 4.4.4r1的源码
2023-04-25 Android内核的编译和调试 工具
2017-04-25 Android http请求
点击右上角即可分享
微信分享提示