Skip to content

Commit 3541cad

Browse files
committed
first
0 parents  commit 3541cad

12 files changed

+1723
-0
lines changed

common.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef _COMMON_H_
2+
#define _COMMON_H_
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <memory.h>
7+
8+
#include "types.h"
9+
10+
#ifndef ZeroMemory
11+
#define ZeroMemory( a, b ) memset( a, 0, b )
12+
#endif
13+
14+
#define PRINTF printf
15+
#define SECTOR DWORD
16+
17+
#define STRINGIFY(x) #x
18+
#define TOSTRING(x) STRINGIFY(x)
19+
20+
#ifndef __FUNCTION__
21+
#define __LOCATION__ __FILE__ "(" TOSTRING(__LINE__) ") "
22+
#else
23+
#define __LOCATION__ __FILE__ "(" TOSTRING(__LINE__) ", " __FUNCTION__ ") "
24+
#endif
25+
26+
#define WARNING( ... ) PRINTF( __VA_ARGS__ )
27+
28+
#ifndef _DEBUG
29+
#define TRACE
30+
#else
31+
#define TRACE PRINTF
32+
#endif
33+
34+
#define STEP( a ) { PRINTF( "%s(%d): %s;\n", __FILE__, __LINE__, # a ); a; }
35+
36+
#define EXT2_ERROR -1
37+
#define EXT2_SUCCESS 0
38+
39+
#endif

disk.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _DISK_H_
2+
#define _DISK_H_
3+
4+
#include "common.h"
5+
6+
typedef struct DISK_OPERATIONS
7+
{
8+
int ( *read_sector )( struct DISK_OPERATIONS*, SECTOR, void* );
9+
int ( *write_sector )( struct DISK_OPERATIONS*, SECTOR, const void* );
10+
SECTOR numberOfSectors;
11+
int bytesPerSector;
12+
void* pdata;
13+
} DISK_OPERATIONS;
14+
15+
#endif
16+

disksim.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <stdlib.h>
2+
#include <memory.h>
3+
#include "ext2.h"
4+
#include "disk.h"
5+
#include "disksim.h"
6+
7+
typedef struct
8+
{
9+
char* address;
10+
} DISK_MEMORY;
11+
12+
int disksim_read(DISK_OPERATIONS* this, SECTOR sector, void* data);
13+
int disksim_write(DISK_OPERATIONS* this, SECTOR sector, const void* data);
14+
15+
int disksim_init(SECTOR numberOfSectors, unsigned int bytesPerSector, DISK_OPERATIONS* disk)
16+
{
17+
if (disk == NULL)
18+
return -1;
19+
20+
disk->pdata = malloc(sizeof(DISK_MEMORY));
21+
if (disk->pdata == NULL)
22+
{
23+
disksim_uninit(disk);
24+
return -1;
25+
}
26+
27+
((DISK_MEMORY*)disk->pdata)->address = (char*)malloc(bytesPerSector * numberOfSectors);
28+
if (disk->pdata == NULL)
29+
{
30+
disksim_uninit(disk);
31+
return -1;
32+
}
33+
34+
memset(((DISK_MEMORY*)disk->pdata)->address, 0, bytesPerSector * numberOfSectors);
35+
36+
disk->read_sector = disksim_read;
37+
disk->write_sector = disksim_write;
38+
disk->numberOfSectors = numberOfSectors;
39+
disk->bytesPerSector = bytesPerSector;
40+
return 0;
41+
}
42+
43+
void disksim_uninit(DISK_OPERATIONS* this)
44+
{
45+
if (this)
46+
{
47+
if (this->pdata)
48+
free(this->pdata);
49+
}
50+
}
51+
52+
int disksim_read(DISK_OPERATIONS* this, SECTOR sector, void* data)
53+
{
54+
char* disk = ((DISK_MEMORY*)this->pdata)->address;
55+
56+
if (sector < 0 || sector >= this->numberOfSectors)
57+
return -1;
58+
59+
memcpy(data, &disk[sector * this->bytesPerSector], this->bytesPerSector);
60+
61+
return 0;
62+
}
63+
64+
int disksim_write(DISK_OPERATIONS* this, SECTOR sector, const void* data)
65+
{
66+
char* disk = ((DISK_MEMORY*)this->pdata)->address;
67+
68+
if (sector < 0 || sector >= this->numberOfSectors)
69+
return -1;
70+
71+
memcpy(&disk[sector * this->bytesPerSector], data, this->bytesPerSector);
72+
73+
return 0;
74+
}
75+

disksim.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _DISKSIM_H_
2+
#define _DISKSIM_H_
3+
4+
#include "common.h"
5+
6+
int disksim_init( SECTOR, unsigned int, DISK_OPERATIONS* );
7+
void disksim_uninit( DISK_OPERATIONS* );
8+
9+
#endif

entrylist.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "common.h"
2+
#include "shell.h"
3+
4+
#ifndef NULL
5+
#define NULL ( ( void* )0 )
6+
#endif
7+
8+
int init_entry_list(SHELL_ENTRY_LIST* list)
9+
{
10+
memset(list, 0, sizeof(SHELL_ENTRY_LIST));
11+
return 0;
12+
}
13+
14+
int add_entry_list(SHELL_ENTRY_LIST* list, SHELL_ENTRY* entry)
15+
{
16+
SHELL_ENTRY_LIST_ITEM* newItem;
17+
18+
newItem = (SHELL_ENTRY_LIST_ITEM*)malloc(sizeof(SHELL_ENTRY_LIST_ITEM));
19+
newItem->entry = *entry;
20+
newItem->next = NULL;
21+
22+
if (list->count == 0)
23+
list->first = list->last = newItem;
24+
else
25+
{
26+
list->last->next = newItem;
27+
list->last = newItem;
28+
}
29+
30+
list->count++;
31+
32+
return 0;
33+
}
34+
35+
void release_entry_list(SHELL_ENTRY_LIST* list)
36+
{
37+
SHELL_ENTRY_LIST_ITEM* currentItem;
38+
SHELL_ENTRY_LIST_ITEM* nextItem;
39+
40+
if (list->count == 0)
41+
return;
42+
43+
nextItem = list->first;
44+
45+
do
46+
{
47+
currentItem = nextItem;
48+
nextItem = currentItem->next;
49+
free(currentItem);
50+
} while (nextItem);
51+
52+
list->count = 0;
53+
list->first = NULL;
54+
list->last = NULL;
55+
}
56+

0 commit comments

Comments
 (0)