Getting the owner name of a file in C
I am writing a function works like "ls -l".
How can i get the owner name of a file? I only know how to get the owner id from "lstat", is there a function convert the uid to user name? And also how can i print out the permission in this format "drwxr-xr-x"? This is what my code looks like so far: Code:
void dir(char *dir_name) |
See "man getpwuid"
|
Quote:
How about... Do you know how to print the permissions like that? |
Use stat(), fstat(), or lstat() on the file/directory. Then check the st_mode member of the "struct stat" , which is filled by stat(), against all bitmasks to decide what character ( '-', 'r', 's', 'x', 'S' ....) to print. Like so:
Code:
if (status.st_mode & S_IRUSR) { |
Quote:
|
This seems to work for me. I'm sure it can be beautified using a loop, but this at least illustrates it (also, I didn't bother looking up the actual letters for the different types of files. I made some of them up):
Code:
#include <stdio.h> |
All times are GMT -5. The time now is 03:24 PM. |