executing commands in own shell - C -
i have troubles creating own shell in c. here code have far
#include "interpreter.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> #include <syslog.h> void readconfig(int *timeout, char *file){ file * fp; char * line = null; size_t len = 0; char * token; fp = fopen(config, "r"); if (fp == null) { *timeout = defaulttime; } else { while ((getline(&line, &len, fp)) != -1){ token = strtok(line, token); if ((strcmp(token,time_period) == 0)){ *timeout = atoi(strtok(null, token)); } if ((strcmp(token,history)==0)){ strcpy(file,trim_command(strtok(null, token))); } } } } int waitperiod(pid_t pid){ if(pid) { int stat_val; pid_t child_pid; int time = 0; while(time < time_period){ child_pid = waitpid(pid, &stat_val, wnohang); if (child_pid == pid){ break; } else if (child_pid == 0) { time++; sleep(1); } } if (time == time_period){ printf("process ended. execution time over. \n"); kill(pid, sigint); if (kill(pid, 0) == 0){ kill(pid, sigkill); } } if(wifexited(stat_val)) { return wexitstatus(stat_val); } return 0; } else { return -1; } } void splitcommand(char ** parameters, char * command, int count){ char * cm = strtok(command, " "); int = 0; while (cm != null){ parameters[i] = malloc(strlen(cm) * sizeof(char *)); parameters[i] = cm; i++; cm = strtok (null, " "); } i++; parameters[i] = null; } int getparamscount(char command[]){ int i; int count = 1; int com_length = strlen(command); for(i=0; i<com_length; i++){ if (command[i] == ' '){ count++; } } return count; } void gethistory(file * history_file, char ** history_commands){ char line[max_input]; int = 0; int j; rewind(history_file); while (fgets(line, max_input, history_file)!= null){ history_commands[i] = malloc(strlen(line) * sizeof(char *)); history_commands[i] = strdup(line); for(j=0; j<max_input; j++){ if (history_commands[i][j] == '\n'){ history_commands[i][j] = '\0'; break; } } i++; } } int getnumberofrows(file * history_file){ rewind(history_file); int lines = 0; char ch; while(!feof(history_file)){ ch = fgetc(history_file); if(ch == '\n'){ lines++; } } return lines; } void printcommands(char ** history_commands, int lines){ int = 0; int j = 0; for(j = i; j < lines; j++){ printf("[%d] %s\n", j, history_commands[j]); } } int runcommand(pid_t pid, char * command, char ** parameters, int child){ int count = 0; switch(pid) { case -1 : return -1; case 0 : count = getparamscount(command); parameters = malloc((count+1) * sizeof(char *)); splitcommand(parameters, command, count); if (strncmp("cd", parameters[0], 2) == 0){ chdir(parameters[1]); } else { execvp(parameters[0], parameters[0]); } free(parameters); return 0; default : child = waitperiod(pid); printf("child status: %d\n", child); command[0] = 0; free(command); return 0; } } char * trim_command(char * string){ int l = strlen(string); int i; (i = 0; i<l; i++){ if (string[i] == '\n'){ string[i] = '\0'; } } return string; } int main(){ char * command; file * history; char cwd[max_input]; pid_t pid = 0; int child = 0; int number_of_rows = 0; int count = 0; char ** history_commands; char ** parameters[10]; printf(" safe comand line interpreter \n"); printf(" type \"quit\" cancel interpreter or command \n"); printf(" type \"history\" show full command history \n"); printf(" type \"history number\" execute command history \n"); printf("________________________________________________________________________________________\n"); sleep(1); history = fopen(history, "a+"); if (history == null){ printf("could not open history file\n"); return -1; } while(1){ getcwd(cwd, sizeof(cwd)); printf("command: [%d]%s : ", child, cwd); command = malloc(max_input * sizeof(char *)); fgets(command, max_input, stdin); trim_command(command); if (strncmp(quit, command, 4) == 0) { fclose(history); return exit_success; } else if (strncmp("history", command, 7) == 0) { number_of_rows = getnumberofrows(history); history_commands = malloc(number_of_rows * sizeof(char *)); gethistory(history, history_commands); count = getparamscount(command); if (strcmp("history", command) == 0){ printf("history of commands\n"); printcommands(history_commands, number_of_rows); } else { char * cm = strtok(command, " "); cm = strtok(null, " "); int x; x = atoi(cm); if (x >= number_of_rows){ printf("command not exist\n"); continue; } command = strdup(history_commands[x]); count = getparamscount(command); trim_command(command); fprintf(history, "%s\n", command); pid = fork(); runcommand(pid, command, parameters, child); } } else if (command[0] != 0){ fprintf(history, "%s\n", command); pid = fork(); runcommand(pid, command, parameters, child); } } fclose(history); return 0; }
the problem is, doesn't execute cd command. when comment malloc , free lines in runcommand method, cd command starts working, every other command without parameter not working... me this?
Comments
Post a Comment