From 0ffe156bcd6e95d7e76ca1946645b2d04fa9031a Mon Sep 17 00:00:00 2001 From: Gregory Gauthier Date: Fri, 30 Jan 2026 12:06:07 +0000 Subject: [PATCH] restructure as a proper C90 project; add makefiles --- .gitignore | 2 +- BUILD.BAT | 32 +++++++++++++++++++++++++ MAKEFILE.TC | 40 ++++++++++++++++++++++++++++++++ Makefile | 39 +++++++++++++++++++++++++++++++ platform.h => include/platform.h | 0 addnote.c => src/addnote.c | 9 ++++--- dumpnotes.c => src/dumpnotes.c | 28 +++++++++++++++------- 7 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 BUILD.BAT create mode 100644 MAKEFILE.TC create mode 100644 Makefile rename platform.h => include/platform.h (100%) rename addnote.c => src/addnote.c (94%) rename dumpnotes.c => src/dumpnotes.c (91%) diff --git a/.gitignore b/.gitignore index 7f9cada..1b3e6c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .claude/ .grok/ .idea/ -cmake-build-debug/ +build/ diff --git a/BUILD.BAT b/BUILD.BAT new file mode 100644 index 0000000..177be0a --- /dev/null +++ b/BUILD.BAT @@ -0,0 +1,32 @@ +@ECHO OFF +REM Build script for cnotes - Turbo C++ 3.0 on DOS/Win32 +REM Run this if you don't have MAKE available +REM +REM Assumes TCC.EXE is in your PATH + +ECHO Building cnotes with Turbo C++ 3.0... +ECHO. + +REM Create build directory if it doesn't exist +IF NOT EXIST build MKDIR build + +ECHO Compiling addnote.exe... +TCC -A -w -ml -I.\include -ebuild\addnote.exe src\addnote.c +IF ERRORLEVEL 1 GOTO ERROR + +ECHO Compiling dumpnotes.exe... +TCC -A -w -ml -I.\include -ebuild\dumpnotes.exe src\dumpnotes.c +IF ERRORLEVEL 1 GOTO ERROR + +ECHO. +ECHO Build successful! +ECHO. +DIR build\*.EXE +GOTO END + +:ERROR +ECHO. +ECHO Build failed! +GOTO END + +:END diff --git a/MAKEFILE.TC b/MAKEFILE.TC new file mode 100644 index 0000000..d9ca50c --- /dev/null +++ b/MAKEFILE.TC @@ -0,0 +1,40 @@ +# Makefile for cnotes - Turbo C++ 3.0 on DOS/Win32 +# Strict C89/ANSI compliant build +# +# Usage from DOS/Windows command line: +# make -f makefile.tc +# +# Or rename to MAKEFILE and run: +# make +# +# Turbo C++ 3.0 should be in PATH, or adjust CC path below. + +CC = tcc +CFLAGS = -A -w -ml -I.\include +# -A = ANSI keywords only +# -w = Enable all warnings +# -ml = Large memory model (for DOS) +# -I = Include path + +SRCDIR = src +INCDIR = include +BUILDDIR = build + +.c.obj: + $(CC) $(CFLAGS) -c -o$*.obj $< + +all: $(BUILDDIR) $(BUILDDIR)\addnote.exe $(BUILDDIR)\dumpnotes.exe + +$(BUILDDIR): + if not exist $(BUILDDIR) mkdir $(BUILDDIR) + +$(BUILDDIR)\addnote.exe: $(SRCDIR)\addnote.c $(INCDIR)\platform.h + $(CC) $(CFLAGS) -e$(BUILDDIR)\addnote.exe $(SRCDIR)\addnote.c + +$(BUILDDIR)\dumpnotes.exe: $(SRCDIR)\dumpnotes.c $(INCDIR)\platform.h + $(CC) $(CFLAGS) -e$(BUILDDIR)\dumpnotes.exe $(SRCDIR)\dumpnotes.c + +clean: + if exist $(BUILDDIR)\*.exe del $(BUILDDIR)\*.exe + if exist $(BUILDDIR)\*.obj del $(BUILDDIR)\*.obj + if exist $(BUILDDIR) rmdir $(BUILDDIR) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..db62457 --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +# Makefile for cnotes - GCC on Linux/Unix/macOS +# Strict C89/ANSI compliant build + +CC = gcc +CFLAGS = -ansi -pedantic -Wall -Wextra -O2 +INCLUDES = -I include + +SRCDIR = src +INCDIR = include +BUILDDIR = build + +SOURCES = $(SRCDIR)/addnote.c $(SRCDIR)/dumpnotes.c +HEADERS = $(INCDIR)/platform.h + +TARGETS = $(BUILDDIR)/addnote $(BUILDDIR)/dumpnotes + +.PHONY: all clean install uninstall + +all: $(BUILDDIR) $(TARGETS) + +$(BUILDDIR): + mkdir -p $(BUILDDIR) + +$(BUILDDIR)/addnote: $(SRCDIR)/addnote.c $(HEADERS) + $(CC) $(CFLAGS) $(INCLUDES) -o $@ $(SRCDIR)/addnote.c + +$(BUILDDIR)/dumpnotes: $(SRCDIR)/dumpnotes.c $(HEADERS) + $(CC) $(CFLAGS) $(INCLUDES) -o $@ $(SRCDIR)/dumpnotes.c + +clean: + rm -rf $(BUILDDIR) + +# Install to /usr/local/bin (optional, run with sudo) +install: $(TARGETS) + install -m 755 $(BUILDDIR)/addnote /usr/local/bin/ + install -m 755 $(BUILDDIR)/dumpnotes /usr/local/bin/ + +uninstall: + rm -f /usr/local/bin/addnote /usr/local/bin/dumpnotes diff --git a/platform.h b/include/platform.h similarity index 100% rename from platform.h rename to include/platform.h diff --git a/addnote.c b/src/addnote.c similarity index 94% rename from addnote.c rename to src/addnote.c index a29a07a..d6e3570 100644 --- a/addnote.c +++ b/src/addnote.c @@ -3,7 +3,6 @@ #include #include #include -#include #include "platform.h" @@ -38,9 +37,9 @@ int get_cnotes_path(char *buffer, size_t bufsize) { const char *home; size_t len; - home = getenv("HOME"); + home = getenv(HOME_ENV); if (home == NULL) { - fprintf(stderr, "Error: HOME environment variable not set\n"); + fprintf(stderr, "Error: %s environment variable not set\n", HOME_ENV); return 0; } @@ -51,7 +50,7 @@ int get_cnotes_path(char *buffer, size_t bufsize) { return 0; } - sprintf(buffer, "%s/%s", home, CNOTES_DIR); + sprintf(buffer, "%s" PATH_SEP_STR "%s", home, CNOTES_DIR); return 1; } @@ -109,7 +108,7 @@ int write_entry(const char *category, const char *message) { } /* Build full file path */ - sprintf(file_path, "%s/%s", dir_path, CNOTES_FILE); + sprintf(file_path, "%s" PATH_SEP_STR "%s", dir_path, CNOTES_FILE); /* Open file in append mode */ file = fopen(file_path, "a"); diff --git a/dumpnotes.c b/src/dumpnotes.c similarity index 91% rename from dumpnotes.c rename to src/dumpnotes.c index b106804..cc7f897 100644 --- a/dumpnotes.c +++ b/src/dumpnotes.c @@ -5,6 +5,7 @@ #include #include #include +#include "platform.h" #define MAX_LENGTH 500 /* Allow fgets to read up to this many characters */ #define DATE_LENGTH 10 /* YYYY-MM-DD */ @@ -101,7 +102,7 @@ void print_entry(const Entry *entry) { /* Parse a fixed-length field followed by a delimiter */ static const char *parse_fixed_field(const char *ptr, char *dest, int length, char delimiter) { - if (strlen(ptr) < length) return NULL; + if ((int)strlen(ptr) < length) return NULL; strncpy(dest, ptr, length); dest[length] = '\0'; ptr += length; @@ -152,9 +153,9 @@ int get_cnotes_path(char *buffer, size_t bufsize) { const char *home; size_t len; - home = getenv("HOME"); + home = getenv(HOME_ENV); if (home == NULL) { - fprintf(stderr, "Error: HOME environment variable not set\n"); + fprintf(stderr, "Error: %s environment variable not set\n", HOME_ENV); return 0; } @@ -165,20 +166,27 @@ int get_cnotes_path(char *buffer, size_t bufsize) { return 0; } - sprintf(buffer, "%s/%s/%s", home, CNOTES_DIR, CNOTES_FILE); + sprintf(buffer, "%s" PATH_SEP_STR "%s" PATH_SEP_STR "%s", home, CNOTES_DIR, CNOTES_FILE); return 1; } /* start with a simple cat of the file */ int main(int argc, char *argv[]) { + int i; FILE *notesfile; char file_path[600]; - Entry entries[MAX_ENTRIES]; + Entry *entries; int entry_count = 0; char line[MAX_LENGTH +1]; - char *filename = CNOTES_FILE; + char *filename = NULL; SortMode sort_mode = SORT_NONE; - int i; + + /* Insures that we won't run out of memory in DOS */ + entries = (Entry *)malloc(MAX_ENTRIES * sizeof(Entry)); + if (entries == NULL) { + fprintf(stderr, "Error: Cannot allocate memory\n"); + return 1; + } /* Parse command line arguments */ for (i = 1; i < argc; i++) { @@ -209,6 +217,9 @@ int main(int argc, char *argv[]) { } } + /* TODO: filename argument is parsed but not yet used */ + (void)filename; + /* Get cnotes file path */ if (!get_cnotes_path(file_path, sizeof(file_path))) { return 1; @@ -247,5 +258,6 @@ int main(int argc, char *argv[]) { } print_footer(); + free(entries); return 0; -} \ No newline at end of file +}