shell / myremove.c
myremove.c
Raw
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) {
    
    int ec;
    
    struct stat check;
    
    if (argc != 2){
        fprintf(stderr, "Error: Invalid number of arguments.\n");
        return EXIT_FAILURE;
    }
    
    if (stat(argv[1], &check) == 0){
        
        if (S_ISREG(check.st_mode)){
            ec = unlink(argv[1]);
            if (ec != 0){
                fprintf(stderr, "An error occured while deleting the file.\n");
                return EXIT_FAILURE;
            }
        }
        
        else if (S_ISDIR(check.st_mode)){
            ec = rmdir(argv[1]);
            if (ec != 0){
                fprintf(stderr, "An error occured while deleting the directory.\n");
                return EXIT_FAILURE;
            }
        }
        
        else{
            fprintf(stderr, "Error: Invalid file type, not a directory or regular file.\n");
            return EXIT_FAILURE;
        }
    }
    
    else {
        fprintf(stderr, "Error: File could not be found.\n");
        return EXIT_FAILURE;
    }
    
}