summary refs log tree commit diff
diff options
context:
space:
mode:
authorSingustromo <singustromo@disroot.org>2024-05-19 05:04:42 +0200
committerSingustromo <singustromo@disroot.org>2024-05-19 05:04:42 +0200
commit49ff6cdff74cb46d2cd585ee6c97a3bba8262a86 (patch)
tree4144a1adb0737501ca493465a7b4321dfbb5ac21
parentb5834f69641f54110c78d8d642cb6afa5b3a124e (diff)
downloadoperating-systems_3_clash-49ff6cdff74cb46d2cd585ee6c97a3bba8262a86.tar.gz
operating-systems_3_clash-49ff6cdff74cb46d2cd585ee6c97a3bba8262a86.zip
Minor changes to command structuring
-rw-r--r--.gitignore1
-rw-r--r--src/clash.c20
2 files changed, 11 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index aac1e73..e6f220e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 bin/**
+ref/**
 clash
 
 MD5SUM
diff --git a/src/clash.c b/src/clash.c
index 2717e1f..705ca2a 100644
--- a/src/clash.c
+++ b/src/clash.c
@@ -1,10 +1,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
-
-#include <unistd.h>
 #include <string.h>             // e.g. strtok
 #include <linux/limits.h>       // e.g. PATH_MAX
+
+#include <unistd.h>             // pid_t, fork ..
 #include <sys/wait.h>
 
 #include "plist.h"
@@ -17,7 +17,7 @@ static int getline_skew(char *buffer, int limit);
 static bool update_current_path(char *path);
 static void print_prompt(char *current_path, int child_exitcode, int job_count);
 
-static int process_commandline(char *prompt, char **command, char *parameters[]);
+static int parse_commands(char *prompt, char *parameters[]);
 
 int main(int argc, char *argv[]) {
         char current_path[PATH_MAX];
@@ -29,7 +29,7 @@ int main(int argc, char *argv[]) {
         int prompt_length, cmdline_arg_count;
         prompt_length = cmdline_arg_count = 0;
 
-        char *command = NULL, *cmdline[MAX_PARAMETERS];
+        char *cmdline[MAX_PARAMETERS];
         int background_jobs = 0;
 
         print_prompt(current_path, 0, 0);
@@ -43,7 +43,7 @@ int main(int argc, char *argv[]) {
                         continue;
                 }
 
-                if ((cmdline_arg_count = process_commandline(prompt, &command, cmdline)) < 1) {
+                if ((cmdline_arg_count = parse_commands(prompt, cmdline)) < 1) {
                         printf("[!] ");
                         print_prompt(current_path, 0, 0);
                         continue;
@@ -65,7 +65,7 @@ int main(int argc, char *argv[]) {
                         print_prompt(current_path, 0, background_jobs);
                         continue;
                 } else if (child_pid == 0) // child process
-                       if (execvp(command, cmdline) == -1) // command failed?
+                       if (execvp(cmdline[0], cmdline) == -1) // command failed?
                                 exit(EXIT_FAILURE);
 
                 if (! background_job) {
@@ -89,15 +89,15 @@ int main(int argc, char *argv[]) {
  *
  * @returns     token count (0 on error)
  */
-static int process_commandline(char *prompt, char **command, char *parameters[]) {
+static int parse_commands(char *prompt, char *parameters[]) {
         char *previous_token, *current_token;
         int token_index = 0;
 
-        *command = strtok(prompt, "\n\t "); // do not include new line
-        if (! *command) return token_index;
+        char *tmp = strtok(prompt, "\n\t "); // do not include new line
+        if (! tmp) return token_index;
 
         // according to specification argv[0]
-        parameters[token_index++] = *command;
+        parameters[token_index++] = tmp;
 
         previous_token = prompt;
         while ((current_token = strtok(NULL, "\n\t ")) != NULL) {