LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 11-27-2007, 09:26 PM   #1
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Rep: Reputation: 30
Need help determining file mode from stat function


I'm trying to get the permissions of files in a directory. Here's what I think are the most relevant sections of code. If you need more let me know and I'll make short program that demonstrates the whole thing.
Code:
  dirent* e = readdir(dirHandle);
  if (!e)
    return false;
  stat(e->d_name, &buf);
  name = e->d_name;
  if (buf.st_mode & S_IRUSR == S_IRUSR)
    permissions = "r";
  else
    permissions += "-";
  if (buf.st_mode & S_IWUSR == S_IWUSR)
    permissions += "w";
  else
    permissions += "-";
  if (buf.st_mode & S_IXUSR == S_IXUSR)
    permissions += "x";
  else
    permissions += "-";
  if (buf.st_mode & S_IRGRP == S_IRGRP)
    permissions += "r";
  else
    permissions += "-";
  if (buf.st_mode & S_IWGRP == S_IWGRP)
    permissions += "w";
  else
    permissions += "-";
  if (buf.st_mode & S_IXGRP == S_IXGRP)
    permissions += "x";
  else
    permissions += "-";
  if (buf.st_mode & S_IROTH == S_IROTH)
    permissions += "r";
  else
    permissions += "-";
  if (buf.st_mode & S_IWOTH == S_IWOTH)
    permissions += "w";
  else
    permissions += "-";
  if (buf.st_mode & S_IXOTH == S_IXOTH)
    permissions += "x";
  else
    permissions += "-";
The string permission always comes out rwxrwxrwx. How do I properly determine the permissions on the file?
 
Old 11-28-2007, 01:03 AM   #2
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Original Poster
Rep: Reputation: 30
Well, I didn't change much but I got things to work. Just changed if (buf.st_mode & S_IRUSR == S_IRUSR) to (buf.st_mode & S_IRUSR. I also added an array filled with the modes so I could use a for loop to test them. Much more compact and elegant. Well, thanks for the help.
 
Old 11-28-2007, 03:50 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
yer, it should probably have been
(buf.st_mode & S_IROTH) == S_IROTH

precedence and all that, perhaps
 
  


Reply



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
PHP mail() function and sendmail: stat=Service unavailable xpucto Linux - Server 1 03-22-2007 10:25 AM
*NIX systems programming: error using stat function MDBlueIce Programming 7 06-27-2005 06:42 AM
stat system call mode checking ppy Programming 1 11-05-2004 09:24 AM
Write own stat() function... indian Programming 1 09-14-2004 07:06 AM
stat function in BASH... PokerFace Programming 1 11-04-2002 11:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration