5

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Datta
  • 150
  • 1
  • 1
  • 9
  • 3
    Any reason why you don't want to use the remove-call? – Gjordis May 15 '13 at 11:55
  • I dont think we can achieve this, as deleting a file from memory (i.e. hard disc) is as simple as our code is interacting with harwares, and for this we have to take use of kernels(i.e. System Calls). – CodeCodeCode May 15 '13 at 12:01
  • `remove` isn't even a system call. – Cairnarvon May 15 '13 at 12:02
  • May be [This](http://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx) can be useful. – Ayse May 15 '13 at 12:02
  • Possible duplicate of [How to remove a file in C program?](https://stackoverflow.com/q/5769785/608639) – jww Dec 19 '19 at 15:43

5 Answers5

11
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

Dayal rai
  • 6,548
  • 22
  • 29
  • I mentioned above i dont want to ue system calls is it possible with c filing like we do copying with it – Datta May 15 '13 at 13:14
  • 2
    @Datta: You can't remove anything without using a system call. The only question is how convenient the system call will be, and how direct. For example, you can use `system()` to run the `rm` command, but that uses `fork()` and `exec*()` and `waitpid()` and other system calls in the program that calls `system()`, and the `rm` program ends up using the `unlink()` system call anyway, because there's no way to do it without using a system call, whether that's via 'INTERNAL_SYSCALL' (a system call interface) or via some other mechanism. An ordinary program can't delete a file; only the kernel can. – Jonathan Leffler Apr 19 '15 at 05:38
6

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.

urzeit
  • 2,863
  • 20
  • 36
2

If you don't want to use the clean, usual way, you can open /dev/sd** and play with your file system.

Btw, remove() isn't a syscall (man 3 remove).

yoones
  • 2,394
  • 1
  • 16
  • 20
1

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

jbr
  • 6,198
  • 3
  • 30
  • 42
  • 1
    Or better... use the `remove()` function which does the very same thing, but is C standard. `#include ... int remove(const char *filename);` – Lundin May 15 '13 at 12:01
0
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


int main(){
        int status;
        pid_t pid = fork();
        if(-1 == pid){
                printf("fork() failed");
                exit(EXIT_FAILURE);
        }else if(pid == 0){
                execl("/bin/sh", "sh", "-c", "rm /tmp/san.txt", (char *) NULL);
        }else{
                printf("[%d]fork with id %d\n",pid);
                waitpid(pid,&status,0);
        }
return 0;
}
Sandeep_black
  • 1,352
  • 17
  • 18