C File Permission Conversion


 
Thread Tools Search this Thread
Top Forums Programming C File Permission Conversion
# 1  
Old 11-01-2006
C File Permission Conversion

I'm new to C, and I'm attempting to write a script similar to the stat command for practice. I only had a problem converting st_mode to an octal permission format. I remembered I had littleutils installed which contains a script called filemode, so I checked the source and it yielded something like :

Code:
%04o, file_stats.st_mode & 4095

Now I'd just like to understand that bit of code. Any help is appreciated.
# 2  
Old 11-01-2006
& by itself like that is a bitwise and, and 4095 is one away from being 4096, a nice round binary number. In binary, 4095 looks like:
Code:
0000111111111111

All the higher-order bits, where 4095 is zero, get turned off, only the lower-order bits get let through.

C supports octal directly, though. Numbers beginning with a 0 are assumed to be octal. The value of 0777 is the same as that of 777 in the chmod sense, and you can get the same value as 4095 with the less-inexplicable octal value of 07777.
# 3  
Old 11-01-2006
Hrm, I'm not quite sure I understand. If I just put the value returned from st_mode(33152)through like this:

Code:
%04o\n", statbuf.st_mode

I get 100600. If it's not too much trouble could you break down the code?
# 4  
Old 11-01-2006
See Unix File Permissions...at least the beginning where I show how the mode is stored.

Your value of 100600 is two pieces of data. The last 4 digits tell you that the file has the same permissions as could be achieved via "chmod 600 file". So you need to isolate those 4 digits and throw away that leading 10. That is what the code is doing.
# 5  
Old 11-01-2006
Thank you for the replies. I believe I've got it now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

File conversion and removing special characters from a file in Linux

I have a .CSV file when I check for the special characters in the file using the command cat -vet filename.csv, i get very lengthy lines with "^@", "^I^@" and "^@^M" characters in between each alphabet in all of the records. Using the code below file filename.csv I get the output as I have a... (2 Replies)
Discussion started by: dhruuv369
2 Replies

2. Shell Programming and Scripting

file conversion

Hi, I have an excel file in unix and I want a script which changes the excel file to .csv file i.e (comma separated value file). Would be thankful to you if some one helps me with this. Thanks in advance. (3 Replies)
Discussion started by: karthikkasarla
3 Replies

3. Shell Programming and Scripting

Conversion of spaces Text file into CSV format file

Input file (each line is separaed by spaces )given below: Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- -----... (18 Replies)
Discussion started by: sreenath1037
18 Replies

4. Shell Programming and Scripting

Conversion of below Tabs Tex file into CSV format file : shell script needed

Request if some one could provide me shell script that converts the below "input file" to "CSV format file" given Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- ----- ---------------------------------... (7 Replies)
Discussion started by: sreenath1037
7 Replies

5. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

6. Shell Programming and Scripting

binary file conversion

Hello folks, i have a binary text file but i am not able to convert into text format, please suggest. thanks. (2 Replies)
Discussion started by: learnbash
2 Replies

7. Shell Programming and Scripting

File conversion

Hi Everyone, Can someone plesae advise on converting the inputted file into required output? First field is L then amount will - symbol. Sample input file B|T|SASOFTB00016|20090330|20090330|15000|9089001 B|T|SABH00000012|20090330|20090330|7000|9089003... (4 Replies)
Discussion started by: gehlnar
4 Replies

8. Shell Programming and Scripting

File Conversion

Hi all, How can i convert a file from one encoding to another? Lets say I have a file which is of utf-8 encoding and I want to convert it to cp875. Can anyone tell me how can I achieve this in shell script? Thanks, Sridhar (2 Replies)
Discussion started by: sridhar_423
2 Replies

9. Solaris

COnversion utility xhtml file to Postscript file

Hi, Can any suggest me some utility to convert xhtml file to postscript file format? Also tell me from where to down load such utility.. With Regards, Dattatray (0 Replies)
Discussion started by: dattatray.b
0 Replies

10. UNIX for Dummies Questions & Answers

file conversion

How can I suppress the 0a (line feed) in a text file (HP UX) (1 Reply)
Discussion started by: hipo
1 Replies
Login or Register to Ask a Question