Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <string.h>
- const int BUF_SIZE = 1024;
- const int TOK_BUF_SIZE = 64;
- const char DELIM[] = " ";
- static int isOutput = 0;
- static char *path;
- static size_t size = 0;
- void msh_loop(void);
- char* msh_readLine(void);
- char** msh_readArgs(char *line);
- int msh_executeCommand(char **args);
- void msh_releaseMemory(char *line, char **args, char *path);
- void mhs_removeChar(char *str, char garbage);
- int main(int argc, char* argv[]) {
- msh_loop();
- return 0;
- }
- void msh_loop(void) {
- char *line;
- char **args;
- int status;
- do {
- printf("minish > ");
- line = msh_readLine();
- args = msh_readArgs(line);
- status = msh_executeCommand(args);
- msh_releaseMemory(line, args, path);
- }
- while(status);
- }
- char* msh_readLine(void) {
- int i = 0;
- char *buffer = malloc(sizeof(char) * BUF_SIZE);
- if(buffer == NULL) {
- perror("allocatting memory for buffer");
- exit(1);
- }
- //Reads input from console
- while(1) {
- char c = getchar();
- if(c == EOF || c == '\n')
- break;
- else
- buffer[i] = c;
- i++;
- }
- return buffer;
- }
- char** msh_readArgs(char *line) {
- if(line == NULL)
- return NULL;
- int i = 0;
- char *token;
- char **tokens = malloc(TOK_BUF_SIZE * sizeof(char*));
- if(tokens == NULL) {
- perror("allocatting memory");
- exit(1);
- }
- token = strtok(line, DELIM);
- //Separate words from line
- while(token != NULL) {
- //No need to save "-o outputFileName" in args
- if(strcmp(token, "-o") == 0) {
- isOutput = 1;
- i++;
- break;
- }
- tokens[i] = malloc(strlen(token) * sizeof(char));
- if(tokens[i] == NULL) {
- perror("allocatting memory");
- exit(1);
- }
- strcpy(tokens[i], token);
- i++;
- token = strtok(NULL, DELIM);
- }
- size = i - 1;
- tokens[i] = NULL;
- //Save output filename
- if(isOutput == 1) {
- //Get output file from line
- token = strtok(NULL, DELIM);
- mhs_removeChar(token, '\n');
- const char* txt = ".txt";
- path = malloc(strlen(token) + 1 + 4);
- if(path == NULL) {
- perror("allocating memory");
- exit(1);
- }
- strcpy(path, token);
- strcat(path, txt);
- }
- else
- mhs_removeChar(tokens[i - 1], '\n');
- return tokens;
- }
- int msh_executeCommand(char **args) {
- if(args == NULL)
- return 1;
- pid_t pid;
- pid = fork();
- if(pid < 0)
- perror("fork");
- else if(pid > 0) {
- int status;
- waitpid(pid, &status, 0);
- }
- else if(pid == 0) {
- if(isOutput == 1) {
- int fd = open(path, O_CREAT | O_WRONLY, S_IRUSR);
- if(fd < 0) {
- perror("creating file.");
- exit(1);
- }
- if(dup2(fd, 1) < 0) {
- perror("redirectioning output.");
- exit(1);
- }
- if(fd != STDOUT_FILENO)
- close(fd);
- isOutput = 0;
- }
- if(execvp(args[0], args) == -1)
- perror("exec");
- }
- return 1;
- }
- void msh_releaseMemory(char *line, char **args, char *path) {
- if(line == NULL)
- return;
- else
- free(line);
- if(args == NULL)
- return;
- else {
- int i;
- for(i = 0; i < size; i++)
- free(args[i]);
- free(args);
- size = 0;
- }
- if(path == NULL)
- return;
- else
- free(path);
- }
- void mhs_removeChar(char *str, char garbage) {
- char *src, *dst;
- for (src = dst = str; *src != '\0'; src++) {
- *dst = *src;
- if (*dst != garbage)
- dst++;
- }
- *dst = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement