Skip to content

Commit 02156cb

Browse files
committed
Initial commit of block device interface and emulated block device
1 parent b113bba commit 02156cb

File tree

7 files changed

+471
-43
lines changed

7 files changed

+471
-43
lines changed

Makefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
TARGET = lfs
2+
3+
CC = gcc
4+
AR = ar
5+
SIZE = size
6+
7+
SRC += $(wildcard *.c emubd/*.c)
8+
OBJ := $(SRC:.c=.o)
9+
DEP := $(SRC:.c=.d)
10+
ASM := $(SRC:.c=.s)
11+
12+
ifdef DEBUG
13+
CFLAGS += -O0 -g3
14+
else
15+
CFLAGS += -O2
16+
endif
17+
ifdef WORD
18+
CFLAGS += -m$(WORD)
19+
endif
20+
CFLAGS += -I.
21+
CFLAGS += -std=c99 -Wall -pedantic
22+
23+
24+
all: $(TARGET)
25+
26+
asm: $(ASM)
27+
28+
size: $(OBJ)
29+
$(SIZE) -t $^
30+
31+
-include $(DEP)
32+
33+
$(TARGET): $(OBJ)
34+
$(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
35+
36+
%.a: $(OBJ)
37+
$(AR) rcs $@ $^
38+
39+
%.o: %.c
40+
$(CC) -c -MMD $(CFLAGS) $< -o $@
41+
42+
%.s: %.c
43+
$(CC) -S $(CFLAGS) $< -o $@
44+
45+
clean:
46+
rm -f $(TARGET)
47+
rm -f $(OBJ)
48+
rm -f $(DEP)
49+
rm -f $(ASM)

emu/cfg.c renamed to emubd/lfs_cfg.c

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
#include "cfg.h"
1+
/*
2+
* Simple config parser
3+
*
4+
* Copyright (c) 2017 Christopher Haster
5+
* Distributed under the MIT license
6+
*/
7+
#include "emubd/lfs_cfg.h"
28

39
#include <stdlib.h>
410
#include <errno.h>
511
#include <string.h>
612
#include <stdio.h>
713

814

9-
static int cfg_buffer(cfg_t *cfg, char c) {
15+
static int lfs_cfg_buffer(lfs_cfg_t *cfg, char c) {
1016
// Amortize double
1117
if (cfg->blen == cfg->bsize) {
1218
size_t nsize = cfg->bsize * 2;
@@ -26,16 +32,16 @@ static int cfg_buffer(cfg_t *cfg, char c) {
2632
return 0;
2733
}
2834

29-
static int cfg_attr(cfg_t *cfg, unsigned key, unsigned val) {
35+
static int lfs_cfg_attr(lfs_cfg_t *cfg, unsigned key, unsigned val) {
3036
// Amortize double
3137
if (cfg->len == cfg->size) {
3238
size_t nsize = cfg->size * 2;
33-
struct cfg_attr *nattrs = malloc(nsize*sizeof(struct cfg_attr));
39+
struct lfs_cfg_attr *nattrs = malloc(nsize*sizeof(struct lfs_cfg_attr));
3440
if (!nattrs) {
3541
return -ENOMEM;
3642
}
3743

38-
memcpy(nattrs, cfg->attrs, cfg->size*sizeof(struct cfg_attr));
44+
memcpy(nattrs, cfg->attrs, cfg->size*sizeof(struct lfs_cfg_attr));
3945
free(cfg->attrs);
4046
cfg->attrs = nattrs;
4147
cfg->size = nsize;
@@ -50,14 +56,14 @@ static int cfg_attr(cfg_t *cfg, unsigned key, unsigned val) {
5056
}
5157

5258
memmove(&cfg->attrs[i+1], &cfg->attrs[i],
53-
(cfg->size - i)*sizeof(struct cfg_attr));
59+
(cfg->size - i)*sizeof(struct lfs_cfg_attr));
5460
cfg->attrs[i].key = key;
5561
cfg->attrs[i].val = val;
5662
cfg->len += 1;
5763
return 0;
5864
}
5965

60-
static bool cfg_match(FILE *f, const char *matches) {
66+
static bool lfs_cfg_match(FILE *f, const char *matches) {
6167
char c = getc(f);
6268
ungetc(c, f);
6369

@@ -70,11 +76,11 @@ static bool cfg_match(FILE *f, const char *matches) {
7076
return false;
7177
}
7278

73-
int cfg_create(cfg_t *cfg, const char *filename) {
79+
int lfs_cfg_create(lfs_cfg_t *cfg, const char *filename) {
7480
// start with some initial space
7581
cfg->len = 0;
7682
cfg->size = 4;
77-
cfg->attrs = malloc(cfg->size*sizeof(struct cfg_attr));
83+
cfg->attrs = malloc(cfg->size*sizeof(struct lfs_cfg_attr));
7884

7985
cfg->blen = 0;
8086
cfg->bsize = 16;
@@ -88,50 +94,50 @@ int cfg_create(cfg_t *cfg, const char *filename) {
8894
while (!feof(f)) {
8995
int err;
9096

91-
while (cfg_match(f, " \t\v\f")) {
97+
while (lfs_cfg_match(f, " \t\v\f")) {
9298
fgetc(f);
9399
}
94100

95-
if (!cfg_match(f, "#\r\n")) {
101+
if (!lfs_cfg_match(f, "#\r\n")) {
96102
unsigned key = cfg->blen;
97-
while (!cfg_match(f, " \t\v\f:#") && !feof(f)) {
98-
if ((err = cfg_buffer(cfg, fgetc(f)))) {
103+
while (!lfs_cfg_match(f, " \t\v\f:#") && !feof(f)) {
104+
if ((err = lfs_cfg_buffer(cfg, fgetc(f)))) {
99105
return err;
100106
}
101107
}
102-
if ((err = cfg_buffer(cfg, 0))) {
108+
if ((err = lfs_cfg_buffer(cfg, 0))) {
103109
return err;
104110
}
105111

106-
while (cfg_match(f, " \t\v\f")) {
112+
while (lfs_cfg_match(f, " \t\v\f")) {
107113
fgetc(f);
108114
}
109115

110-
if (cfg_match(f, ":")) {
116+
if (lfs_cfg_match(f, ":")) {
111117
fgetc(f);
112-
while (cfg_match(f, " \t\v\f")) {
118+
while (lfs_cfg_match(f, " \t\v\f")) {
113119
fgetc(f);
114120
}
115121

116122
unsigned val = cfg->blen;
117-
while (!cfg_match(f, " \t\v\f#\r\n") && !feof(f)) {
118-
if ((err = cfg_buffer(cfg, fgetc(f)))) {
123+
while (!lfs_cfg_match(f, " \t\v\f#\r\n") && !feof(f)) {
124+
if ((err = lfs_cfg_buffer(cfg, fgetc(f)))) {
119125
return err;
120126
}
121127
}
122-
if ((err = cfg_buffer(cfg, 0))) {
128+
if ((err = lfs_cfg_buffer(cfg, 0))) {
123129
return err;
124130
}
125131

126-
if ((err = cfg_attr(cfg, key, val))) {
132+
if ((err = lfs_cfg_attr(cfg, key, val))) {
127133
return err;
128134
}
129135
} else {
130136
cfg->blen = key;
131137
}
132138
}
133139

134-
while (!cfg_match(f, "\r\n") && !feof(f)) {
140+
while (!lfs_cfg_match(f, "\r\n") && !feof(f)) {
135141
fgetc(f);
136142
}
137143
fgetc(f);
@@ -140,15 +146,15 @@ int cfg_create(cfg_t *cfg, const char *filename) {
140146
return 0;
141147
}
142148

143-
void cfg_destroy(cfg_t *cfg) {
149+
void lfs_cfg_destroy(lfs_cfg_t *cfg) {
144150
free(cfg->attrs);
145151
}
146152

147-
bool cfg_has(cfg_t *cfg, const char *key) {
148-
return cfg_get(cfg, key, 0);
153+
bool lfs_cfg_has(lfs_cfg_t *cfg, const char *key) {
154+
return lfs_cfg_get(cfg, key, 0);
149155
}
150156

151-
const char *cfg_get(cfg_t *cfg, const char *key, const char *def) {
157+
const char *lfs_cfg_get(lfs_cfg_t *cfg, const char *key, const char *def) {
152158
// binary search for attribute
153159
int lo = 0;
154160
int hi = cfg->len-1;
@@ -168,8 +174,8 @@ const char *cfg_get(cfg_t *cfg, const char *key, const char *def) {
168174
return def;
169175
}
170176

171-
ssize_t cfg_geti(cfg_t *cfg, const char *key, ssize_t def) {
172-
const char *val = cfg_get(cfg, key, 0);
177+
ssize_t lfs_cfg_geti(lfs_cfg_t *cfg, const char *key, ssize_t def) {
178+
const char *val = lfs_cfg_get(cfg, key, 0);
173179
if (!val) {
174180
return def;
175181
}
@@ -179,8 +185,8 @@ ssize_t cfg_geti(cfg_t *cfg, const char *key, ssize_t def) {
179185
return (end == val) ? def : res;
180186
}
181187

182-
size_t cfg_getu(cfg_t *cfg, const char *key, size_t def) {
183-
const char *val = cfg_get(cfg, key, 0);
188+
size_t lfs_cfg_getu(lfs_cfg_t *cfg, const char *key, size_t def) {
189+
const char *val = lfs_cfg_get(cfg, key, 0);
184190
if (!val) {
185191
return def;
186192
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* Simple config parser
33
*
4-
* Copyright (c) 2016 Christopher Haster
4+
* Copyright (c) 2017 Christopher Haster
55
* Distributed under the MIT license
66
*/
7-
#ifndef CFG_H
8-
#define CFG_H
7+
#ifndef LFS_CFG_H
8+
#define LFS_CFG_H
99

1010
#include <stddef.h>
1111
#include <stdbool.h>
@@ -26,49 +26,49 @@
2626
// huh: yeah_that's_basically_it # basically it
2727

2828
// Internal config structure
29-
typedef struct cfg {
29+
typedef struct lfs_cfg {
3030
size_t len;
3131
size_t size;
3232

3333
size_t blen;
3434
size_t bsize;
3535
char *buf;
3636

37-
struct cfg_attr {
37+
struct lfs_cfg_attr {
3838
unsigned key;
3939
unsigned val;
4040
} *attrs;
41-
} cfg_t;
41+
} lfs_cfg_t;
4242

4343

4444

4545
// Creates a cfg object and reads in the cfg file from the filename
4646
//
47-
// If the cfg_read fails, returns a negative value from the underlying
47+
// If the lfs_cfg_read fails, returns a negative value from the underlying
4848
// stdio functions
49-
int cfg_create(cfg_t *cfg, const char *filename);
49+
int lfs_cfg_create(lfs_cfg_t *cfg, const char *filename);
5050

5151
// Destroys the cfg object and frees any used memory
52-
void cfg_destroy(cfg_t *cfg);
52+
void lfs_cfg_destroy(lfs_cfg_t *cfg);
5353

5454
// Checks if a cfg attribute exists
55-
bool cfg_has(cfg_t *cfg, const char *key);
55+
bool lfs_cfg_has(lfs_cfg_t *cfg, const char *key);
5656

5757
// Retrieves a cfg attribute as a null-terminated string
5858
//
5959
// If the attribute does not exist, returns the string passed as def
60-
const char *cfg_get(cfg_t *cfg, const char *key, const char *def);
60+
const char *lfs_cfg_get(lfs_cfg_t *cfg, const char *key, const char *def);
6161

6262
// Retrieves a cfg attribute parsed as an int
6363
//
6464
// If the attribute does not exist or can't be parsed, returns the
6565
// integer passed as def
66-
ssize_t cfg_geti(cfg_t *cfg, const char *name, ssize_t def);
66+
ssize_t lfs_cfg_geti(lfs_cfg_t *cfg, const char *name, ssize_t def);
6767

6868
// Retrieves a cfg attribute parsed as an unsigned int
6969
//
7070
// If the attribute does not exist or can't be parsed, returns the
7171
// integer passed as def
72-
size_t cfg_getu(cfg_t *cfg, const char *name, size_t def);
72+
size_t lfs_cfg_getu(lfs_cfg_t *cfg, const char *name, size_t def);
7373

7474
#endif

0 commit comments

Comments
 (0)