Complete Unix Permissions System (UGO × RWE)

Authentic chmod 755, 644 implementation using 9 bits.

User/Group/Other × Read/Write/Execute = full Unix permissions.

1. Bit Layout (Exactly Like Unix)

TEXT
9-bit permission structure
Octal | Binary     | Meaning
------|------------|-------
7    | 111 rwxr-x | Owner
5    | 101 r-x    | Group
5    | 101 r-x    | Other
─────
755  | 111101101  | rwxr-xr-x

Bit positions: 876 543 210
             rwx rwx rwx

2. Permission Constants (Octal)

C
Unix-standard permission bits
// User/Owner
#define U_R 0400
#define U_W 0200
#define U_X 0100

// Group
#define G_R 0040
#define G_W 0020
#define G_X 0010

// Other
#define O_R 0004
#define O_W 0002
#define O_X 0001

// Common combinations
#define ALL_RWX 0777
#define OWNER_RWX 0700
#define GROUP_RX 0050
#define OTHER_R 0004

3. Complete Permission API

C
Add/Remove/Test + Display functions
int addPerm(int perms, int flag) {
    return perms | flag;
}

int removePerm(int perms, int flag) {
    return perms & ~flag;
}

int hasPerm(int perms, int flag) {
    return (perms & flag) != 0;
}

void printPerms(int perms) {
    printf("%o ", perms);
    printf("u:%c%c%c ",
        hasPerm(perms,U_R)?'r':'-',
        hasPerm(perms,U_W)?'w':'-',
        hasPerm(perms,U_X)?'x':'-');
    printf("g:%c%c%c ",
        hasPerm(perms,G_R)?'r':'-',
        hasPerm(perms,G_W)?'w':'-',
        hasPerm(perms,G_X)?'x':'-');
    printf("o:%c%c%c\n",
        hasPerm(perms,O_R)?'r':'-',
        hasPerm(perms,O_W)?'w':'-',
        hasPerm(perms,O_X)?'x':'-');
}

4. Full Interactive System

C
chmod-style permission manager
#include <stdio.h>

// Permission constants (octal)
#define U_R 0400 #define U_W 0200 #define U_X 0100
#define G_R 0040 #define G_W 0020 #define G_X 0010
#define O_R 0004 #define O_W 0002 #define O_X 0001

int addPerm(int perms, int flag) { return perms | flag; }
int removePerm(int perms, int flag) { return perms & ~flag; }
int hasPerm(int perms, int flag) { return (perms & flag) != 0; }

void printPerms(int perms) {
    printf("%o ", perms);
    printf("u:%c%c%c ", hasPerm(perms,U_R)?'r':'-', hasPerm(perms,U_W)?'w':'-', hasPerm(perms,U_X)?'x':'-');
    printf("g:%c%c%c ", hasPerm(perms,G_R)?'r':'-', hasPerm(perms,G_W)?'w':'-', hasPerm(perms,G_X)?'x':'-');
    printf("o:%c%c%c\n", hasPerm(perms,O_R)?'r':'-', hasPerm(perms,O_W)?'w':'-', hasPerm(perms,O_X)?'x':'-');
}

int main() {
    int perms = 0;
    int choice, target_perm;
    
    printf("=== Unix Permissions Manager ===\n");
    
    while (1) {
        printPerms(perms);
        printf("1=chmod # 2=Add Perm 3=Remove Perm 0=Exit: ");
        scanf("%d", &choice);
        
        if (choice == 0) break;
        
        if (choice == 1) {
            printf("Enter octal (ex: 755): ");
            scanf("%o", &perms);
        } else {
            printf("Permission (400=U_R,40=G_R,4=O_R,etc): ");
            scanf("%d", &target_perm);
            if (choice == 2)
                perms = addPerm(perms, target_perm);
            else
                perms = removePerm(perms, target_perm);
        }
    }
    return 0;
}

5. Sample Interactive Run

TEXT
Console session
0 u:--- g:--- o:---
1=chmod # 2=Add 3=Remove 0=Exit: 1
Enter octal: 755
755 u:rwx g:r-x o:r-x
1=chmod # 2=Add 3=Remove 0=Exit: 3
Permission: 20
755 u:rwx g:r-- o:r-x
1=chmod # 2=Add 3=Remove 0=Exit: 0

6. Common chmod Values

TEXT
Standard permissions
777 rwxrwxrwx (everyone)
755 rwxr-xr-x (exec files)
644 rw-r--r-- (config files)
600 rw------- (private)
711 rwx--x--x (directories)

Conclusion

Authentic Unix permissions system! Octal input/output.

Copy, compile, test chmod 755 → rwxr-xr-x ✓