Final
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
|
||||
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
|
||||
{
|
||||
"name": "Ubuntu",
|
||||
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
||||
"image": "mcr.microsoft.com/devcontainers/base:jammy"
|
||||
|
||||
// Features to add to the dev container. More info: https://containers.dev/features.
|
||||
// "features": {},
|
||||
|
||||
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||
// "forwardPorts": [],
|
||||
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
// "postCreateCommand": "uname -a",
|
||||
|
||||
// Configure tool-specific properties.
|
||||
// "customizations": {},
|
||||
|
||||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
||||
// "remoteUser": "root"
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,7 @@
|
||||
## cos331-hw2 ##
|
||||
# Nicholas Pease #
|
||||
# All work is my own #
|
||||
|
||||
Attached is my HW2 code. One note, nowhere in the homework specifications does it call for this assignment being an "infinite loop", so as a result this program will attempt one execution and finish. If the intent was for it to run continously, uncommenting the lines 15,16 and 67 will restore this functionality.
|
||||
|
||||
This program takes the complete filepath of a program and up to three parameters for that program, and attempts to execute it in a new thread. If it fails, it will print out an error message indicating this and exit. If it succeeds, it will run the program and exit both the new thread and the source thread.
|
||||
Binary file not shown.
@@ -0,0 +1,68 @@
|
||||
// HW2 - COS331
|
||||
// Nicholas Pease
|
||||
// 03/06/2025
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#define COMMAND_LENGTH 80
|
||||
|
||||
const char *terminal_line = "$ ";
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *command = malloc(COMMAND_LENGTH);
|
||||
// while (1)
|
||||
// {
|
||||
write(0, terminal_line, sizeof terminal_line); // write to terminal
|
||||
read(0, command, COMMAND_LENGTH); // read from terminal
|
||||
|
||||
char *args[5] = { '\0', '\0', '\0', '\0', '\0' }; // 3 arguments + one program name + NULL
|
||||
|
||||
args[0] = command;
|
||||
int start_vector = 1;
|
||||
|
||||
while (*command != '\n') {
|
||||
if (*command == ' ') {
|
||||
*command = '\0';
|
||||
args[start_vector] = command + 1;
|
||||
start_vector++;
|
||||
}
|
||||
command++;
|
||||
}
|
||||
*command = '\0'; // For newline
|
||||
|
||||
int ret = fork(); // Fork
|
||||
if (ret) {
|
||||
wait(NULL);
|
||||
// continue on the loop
|
||||
} else {
|
||||
printf("Getting ready to execute program %s", args[0]);
|
||||
|
||||
switch (start_vector) {
|
||||
case 1: // No parameters just filename
|
||||
printf("\n");
|
||||
break;
|
||||
|
||||
case 2: // one parameter, no need to loop since commas wont work
|
||||
printf(" with parameter %s.\n", args[1]);
|
||||
break;
|
||||
|
||||
default: // multiple parameters
|
||||
printf(" with parameters: ");
|
||||
for (int i = 1; i < start_vector - 1; i++) {
|
||||
printf("%s, ", args[i]);
|
||||
}
|
||||
printf("%s.\n", args[start_vector - 1]);
|
||||
break;
|
||||
}
|
||||
|
||||
execv(args[0], args);
|
||||
|
||||
// Errored out
|
||||
printf("Program %s failed to launch. Please ensure the file exists and try again.\n", args[0]);
|
||||
perror("Failed"); // if fails bomb out and print error
|
||||
exit(0);
|
||||
}
|
||||
// }
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
## cos331-hw2 ##
|
||||
|
||||
[](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
|
||||
[](https://drone.nicholaspease.com/umaine-npease/cos331-hw2)
|
||||
[](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos331-hw2)
|
||||

|
||||
<hr>
|
||||
Reference in New Issue
Block a user