Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "exec.h"
#include "listnode.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#define MAX_HISTORY 100
#define MAX_INPUT_SIZE 10000
#define PROMPT "262$"
#define MAX_ARGS 127
// Helper function to free ListNodes
void free_list_nodes(ListNode *head) {
while (head != NULL) {
ListNode *temp = head;
head = head->next;
free(temp->command);
for (int i = 0; i < temp->arguments_length; i++) {
free(temp->arguments[i]);
}
free(temp->arguments);
free(temp->file_contents);
free(temp);
}
}
// Helper function to add a command to history
void add_to_history(char **history, int *history_size, const char *command) {
if (strlen(command) == 0) {
return;
}
char *full_command = strdup(command);
if (*history_size == MAX_HISTORY) {
free(history[0]);
for (int i = 1; i < *history_size; i++) {
history[i - 1] = history[i];
}
(*history_size)--;
}
history[*history_size] = full_command;
(*history_size)++;
}
// Helper function to handle history commands
void handle_history(char **history, int *history_size, char **args, int argc) {
if (argc == 2 && strcmp(args[1], "-c") == 0) {
for (int i = 0; i < *history_size; i++) {
free(history[i]);
}
*history_size = 0;
} else if (argc == 1) {
for (int i = 0; i < *history_size; i++) {
printf("%d: %sn", i, history[i]);
}
} else {
fprintf(stderr, "error: invalid history usagen");
}
}
// Handle "list" command
void handle_list(ListNode *head) {
ListNode *current = head;
while (current) {
printf("List Node %dn", current->id);
printf("tCommand: %sn", current->command);
printf("tFile Contents:n");
if (current->file_contents) {
char *content_copy = strdup(current->file_contents);
if (content_copy) {
char *line = strtok(content_copy, "n");
while (line) {
printf("tt%sn", line);
line = strtok(NULL, "n");
}
free(content_copy);
}
}
current = current->next;
}
}
// Handle "execute" command
void handle_execute(ListNode *head, char **args, int argc) {
if (argc != 2) {
fprintf(stderr, "error: incorrect number of argumentsn");
return;
}
int id = atoi(args[1]);
ListNode *current = head;
while (current && current->id != id) {
current = current->next;
}
if (!current) {
fprintf(stderr, "error: Id does not existn");
return;
}
if (current->arguments_length > MAX_ARGS) {
fprintf(stderr, "error: too many argumentsn");
return;
}
int status = run_command(current);
int exit_status = (status >> 8) & 0xFF;
if (exit_status != 0) {
fprintf(stderr, "error: %sn", strerror(exit_status));
}
}
// Handle "cd" command
void handle_cd(char **args, int argc) {
if (argc > 2) {
fprintf(stderr, "error: too many arguments providedn");
return;
}
if (argc == 1) {
if (chdir(getenv("HOME")) != 0) {
fprintf(stderr, "error: %sn", strerror(errno));
}
} else {
if (chdir(args[1]) != 0) {
fprintf(stderr, "error: %sn", strerror(errno));
}
}
}
// Handle "open" command
void handle_open(ListNode *head, char **args, int argc) {
if (argc != 3) {
fprintf(stderr, "error: incorrect number of argumentsn");
return;
}
int id = atoi(args[1]);
ListNode *current = head;
while (current && current->id != id) {
current = current->next;
}
if (!current) {
fprintf(stderr, "error: Id does not existn");
return;
}
FILE *file = fopen(args[2], "r");
if (!file) {
fprintf(stderr, "error: file cannot be openedn");
return;
}
free(current->file_contents);
current->file_contents = NULL;
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
current->file_contents = malloc(file_size + 1);
if (!current->file_contents) {
fclose(file);
fprintf(stderr, "error: memory allocation failedn");
return;
}
size_t bytes_read = fread(current->file_contents, 1, file_size, file);
current->file_contents[bytes_read] = ' ';
fclose(file);
}
// Function to handle the "new" command
void handle_new(ListNode **head, char **args, int argc) {
if (argc < 2) {
fprintf(stderr, "error: too few arguments providedn");
return;
}
ListNode *node = malloc(sizeof(ListNode));
if (!node) {
fprintf(stderr, "error: memory allocation failedn");
return;
}
node->command = strdup(args[1]);
node->arguments = malloc(sizeof(char *) * argc);
if (!node->arguments) {
free(node->command);
free(node);
fprintf(stderr, "error: memory allocation failedn");
return;
}
for (int i = 1; i < argc; i++) {
node->arguments[i - 1] = strdup(args[i]);
}
node->arguments[argc - 1] = NULL;
node->arguments_length = argc - 1;
node->file_contents = NULL;
node->next = NULL;
if (*head == NULL) {
node->id = 0;
*head = node;
} else {
ListNode *current = *head;
while (current->next != NULL) {
current = current->next;
}
node->id = current->id + 1;
current->next = node;
}
}
// Function to handle the "quit" command
void handle_quit(ListNode **head, char **history, int history_size) {
free_list_nodes(*head);
for (int i = 0; i < history_size; i++) {
free(history[i]);
}
exit(0);
}
// Main function
int main() {
char input[MAX_INPUT_SIZE];
char *history[MAX_HISTORY];
int history_size = 0;
ListNode *head = NULL;
while (1) {
printf("%s", PROMPT);
fflush(stdout);
if (!fgets(input, MAX_INPUT_SIZE, stdin)) {
handle_quit(&head, history, history_size);
}
// Remove newline
input[strcspn(input, "n")] = ' ';
// Skip empty input but still show prompt
if (strlen(input) == 0) {
continue;
}
char *args[MAX_INPUT_SIZE / 2];
int argc = 0;
char *token = strtok(input, " ");
while (token) {
args[argc++] = token;
token = strtok(NULL, " ");
}
args[argc] = NULL;
// Only add non-empty commands to history
if (argc > 0) {
add_to_history(history, &history_size, input);
}
if (argc == 0) {
continue;
} else if (strcmp(args[0], "new") == 0) {
handle_new(&head, args, argc);
} else if (strcmp(args[0], "list") == 0) {
handle_list(head);
} else if (strcmp(args[0], "execute") == 0) {
handle_execute(head, args, argc);
} else if (strcmp(args[0], "history") == 0) {
handle_history(history, &history_size, args, argc);
} else if (strcmp(args[0], "quit") == 0) {
handle_quit(&head, history, history_size);
} else if (strcmp(args[0], "cd") == 0) {
handle_cd(args, argc);
} else if (strcmp(args[0], "open") == 0) {
handle_open(head, args, argc);
} else {
fprintf(stderr, "error: unknown command: %sn", args[0]);
}
}
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team