Commit 4e15f25b authored by Son Pham's avatar Son Pham

Lab 10.1 completed

parent 68102c78
CC = gcc
CFLAGS = -g
LFLAGS = -lpthread
ALL = ishell
all : $(ALL)
ishell: ishell.c wrappers.o
$(CC) $(CFLAGS) -c ishell.c -o ishell.o
$(CC) $(CFLAGS) ishell.o wrappers.o -o ishell
wrappers: wrappers.c
$(CC) $(CFLAGS) -c wrappers.c -o wrappers.o
clean:
/bin/rm -f *.o *~ core $(ALL)
\ No newline at end of file
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* Lab 10 - UNIX Shell
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wrappers.h"
#define STRING_LENGTH 256 // String length
int status;
int main(int argc, char* argv[]) {
// Print the fun ASCII shell image
printf("\n");
printf("██╗███████╗██╗ ██╗███████╗██╗ ██╗\n");
printf("██║██╔════╝██║ ██║██╔════╝██║ ██║\n");
printf("██║███████╗███████║█████╗ ██║ ██║\n");
printf("██║╚════██║██╔══██║██╔══╝ ██║ ██║\n");
printf("██║███████║██║ ██║███████╗███████╗███████╗\n");
printf("╚═╝╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝\n");
printf("-------------------------------------------\n");
printf("by Son Pham\n");
printf("\n");
// Set up string for input from the user
char s[256];
// While loop, constantly taking user input
while(1) {
printf("ishell> ");
// Read command line
fgets(s, sizeof(s), stdin); // Read 255 character upuntil the new line
printf("%s\n", s);
// Parse command line into a list of argument
char ** param_list = NULL;
char * token = strtok(s, " \n");
int n_args = 0; // Counting the number of arguments in the string
while(token) {
param_list = realloc(param_list, sizeof(char*) *++n_args); // TODO: Wrapper
param_list[n_args-1] = token;
token = strtok(NULL, " \n");
}
// Allocate one extra element for the last NULL */
param_list = realloc(param_list, sizeof (char*) * (n_args+1));
param_list[n_args] = 0;
// Spawn a child to execute command line
pid_t pid = Fork();
if (pid == 0) { // child process exec
int value = execv(param_list[0], param_list); // TODO: WRAPPER
exit(0);
} else { // Parent process
waitpid(pid, &status, 0);
}
}
}
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* Lab 10 - UNIX Shell
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wrappers.h"
#define STRING_LENGTH 256 // String length
int status;
int main(int argc, char* argv[]) {
// Print the fun ASCII shell image
printf("\n");
printf("██╗███████╗██╗ ██╗███████╗██╗ ██╗\n");
printf("██║██╔════╝██║ ██║██╔════╝██║ ██║\n");
printf("██║███████╗███████║█████╗ ██║ ██║\n");
printf("██║╚════██║██╔══██║██╔══╝ ██║ ██║\n");
printf("██║███████║██║ ██║███████╗███████╗███████╗\n");
printf("╚═╝╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝\n");
printf("-------------------------------------------\n");
printf("by Son Pham\n");
printf("\n");
// Set up string for input from the user
char s[256];
// While loop, constantly taking user input
while(1) {
printf("ishell> ");
// Read command line
fgets(s, sizeof(s), stdin); // Read 255 character upuntil the new line
printf("%s\n", s);
// Parse command line into a list of argument
char ** param_list = NULL;
char * token = strtok(s, " \n");
int n_args = 0; // Counting the number of arguments in the string
while(token) {
param_list = realloc(param_list, sizeof(char*) *++n_args);
param_list[n_args-1] = token;
token = strtok(NULL, " \n");
}
// Allocate one extra element for the last NULL */
param_list = realloc(param_list, sizeof (char*) * (n_args+1));
param_list[n_args] = 0;
// Spawn a child to execute command line
pid_t pid = Fork();
if (pid == 0) { // child process exec
int value = execv(param_list[0], param_list);
printf("%s\n", param_list[0]);
printf("Result %d\n", value);
exit(0);
} else { // Parent process
waitpid(pid, &status, 0);
}
}
}
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone\
* Lab 4 - Wrapper functions
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "wrappers.h"
/**
* Fork Wrapper
*/
pid_t Fork(void) {
// Fork the thing to get the pid
pid_t pid = fork();
// Check and make appropriate code for each pid case
if (pid == 0) return 0;
else if (pid == -1) {
char error_mes[] = "Fork failed!";
perror(error_mes);
exit(-1);
}
}
/**
* Pipe Wrapper
*/
int Pipe(int pipefd[2]) {
// Call Pipe with system call
int ret = pipe(pipefd);
// Check and make appropriate code for each ret case
if (ret == 0) return 0;
else if (ret == -1) {
char error_mes[] = "Pipe failed";
perror(error_mes);
exit(-1);
}
}
/**
* Wait Wrapper
*/
pid_t Wait(int *status) {
// Call wait system call
pid_t ret = wait(status);
// Check for appropriate cases
if (ret == 0) return 0;
else if (ret == -1) {
char error_mes[] = "Wait failed";
perror(error_mes);
exit(-1);
}
}
/**
* Waitpid Wrapper
*/
pid_t Waitpid(pid_t pid, int *status, int options) {
// Call waitpid system call
pid_t ret = waitpid(pid, status, options);
// Check for appropriate cases
if (ret == 0) return 0;
else if (ret == -1) {
char error_mes[] = "Pipe failed";
perror(error_mes);
exit(-1);
}
}
/**
* Open Wrapper
*/
int Open(const char *pathname, int flags) {
// Call open system call
int ret = open(pathname, flags);
// Check for appropriate cases
if (ret == -1) {
char error_mes[] = "Open failed";
perror(error_mes);
exit(-1);
} else return ret;
}
/**
* Close Wrapper
*/
int Close(int fd) {
// Call close ssytem call
int ret = close(fd);
// Check for appropriate cases
if (ret == 0) return 0;
else if (ret == -1) {
char error_mes[] = "Close failed";
perror(error_mes);
exit(-1);
}
}
/**
* Write wrapper
*/
ssize_t Write(int fd, const void *buf, size_t count) {
// Call write with system call
ssize_t ret = write(fd, buf, count);
// Check and make appropriate code for each write case
if (ret >= 0) return ret;
else if (ret < 0) {
char error_mes[] = "Write terribly failed";
perror(error_mes);
exit(-1);
}
}
/**
* Read Wrapper
*/
ssize_t Read(int fd, void *buf, size_t count) {
// Call read with system call
ssize_t ret = read(fd, buf, count);
// Check and make appropriate code for each read case
if (ret >= 0) return ret;
else if (ret < 0) {
char error_mes[] = "Read failed";
perror(error_mes);
exit(-1);
}
}
/**
* Connect Wrapper
*/
int Connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
// Call connect system call
int ret = connect(sockfd, addr, addrlen);
// Check for appropriate cases
if (ret == 0) return 0;
else if (ret == -1) {
char error_mes[] = "Connect failed";
perror(error_mes);
exit(-1);
}
}
/**
* Bind Wrapper
*/
int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
// Call bind system call
int ret = bind(sockfd, addr, addrlen);
// Check for appropriate cases
if (ret == 0) return 0;
else if (ret == -1) {
char error_mes[] = "Bind failed";
perror(error_mes);
exit(-1);
}
}
/**
* Listen Wrapper
*/
int Listen(int sockfd, int backlog) {
// Call listen system call
int ret = listen(sockfd, backlog);
// Check for appropriate listen cases
if (ret == 0) return 0;
else if (ret == -1) {
char error_mes[] = "Listen failed";
perror(error_mes);
exit(-1);
}
}
/**
* Accept Wrapper
*/
int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
// Call accept system call
int ret = accept(sockfd, addr, addrlen);
// Check for appropriate accept case
if (ret >= 0) return ret;
else if (ret == -1) {
char error_mes[] = "Accept failed\n";
perror(error_mes);
exit(-1);
}
}
void ReadXBytes(int sockfd, unsigned int x, void* buffer)
{
int bytesRead = 0;
int ret;
while (bytesRead < x)
{
ret = read(sockfd, buffer + bytesRead, x - bytesRead);
if (ret < 1)
{
char error_mes[] = "Read error\n";
perror(error_mes);
exit(-1);
}
bytesRead += ret;
}
}
/**
* Socket wrapper
*/
int Socket(int domain, int type, int protocol) {
// Call the system call
int ret = socket(domain,type,protocol);
// Check the return value
if (ret >= 0) return ret;
else if (ret == -1) {
char error_mes[] = "Socket creation error";
perror(error_mes);
exit(-1);
}
}
/**
* Sendto wrapper
*/
ssize_t Sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) {
int ret = sendto(sockfd, buf, len, flags, dest_addr, addrlen);
if (ret >= 0) return ret;
else if (ret == -1) {
char error_mes[] = "Sendto error";
perror(error_mes);
exit(-1);
}
}
/**
* Recvfrom wrapper
*/
ssize_t Recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) {
int ret = recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
if (ret >= 0) return ret;
else if (ret == -1) {
char error_mes[] = "Recvfrom error";
perror(error_mes);
exit(-1);
}
}
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* Lab 4 - All Wrappers Headers
*/
#ifndef WRAPPERS_H
#define WRAPPERS_H
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
pid_t Fork(void);
int Pipe(int pipefd[2]);
pid_t Wait(int *status);
pid_t Waitpid(pid_t pid, int *status, int options);
int Open(const char *pathname, int flags);
int Close(int fd);
ssize_t Write(int fd, const void *buf, size_t count);
ssize_t Read(int fd, void *buf, size_t count);
int Connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int Listen(int sockfd, int backlog);
int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
void ReadXBytes(int sockfd, unsigned int x, void* buffer);
int Socket(int domain, int type, int protocol);
ssize_t Sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t Recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
#endif
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