Commit 72d442d2 authored by Zhaozhong Liu's avatar Zhaozhong Liu
Browse files

lab 9 finished

parent ce461dd7
CC = gcc
CFLAGS = -std=gnu99 -Wall -g #-DDEBUG
LIB = -lreadline
EXECS = ishell
all: $(EXECS)
wrappers.o: wrappers.c wrappers.h
$(CC) $(CFLAGS) -c wrappers.c
ishell.o: ishell.c wrappers.h
$(CC) $(CFLAGS) -c ishell.c $(LIB)
ishell: ishell.o wrappers.o
$(CC) $(CFLAGS) ishell.o wrappers.o -o ishell $(LIB)
.PHONY: clean
clean:
/bin/rm -rf *.o $(EXECS)
CC = gcc
CFLAGS = -std=gnu99 -Wall -g #-DDEBUG
LFLAGS = -lpthread -lm
EXECS = helloworld helloworld1
all: $(EXECS)
helloworld: helloworld.c
$(CC) $(CFLAGS) -o $@ $< $(LFLAGS)
helloworld1: helloworld1.c
$(CC) $(CFLAGS) -o $@ $< $(LFLAGS)
.PHONY: clean
clean:
/bin/rm -f *.o $(EXECS)
#include <stdio.h>
#define MAX_LIMIT 20
int main()
{
char str[MAX_LIMIT];
printf("enter a string: ");
fgets(str, MAX_LIMIT, stdin);
printf("%s", str);
return 0;
}
#inlcude <stdio>
int main()
{
char str[100];
printf("Enter an string: ");
gets(str);
printf("%s", str);
return 0;
}
#include<string.h>
#include<stdio.h>
char** token(char *str, int num_strings){
//create an array of strings
char ** ret = malloc(num_strings * sizeof(char*));
for (int i = 0; i < 10; i++){
ret[i] = malloc(10* sizeof(char));
}
//token
const char *s = " ";
char *tok;
/*get the first token */
tok = strtok(str, s);
int j = 0;
/*walk through other tokens */
while( tok != NULL){
printf(" %s\n", tok);
ret[j] = tok;
j ++;
tok = strtok(NULL, s);
}
return(0);
}
#include<string.h>
#include<stdio.h>
char** token(char *str, int num_strings){
//create an array of strings
char ** ret = malloc(num_strings * sizeof(char*));
for (int i = 0; i < 10; i++){
ret[i] = malloc(10* sizeof(char));
}
//token
const char *s = " ";
char *tok;
/*get the first token */
tok = strtok(str, s);
int j = 0
/*walk through other tokens */
while( tok != NULL){
printf(" %s\n", tok);
ret[j] = tok;
j ++;
tok = strtok(NULL, s);
}
return(0);
}
#include "wrappers.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
char **tokenizer(char *in_str, const char *delim) {
int arr_len = 0;
char **args_arr = malloc(sizeof(char *) * 50);
char *token = strtok(in_str, delim);
while (token) {
args_arr[arr_len] = token;
token = strtok(NULL, delim);
arr_len++;
}
args_arr[arr_len] = NULL;
return args_arr;
}
void displayStatus(int status) {
if (status == 0)
printf("[ishell: program terminated successfully]\n");
else
printf("[ishell: program terminated successfully][%d]\n", status);
}
int main(int argc, char *argv[]) {
char *in_str;
char *prev_str = NULL;
char **prev_cmds = NULL;
char **cmd;
char **arr_cmds;
pid_t pid;
int status;
int i;
int path = 0;
while (1) {
in_str = readline("ishell> ");
arr_cmds = tokenizer(in_str, ";");
// enter detected
if ((arr_cmds[0] == NULL) && (path < 2)) {
path++;
in_str = readline("");
arr_cmds = tokenizer(in_str, ";");
if (arr_cmds[0] == NULL) {
path++;
}
}
if ((strcmp(arr_cmds[0], "!!") == 0) && (prev_cmds != NULL)) {
arr_cmds = prev_cmds;
printf("prev command(s): %s\n", prev_str);
} else {
prev_cmds = arr_cmds;
prev_str = in_str;
}
i = 0;
while ((arr_cmds[i] != NULL) || (path == 2)) {
pid = Fork();
if (pid == 0) {
if (path == 2) {
execl("/bin/ls", "ls", (char *)0);
} else {
cmd = tokenizer(arr_cmds[i], " ");
execvp(cmd[0], cmd);
}
} else {
Wait(&status);
displayStatus(status);
}
i++; path = 0;
}
}
}
\ No newline at end of file
Lab 9
Problem 1:
I completed all the requirements and the code works as expected.
Problem 2:
I completed all the requirements and the code works as expected.
Problem 3:
I completed all the requirements and the code works as expected.
Problem 4:
I completed all the requirements and the code works as expected.
The way I wrote the program, both [enter][enter] and [tab] produce
the required results. Hence I did the challenge as well.
Problem 5:
I completed all the requirements and the code works as expected.
See ishell.c comments for more about the additional functionality.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment