Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
kahleeeb3 committed Apr 9, 2023
1 parent fc7066c commit 5d8fc5e
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cpmRun: diskSimulator.o cpmfsys.o fsysdriver.o
gcc -o cpmRun diskSimulator.o cpmfsys.o fsysdriver.o

diskSimulator.o: diskSimulator.c diskSimulator.h
gcc -c diskSimulator.c

cpmfsys.o: cpmfsys.h cpmfsys.c
gcc -c cpmfsys.c

fsysdriver.o: fsysdriver.c
gcc -c fsysdriver.c

all:
cpmRun

clean:
rm *.o

53 changes: 53 additions & 0 deletions src/cpmfsys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "cpmfsys.h"
#include "diskSimulator.h"

bool freeList[NUM_BLOCKS];


DirStructType *mkDirStruct(int index,uint8_t *e) {
/* Add your code here */

return d;
}

void writeDirStruct(DirStructType *d,uint8_t index, uint8_t *e) {
/* Add your code here */

}

void makeFreeList() {
/* Add your code here */


}

void printFreeList() {
/* Add your code here */

}

// print all directory entries, just the names and sizes
void cpmDir() {
/* Add your code here */
}

bool checkLegalName(char *name) {
/* Add your code here */


}

int findExtentWithName(char *name, uint8_t *block0) {
/* Add your code here */

return -1; // file just not found in directory block
}

int cpmDelete(char *fileName) {
/* Add your code here */

}

int cpmRename(char *oldName, char *newName) {
/* Add your code here */
}
103 changes: 103 additions & 0 deletions src/cpmfsys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <stdint.h>
#include <stdlib.h>
#include "diskSimulator.h"
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

#define EXTENT_SIZE 32
#define BLOCKS_PER_EXTENT 16
#define debugging false

typedef struct dirStruct {
uint8_t status; // 0xe5 = unused, 0-16 = user number
char name[9]; // no need to support attributes in msb of bytes 0,1,2,7
char extension[4]; // no need to support attributes in msb of bytes 0,1,2
uint8_t XL; // see below for these 4 bytes' meaning
uint8_t BC;
uint8_t XH;
uint8_t RC;
uint8_t blocks[BLOCKS_PER_EXTENT]; // array of disk sectors used
} DirStructType;


/* XL and XH store the extent number, bits 5-7 of XL are zero, bits 0-4 are
low-order bits of extent number, bits 6-7 of XH are zero, bits 0-5 hold high
-order bits of extent number
BC = number of bytes in last used physical sector
RC = number of 128 byte sectors in last used block
blocks are logical block numbers 0-255, 0 indicates a hole in file,
*/

typedef uint8_t Extent[32];

//function to allocate memory for a DirStructType (see above), and populate it, given a
//pointer to a buffer of memory holding the contents of disk block 0 (e), and an integer index
// which tells which extent from block zero (extent numbers start with 0) to use to make the
// DirStructType value to return.
DirStructType *mkDirStruct(int index,uint8_t *e);

// function to write contents of a DirStructType struct back to the specified index of the extent
// in block of memory (disk block 0) pointed to by e
void writeDirStruct(DirStructType *d, uint8_t index, uint8_t *e);

// populate the FreeList global data structure. freeList[i] == true means
// that block i of the disk is free. block zero is never free, since it holds
// the directory. freeList[i] == false means the block is in use.
void makeFreeList();
// debugging function, print out the contents of the free list in 16 rows of 16, with each
// row prefixed by the 2-digit hex address of the first block in that row. Denote a used
// block with a *, a free block with a .
void printFreeList();


// internal function, returns -1 for illegal name or name not found
// otherwise returns extent nunber 0-31
int findExtentWithName(char *name, uint8_t *block0);

// internal function, returns true for legal name (8.3 format), false for illegal
// (name or extension too long, name blank, or illegal characters in name or extension)
bool checkLegalName(char *name);


// print the file directory to stdout. Each filename should be printed on its own line,
// with the file size, in base 10, following the name and extension, with one space between
// the extension and the size. If a file does not have an extension it is acceptable to print
// the dot anyway, e.g. "myfile. 234" would indicate a file whose name was myfile, with no
// extension and a size of 234 bytes. This function returns no error codes, since it should
// never fail unless something is seriously wrong with the disk
void cpmDir();

// error codes for next five functions (not all errors apply to all 5 functions)
/*
0 -- normal completion
-1 -- source file not found
-2 -- invalid filename
-3 -- dest filename already exists
-4 -- insufficient disk space
*/

//read directory block,
// modify the extent for file named oldName with newName, and write to the disk
int cpmRename(char *oldName, char * newName);

// delete the file named name, and free its disk blocks in the free list
int cpmDelete(char * name);

// following functions need not be implemented for Project 4

int cpmCopy(char *oldName, char *newName);


int cpmOpen( char *fileName, char mode);

// non-zero return indicates filePointer did not point to open file
int cpmClose(int filePointer);

// returns number of bytes read, 0 = error
int cpmRead(int pointer, uint8_t *buffer, int size);

// returns number of bytes written, 0 = error
int cpmWrite(int pointer, uint8_t *buffer, int size);
59 changes: 59 additions & 0 deletions src/diskSimulator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdint.h>
#include <stdio.h>
#include "diskSimulator.h"
// first index is sector number, second index is byte in the block of bytes
static uint8_t disk[256][1024];


int blockRead(uint8_t *buffer,uint8_t blockNum) {
int i = 0;
for (i=0; i<1024;i++) {
*(buffer+i) = disk[(int) blockNum][(int) i];
}
return 0;
}

int blockWrite(uint8_t *buffer,uint8_t blockNum) {
int i = 0;
for (i=0; i<1024;i++) {
disk[(int) blockNum][i] = *(buffer+i);
}
return 0;
}

// for debugging purposes
void printBlock(uint8_t blockNum) {
int i;
fprintf(stdout,"\nDISK BLOCK %x:\n",blockNum);
for (i = 0; i < BLOCK_SIZE; i++) {
if (i % 16 == 0) {
fprintf(stdout,"%4x: ",i);
}
fprintf(stdout, "%2x ",disk[(int) blockNum][i]);
if (i % 16 == 15) {
fprintf(stdout,"\n");
}
}
fprintf(stdout,"\n");
}

// read and write whole disk image, byte for byte from a Unix file
size_t writeImage(char *fileName) {
FILE *fp;
size_t bytesWritten = -1;
fp = fopen(fileName,"w");
bytesWritten = fwrite(disk,BLOCK_SIZE,NUM_BLOCKS,fp);
fclose(fp);
return bytesWritten;
}

size_t readImage(char *fileName) {
FILE *fp;
size_t bytesRead = -1;
fp = fopen(fileName,"r");
// C uses row major order for multi dim arrays
bytesRead = fread(disk,BLOCK_SIZE,NUM_BLOCKS,fp);
fclose(fp);
return bytesRead;

}
15 changes: 15 additions & 0 deletions src/diskSimulator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdint.h>
#include <stddef.h>

// prototypes for disk simulator functions
#define BLOCK_SIZE 1024
#define NUM_BLOCKS 256
int blockRead(uint8_t *buffer,uint8_t blockNum);
int blockWrite(uint8_t *buffer,uint8_t blockNum);
void printBlock(uint8_t blocknum);
size_t writeImage(char *fileName);
size_t readImage(char *fileName);
// error codes
// 0 = correct operation
// 1 = error

37 changes: 37 additions & 0 deletions src/fsysdriver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdint.h>
#include "cpmfsys.h"
#include "diskSimulator.h"

// for debugging, prints a region of memory starting at buffer with
void printBuffer(uint8_t buffer[],int size) {
int i;
fprintf(stdout,"\nBUFFER PRINT:\n");
for (i = 0; i < size; i++) {
if (i % 16 == 0) {
fprintf(stdout,"%4x: ",i);
}
fprintf(stdout, "%2x ",buffer[i]);
if (i % 16 == 15) {
fprintf(stdout,"\n");
}
}
fprintf(stdout,"\n");
}


int main(int argc, char * argv[]) {
uint8_t buffer1[BLOCK_SIZE],buffer2[BLOCK_SIZE];
int i;
readImage("image1.img");
makeFreeList();
cpmDir();
printFreeList();
cpmDelete("shortf.ps");
cpmDir();
cpmRename("mytestf1.txt","mytest2.tx");
fprintf(stdout,"cpmRename return code = %d,\n",cpmRename("mytestf","mytestv2.x"));
cpmDir();
printFreeList();
}

Binary file added src/image1.img
Binary file not shown.
48 changes: 48 additions & 0 deletions src/sampleOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
DIRECTORY LISTING
mytestf1.txt 15874
holefile.txt 1152
shortf.ps 1032
mytestf. 1026
FREE BLOCK LIST: (* means in-use)
0: * . * . * . * . * . * . * . * .
10: * . . . . . . . . . . . . . . .
20: * . . . * . . . . . . . . . . .
30: * * . . . . . . . . . . . . . .
40: . . . . . . . . . . . . . . . .
50: . . . . . . . . . . . . . . . .
60: . . . . . . . . . . . . . . . .
70: . . . . . . . . . . . . . . . .
80: . . . . . . . . . . . . . . . .
90: . . . . . . . . . . . . . . . .
a0: * . . . * . . . . . . . . . . .
b0: . . . . . . . . . . . . . . . .
c0: . . . . . . . . . . . . . . . .
d0: . . . . . . . . . . . . . . . .
e0: . . . . . . . . . . . . . . . .
f0: . * . * . * . * . * . * . * . *
DIRECTORY LISTING
mytestf1.txt 15874
holefile.txt 1152
mytestf. 1026
cpmRename return code = 0,
DIRECTORY LISTING
mytest2.tx 15874
holefile.txt 1152
mytestv2.x 1026
FREE BLOCK LIST: (* means in-use)
0: * . * . * . * . * . * . * . * .
10: * . . . . . . . . . . . . . . .
20: * . . . * . . . . . . . . . . .
30: . . . . . . . . . . . . . . . .
40: . . . . . . . . . . . . . . . .
50: . . . . . . . . . . . . . . . .
60: . . . . . . . . . . . . . . . .
70: . . . . . . . . . . . . . . . .
80: . . . . . . . . . . . . . . . .
90: . . . . . . . . . . . . . . . .
a0: * . . . * . . . . . . . . . . .
b0: . . . . . . . . . . . . . . . .
c0: . . . . . . . . . . . . . . . .
d0: . . . . . . . . . . . . . . . .
e0: . . . . . . . . . . . . . . . .
f0: . * . * . * . * . * . * . * . *

0 comments on commit 5d8fc5e

Please sign in to comment.