Created
May 2, 2011 21:25
-
-
Save jatkins/952403 to your computer and use it in GitHub Desktop.
Fake Filesystem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef create | |
#define create | |
#define SIZE 255 | |
#define TRUE 1 | |
void newFile(char *filename, char *data){ | |
int i = 0; | |
printf("Enter the file name: "); | |
while(i < 254){ // User input | |
filename[i] = getchar(); | |
if(filename[i] == '\n'){ | |
filename[i]= 0; | |
i=0; | |
break; | |
} | |
i++; | |
} | |
i = 0; | |
printf("Type what you want to be in this file. ctrl+d to close:\n"); | |
while(i < 8999){ | |
data[i] = getchar(); | |
if(data[i] == EOF){ | |
data[i] = 0; | |
i = 0; | |
break; | |
} | |
i++; | |
} | |
printf("\n"); | |
} | |
void getFile(char *name){ | |
int i = 0; | |
printf("Enter the file name: "); | |
while(i < 254){ // User input | |
name[i] = getchar(); | |
if(name[i] == '\n'){ | |
name[i]= 0; | |
i=0; | |
break; | |
} | |
i++; | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef fs | |
#define fs | |
/******************************** | |
/ Filesystem implementation with | |
/ linked lists. | |
/ | |
/ Generic Linked list code found | |
/ at http://en.wikipedia.org/wiki/Linked_list#Language_support | |
********************************/ | |
typedef struct node { | |
char *data; | |
struct node *next; | |
} LLIST; | |
LLIST *list_add(LLIST **p, char *d); | |
void list_remove(LLIST **p); | |
LLIST **list_search(LLIST **n, char *d); | |
LLIST *list_load(LLIST *n, char *disk_name); | |
void list_save(LLIST *n, char *disk_name); | |
void list_dump(LLIST *n); | |
LLIST *list_add(LLIST **p, char *d){ | |
if(p == NULL) | |
return NULL; | |
LLIST *n = malloc(sizeof(LLIST)); | |
if(n == NULL) | |
return NULL; | |
n->next = *p; | |
*p = n; | |
n->data = malloc(sizeof(d)); | |
strcpy(n->data, d); | |
return *p; | |
} | |
void list_remove(LLIST **p){ | |
if (p != NULL && *p != NULL){ | |
LLIST *n = *p; | |
*p = (*p)->next; | |
free(n); | |
} | |
} | |
LLIST **list_search(LLIST **n, char *d){ | |
if (n == NULL) | |
return NULL; | |
while (*n != NULL){ | |
printf("hello"); | |
printf("%s\n", (*n)->data); | |
n = &(*n)->next; | |
} | |
return NULL; | |
} | |
LLIST* list_load(LLIST *list_n, char *disk_name){ | |
FILE *disk; // Pointer for file image | |
size_t count; // Holds the number of bytes read | |
char data[10000000]; // where the data is loaded to | |
char *tmp; // used for strtok | |
disk = fopen(disk_name, "r"); // open the disk | |
// If the disk isn't open let the user know | |
if(disk == NULL){ | |
printf("Failed to open the disk"); | |
return; | |
} | |
// Lets read the data | |
count = fread(data, 1, sizeof(data), disk); | |
fclose(disk); // close the disk | |
// start breaking the disk down | |
tmp = strtok(data, "~"); | |
while(tmp != NULL){ | |
char write[9258] = {0}; // used to hold the data as its rebuild | |
strcat(write, tmp); | |
strcat(write, "~"); // have to readd the '~' to the string | |
list_add(&list_n, write); | |
tmp = strtok(NULL, "~"); | |
} | |
return list_n; | |
} | |
void list_save(LLIST *n, char *disk_name){ | |
FILE *disk; | |
size_t count; | |
disk = fopen(disk_name, "w"); | |
if(disk == NULL){ | |
printf("Failed to open the disk"); | |
return; | |
} | |
while (n != NULL){ | |
count = fwrite(n->data, 1, strlen(n->data), disk); | |
n = n->next; | |
} | |
fclose(disk); | |
} | |
void list_dump(LLIST *n){ | |
if(n == NULL){ | |
printf("There are no files in the list\n"); | |
} | |
else{ | |
printf("%-10s: %-10s %-10s - %s\n", "Filename", "File_Size", "Total_Size", "Memory Location"); | |
} | |
while(n != NULL){ | |
char data_tmp[8258]=""; | |
char *filename; | |
size_t filesize=0; | |
strcpy(data_tmp, n->data); | |
filename = strtok(data_tmp, ":"); | |
filesize = strlen(n->data) - strlen(filename) - 2; | |
printf("%-10s: %-10u %-10u - %p \n", filename, filesize, strlen(n->data), n); | |
n = n->next; | |
} | |
} | |
void list_print(LLIST *n){ | |
if(n == NULL){ | |
printf("There are no files in the list\n"); | |
} | |
else { | |
printf("%-10s: %s\n", "Filename", "Filesize"); | |
} | |
while(n != NULL){ | |
char data_tmp[8258]=""; | |
char *filename; | |
size_t filesize=0; | |
strcpy(data_tmp, n->data); | |
filename = strtok(data_tmp, ":"); | |
filesize = strlen(n->data) - strlen(filename) - 2; | |
printf("%-10s: %u\n", filename, filesize); | |
n = n->next; | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************/ | |
/* Jacob Atkins */ | |
/* jta4j@mcs.uvawise.edu */ | |
/* */ | |
/* Project 2 - Simple Shell */ | |
/******************************/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include "fs.h" | |
#include "create.h" | |
#define TRUE 1 | |
#define SIZE 255 | |
#define DISK_SIZE 10000000 | |
#define DISK ".hd" | |
int main() { | |
LLIST* n = NULL; | |
char cli[SIZE]; // User input buffer | |
char *command[10]; // Command buffer | |
char *tmp; | |
int i; | |
n = list_load(n, DISK); | |
while(TRUE) { // Master loop | |
i = 0; // make sure that i is 0 for each loop | |
printf(":> "); | |
while(TRUE) { // User input loop | |
cli[i] = getchar(); | |
if(cli[i] == '\n') { | |
cli[i] = 0; | |
i=0; //reset i | |
break; | |
} | |
i++; | |
} | |
// Start breaking the char array down | |
tmp = strtok(cli, " "); | |
while(tmp != NULL) { | |
command[i] = tmp; | |
tmp = strtok(NULL, " "); | |
i++; | |
} | |
int j=1; // Create new counter var | |
char args[SIZE] = ""; // Create a place to hold the args | |
// Start building the args string | |
while(j<i){ | |
strcat(args, command[j]); | |
j++; | |
if(i>j) | |
strcat(args, " "); | |
} | |
if(strcmp(command[0], "help") == 0) { | |
printf("\nCommands - What they do:\n"); | |
printf("create - Starts the creater\n"); | |
printf("delete - Starts the deleter\n"); | |
printf("list - Lists all the files,"); | |
printf(" their memory address, filename, and size\n\n"); | |
} | |
if(strcmp(command[0], "create") == 0) { | |
char filename[255] = {0}; // Filenames can be upto 254 in length -1 for \0 | |
char data[9000] = {0}; // user can enter 7999 of data, -1 for \0 | |
char write[9258] = {0}; // used to store compound data | |
// Call the create function | |
newFile(filename, data); | |
// Start building the data | |
strcat(write, filename); | |
strcat(write, ":"); | |
strcat(write, data); | |
strcat(write, "~"); | |
list_add(&n, write); | |
} | |
if(strcmp(command[0], "list") == 0){ | |
list_print(n); | |
} | |
if(strcmp(command[0], "delete") == 0){ | |
char filename[255] = {0}; | |
getFile(filename); | |
list_search(n, filename); | |
} | |
if(strcmp(command[0], "defrag") == 0){ | |
printf("Defrag complete\n"); | |
} | |
if(strcmp(command[0], "dump") == 0){ | |
list_dump(n); | |
} | |
if(strcmp(command[0], "exit") == 0){ | |
list_save(n, DISK); | |
return 0; | |
} | |
int status; // Create waitpid's return var | |
// Fork the system | |
if(fork() != 0) { | |
waitpid(-1, &status, 0); | |
} | |
else { | |
if(execvp(command[0], command) != 0){ | |
exit(1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment