Solved How to determine what type of file is it?

when looking for a file type that can be opened and read (the contents within the file) or not. How to determine what type of file is it?
C:
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>



int isDirectory(const char *path)
{
     struct stat status;
     stat(path, &status);
  
     if (S_ISDIR(status.st_mode))
        return 1;
  
    return 0;
  
}

int is_regular_file(const char *path)
{
    struct stat path_stat;
    stat(path, &path_stat);
    return S_ISREG(path_stat.st_mode);
}

int main (int argc, const char **argv) {
        if (argc < 2) {
            printf("Need a path to dir or file\n");
            return EXIT_FAILURE;
        }
      
        if (isDirectory(argv[1])) {
            printf("is dir %s\n", argv[1]);
        }
        else
        {
            printf(" is not a dir %s\n", argv[1]);
        }
      
        if (is_regular_file(argv[1])) {
            printf("is file %s\n", argv[1]);
        }
        else
        {
            printf(" is not a file %s\n", argv[1]);
        }
  
     return EXIT_SUCCESS;
}
using this guild line
what type of file would I be lookng for to determine between an image file and a file with contents within it to be opened via fopen?

reg file returns for both as a match for reg file
Code:
userx@FreeBSD.net:/usr/home/userx/test
$ ./a.out  /home/userx/SavedImages
is not a dir /home/userx/SavedImages
is file /home/userx/SavedImages

//
userx@FreeBSD.net:/usr/home/userx/test
$ ./a.out /home/userx/wallhaven-42ok96.jpg
is not a dir /home/userx/wallhaven-42ok96.jpg
is file /home/userx/wallhaven-42ok96.jpg
 
what type of file would I be lookng for to determine between an image file and a file with contents within it to be opened via fopen?
Any file can be opened and read with fopen(3) (as long as you have read access to it of course). The file's actual content is completely irrelevant.

What determines the type of content is dictated by the first few bytes, generally called the "magic number", see magic(5).

 
Otay, I remembered I have this function I wrote in my other file I'm using.
C:
int check_file_ext(char *name1)
{
    /*
    returns:
         
         1 if proper extension found
         0 if improper extension found
    */

    char *ext = NULL;
    char *extensions[] = {".jpg", ".JPG", ".png", ".PNG", ".jpeg",
     ".JPEG", ".xpm", ".gif"};

    ext = strrchr(name1, '.');
    if (!ext)
         return 0;  

    for (int i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
       if ( !strcmp( extensions[i], ext) )
            return 1;

    return 0;
}
 
Any file can be opened and read with fopen(3) (as long as you have read access to it). The file's contents are completely irrelevant.

What determines the type of content is dictated by the first few bytes, called the "magic number", see magic(5).

that is my very suspicion. I posted a function I remember I wrote for that issue in what I am doing. but that magic I'll look at to see what I see. thanks.
 
So-called file extensions (.jpg, .doc, etc) are a relic from the old DOS era (8.3 file naming). Unix(-like) systems have never relied on them. Heck, even Windows doesn't actually use them any more, although it still insists on that naming convention.
 
So-called file extensions (.jpg, .doc, etc) are a relic from the old DOS era (8.3 file naming). Unix(-like) systems have never relied on them. Heck, even Windows doesn't actually use them any more, although it still insists on that naming convention.
that is what this function does, if no ext then return 0 ... if ext found check to see if it is an image file extension, which I have always seen jpg, gif, etc.. for images only. I do not even use txt, or doc in Linux or FBSD. As stated that is a Windows thing. I do not even use the .sh for scripts.

in the context of this program if image load it as an image, if not then try opening it then read it and use the same function to get the paths to the images out of the file.

it still has a fail safe, because the read line in a file sends one line at a time. and checks it just the same, if no image extension then skip it, go to next line.

gimp still uses extentions for images,, so ... what are we talking here? I am just refering to images ver files with the contents of paths to images within it. anything else falls under user error.

Yet, still some error checking and validating is needed.
 
userxbw,

There is an executable called "file". Use that followed by the path and name of the file and it will tell you if it's a text file or binary.
 
userxbw,

There is an executable called "file". Use that followed by the path and name of the file and it will tell you if it's a text file or binary.
this is what I've pieced together using magic.
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <magic.h>



int isDirectory(const char *path)
{
     struct stat status;
     stat(path, &status);
   
     if (S_ISDIR(status.st_mode))
        return 1;
   
    return 0;
   
}

int is_regular_file(const char *path)
{
    struct stat path_stat;
    stat(path, &path_stat);
    return S_ISREG(path_stat.st_mode);
}


int check_file_ext(char *name1)
{
        /*

    returns:

         1 if proper extension found

         0 if improper extension found

    */

    char *ext = NULL;
    char *extensions[] = {".jpg", ".JPG", ".png", ".PNG", ".jpeg",
     ".JPEG", ".xpm", ".gif"};

    ext = strrchr(name1, '.');
    if (!ext)
         return 0;

    for (int i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
       if ( !strcmp( extensions[i], ext) )
            return 1;

    return 0;
}



int main (int argc, const char **argv) {
        if (argc < 2) {
            printf("Need a path to dir or file\n");
            return EXIT_FAILURE;
        }
       
        char *actual_file = strdup( argv[1] );
        const char *magic_full;
        char line[1024] = "";
        magic_t magic_cookie;
        FILE *pt;
        magic_cookie = magic_open(MAGIC_MIME_TYPE);
   
        if (magic_cookie == NULL) {
            printf("unable to initialize magic library\n");
            return 1;
        }
   
        if (magic_load(magic_cookie, NULL) != 0) {
            printf("cannot load magic database - %s\n", magic_error(magic_cookie));
            magic_close(magic_cookie);
            return 1;
        }
        magic_full = magic_file(magic_cookie, actual_file);
        if ( ! strcmp( "text/plain"  , magic_full) ) {
                    if (( pt = fopen(actual_file, "r") ) == NULL ) {
                    printf("cannot open file\n");
                    return EXIT_FAILURE;
                }
               
                while ( fgets(line, sizeof(line), pt) != NULL )
                {
                   
                        if ( ! check_file_ext(line) ) {
                            printf("%s\n", line);
                        } else {
                            printf("improper ext\n");
                        }
                   
                }
            }
            else
            {
                printf("no file to read in:: %s\n", actual_file );
            }
           
        magic_close(magic_cookie);
        if (pt)
            fclose(pt);
        free(actual_file);
       
     return EXIT_SUCCESS;
}
clang finddir.c -o finddir -lmagic
 
userxbw,

type "man file" to see the various options.
userxbw,

type "man file" to see the various options.
I thought of that too, but I wasn't sure if I wanted to call outside commands ( sys commands) into the program.

Code:
Maximum number of clients reachedmhsetroot:  Unable to open display unix:0.0
can't open term.. lol .. got bail and come back to man that just to see.

okay back:
interesting..
Code:
     -C, --compile
             Write a magic.mgc output file that contains a pre-parsed version
             of the magic file or directory.
     -m, --magic-file magicfiles
             Specify an alternate list of files and directories containing
             magic.  This can be a single item, or a colon-separated list.  If
             a compiled magic file is found alongside a file or directory, it
             will be used instead.
 
Back
Top