Skip to content

Commit

Permalink
Merge from labs repo
Browse files Browse the repository at this point in the history
- fisop/labs#13: "use isatty(3)"
  • Loading branch information
dato committed Sep 8, 2023
2 parents f85de3f + 61da5e1 commit 5397e24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
41 changes: 20 additions & 21 deletions shell/printstatus.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
#include <unistd.h>

#include "printstatus.h"

// prints information of process' status
void
print_status_info(struct cmd *cmd)
{
const char *action;

if (strlen(cmd->scmd) == 0 || cmd->type == PIPE)
return;

if (WIFEXITED(status)) {
#ifndef SHELL_NO_INTERACTIVE
fprintf(stdout,
"%s Program: [%s] exited, status: %d %s\n",
COLOR_BLUE,
cmd->scmd,
WEXITSTATUS(status),
COLOR_RESET);
#endif
action = "exited";
status = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
#ifndef SHELL_NO_INTERACTIVE
fprintf(stdout,
"%s Program: [%s] killed, status: %d %s\n",
COLOR_BLUE,
cmd->scmd,
-WTERMSIG(status),
COLOR_RESET);
#endif
action = "killed";
status = -WTERMSIG(status);
} else if (WTERMSIG(status)) {
action = "stopped";
status = -WSTOPSIG(status);
} else {
return;
}

#ifndef SHELL_NO_INTERACTIVE
if (isatty(1)) {
fprintf(stdout,
"%s Program: [%s] stopped, status: %d %s\n",
"%s Program: [%s] %s, status: %d %s\n",
COLOR_BLUE,
cmd->scmd,
-WSTOPSIG(status),
action,
status,
COLOR_RESET);
#endif
status = -WSTOPSIG(status);
}
#endif
}

// prints info when a background process is spawned
void
print_back_info(struct cmd *back)
{
#ifndef SHELL_NO_INTERACTIVE
fprintf(stdout, "%s [PID=%d] %s\n", COLOR_BLUE, back->pid, COLOR_RESET);
if (isatty(1)) {
fprintf(stdout, "%s [PID=%d] %s\n", COLOR_BLUE, back->pid, COLOR_RESET);
}
#endif
}
8 changes: 6 additions & 2 deletions shell/readline.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <unistd.h>

#include "defs.h"
#include "readline.h"

Expand All @@ -11,8 +13,10 @@ read_line(const char *prompt)
int i = 0, c = 0;

#ifndef SHELL_NO_INTERACTIVE
fprintf(stdout, "%s %s %s\n", COLOR_RED, prompt, COLOR_RESET);
fprintf(stdout, "%s", "$ ");
if (isatty(1)) {
fprintf(stdout, "%s %s %s\n", COLOR_RED, prompt, COLOR_RESET);
fprintf(stdout, "%s", "$ ");
}
#endif

memset(buffer, 0, BUFLEN);
Expand Down

0 comments on commit 5397e24

Please sign in to comment.