aboutsummaryrefslogtreecommitdiffstats
path: root/src/command.c
diff options
context:
space:
mode:
authorDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2025-11-02 21:56:58 -0500
committerDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2025-11-02 21:56:58 -0500
commit26da453832ca90ce22415a59a2aab57ab16cba66 (patch)
treef773bf84632158537fc72a9b869cd87066d73946 /src/command.c
parentf84e64b593c6e6fda9e2a907d25ed99d8742d619 (diff)
downloadhush-26da453832ca90ce22415a59a2aab57ab16cba66.tar.gz
Comment header + clang tidy
Diffstat (limited to 'src/command.c')
-rw-r--r--src/command.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/command.c b/src/command.c
index 3d63121..34cdf89 100644
--- a/src/command.c
+++ b/src/command.c
@@ -1,7 +1,14 @@
/*
- *
+ * src/command.c
+ *
+ * Abstractions for parsing commands from tokens and executing them
+ * CISC 301 -- Operating Systems, Project 2
+ *
+ * Copyright (C) 2025 Douglas B. Rumbaugh <dbrumbaugh@harrisburgu.edu>
+ *
+ * Distributed under the Modified BSD License
+ *
*/
-
#include "command.h"
#include "config.h"
#include "lexer.h"
@@ -15,7 +22,7 @@ command *create_command() {
}
cmd->command = NULL;
- memset(cmd->args, 0, sizeof(char*));
+ memset(cmd->args, 0, sizeof(char *));
cmd->pipe[0] = -1;
cmd->pipe[1] = -1;
cmd->infile = NULL;
@@ -23,7 +30,7 @@ command *create_command() {
cmd->pid = -1;
cmd->next = NULL;
- cmd->prev= NULL;
+ cmd->prev = NULL;
return cmd;
}
@@ -56,21 +63,22 @@ command *commands_from_tokens(token *parsed_cmdstr, size_t *cnt) {
cmd->next->prev = cmd;
cmd = cmd->next;
}
-
}
return cmd_head;
}
void print_commands(FILE *file, command *cmds) {
- for (command *cmd=cmds; cmd; cmd = cmd->next) {
+ for (command *cmd = cmds; cmd; cmd = cmd->next) {
fprintf(file, "Command: %s\n", cmd->command);
fprintf(file, "\t");
- for (size_t i=0; i<MAX_ARGUMENT_CNT; i++) {
+ for (size_t i = 0; i < MAX_ARGUMENT_CNT; i++) {
fprintf(file, "%s ", cmd->args[i]);
}
- fprintf(file, "\n\tInfile: %s\n\tOutfile: %s\\n\tPid:%d\n", cmd->infile, cmd->outfile, cmd->pid);
- fprintf(file, "\tRead Pipe: %d\tWrite Pipe: %d\n", cmd->pipe[0], cmd->pipe[1]);
+ fprintf(file, "\n\tInfile: %s\n\tOutfile: %s\\n\tPid:%d\n", cmd->infile,
+ cmd->outfile, cmd->pid);
+ fprintf(file, "\tRead Pipe: %d\tWrite Pipe: %d\n", cmd->pipe[0],
+ cmd->pipe[1]);
}
}
@@ -108,8 +116,8 @@ pid_t execute_command(command *cmd) {
}
if (cmd->outfile && !freopen(cmd->outfile, "w", stdout)) {
- perror("Could not open output file");
- exit(EXIT_FAILURE);
+ perror("Could not open output file");
+ exit(EXIT_FAILURE);
} else if (cmd->next) {
close(cmd->pipe[0]);
if (dup2(cmd->pipe[1], STDOUT_FILENO) == -1) {
@@ -118,19 +126,19 @@ pid_t execute_command(command *cmd) {
}
close(cmd->pipe[1]);
}
-
- /*
+
+ /*
* NOTE: discarding the const qualifier here is okay because either
* 1) exec fails, in which case the process is aborted immediately
* 2) exec succeeds, in which case the memory is released immediately
*
* In either case, the args won't be accessed again.
*/
- execvp(cmd->command, (char**)cmd->args);
+ execvp(cmd->command, (char **)cmd->args);
perror("Could not run command");
exit(EXIT_FAILURE);
- } else if (res < 0) {
+ } else if (res < 0) {
perror("Could not run command");
return -1;
}