LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-02-2004, 01:17 AM   #1
akin81
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Rep: Reputation: 0
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;
}
 
Old 04-02-2004, 03:47 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
See "man getpwuid"
 
Old 04-02-2004, 12:09 PM   #3
akin81
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by Hko
See "man getpwuid"
Thanks.

How about...
Do you know how to print the permissions like that?
 
Old 04-02-2004, 03:04 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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
 
Old 04-02-2004, 03:32 PM   #5
akin81
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Original Poster
Rep: Reputation: 0
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!
 
Old 04-02-2004, 03:36 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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;
}

Last edited by itsme86; 04-02-2004 at 03:37 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File owner and File group locked - root deiphage Linux - Hardware 5 02-05-2005 01:30 PM
file and folder owner ship wisdom Linux - Software 2 12-24-2004 08:28 PM
how to i change file owner back to root ? glacier1985 Linux - Newbie 10 06-17-2004 09:20 PM
file owner on an NFS shishir Linux - Networking 4 11-30-2003 11:09 PM
file permissions and file owner won't change Nadim Slackware 5 11-29-2003 06:03 PM


All times are GMT -5. The time now is 09:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration