C – File Functions

Background

Wanted to package up a couple of functions in the C Language that allows one to determine if a file exists.

And, also to get the file type and last modification time.

We rely in the C function, stat.

Coincidentally, it is both a structure and a function.

Outline

  1. Functions
    • isFileExistsStats
      • Passes file name to the stat function
      • Returns back int & fills out stat structure
      • if return code is 0, then file exists
      • Else file does not exist
    • fileInfoDisplay
      • Passes file name to the stat function
      • Returns back int & fills out stat structure
      • If return code is not 0, then file does not exist
      • if return code is 0, then file exists
        • File Type
          • statbuf.st_mode & & S_IFMT
            • If S_IFDIR, then Directory
            • If S_IFREG, then  regular File
        • Modification Time
          • statbuf.st_mtime
            • To get Local time, pass statbuf.st_mtime to localtime function
        • Last Access Time
          • statbuf.st_atime
            • To get Local time, pass statbuf.st_atime to localtime function

Code

Function isFileExistsStats


int isFileExistsStats
(
    const char *filename
)
{

    struct stat buffer;
    int exist;

    exist = stat
            (
                  filename
                , &buffer
            );

    if (exist == 0)
    {
        return 1;
    }
    else // -1
    {
        return 0;
    }

}

Function sayStructTMAsString


void sayStructTMAsString
(
      struct tm *fileLocalTime
    , char *timestr
)
{

    char formatTimeStamp[] = "%Y-%m-%d %H:%M:%S";
    char timestrLocal[80];

    if (fileLocalTime == NULL)
    {

    }
    else
    {

        strftime
        (
              timestrLocal
            , sizeof(timestrLocal)
            , formatTimeStamp
            , fileLocalTime
        );

    }

    strcpy
    (
          timestr
        , timestrLocal
    );

}

Function fileInfoDisplay


/*
    https://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html
*/
int fileInfoDisplay
(
    const char *filename
)
{

    struct stat statbuf;
    int    exist;
    struct tm *fileLocalTime;
    char   timestr[4000];

    char   formatDataLong[] =   "\t %-30s :- \t %ld bytes \n";
    char   formatDataString[] = "\t %-30s :- \t %-30s \n";

    char   strFileType[60];

    memset
    (
          &statbuf
        , '\0'
        , sizeof(statbuf)
    );

    exist = stat
            (
                  filename
                , &statbuf
            );

    if (exist != 0)
    {

        return 1;
    }
    else
    {

        /*
            stat(2) - Linux man page
            https://linux.die.net/man/2/stat
        */
        switch
        (
            statbuf.st_mode & S_IFMT
        )
        {
            case S_IFDIR:
                    strcpy(strFileType, "Directory");
                    break;

            case S_IFREG:
                    strcpy(strFileType, "regular file");
                    break;

            default:
                strcpy(strFileType, "");
                break;
        }

        /* Print file type */
        printf
        (
             formatDataString
            ,"File Type"
            , strFileType
        );

        /* Print size of file. */
        printf
        (
             formatDataLong
            ,"File Size"
            , statbuf.st_size
        );

        // File Modification TS
        fileLocalTime = (struct tm *)
                        localtime
                        (
                            &statbuf.st_mtime
                        );

        sayStructTMAsString
        (
              fileLocalTime
            , timestr
        );

        if (timestr != NULL)
        {

            /*
                Print File Modification Time
            */
            printf
            (
                 formatDataString
                ,"File Modification Time"
                , timestr
            );              

        }       

        // File Access TS
        fileLocalTime = (struct tm *)
                        localtime
                        (
                            &statbuf.st_atime
                        );

        sayStructTMAsString
        (
              fileLocalTime
            , timestr
        );

        if (timestr != NULL)
        {

            /* Print File Access Time. */
            printf
            (
                 formatDataString
                ,"File Last Access Time"
                , timestr
            );              

        }                       

        return 0;
    }

}

Code Sharing

Gist

Link

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s