Skip to content

Commit

Permalink
Merge pull request #36 from earlephilhower/readline
Browse files Browse the repository at this point in the history
Fix Windows build with custom readline
  • Loading branch information
earlephilhower authored Sep 23, 2023
2 parents 5190e97 + d536022 commit 0826fc4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ override CPPFLAGS := \
-D LFS_NAME_MAX=32 \
$(CPPFLAGS)

override CFLAGS := -std=gnu99 -Os -Wall $(TARGET_CFLAGS) $(CFLAGS)
override CXXFLAGS := -std=gnu++11 -Os -Wall $(TARGET_CXXFLAGS) $(CXXFLAGS)
override CFLAGS := -std=gnu99 -Os -Wall -Wextra -Werror $(TARGET_CFLAGS) $(CFLAGS)
override CXXFLAGS := -std=gnu++11 -Os -Wall -Wextra -Werror $(TARGET_CXXFLAGS) $(CXXFLAGS)
override LDFLAGS := $(TARGET_LDFLAGS) $(LDFLAGS)

DIST_NAME := mklittlefs-$(VERSION)$(BUILD_CONFIG_NAME)-$(TARGET_OS)
Expand Down
37 changes: 33 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,46 @@ int addFile(char* name, const char* path) {
return 0;
}

// Getline doesn't exist in WIN32/64, so replace with our own simplified version
ssize_t readline(char **line, FILE *f) {
// Clear out any old ptr
if (*line) {
free(*line);
*line = nullptr;
}
int lineSize = 0; // Will be extended on first byte
int cnt = 0;
int c;
while ((c = fgetc(f)) != EOF) {
cnt++;
if (cnt >= lineSize - 1) {
// Got too big, extend
lineSize += 128;
*line = (char*)realloc(*line, lineSize);
if (!*line) {
return -1; // OOM
}
}
(*line)[cnt - 1] = c;
(*line)[cnt] = 0;
if (c == '\n') {
return cnt;
}
}
return -1;
}

int addFilesFromFile(std::string const& dirname, std::string const& fromFile) {
FILE* listing = fopen(fromFile.c_str(), "rb");
FILE* listing = fopen(fromFile.c_str(), "r");
if (!listing) {
std::cerr << "error: failed to open " << fromFile << " for reading" << std::endl;
return 1;
}

char* srcpath = NULL;
size_t len = 0;
char *srcpath = nullptr;
bool error = false;
ssize_t linelen;
while ((linelen = getline(&srcpath, &len, listing)) != -1) {
while ((linelen = readline(&srcpath, listing)) != -1) {
if (!linelen) continue;
if (srcpath[linelen - 1] == '\n') {
if (linelen - 1 > 0 && srcpath[linelen - 2] == '\r') {
Expand Down Expand Up @@ -757,6 +785,7 @@ class CustomOutput : public TCLAP::StdOutput
public:
virtual void version(TCLAP::CmdLineInterface& c)
{
(void) c;
std::cout << "mklittlefs ver. " VERSION << std::endl;
const char* configName = BUILD_CONFIG_NAME;
if (configName[0] == '-') {
Expand Down

0 comments on commit 0826fc4

Please sign in to comment.