Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <unistd.h>
- #include <limits.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #define LOGOUT 1000
- #define MAXNUM 40
- #define MAXLEN 160
- void sighandler(int sig)
- {
- switch (sig) {
- case SIGALRM:
- printf("\nautologout\n");
- exit(0);
- default:
- break;
- }
- return;
- }
- void histRead(char* currentdir){
- char* history;
- char* line;
- size_t len = 0;
- ssize_t read;
- int id = 1;
- FILE *tiedosto;
- tiedosto = fopen(currentdir, "r");
- while((read = getline(&line, &len, tiedosto)) != -1) {
- printf("%d %s", id, line);
- id ++;
- }
- fclose(tiedosto);
- }
- char* historynum(int i) {
- char* history;
- char* line;
- size_t len = 0;
- ssize_t read;
- int j = 1, cmd_in=0;
- char * cmd, * args[MAXNUM];
- int pid;
- int background;
- int id = 1;
- FILE *tiedosto;
- tiedosto = fopen("history.txt", "r");
- while((read = getline(&line, &len, tiedosto)) != -1) {
- if (i==j) {
- printf("%s", line);
- cmd = line;
- break;
- }
- else {
- j++;
- }
- }
- fclose(tiedosto);
- return cmd;
- }
- //void run_command(args) {
- // }
- int main(void)
- {
- char * cmd, line[MAXLEN], * args[MAXNUM];
- int background, i;
- int pid;
- char buff[PATH_MAX + 1];
- char* cwd;
- char buf[512];
- char* joku;
- int cmd_in = 0;
- signal(SIGALRM, sighandler);
- signal(SIGINT, sighandler);
- char currentdir[1234];
- if ((getcwd(currentdir, sizeof(currentdir)) != NULL))
- strcat(currentdir, "/history.txt");
- while (1) {
- background = 0;
- cwd = getcwd(buff, PATH_MAX + 1);
- /* print the prompt */
- if (cwd != NULL) {
- printf("%s ", cwd);
- };
- /* set the timeout for alarm signal (autologout) */
- alarm(LOGOUT);
- /* read the users command */
- if (fgets(line,MAXLEN,stdin) == NULL) {
- printf("\nlogout\n");
- exit(0);
- }
- line[strlen(line) - 1] = '\0';
- if (strlen(line) == 0)
- continue;
- /* start to background? */
- if (line[strlen(line)-1] == '&') {
- line[strlen(line)-1] = 0;
- background = 1;
- }
- /* write history log */
- if (strlen(line) != 0) {
- FILE* tiedosto = fopen(currentdir, "a");
- fprintf(tiedosto, "%s\n", line);
- fclose(tiedosto);
- }
- /* split the command line */
- i = 0;
- cmd = line;
- while ( (args[i] = strtok(cmd, " ")) != NULL) {
- printf("arg %d: %s\n", i, args[i]);
- i++;
- cmd = NULL;
- }
- /*tarkistetaan onko kyseessä pipetys*/
- int x=0;
- pid_t childpid;
- while(args[x]){
- if (strchr(args[x], '|')!=NULL) {
- printf("PIPE\n");
- break;
- }
- x++;
- }
- /*tarkistetaan onko kyseessä redirect*/
- /* int k = 0;
- while(args[k]){
- if (strchr(args[k], '<')!=NULL || strchr(args[k], '>')!=NULL) {
- printf("redirect\n");
- break;
- }
- k++;
- int in, out;
- char *grep_args[] = {"grep", "line", NULL};
- // open input and output files
- in = open("ht.c", O_RDONLY);
- out = open("checksum.txt", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
- // replace standard input with input file
- dup2(in, 0);
- // replace standard output with output file
- dup2(out, 1);
- // close unused file descriptors
- close(in);
- close(out);
- }*/
- int cmd_in = 0;
- /* print history number */
- if (strlen(args[0]) >= 2 && args[0][0] == '!' && atoi(args[0]+1)) {
- int i = atoi(args[0]+1);
- joku = historynum(i);
- //printf("%s", joku);
- joku[strlen(joku) - 1] = '\0';
- while ( (args[cmd_in] = strtok(joku, " ")) != NULL) {
- printf("arg %d: %s\n", cmd_in, args[cmd_in]);
- cmd_in++;
- joku = NULL;
- }
- }
- if (strcmp(args[0],"exit")==0) {
- exit(0);
- }
- if (strcmp(args[0], "cd")==0) {
- if (args[1] == NULL) {
- const char* home = getenv("HOME");
- chdir(home);
- }
- char *token = args[1];
- chdir(token);
- continue;
- }
- /* print history log */
- if (strcmp(args[0],"history")==0) {
- histRead(currentdir);
- continue;
- }
- /******************************************************************************/
- /*PIPE*/
- /* int fd[2], nbytes;
- pid_t childpid;
- char string[] = "Hello, world!\n";
- char readbuffer[80];
- int x;
- pipe(fd);
- while ( (args[x] = strtok(cmd, " ")) != NULL) {
- printf("arg %d: %s\n", x, args[x]);
- x++;
- cmd = NULL;
- }
- if (strchr(args[0], '|')!=NULL) {
- printf("PIPE\n");
- if((childpid = fork()) == -1) {
- perror("fork");
- exit(1);
- }
- if(childpid == 0) {
- /* Child process closes up input side of pipe */
- // close(fd[0]);
- /* Send "string" through the output side of pipe */
- // write(fd[1], string, (strlen(string)+1));
- // exit(0);
- //}
- //else {
- /* Parent process closes up output side of pipe */
- // close(fd[1]);
- /* Read in a string from the pipe */
- // nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
- // printf("Received string: %s", readbuffer);
- //}
- //}
- /* end of PIPE*/
- /* REDIRECT */
- /*int in, out;
- char *grep_args[] = {"grep", "Villanova", NULL};
- // open input and output files
- in = open("scores", O_RDONLY);
- out = open("out", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
- // replace standard input with input file
- dup2(in, 0);
- // replace standard output with output file
- dup2(out, 1);
- // close unused file descriptors
- close(in);
- close(out);
- // execute grep
- execvp("grep", grep_args);
- */
- /* end of REDIRECT */
- /*int do_command(char **args, int pipes) {
- // The number of commands to run
- const int commands = pipes + 1;
- int i = 0;
- int pipefds[2*pipes];
- for(i = 0; i < pipes; i++){
- if(pipe(pipefds + i*2) < 0) {
- perror("Couldn't Pipe");
- exit(EXIT_FAILURE);
- }
- }
- int pid;
- int status;
- int j = 0;
- int k = 0;
- int s = 1;
- int place;
- int commandStarts[10];
- commandStarts[0] = 0;
- // This loop sets all of the pipes to NULL
- // And creates an array of where the next
- // Command starts
- while (args[k] != NULL){
- if(!strcmp(args[k], "|")){
- args[k] = NULL;
- // printf("args[%d] is now NULL", k);
- commandStarts[s] = k+1;
- s++;
- }
- k++;
- }
- for (i = 0; i < commands; ++i) {
- // place is where in args the program should
- // start running when it gets to the execution
- // command
- place = commandStarts[i];
- pid = fork();
- if(pid == 0) {
- //if not last command
- if(i < pipes){
- if(dup2(pipefds[j + 1], 1) < 0){
- perror("dup2");
- exit(EXIT_FAILURE);
- }
- }
- //if not first command&& j!= 2*pipes
- if(j != 0 ){
- if(dup2(pipefds[j-2], 0) < 0){
- perror("dup2");
- exit(EXIT_FAILURE);
- }
- }
- int q;
- for(q = 0; q < 2*pipes; q++){
- close(pipefds[q]);
- }
- // The commands are executed here,
- // but it must be doing it a bit wrong
- if( execvp(args[place], args) < 0 ){
- perror(*args);
- exit(EXIT_FAILURE);
- }
- }
- else if(pid < 0){
- perror("error");
- exit(EXIT_FAILURE);
- }
- j+=2;
- }
- for(i = 0; i < 2 * pipes; i++){
- close(pipefds[i]);
- }
- for(i = 0; i < pipes + 1; i++){
- wait(&status);
- }
- }
- /* fork to run the command */
- /******************************************************************************/
- switch (pid = fork()) {
- case -1:
- /* error */
- perror("fork");
- continue;
- case 0:
- /* child process */
- /*if (output_mode == 1) {
- f = fopen(args[outArg], "w");
- if (f==NULL) {
- perror("fopen");
- exit(1);
- }
- dup2(fileno(f), fileno(stdout));
- fclose(f);
- }
- if (input_mode == 1) {
- f = fopen(args[inArg], "r");
- if (f==NULL) {
- perror("fopen");
- exit(1);
- }
- dup2(fileno(f), fileno(stdin));
- fclose(f);
- }*/
- execvp(args[0], args);
- perror("execvp");
- exit(1);
- default:
- /* parent (shell) */
- if (!background) {
- alarm(0);
- //waitpid(pid, NULL, 0);
- while (wait(NULL)!=pid)
- printf("some other child process exited\n");
- }
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement