commit da6af4863c09169ebe9134915e97931360669464
parent a4d9b3576d0e8380b0b28f22646a48f044bf2cdd
Author: Christos Margiolis <christos@margiolis.net>
Date: Wed, 21 Oct 2020 13:42:12 +0300
added config.mk
Diffstat:
8 files changed, 80 insertions(+), 50 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,37 +1,16 @@
# See LICENSE file for copyright and license details.
+# cstring - a simple and lightweight string library for C
+.POSIX:
+
+include config.mk
LIB = cstring
-VERSION = 0.1
DIST = ${LIB}-${VERSION}
MAN3 = ${LIB}.3
-PREFIX = /usr/local
-MAN_DIR = ${PREFIX}/man/man3
-INC_DIR = ${PREFIX}/include
-LIB_DIR = ${PREFIX}/lib
-EXT = c
-#SRC = ${wildcard *.${EXT}}
-#OBJ = ${SRC:%.${EXT}=%.o}
SRC = cstring.c
OBJ = cstring.o
-AR = ar
-ARFLAGS = rs
-CC = gcc
-INCS = -Iinclude
-# Uncomment if you want to compile the library in debug mode
-#CPPFLAGS = -DCSTRING_DBG -DVERSION=\"${VERSION}\"
-CPPFLAGS = -DVERSION=\"${VERSION}\"
-CFLAGS = -Wall -std=c99 -pedantic -O3 ${INCS} ${CPPFLAGS}
-LDFLAGS = -Llib
-
-CP = cp -f
-RM = rm -f
-RM_DIR = rm -rf
-MKDIR = mkdir -p
-TAR = tar -cf
-GZIP = gzip
-
all: options ${LIB}
options:
@@ -43,12 +22,13 @@ options:
${LIB}: ${OBJ}
${AR} ${ARFLAGS} lib${LIB}.a ${OBJ}
-${OBJ}: ${SRC}
+${OBJ}: ${SRC} cstring.h
${CC} ${CFLAGS} -c ${SRC} -o $@
dist: clean
${MKDIR} ${DIST}
- ${CP} -R tests ${SRC} ${MAN3} LICENSE Makefile README.md ${DIST}
+ ${CP} -R tests/ config.mk ${MAN3} ${SRC} cstring.h LICENSE Makefile \
+ README.md ${DIST}
${TAR} ${DIST}.tar ${DIST}
${GZIP} ${DIST}.tar
${RM_DIR} ${DIST}
diff --git a/README.md b/README.md
@@ -6,23 +6,34 @@ but with a many additions.
## Building
`cstring` is a static library. The header file is installed in `/usr/local/include` and
-the library file in `/usr/local/lib`. In order to install it do the following
+the library file in `/usr/local/lib`.
+In order to install it do the following
```shell
$ cd /path/to/cstring
-$ sudo make install
-$ make clean
+$ sudo make install clean
```
-If you want to uninstall the library do the following
+You can also run a few tests
+```shell
+$ cd /path/to/cstring/tests
+$ make && make run clean
+```
+
+In order to make a distribution do
+```shell
+$ cd /path/to/cstring
+$ make dist
+```
+If you want to uninstall the library do the following
```shell
$ cd /path/to/cstring
$ sudo make uninstall
```
-In order to link `cstring` to your project use the `-lcstring` flag during compilation.
-In case you want to run your project in debug mode, compile it using the `-DCSTRING_DBG` option.
+A file using `cstring` has to be linked using the `-lcstring` linker flag.
+In case you want to run your project in debug mode, compile the library using the `-DCSTRING_DBG` option.
## Usage
diff --git a/config.mk b/config.mk
@@ -0,0 +1,40 @@
+# See LICENSE file for copyright and license details.
+# cstring version
+VERSION = 0.1
+
+# paths
+PREFIX = /usr/local
+#MAN_DIR = ${PREFIX}/man/man1
+BIN_DIR = ${PREFIX}/bin
+# uncomment if you're making a library
+MAN_DIR = ${PREFIX}/man/man3
+INC_DIR = ${PREFIX}/include
+LIB_DIR = ${PREFIX}/lib
+
+# includes and libs
+INCS = -Iinclude
+LIBS = -Llib
+
+# flags
+CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \
+ -DVERSION=\"${VERSION}\"
+CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations \
+ -O3 ${INCS} ${CPPFLAGS}
+LDFLAGS = ${LIBS}
+# uncomment if you're making a library
+ARFLAGS = rs
+
+# utils
+CP = cp -f
+RM = rm -f
+RM_DIR = rm -rf
+MV = mv
+MKDIR = mkdir -p
+RM_DIR = rm -rf
+TAR = tar -cf
+GZIP = gzip
+
+# compiler
+CC = gcc
+# uncomment if you're making a library
+AR = ar
diff --git a/cstring.3 b/cstring.3
@@ -27,9 +27,9 @@ option.
.Bl -tag -width Ds
.It cstring
struct cstring {
- char *str; /* contents of string */
size_t len; /* string length */
size_t capacity; /* string capacity */
+ char *str; /* contents of string */
.br
};
.It cstring_sort_flags
diff --git a/cstring.h b/cstring.h
@@ -41,9 +41,9 @@ extern "C" {
#endif /* CSTRING_DBG */
struct cstring {
- char *str;
size_t len;
size_t capacity;
+ char *str;
};
enum cstring_sort_flags {
diff --git a/tests/Makefile b/tests/Makefile
@@ -1,3 +1,7 @@
+# See LICENSE file for copyright and license details.
+# cstring tests
+.POSIX:
+
BINS = test_basic test_insert
CC = gcc
CFLAGS = -Wall -std=c99 -pedantic -O3
@@ -12,19 +16,14 @@ options:
@echo "CC = ${CC}"
run:
- @echo "---------------------------"
- @echo "---------------------------"
- @echo "RUNNING: test_basic"
- @echo "---------------------------"
- @echo "---------------------------"
- ./test_basic
-
- @echo "---------------------------"
- @echo "---------------------------"
- @echo "RUNNING: test_insert"
- @echo "---------------------------"
- @echo "---------------------------"
- ./test_insert
+ for bin in ${BINS}; do \
+ echo "---------------------------"; \
+ echo "---------------------------"; \
+ echo "RUNNING: $${bin}"; \
+ echo "---------------------------"; \
+ echo "---------------------------"; \
+ ./$${bin}; \
+ done
clean:
rm -f ${BINS} *.o
diff --git a/tests/test_basic.c b/tests/test_basic.c
@@ -3,7 +3,7 @@
// Compilation: gcc test_basic.c -lcstring
int
-main(int argc, char **argv)
+main(int argc, char *argv[])
{
cstring s = cstring_create("Hello world");
printf("cstring_create: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
diff --git a/tests/test_insert.c b/tests/test_insert.c
@@ -3,7 +3,7 @@
// Compilation: gcc test_insert.c -lcstring
int
-main(int argc, char **argv)
+main(int argc, char *argv[])
{
cstring s = cstring_create("HHHHHHEEEEEEEEEEEEEEEEEEEEEYYYYYYYYYYYYYY");
printf("%s\n", s.str);