26
#define COPYMODE 0644
creat(argV[2],COPYMODE);

I have these two lines of code in a copy.c file. I don't know what it means. Please give some example about it

2
  • plus how to copy a file use a c program and maintain executable for the copy file
    – Sycx
    Aug 24, 2013 at 6:59
  • 2
    Remove semi-colon at end of #define
    – P0W
    Aug 24, 2013 at 7:01

2 Answers 2

75

There are 3x3 bit flags for a mode:

  • (owning) User
    • read
    • write
    • execute
  • Group
    • read
    • write
    • execute
  • Other
    • read
    • write
    • execute

So each triple encodes nicely as an octal digit.

rwx oct    meaning
--- ---    -------
001 01   = execute
010 02   = write
011 03   = write & execute
100 04   = read
101 05   = read & execute
110 06   = read & write
111 07   = read & write & execute

So 0644 is:

* (owning) User: read & write
* Group: read
* Other: read

Note that in C, an initial 0 indicates octal notation, just like 0x indicates hexadecimal notation. So every time you write plain zero in C, it's actually an octal zero (fun fact).

This might also be written:

-rw-r--r--

Whereas full permissions, 0777 can also be written:

-rwxrwxrwx

So the octal number passed to creat corresponds directly (via octal encoding of the bit-pattern) to the file permissions as displayed by ls -l.

5
  • I think it's better to explain them as bit flags
    – JosephH
    Aug 24, 2013 at 7:02
  • In this context Owner is uncommon. User is common. Also please see man chmod on this.
    – alk
    Aug 24, 2013 at 12:46
  • 2
    To not lose the meaning I'd propse to use the wording (owning) User instead of Owner..
    – alk
    Aug 24, 2013 at 13:00
  • By the way, why can we just say - 644, why 0644 is required?
    – dexterous
    Apr 18, 2014 at 9:27
  • @SHREYASJOSHI In C source, you must use an initial zero to specify an octal constant, otherwise it will be considered a decimal constant. With command-line tools like chmod, you may omit the 0 and chmod will interpret the number as an octal constant anyway. Apr 19, 2014 at 4:42
19

It means that:

  • The file's owner can read and write (6)
  • Users in the same group as the file's owner can read (first 4)
  • All users can read (second 4)

See http://www.ss64.com/bash/chmod.html.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.