gangadhars
gangadhars

Reputation: 2728

How to display uid, size and file's type?

I'm using gcc 32bit compiler. I used stat() function, but it is not giving the information about type of file. Is there any function or way to find all these?

Upvotes: 2

Views: 4267

Answers (5)

hexist
hexist

Reputation: 5250

libmagic is the library you'll want to look into for determining types of files without guessing based on extensions and that sort of stuff. It's what powers the common file command.

See: http://linux.die.net/man/3/libmagic

It'll (very quickly) inspect the file and return to you the actual mime type of the file, amongst other useful and interesting things.

Upvotes: 1

Lidong Guo
Lidong Guo

Reputation: 2857

I used stat() function, but it is not giving the information about type of file.

No, stat() in fact give you all you want about file.

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for file system I/O */
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
    time_t    st_atime;   /* time of last access */
    time_t    st_mtime;   /* time of last modification */
    time_t    st_ctime;   /* time of last status change */
};

So if you want the type of file , you can use st_mode information.

There are some marco you can use: (all param is st_mode)

 S_ISREG(m)
is it a regular file?

S_ISDIR(m)

directory?

S_ISCHR(m)

character device?

S_ISBLK(m)

block device?

S_ISFIFO(m)

FIFO (named pipe)?

S_ISLNK(m)

symbolic link? (Not in POSIX.1-1996.)

S_ISSOCK(m)

socket? (Not in POSIX.1-1996.)

If you want learn more about stat . come here.

Upvotes: 2

alk
alk

Reputation: 70979

uid and size are returned by stat() as members st_size and st_uid of the struct stat filled by a succesful call to stat().

#include <sys/stat.h>

...

struct stat s = {0};
int result = stat("somefilename", &s);
if (-1 = result)
  perror("stat() failed");
else
{
  printf("%u\n", s.st_uid); /* Print uid. */
  printf("%ld\n", s.st_size); /* Print size. */
}

Upvotes: 0

Shaun Kruger
Shaun Kruger

Reputation: 500

I believe you are looking for the st_mode member of struct stat.

From the stat(2) manpage:

     The status information word st_mode has the following bits:

 #define S_IFMT 0170000           /* type of file */
 #define        S_IFIFO  0010000  /* named pipe (fifo) */
 #define        S_IFCHR  0020000  /* character special */
 #define        S_IFDIR  0040000  /* directory */
 #define        S_IFBLK  0060000  /* block special */
 #define        S_IFREG  0100000  /* regular */
 #define        S_IFLNK  0120000  /* symbolic link */
 #define        S_IFSOCK 0140000  /* socket */
 #define        S_IFWHT  0160000  /* whiteout */
 #define S_ISUID 0004000  /* set user id on execution */
 #define S_ISGID 0002000  /* set group id on execution */
 #define S_ISVTX 0001000  /* save swapped text even after use */
 #define S_IRUSR 0000400  /* read permission, owner */
 #define S_IWUSR 0000200  /* write permission, owner */
 #define S_IXUSR 0000100  /* execute/search permission, owner */

sys/stat.h also provides the following defines for testing for file type. You use them by passing the value of st_mode as a parameter:

#define S_ISBLK(m)      (((m) & S_IFMT) == S_IFBLK)     /* block special */
#define S_ISCHR(m)      (((m) & S_IFMT) == S_IFCHR)     /* char special */
#define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)     /* directory */
#define S_ISFIFO(m)     (((m) & S_IFMT) == S_IFIFO)     /* fifo or socket */
#define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)     /* regular file */
#define S_ISLNK(m)      (((m) & S_IFMT) == S_IFLNK)     /* symbolic link */
#define S_ISSOCK(m)     (((m) & S_IFMT) == S_IFSOCK)    /* socket */

Upvotes: 4

KrisSodroski
KrisSodroski

Reputation: 2852

You can use the file command provided in c:

http://linux.die.net/man/1/file

From bash (but can be run inside a c program):

 $ file file.c file /dev/{wd0a,hda}
 file.c:   C program text
 file:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),

Upvotes: -1

Related Questions