Commit 3892a061 authored by CentOS's avatar CentOS

new file

parents
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 60
#define MAX_HISTORY_SIZE 10
// History struct to store command history
char* history[MAX_HISTORY_SIZE];
int history_count = 0;
// Read a line from stdin
char *readcommand() {
char *line = (char*) malloc(sizeof(char) * MAX);
printf("$> ");
fgets(line, MAX, stdin);
size_t ln = strlen(line)-1;
if (line[ln] == '\n')
line[ln] = '\0';
return line;
}
// Split the line into tokens
char **splitcommand(char *line) {
char delim[] = " ";
char *ptr = strtok(line, delim);
char **tokens = (char**) malloc(sizeof(char*) * MAX);
int index = 0;
while(ptr != NULL) {
tokens[index] = ptr;
index++;
ptr = strtok(NULL, delim);
}
tokens[index] = NULL; // Set the last element to NULL for execvp
return tokens;
}
// Add command to history
void add_to_history(const char* command) {
if (history_count < MAX_HISTORY_SIZE) {
history[history_count] = strdup(command);
history_count++;
} else {
free(history[0]);
int i;
for (i = 0; i < MAX_HISTORY_SIZE - 1; i++) {
history[i] = history[i + 1];
}
history[MAX_HISTORY_SIZE - 1] = strdup(command);
}
}
// Write to file
void writeBye(const char *filename) {
FILE *fptr;
// Opening file in writing mode
fptr = fopen(filename, "w");
// Exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr, "%s", "Bye!\n");
fclose(fptr);
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Please insert exitfile as an argument!\n");
return 1;
}
while (1) {
char *line;
char **tokens;
line = readcommand();
char* line_copy = strdup(line);
tokens = splitcommand(line_copy);
if (strcmp(tokens[0], "exit") == 0) {
writeBye(argv[1]);
exit(0);
} else if (strcmp(tokens[0], "history") == 0) {
add_to_history(line_copy);
int i;
for (i = 0; i < history_count; i++) {
printf("%d: %s\n", i + 1, history[i]);
}
continue;
}
else{
add_to_history(line); // Add the command to history
}
if (fork() == 0) {
if (execvp(tokens[0], tokens) == -1) {
perror("The following error occurred: ");
}
exit(1);
} else {
wait(0);
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 60
#define MAX_HISTORY_SIZE 10
// History struct to store command history
char* history[MAX_HISTORY_SIZE];
int history_count = 0;
// Read a line from stdin
char *readcommand() {
char *line = (char*) malloc(sizeof(char) * MAX);
printf("$> ");
fgets(line, MAX, stdin);
size_t ln = strlen(line)-1;
if (line[ln] == '\n')
line[ln] = '\0';
return line;
}
// Split the line into tokens
char **splitcommand(char *line) {
char delim[] = " ";
char *ptr = strtok(line, delim);
char **tokens = (char**) malloc(sizeof(char*) * MAX);
int index = 0;
while(ptr != NULL) {
tokens[index] = ptr;
index++;
ptr = strtok(NULL, delim);
}
tokens[index] = NULL; // Set the last element to NULL for execvp
return tokens;
}
// Add command to history
void add_to_history(const char* command) {
if (history_count < MAX_HISTORY_SIZE) {
history[history_count] = strdup(command);
history_count++;
} else {
free(history[0]);
int i;
for (i = 0; i < MAX_HISTORY_SIZE - 1; i++) {
history[i] = history[i + 1];
}
history[MAX_HISTORY_SIZE - 1] = strdup(command);
}
}
// Write to file
void writeBye(const char *filename) {
FILE *fptr;
// Opening file in writing mode
fptr = fopen(filename, "w");
// Exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr, "%s", "Bye!\n");
fclose(fptr);
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Please insert exitfile as an argument!\n");
return 1;
}
while (1) {
char *line;
char **tokens;
line = readcommand();
char* line_copy = strdup(line);
tokens = splitcommand(line_copy);
if (strcmp(tokens[0], "exit") == 0) {
writeBye(argv[1]);
exit(0);
} else if (strcmp(tokens[0], "history") == 0) {
add_to_history(line_copy);
int i;
for (i = 0; i < history_count; i++) {
printf("%d: %s\n", i + 1, history[i]);
}
continue;
}
else if (tokens[0][0] == '!') {
int index = atoi(tokens[0] + 1);
if (index > 0 && index <= history_count) {
add_to_history(history[index - 1]); // Add the command to history
tokens = splitcommand(history[index - 1]); // Split the command into tokens
} else {
printf("Invalid history index\n");
continue;
}
}
else{
add_to_history(line); // Add the command to history
}
if (fork() == 0) {
if (execvp(tokens[0], tokens) == -1) {
perror("The following error occurred: ");
}
exit(1);
} else {
wait(0);
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 60
#define MAX_HISTORY_SIZE 10
// History struct to store command history
char* history[MAX_HISTORY_SIZE];
int history_count = 0;
// Read a line from stdin
char *readcommand() {
char *line = (char*) malloc(sizeof(char) * MAX);
printf("$> ");
fgets(line, MAX, stdin);
size_t ln = strlen(line)-1;
if (line[ln] == '\n')
line[ln] = '\0';
return line;
}
// Split the line into tokens
char **splitcommand(char *line) {
char delim[] = " ";
char *ptr = strtok(line, delim);
char **tokens = (char**) malloc(sizeof(char*) * MAX);
int index = 0;
while(ptr != NULL) {
tokens[index] = ptr;
index++;
ptr = strtok(NULL, delim);
}
tokens[index] = NULL; // Set the last element to NULL for execvp
return tokens;
}
// Add command to history
void add_to_history(const char* command) {
if (history_count < MAX_HISTORY_SIZE) {
history[history_count] = strdup(command);
history_count++;
} else {
free(history[0]);
int i;
for (i = 0; i < MAX_HISTORY_SIZE - 1; i++) {
history[i] = history[i + 1];
}
history[MAX_HISTORY_SIZE - 1] = strdup(command);
}
}
// Write to file
void writeBye(const char *filename) {
FILE *fptr;
// Opening file in writing mode
fptr = fopen(filename, "w");
// Exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr, "%s", "Bye!\n");
fclose(fptr);
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Please insert exitfile as an argument!\n");
return 1;
}
while (1) {
char *line;
char **tokens;
line = readcommand();
char* line_copy = strdup(line);
tokens = splitcommand(line_copy);
if (strcmp(tokens[0], "exit") == 0) {
writeBye(argv[1]);
exit(0);
} else if (strcmp(tokens[0], "history") == 0) {
if (tokens[1]!=NULL && strcmp(tokens[1], "-del") == 0) {
int index = atoi(tokens[2]);
if (index > 0 && index <= history_count) {
free(history[index - 1]); // Free the memory allocated for the command
int i;
for (i = index - 1; i < history_count - 1; i++) {
history[i] = history[i + 1]; // Shift the remaining commands in the array
}
history_count--;
continue;
} else {
printf("Invalid history index\n");
continue;
}
}
else{
add_to_history(line_copy);
int i;
for (i = 0; i < history_count; i++) {
printf("%d: %s\n", i + 1, history[i]);
}
continue;
}
}
else if (tokens[0][0] == '!') {
int index = atoi(tokens[0] + 1);
if (index > 0 && index <= history_count) {
add_to_history(history[index - 1]); // Add the command to history
tokens = splitcommand(history[index - 1]); // Split the command into tokens
} else {
printf("Invalid history index\n");
continue;
}
}
else{
add_to_history(line); // Add the command to history
}
if (fork() == 0) {
if (execvp(tokens[0], tokens) == -1) {
perror("The following error occurred: ");
}
exit(1);
} else {
wait(0);
}
}
return 0;
}
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