Privileged instructions

A program running in user mode may not use privileged instructions. When it tries, the CPU traps into the kernel, which usually stops the program.

What to build

Read the instructions a user program tries to run, and say for each one whether the CPU allows it or traps.

Requirements

  • The privileged instructions are in, out, cli, sti, halt and lgdt. Every other word is an ordinary instruction.
  • Print word: ok for an allowed instruction and word: trap for a privileged one. Check every instruction on its own; do not stop at the first trap.
  • After the list, print traps: T with the number of traps.

Input

The first line has n (1 ≤ n ≤ 30). The next n words are the instructions, each in lower case and at most 8 letters.

Output

n lines word: ok or word: trap, then the line traps: T.

Constraints

Instruction words use only the letters a to z. At most 30 instructions.

Example

Input:

4
add
load
in
store

Output:

add: ok
load: ok
in: trap
store: ok
traps: 1

Why it works

The OS decides what is privileged by a fixed list, and the CPU checks that list before every instruction. A user program cannot talk to the hardware directly, so one buggy program cannot turn off interrupts or read the disk.

Examples

Example 1 · Input
4
add
load
in
store
Expected output
add: ok
load: ok
in: trap
store: ok
traps: 1
Example 2 · Input
3
mov
jmp
cmp
Expected output
mov: ok
jmp: ok
cmp: ok
traps: 0

Hints

Hint 1

Keep the six privileged words in an array and compare each input word with all of them using strcmp (from string.h).

Hint 2

Read each word with scanf("%15s", word). Set a flag if it matches one of the six.

Hint 3

Count the traps as you go and print the total after the loop.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *privileged[6] = {"in", "out", "cli", "sti", "halt", "lgdt"};
    int n, traps = 0;
    char word[16];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%15s", word);
        int bad = 0;
        for (int k = 0; k < 6; k++)
            if (strcmp(word, privileged[k]) == 0) bad = 1;
        printf("%s: %s\n", word, bad ? "trap" : "ok");
        traps += bad;
    }
    printf("traps: %d\n", traps);
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: What an operating system does →