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

前端 未结 5 867
抹茶落季
抹茶落季 2021-01-06 02:39

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 t

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 03:16

    #include
    #include
    #include
    #include 
    #include 
    
    
    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;
    }
    

提交回复
热议问题