#P10691. Chmod Permission Converter
Chmod Permission Converter
Chmod Permission Converter
This problem is about converting a numeric mode string into a permission string using the Linux chmod semantics. In Linux and other Unix-like systems, every file or directory has associated permissions that determine who can read, write, or execute the file. The users are divided into three classes: owner, group, and others. Each class has three possible permissions: read (r), write (w), and execute (x).
A permission string is a 9-character string, where the first three characters correspond to the owner's permissions, the next three correspond to the group's permissions, and the final three correspond to the permissions for others. Each character is either the letter (r, w, or x) if that permission is granted or '-' if it is not.
The conversion uses a 3-digit mode string (each digit ranging from 0 to 7). For each digit, its 3 least significant bits (in binary) indicate read, write, and execute permissions respectively. Specifically, if we let a digit be represented as \( d \), then its bits are interpreted as follows:
[ \text{read} = \begin{cases} r & \text{if } d & 4 \neq 0 \ - & \text{otherwise} \end{cases}, \quad \text{write} = \begin{cases} w & \text{if } d & 2 \neq 0 \ - & \text{otherwise} \end{cases}, \quad \text{execute} = \begin{cases} x & \text{if } d & 1 \neq 0 \ - & \text{otherwise} \end{cases} ]
For example, if the input mode string is 760
, then the permission string is generated as follows:
- Owner (
7
): 7 in binary is 111, hence permissions arerwx
. - Group (
6
): 6 in binary is 110, hence permissions arerw-
. - Others (
0
): 0 in binary is 000, hence permissions are---
.
The final permission string will be rwxrw----.
inputFormat
The input consists of one or more lines. Each line contains a valid three-digit mode string. The digits (ranging from 0 to 7) represent the permissions for owner, group, and others (in that order).
outputFormat
For each provided mode string, output the corresponding 9-character permission string on its own line. The permission string is formed by converting each digit to three characters based on its binary representation for read (r), write (w), and execute (x) privileges.
sample
760
rwxrw----