LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting the owner name of a file in C (https://www.linuxquestions.org/questions/programming-9/getting-the-owner-name-of-a-file-in-c-165250/)

akin81 04-02-2004 01:17 AM

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)
{
  DIR *dirStream = NULL;
  struct dirent  *dirEntry  = NULL;
  struct stat    buf;
 
  if ( (dirStream=opendir(dir_name)) == NULL ) {
      printf("%s: error while opening the directory\n",dir_name);
    return;
  }

  while((dirEntry = readdir(dirStream)) != NULL) {
    if ( strcmp(dirEntry->d_name,".")!=0 && strcmp(dirEntry->d_name,"..")!=0 ) {
      lstat(dirEntry->d_name,&buf);
     
      printf("%d",buf.st_uid);
      printf("\t%d",buf.st_size);
      printf("\t%s",dirEntry->d_name);
               
      if (buf.st_mode & S_IFDIR)
        printf("/");

      puts(""); // newline
    }
  }

  return;
}


Hko 04-02-2004 03:47 AM

See "man getpwuid"

akin81 04-02-2004 12:09 PM

Quote:

Originally posted by Hko
See "man getpwuid"
Thanks.

How about...
Do you know how to print the permissions like that?

Hko 04-02-2004 03:04 PM

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) {
    /* owner has read permission */
    /* print corrosponding character in correct position */
}

It's probably best to check in the order you need to print the char's

akin81 04-02-2004 03:32 PM

Quote:

Originally posted by Hko
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) {
    /* owner has read permission */
    /* print corrosponding character in correct position */
}

It's probably best to check in the order you need to print the char's

Yeah i see. Thank you very much!

itsme86 04-02-2004 03:36 PM

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>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
  char str[11], *s = str;
  struct stat st;
  mode_t owner, group, other;

  if(lstat("myfile", &st) < 0)
  {
    printf("Couldn't open file.\n");
    return 1;
  }

  if(S_ISREG(st.st_mode))
    *s++ = '-';
  else if(S_ISDIR(st.st_mode))
    *s++ = 'd';
  else if(S_ISCHR(st.st_mode))
    *s++ = 'c';
  else if(S_ISBLK(st.st_mode))
    *s++ = 'b';
  else if(S_ISFIFO(st.st_mode))
    *s++ = 'f';
  else if(S_ISLNK(st.st_mode))
    *s++ = 'l';
  else
    *s++ = 's';

  owner = st.st_mode & S_IRWXU;
  group = st.st_mode & S_IRWXG;
  other = st.st_mode & S_IRWXO;

  *s++ = owner & S_IRUSR ? 'r' : '-';
  *s++ = owner & S_IWUSR ? 'w' : '-';
  *s++ = owner & S_IXUSR ? 'x' : '-';

  *s++ = group & S_IRGRP ? 'r' : '-';
  *s++ = group & S_IWGRP ? 'w' : '-';
  *s++ = group & S_IXGRP ? 'x' : '-';

  *s++ = other & S_IROTH ? 'r' : '-';
  *s++ = other & S_IWOTH ? 'w' : '-';
  *s++ = other & S_IXOTH ? 'x' : '-';

  *s = '\0';

  puts(str);

  return 0;
}



All times are GMT -5. The time now is 03:24 PM.