C application that copy/moves a file & uses system calls in Linux

compma

I'm having a hard time making a system call to unlink a file work in my C code. I want to remove the file from the filesystem after the code is copied. I am getting an error that says:

declared here extern int unlink (const char *__name) __THROW __nonnull ((1));
 #include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
int main(int argc, char * args [])
{
    int infile, outfile;
    int numofbytesread;
    char buffer[20];

    infile = open(args[1], O_RDONLY, 0700);

    if (infile == ENOENT)
    {
            printf("Could not find file");
            return 1;
    }

    outfile == open(args[2], O_WRONLY | O_CREAT, 0700);

    while ((numofbytesread = read(infile, buffer, 20))){
            write(outfile, buffer, numofbytesread);
    }
    close(infile);
    close(outfile);

     unlink();
 return 0;
 }
Thirupathi Thangavel

After copying, you can invoke the unlink system call.

unlink(args[1])

But be sure to check if the copy was successful before removing the file.

Ref: https://www.gnu.org/software/libc/manual/html_node/Deleting-Files.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Same programm works in C, but not in C++ (uses linux system calls)

From Dev

Linux System calls in C on OSX

From Dev

Calls are getting routed to the driver, when application uses poll() and not with epoll() in linux

From Dev

libuv uses blocking file system calls internally – Why? How?

From Java

Linux system calls vs C lib functions

From Dev

How to Mock Linux System Calls in C

From Dev

Basics questions regarding File and I/O System Calls in C (on Linux/UNIX)

From Dev

How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

From Dev

Concurrent system calls in Linux

From Dev

Linux:System calls for Who

From Java

intercepting file system system calls

From Dev

List the system calls used in an application

From Dev

C system calls fails

From Dev

C system calls open()

From Dev

Linux bare system calls, not glibc

From Dev

Accessing linux local file system from java web application

From Dev

File descriptors table and system calls

From Dev

Assembly Linux system calls vs assembly OS x system calls

From Dev

Build a Linux c++ application runnable on system having libc >= 2.31

From Java

System calls vs C/C++ system calls

From Dev

Storing System Calls in C Programming

From Java

How many system calls are there in linux kernel 2.6?

From Dev

Can I use Linux system calls in android?

From Dev

Why are linux system calls different across architectures

From Java

Are function calls like read() , write() actual system calls in linux?

From Dev

C++ Header File Are Not Included - C++ Sample Application For Linux

From Dev

Geting the file size of a system application on windows in C++

From Dev

Linux file system architecture

From Dev

Distributed file system for linux

Related Related

  1. 1

    Same programm works in C, but not in C++ (uses linux system calls)

  2. 2

    Linux System calls in C on OSX

  3. 3

    Calls are getting routed to the driver, when application uses poll() and not with epoll() in linux

  4. 4

    libuv uses blocking file system calls internally – Why? How?

  5. 5

    Linux system calls vs C lib functions

  6. 6

    How to Mock Linux System Calls in C

  7. 7

    Basics questions regarding File and I/O System Calls in C (on Linux/UNIX)

  8. 8

    How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

  9. 9

    Concurrent system calls in Linux

  10. 10

    Linux:System calls for Who

  11. 11

    intercepting file system system calls

  12. 12

    List the system calls used in an application

  13. 13

    C system calls fails

  14. 14

    C system calls open()

  15. 15

    Linux bare system calls, not glibc

  16. 16

    Accessing linux local file system from java web application

  17. 17

    File descriptors table and system calls

  18. 18

    Assembly Linux system calls vs assembly OS x system calls

  19. 19

    Build a Linux c++ application runnable on system having libc >= 2.31

  20. 20

    System calls vs C/C++ system calls

  21. 21

    Storing System Calls in C Programming

  22. 22

    How many system calls are there in linux kernel 2.6?

  23. 23

    Can I use Linux system calls in android?

  24. 24

    Why are linux system calls different across architectures

  25. 25

    Are function calls like read() , write() actual system calls in linux?

  26. 26

    C++ Header File Are Not Included - C++ Sample Application For Linux

  27. 27

    Geting the file size of a system application on windows in C++

  28. 28

    Linux file system architecture

  29. 29

    Distributed file system for linux

HotTag

Archive