import java.util.Scanner;
public class Chmod {
public static void main (String[] args) {
System.out.println("Welcome to the CHMod Converter! " +
" \nThis will only take numbers mod 8 to ensure that
no invalid numbers are inputted. "
+ "\nFor special permissions, it is taken mod 5.");
Scanner input = new Scanner(System.in);
System.out.print("Enter special permissions #: ");
int a = (input.nextInt()) % 5;
System.out.print("Enter read permissions #: ");
int b = (input.nextInt()) % 8;
System.out.print("Enter write permissions #: ");
int c = (input.nextInt()) % 8;
System.out.print("Enter execute permissions #: ");
int d = (input.nextInt()) % 8;
input.close();
String owner = convertBinary3(b);
String group = convertBinary3(c);
String others = convertBinary3(d);
boolean readOwner = owner.charAt(0) == '1';
boolean writeOwner = owner.charAt(1) == '1';
boolean executeOwner = owner.charAt(2) == '1';
boolean readGroup = group.charAt(0) == '1';
boolean writeGroup = group.charAt(1) == '1';
boolean executeGroup = group.charAt(2) == '1';
boolean readOthers = others.charAt(0) == '1';
boolean writeOthers = others.charAt(1) == '1';
boolean executeOthers = others.charAt(2) == '1';
System.out.print(owner + " " + group + " " + others + " and ");
OwnerTree(readOwner, writeOwner, executeOwner, a);
GroupTree(readGroup, writeGroup, executeGroup, a);
OthersTree(readOthers, writeOthers, executeOthers, a);
}
public static String convertBinary3(int b) {
String owner;
if (b == 1) {
owner = "00" + Integer.toBinaryString(b);
}
else if (b < 4 && b > 1) {
owner = "0" + Integer.toBinaryString(b);
} else if (b == 0) {
owner = "000";
}
else {
owner = Integer.toBinaryString(b);
}
return owner;
}
public static void OwnerTree(boolean read, boolean write,
boolean execute, int SpecialMod) {
if (read) {
System.out.print("r");
} else {
System.out.print("-");
}
if (write) {
System.out.print("w");
} else {
System.out.print("-");
}
if (execute) {
if (SpecialMod == 1) {
System.out.print("s ");
} else {
System.out.print("x ");
}
} else {
System.out.print("- ");
}
}
public static void GroupTree(boolean read, boolean write,
boolean execute, int SpecialMod) {
if (read) {
System.out.print("r");
} else {
System.out.print("-");
}
if (write) {
System.out.print("w");
} else {
System.out.print("-");
}
if (execute) {
if (SpecialMod == 2) {
System.out.print("s ");
} else {
System.out.print("x ");
}
} else {
System.out.print("- ");
}
}
public static void OthersTree(boolean read, boolean write,
boolean execute, int SpecialMod) {
if (read) {
System.out.print("r");
} else {
System.out.print("-");
}
if (write) {
System.out.print("w");
} else {
System.out.print("-");
}
if (execute) {
if (SpecialMod == 4) {
System.out.print("t ");
} else {
System.out.print("x ");
}
} else {
System.out.print("- ");
}
}
}