Thread: How to tell if a input is file or directory

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    2
    Quote Originally Posted by john.c View Post
    Your "err" routine is pointless.
    I made it a function to avoid redundancy, because I might use the same error message in the future.
    Quote Originally Posted by john.c View Post
    And you need to check argc before accessing argv[1].
    Thanks for telling me, I didn't notice it.
    Quote Originally Posted by john.c View Post
    \e is a non-standard escape sequence. Better to use \033 or \x1b.
    I'll make sure to fix that too. What is the difference between these three escape sequences?
    Also, thanks for the example code.

    EDIT:
    Here is my updated code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    
    
    #define ERRMSG "\033[1;91mError:\033[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]);
        }
        if(message == 4)
        {
            printf("%s\"%s\" is not a normal file", ERRMSG, &args[0]);
        }
        puts("");
        exit(EXIT_FAILURE);
    }
    
    
    int main(int argc, char* argv[])
    {
        FILE *fptr;
        
        if(argc < 2)
        {
            err(0, NULL);
        }
        else if(argc > 2)
        {
            err(1, NULL);
        }
        
        fptr = fopen(argv[1], "r");
        
        if(fptr == NULL)
        {
            err(2, argv[1]);
        }
        
        struct stat st;
        fstat(fileno(fptr), &st);
        
        if ((st.st_mode & S_IFMT) == S_IFDIR)
        {
            err(3, argv[1]);
        }
        
        if ((st.st_mode & S_IFMT) != S_IFREG)
        {
            err(4, argv[1]);
        }
        
        fclose(fptr);
        return 0;
    }
    I am still using the err function for the reasons stated above.
    Last edited by teal; 05-19-2022 at 03:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listing files in a user input specified directory
    By BigLe2e in forum C Programming
    Replies: 5
    Last Post: 07-20-2013, 01:01 PM
  2. Replies: 1
    Last Post: 12-06-2012, 02:46 PM
  3. Replies: 6
    Last Post: 04-30-2010, 06:13 PM
  4. Determine if input is file or directory
    By JizJizJiz in forum C++ Programming
    Replies: 20
    Last Post: 06-29-2006, 12:51 AM
  5. File Input/Output Path Directory
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 02-27-2002, 03:00 PM

Tags for this Thread