neo730
neo730

Reputation: 109

printing uid of a file on linux system

I am learning C programming. I am trying to make my own program similar to the ls command but with fewer options. What I am doing is taking input directory/file name as argument and then gets all directory entry with dirent struct (if it is directory.)

After it I use stat() to take all information of the file but here is my problem: When I use write() to print these values it's fine but when I want to print these with printf() I get warning: format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘__uid_t’. I don't know what I should use in place of %ld and also for other special data types.

Upvotes: 4

Views: 4883

Answers (2)

Alex B
Alex B

Reputation: 84972

There is no format specifier for __uid_t, because this type is system-specific and is not the part of C standard, and hence printf is not aware of it.

The usual workaround is to promote it to a type that would fit the entire range of UID values on all the systems you are targeting:

printf("%lu\n", (unsigned long int)uid); /* some systems support 32-bit UIDs */

Upvotes: 11

Johannes Weiss
Johannes Weiss

Reputation: 54111

You could cast it to a long int:

printf("foobar: %ld\n", (long int)your_uid_t_variable);

Upvotes: 1

Related Questions