In C how do you find if a name is a dir or link? st_fstype is not vaild in stat.h

Asked by Harry Tonny

New backup program I am writing in C. I need to know if a name like "/home/test/backup" is a link or directory or file. I tracked it down to st_fstype in stat.h, but it is invalid now. What is the current way to check for directory/link/file.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu util-linux Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Jason (zzs) said :
#1

By calling the stat() and checking the st_mode field of returned stat structure.

Revision history for this message
Harry Tonny (harry2sundown) said :
#2

That will give the same number (16877) for links and directories. Here is the output with junk a directory, stat.c a file, and humor a link. The File Mode is st_mode. Program is below I copied and modified. File Size and links can be the same for directory or link.

Junk stat values are:
****************************************
File Size: 4096
File Mode: 16877
Inode Number: 528503
Device ID: 2054
Raw Device ID: 0
Number Of Links: 2
User ID: 1001
Group ID: 1001
Access Time: Thu, February 21, 2013 - 02:31:50
Modification Time: Sun, February 17, 2013 - 22:30:02
Status Change Time: Sun, February 17, 2013 - 22:30:02
Block Size: 4096
Number Of Blocks: 8
****************************************
stat.c stat values are:
****************************************
File Size: 7166
File Mode: 33188
Inode Number: 656533
Device ID: 2054
Raw Device ID: 0
Number Of Links: 1
User ID: 1001
Group ID: 1001
Access Time: Wed, February 20, 2013 - 02:37:48
Modification Time: Wed, February 20, 2013 - 00:02:27
Status Change Time: Wed, February 20, 2013 - 00:02:27
Block Size: 4096
Number Of Blocks: 16
****************************************
Link to Humor stat values are:
****************************************
File Size: 4096
File Mode: 16877
Inode Number: 1442342
Device ID: 2054
Raw Device ID: 0
Number Of Links: 3
User ID: 1001
Group ID: 1001
Access Time: Thu, February 21, 2013 - 02:31:50
Modification Time: Tue, February 19, 2013 - 14:36:18
Status Change Time: Tue, February 19, 2013 - 14:36:18
Block Size: 4096
Number Of Blocks: 8
****************************************

   #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <pwd.h>
    #include <grp.h>
    #include <time.h>
    #include <locale.h>
    #include <langinfo.h>
    #include <fcntl.h>

    int main (int argc, char *argv[])
    {
    unsigned long zzz;
    if (argc <= 1) {
    printf("Usage: %s file1 file2 fileN...\n", argv[0]);
    exit(1);
    }
    struct stat dirfilebuf;
    int arguments, dirfilestate;
    const char *dirfilename;
    char *nicetime, *errormessage, *divider;

    dirfilename=(char*)(malloc(sizeof(char)));
    nicetime=(char*)(malloc(sizeof(char)));
    errormessage=(char*)(malloc(sizeof(char)));
    divider=(char*)(malloc(sizeof(char)));

    errormessage="File or Directory Not Found!";
    divider="****************************************";

    for (arguments = 1; arguments < argc; arguments++) {
    dirfilename = argv[arguments];
   // printf("%s\nChecking %s\n%s\n", divider,dirfilename,divider);
    dirfilestate = stat(dirfilename, &dirfilebuf);
    if ( dirfilestate == -1 ) {
    printf("%s - Quitting!\n", errormessage);
    exit(1);
    }
    else{

    printf("%s stat values are:\n%s\n", dirfilename,divider);
    zzz = dirfilebuf.st_size;
    printf("File Size: %ld\n", zzz);
    printf("File Mode: %d\n", dirfilebuf.st_mode);
    printf("Inode Number: %ld\n", dirfilebuf.st_ino);
    zzz= dirfilebuf.st_dev;
    printf("Device ID: %ld\n", zzz);
    zzz= dirfilebuf.st_rdev;
    printf("Raw Device ID: %ld\n", zzz);
    printf("Number Of Links: %d\n", dirfilebuf.st_nlink);
    printf("User ID: %-8d\n", dirfilebuf.st_uid);
    printf("Group ID: %-8d\n", dirfilebuf.st_gid);
    //printf("Bytes Size: %jd\n", (intmax_t)dirfilebuf.st_size);
    strftime(nicetime,34,"%a, %B %d, %Y - %H:%M:%S", localtime(&dirfilebuf.st_atime));
    printf("Access Time: %s\n", nicetime);
    strftime(nicetime,34,"%a, %B %d, %Y - %H:%M:%S", localtime(&dirfilebuf.st_mtime));
    printf("Modification Time: %s\n", nicetime);
    strftime(nicetime,34,"%a, %B %d, %Y - %H:%M:%S", localtime(&dirfilebuf.st_ctime));
    printf("Status Change Time: %s\n", nicetime);
    printf("Block Size: %ld\n", dirfilebuf.st_blksize);
    printf("Number Of Blocks: %ld\n", dirfilebuf.st_blocks);
    //printf("File System Type: %s\n", dirfilebuf.st_fstype); // Not used in Mint or Ubuntu
    printf("%s\n", divider);
    }
    }
    }

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#3

Can you help with this problem?

Provide an answer of your own, or ask Harry Tonny for more information if necessary.

To post a message you must log in.