Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a file without using remove system call in a C program?

I've been curious how rem in Linux works and trying to write my own C code that can delete a file but when I searched for the answer, I only got the programs that were using remove() system call.

Is there any other way of doing it without using system call like writing your own code to do the job?

I've accomplished copying file through C filing but can't find a solution to delete a file through C.

like image 736
Datta Avatar asked May 15 '13 11:05

Datta


2 Answers

int unlink (const char *filename)

The unlink function deletes the file name filename. The function unlink is declared in the header file unistd.h. This function returns 0 on successful completion, and -1 on error

like image 148
Dayal rai Avatar answered Oct 05 '22 10:10

Dayal rai


If you want to delete a file use the

remove

function. If you want to have a look behind the scenes of the standard library, you may download the source of the glibc (e.g.) and have a look at the implementation. You will see that actually a INTERNAL_SYSCALL will be performed on linux os:

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

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

If you want to go further and even not use that syscall you will have to implement your own file system logic since the file system syscall just gives an abstraction layer to different filesystems.

like image 41
urzeit Avatar answered Oct 05 '22 09:10

urzeit