3 Replies - 257 Views - Last Post: 16 January 2013 - 06:09 AM Rate Topic: -----

#1 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Set executable mode on the file

Posted 16 January 2013 - 02:19 AM

I have the following code wherein I , fopen (source) in "r" mode, fopen (destination) in "w" mode, copy the contents, fclose both and then attempt to retain the mode.

struct stat fs;
stat (destfile, &fs);
int destmode = fs.st_mode;
destmode |= sourcefile.mode; //could be 444, 555 or 777
destmode &= ~0222; //Remove the write mode
destmode &= 0111; //Will this set the executable mode??

chmod (destfile, destmode);



Is "destmode &= 0111; " the right way to forcibly set the executable bit?

Is This A Good Question/Topic? 0
  • +

Replies To: Set executable mode on the file

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Set executable mode on the file

Posted 16 January 2013 - 02:26 AM

well, its like this

r w x
0 0 0   0 -> no permissions
0 0 1   1 -> only execute
0 1 0   2 -> only write
0 1 1   3 -> only write and execute
1 0 0   4 -> only read
1 0 1   5 -> only read and execute
1 1 0   6 -> only read and write
1 1 1   7 -> read, write as well as execute


So, the answer to your question would be

destmode &=0111;


Where the order of accessiblity is

USERS GROUP OTHERS
  1     1     1


regards,
Raghav

This post has been edited by raghav.naganathan: 16 January 2013 - 02:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Re: Set executable mode on the file

Posted 16 January 2013 - 06:02 AM

destmode |= sourcefile.mode;
destmode &= ~0222;
destmode &=0111;



I see the final mode as
--------- 2 aimac 65760 Jan 16 04:52/md/x

The original file has
-rw-r--r--

What am i doing wrong?

This post has been edited by Skydiver: 16 January 2013 - 06:06 AM
Reason for edit:: Fixed code tag.

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Set executable mode on the file

Posted 16 January 2013 - 06:09 AM

x &= y doesn't set bits - it unsets those bits in x that aren't set in y. |= sets bits.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1