6 static void read_line (char line[], size_t);
7 static bool backspace (char **pos, char line[]);
12 printf ("Shell starting...\n");
19 read_line (command, sizeof command);
21 /* Execute command. */
22 if (!strcmp (command, "exit"))
24 else if (!memcmp (command, "cd ", 3))
26 if (!chdir (command + 3))
27 printf ("\"%s\": chdir failed\n", command + 3);
29 else if (command[0] == '\0')
35 pid_t pid = exec (command);
37 printf ("\"%s\": exit code %d\n", command, wait (pid));
39 printf ("exec failed\n");
43 printf ("Shell exiting.");
47 /* Reads a line of input from the user into LINE, which has room
48 for SIZE bytes. Handles backspace and Ctrl+U in the ways
49 expected by Unix users. On return, LINE will always be
50 null-terminated and will not end in a new-line character. */
52 read_line (char line[], size_t size)
58 read (STDIN_FILENO, &c, 1);
68 backspace (&pos, line);
71 case ('U' - 'A') + 1: /* Ctrl+U. */
72 while (backspace (&pos, line))
77 /* Add character to line. */
78 if (pos < line + size - 1)
88 /* If *POS is past the beginning of LINE, backs up one character
89 position. Returns true if successful, false if nothing was
92 backspace (char **pos, char line[])
96 /* Back up cursor, overwrite character, back up