Reputation: 403
I'm implementing the ls command and I'm doing the -l option right now. I have some issues with the owner's name. It always print's the id instead of name.
Here is my function:
void print_user_ID(char* filepath) {
struct stat sb;
struct passwd pwent;
struct passwd *pwentp;
char buf[_SC_GETPW_R_SIZE_MAX];
if(stat(filepath, &sb) == -1) {
perror("stat");
}
if (!getpwuid_r(sb.st_uid, &pwent, buf, sizeof(buf), &pwentp))
printf("%10s ", pwent.pw_name);
else
printf("%7d ", sb.st_uid);
}
Do you have any idea where is my mistake?
Upvotes: 2
Views: 688
Reputation: 9
Sometimes getpwuid
or getgruid
fails, to fix that just do a itoa
to st_uid
and it will be fixed :D
Like this:
struct passwd *pw;
struct group *gr;
if ((pw = getpwuid(st.st_uid)))
(!arg->g) ? printf("%s", pw->pw_name) : NULL;
else
(!arg->g) ? printf("%s", itoa(st.st_uid)) : NULL;
Upvotes: 0
Reputation: 403
The comment of Alok SInghal answers my question. I had to change _SC_GETPW_R_SIZE_MAX to a bigger number.
Upvotes: 3