ignoring homework folder for now jej

This commit is contained in:
shockrah 2019-09-28 17:48:11 -07:00
parent 261701be7e
commit 9336f1b728
9 changed files with 1 additions and 299 deletions

1
334/.gitignore vendored
View File

@ -2,3 +2,4 @@ bin
msh
*txt
*sh
homework/

View File

@ -1,47 +0,0 @@
# Problem lines begin with #@.
# For each problem, put your answer on
# the line following the problem line.
# Do not modify this file in any way
# except to insert your answers.
#@ 1 -- enter a number
22
#@ 2 -- enter a number
7
#@ 3 -- enter a number
3
#@ 4 -- enter a number
25
#@ 5 -- enter a number
255
#@ 6 -- enter a binary number
1101
#@ 7 -- enter a binary number
10101
#@ 8 -- enter a binary number
10000
#@ 9 -- enter a binary number
110111
#@ 10 -- enter a binary number
1010000
#@ 11 -- enter a number
44
#@ 12 -- enter a number
18
#@ 13 -- enter a number
471
#@ 14 -- enter a number
255
#@ 15 -- enter a number
123
#@ 16 -- enter a hex number (beginning with 0x)
0x12
#@ 17 -- enter a hex number
0x05
#@ 18 -- enter a hex number
0x58
#@ 19 -- enter a hex number
0x7b
#@ 20 -- enter a hex number
0x64

View File

@ -1,19 +0,0 @@
# Homework 1 answer sheet.
#
# Problem lines begin with #@.
# For each problem, put your answer on
# the line following the problem line.
# Do not modify this file in any way
# except to insert your answers.
#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer)
a
#@ 2 -- enter 'a', 'b', or 'c'
b
#@ 3 -- enter one or more letters, separated by commas
b
#@ 4 -- enter 'a' or 'b'
a
#@ 5 -- enter 'a', 'b', or 'c'
b

View File

@ -1,22 +0,0 @@
# Instructions: edit this file to provide your homework answers.
#
# Problem numbers are shown on lines that begin with #@.
# Please put your solution to a problem on the line *after* the
# line with the problem number.
#
# Do not modify this file *except* to add lines as needed after each problem.
# Blank lines are ignored. You don't need to put your name in the file.
#
# No points will be awarded to incorrectly-formatted files.
#
#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer)
b
#@ 2 -- enter 'a', 'b', or 'c'
c
#@ 3 -- enter 'a' or 'b'
b
#@ 4 -- enter 'a' or 'b'
b
#@ 5 -- enter 'a', 'b', or 'c'
c

View File

@ -1,32 +0,0 @@
# Instructions: edit this file to provide your homework answers.
#
# Problem numbers are shown on lines that begin with #@.
# Please put your solution to a problem on the line *after* the
# line with the problem number.
#
# Do not modify this file *except* to add lines as needed after each problem.
# Blank lines are ignored. You don't need to put your name in the file.
#
# No points will be awarded to incorrectly-formatted files.
#
#@ 1 -- enter a single number
1
#@ 2 -- enter 'Y' or 'N'
2
#@ 3 -- enter 'Y' or 'N'
Y
#@ 4 -- enter 'Y' or 'N'
N
#@ 5 -- enter text on a single line
some stuff
#@ 6 -- enter 'Y' or 'N'
Y
#@ 7 -- enter text on a single line
stuff
#@ 8 -- enter 'Y' or 'N'
N
#@ 9 -- enter text on a single line
some stuff
#@ 10 -- enter 'Y' or 'N'
Y

View File

@ -1,14 +0,0 @@
#
# Problem lines begin with "#@".
# For each problem line, enter your answer on the following line.
# Do not modify this file in any way except to enter your answers.
#
#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer)
b
#@ 2 -- enter 'a', 'b', or 'c'
a
#@ 3 -- enter 'a', 'b', or 'c'
b
#@ 4 -- enter 'a', 'b', or 'c'
a

View File

@ -1,14 +0,0 @@
#
# Problem lines begin with "#@".
# For each problem line, enter your answer on the following line.
# Do not modify this file in any way except to enter your answers.
#
#@ 1 -- enter 'a' or 'b' (don't use quotes in any answer)
b
#@ 2 -- enter 'a', 'b', or 'c'
a
#@ 3 -- enter 'T' or 'F'
F
#@ 4 -- enter 'T' or 'F'
F

View File

@ -1,151 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/types.h>
#define PROMPT "msh> "
#define EXIT_CMD "exit"
#define MAX_BUF 121
// Statuses from builtin responses
#define RESP_HELP 1
#define RESP_DATE 2
#define RESP_ECHO 3
#define RESP_SHELL 4
#define RESP_CD_FAIL -1
#define RESP_CD_SUCCESS 6
// Sub statuses that are generally nice to have
#define CD_NOP 0x1
void echo(const char*);
void remove_newline(char*);
void exit_branch(const char*);
int builtin_response(char*);
int cd_handler(char**, const int);
int main(int argc, char** argv) {
FILE* file = NULL;
if(argc == 2) {
file = fopen(argv[1], "r");
if(!file) { return 1; }
}
char buffer[MAX_BUF];
while(1) {
// determine if we're reading from stdin or a stream of some sort
if(file == NULL && isatty(STDIN_FILENO)) {
printf("%s", PROMPT);
fgets(buffer, MAX_BUF, stdin);
}
else {
fgets(buffer, MAX_BUF, file);
printf("buffer: %s\n", buffer);
}
// Deal with EOF input
if(!strlen(buffer)) {
exit(0);
}
exit_branch(buffer);
remove_newline(buffer);
int status = builtin_response(buffer);
// the switch in case we need to add more response flags later
switch(status) {
case RESP_ECHO:
echo(buffer);
case RESP_SHELL: // shell command handler was used
continue;
case RESP_CD_FAIL:
printf("msh: cd: No such file or directory\n");
}
memset(buffer, 0x00, MAX_BUF); // reset buffer after each usage
}
return 0;
}
void echo(const char* buf) {
printf("%s\n", buf);
}
void remove_newline(char* buf) {
char* c = buf;
while(*c != '\n') { c++; }
*c='\0';
}
void exit_branch(const char* buf) {
if(!strcmp(buf, EXIT_CMD)) {
exit(0);
}
}
int builtin_response(char* buffer) {
#define STRING_DELIMITER " "
#define TIME_FMT "%D"
exit_branch(buffer); // exit is builtin so here we are
if(!strcmp(buffer, "help")) {
echo("enter Linux commands, or exit to exit");
return RESP_HELP;
}
// Printout the date today
if(!strcmp(buffer, "date")) {
static char date[64];
time_t now = time(0);
strftime(date, sizeof(date), TIME_FMT, localtime(&now));
echo(date);
return RESP_DATE;
}
// Populate tokens
char *tokens[64];
int idx = 0;
for(char* cur = strtok(buffer, STRING_DELIMITER); cur != NULL; cur=strtok(NULL, STRING_DELIMITER)) {
#ifdef DBG_LINE
printf("%s ", cur);
#endif
tokens[idx] = cur;
idx++;
}
// Handling director changes
int cd_result = cd_handler(tokens, idx);
if(cd_result != CD_NOP) {
return cd_result;
}
// Execute child process
pid_t child = fork();
if(child != 0) {
int status_child = wait(NULL);
if(status_child == 127) {
printf("msh: %s: %d", tokens[0], status_child);
}
}
else {
execvp(tokens[0], tokens);
}
// Cleanup pointers
for(int i = 0; i< 64; i++) {
tokens[i] = NULL;
}
}
int cd_handler(char** tokens, const int t_count) {
if(!strcmp(tokens[0], "cd")) {
if(t_count < 2) {
int ret = chdir(getenv("HOME"));
return ret;
}
return chdir(tokens[1]);
}
return CD_NOP;
}