Hi, I am a beginner at C programming, and I want to tell if the input is a file or a directory. Here is my code so far:
Code:
#include <stdio.h>#include <sys/stat.h>


#define ERRMSG "\e[1;91mError:\e[0m "
#define USAGE "\nUsage: thing [filename]"


int err(unsigned char message, char args[])
{
    if(message == 0)
    {
        printf("%stoo few arguments%s", ERRMSG, USAGE);
    }
    
    if(message == 1)
    {
        printf("%stoo many arguments%s", ERRMSG, USAGE);
    }
    
    if(message == 2)
    {
        printf("%sfile \"%s\" not found", ERRMSG, &args[0]);
    }
    
    if(message == 3)
    {
        printf("%s\"%s\" is a directory", ERRMSG, &args[0]);
    }
    puts("");
    return 1;
}


int main(int argc, char* argv[])
{
    FILE *fptr;
    fptr = fopen(argv[1], "r");
    
    if(argc < 2)
    {
        err(0, NULL);
    }
    else if(argc > 2)
    {
        err(1, NULL);
    }
    else if(fptr == NULL)
    {
        err(2, argv[1]);
    }
    return 0;
}
I know that you nee to use stat, but I do not know how to use it to detect if it is a file or directory. I am using linux.