From 2b3c1171ac6c5da8801897906aeaf0be6428a8ea Mon Sep 17 00:00:00 2001 From: Stefan 'psYchotic' Zwanenburg Date: Tue, 26 Jan 2010 00:37:18 +0100 Subject: [PATCH] Refactoring: - The 'cd', 'kill' and 'exit' builtins were each extracted to a function Others: - Error messages are now printed on stderr --- ss.c | 117 ++++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 39 deletions(-) diff --git a/ss.c b/ss.c index 9de5195..d27df09 100644 --- a/ss.c +++ b/ss.c @@ -54,6 +54,80 @@ char **split_input(char *input) { } /** + * Exits the shell. + * @return non-zero on success (no arguments were passed), zero otherwise. + */ +int exit_cmd(char **argv) { + if (argv[0] != NULL) { + fprintf(stderr, "The 'exit' builtin does not take any arguments.\n"); + return 0; + } else { + return 1; + } +} + +/** + * Sends a signal to a list of PIDs. + * In this implementation, the SIGTERM (15) signal is sent. If an error + * occurs whilst trying to send said signal, a message is printed on stderr. + * Inversely, when sending the signal succeeds, a message is printed on stdout + * to notify the user. + * @return always zero + */ +int kill_cmd(char **argv) { + int i; + pid_t pid; + if (argv[0] == NULL) { + printf("The 'kill' builtin takes at least one argument (a PID).\n"); + } else { + for (i = 0; argv[i] != NULL; i++) { + if (sscanf(argv[i], "%d", &pid)) { + if (kill(pid, SIGTERM)) { + fprintf(stderr, "Couldn't kill the process with PID %d: %s\n", pid, strerror(errno)); + } else { + fprintf(stderr, "Killed (or sent signal %d) to PID %d.\n", SIGTERM, pid); + } + } else { + printf("%s is not a valid PID. Skipping.\n", argv[i]); + } + } + } + + return 0; +} + +/** + * Changes the current working directory. + * Note that the actual changing of the current working directory may fail, + * in which case an error is printed on stderr. This command takes a single + * argument: the directory to change to. + * @return always zero + */ +int cd_cmd(char **argv) { + char *todir; + if (argv[0] == NULL) { + struct passwd *passwd = getpwuid(getuid()); + if (passwd == NULL) { + fprintf(stderr, "Could not get the current user's home directory: %s\n", strerror(errno)); + } else { + todir = passwd->pw_dir; + } + } else if (argv[1] == NULL) { + todir = argv[0]; + } else { + fprintf(stderr, "The 'cd' builtin takes at most one argument (the directory to move to).\n"); + return 0; + } + + if (chdir(todir)) { + fprintf(stderr, "Could not change the current working directory to '%s': %s.\n", todir, strerror(errno)); + } + + return 0; +} + + +/** * Handles a line of input. * This function handles a line of input, which may * result in two things: @@ -79,51 +153,16 @@ int handle_input(char *input) { argv = split_input(input); free(input); if (!strcmp(argv[0], "exit")) { - if (argv[1] != NULL) { - printf("The 'exit' builtin does not take any arguments.\n"); - } else { - return_value = 1; - } + return_value = exit_cmd(&argv[1]); } else if(!strcmp(argv[0], "kill")) { - if (argv[1] == NULL) { - printf("The 'kill' builtin takes at least one argument (a PID).\n"); - } else { - for (i = 1; argv[i] != NULL; i++) { - if (sscanf(argv[i], "%d", &pid)) { - if (kill(pid, SIGTERM)) { - printf("Couldn't kill the process with PID %d: %s\n", pid, strerror(errno)); - } else { - printf("Killed (or sent signal %d) to PID %d.\n", SIGTERM, pid); - } - } else { - printf("%s is not a valid PID. Skipping.\n", argv[i]); - } - } - } + return_value = kill_cmd(&argv[1]); } else if (!strcmp(argv[0], "cd")) { - char *todir; - if (argv[1] == NULL) { - struct passwd *passwd = getpwuid(getuid()); - if (passwd == NULL) { - printf("Could not get the current user's home directory: %s\n", strerror(errno)); - } else { - todir = passwd->pw_dir; - } - } else if (argv[2] == NULL) { - todir = argv[1]; - } else { - printf("The 'cd' builtin takes at most one argument (the directory to move to).\n"); - todir = argv[1] = get_current_dir_name(); - } - - if (chdir(todir)) { - printf("Could not change the current working directory to '%s': %s.\n", todir, strerror(errno)); - } + return_value = cd_cmd(&argv[1]); } else { pid = fork(); if (pid == 0) { if (execvp(argv[0], argv)) { - printf("Could not execute '%s': %s\n", argv[0], strerror(errno)); + fprintf(stderr, "Could not execute '%s': %s\n", argv[0], strerror(errno)); } return_value = 1; } else { -- 2.11.4.GIT