How to delete (completely delete) a file using C without using a system call?

I was curious how rem in Linux works and tries to write its own C code that can delete a file, but when I looked for the answer, I received only programs that used the remove() call.

Is there any other way to do this without using a system call, for example, to write my own code to do this work?

I copied the file using a C entry, but cannot find a solution to delete the file through C.

+4
source share
4 answers

If you want to delete the file, use

 remove 

function. If you want to look behind the scenes of the standard library, you can download the glibc source (for example) and look at the implementation. You will see that on linux os INTERNAL_SYSCALL will be executed:

 result = INTERNAL_SYSCALL (unlink, err, 1, file); 

(from / sysdeps / unix / sysv / linux / unlinkat.c from the debian package eglibc-2.15)

If you want to go further and not even use this syscall, you will have to implement your own file system logic, since the system call of the file system simply provides an abstraction level for different file systems.

+4
source
 int unlink (const char *filename) 

The unlink function removes the file name file name. The unlink function is declared in the unistd.h header file. This function returns 0 on success and -1 on error

+10
source

If you do not want to use the clean, normal way, you can open / dev / sd ** and play with your file system.

Btw, remove () is not syscall (man 3 remove).

+2
source

The traditional way to delete a file is to use the unlink (2) function, which is called from remove (3) if the path is a file.

+1
source

Source: https://habr.com/ru/post/1480934/


All Articles