# HG changeset patch # User Mychaela Falconia # Date 1692047438 28800 # Node ID 60f6e561bfb8dbd4d1efc45a9d7ce1afc09f0ca7 # Parent 78c6e30f52341069ee31f7f1b006f507d0946244 libnumdb2: start with readbin.c diff -r 78c6e30f5234 -r 60f6e561bfb8 libnumdb2/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libnumdb2/Makefile Mon Aug 14 13:10:38 2023 -0800 @@ -0,0 +1,13 @@ +CC= gcc +CFLAGS= -O2 +OBJS= readbin.o +LIB= libnumdb.a + +all: ${LIB} + +${LIB}: ${OBJS} + ar rcu $@ ${OBJS} + ranlib $@ + +clean: + rm -f *.[oa] errs diff -r 78c6e30f5234 -r 60f6e561bfb8 libnumdb2/readbin.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libnumdb2/readbin.c Mon Aug 14 13:10:38 2023 -0800 @@ -0,0 +1,69 @@ +/* + * This library module contains the code that reads /var/gsm/number-db2.bin, + * as well as definitions of global variables into which the booty is read. + */ + +#include +#include +#include +#include +#include +#include +#include "../include/number_db_v2.h" + +char numdb_pathname[] = "/var/gsm/number-db2.bin"; +struct stat numdb_file_stat; +struct numdb_file_hdr numdb_hdr; +struct owned_number_rec *numdb_owned_numbers; +struct short_number_rec *numdb_short_numbers; + +read_number_db() +{ + FILE *inf; + + inf = fopen(numdb_pathname, "r"); + if (!inf) { + syslog(LOG_CRIT, "open %s: %m", numdb_pathname); + return(-1); + } + fstat(fileno(inf), &numdb_file_stat); + if (!S_ISREG(numdb_file_stat.st_mode)) { + syslog(LOG_CRIT, "invalid %s: not a regular file", + numdb_pathname); + fclose(inf); + return(-1); + } + if (fread(&numdb_hdr, sizeof numdb_hdr, 1, inf) != 1) { +read_err: syslog(LOG_CRIT, "error reading from %s: %m", numdb_pathname); + fclose(inf); + return(-1); + } + if (numdb_hdr.owned_number_count) { + numdb_owned_numbers = malloc(numdb_hdr.owned_number_count * + sizeof(struct owned_number_rec)); + if (!numdb_owned_numbers) { + syslog(LOG_CRIT, "malloc for owned number db: %m"); + fclose(inf); + return(-1); + } + if (fread(numdb_owned_numbers, sizeof(struct owned_number_rec), + numdb_hdr.owned_number_count, inf) != + numdb_hdr.owned_number_count) + goto read_err; + } + if (numdb_hdr.short_number_count) { + numdb_short_numbers = malloc(numdb_hdr.short_number_count * + sizeof(struct short_number_rec)); + if (!numdb_short_numbers) { + syslog(LOG_CRIT, "malloc for short number db: %m"); + fclose(inf); + return(-1); + } + if (fread(numdb_short_numbers, sizeof(struct short_number_rec), + numdb_hdr.short_number_count, inf) != + numdb_hdr.short_number_count) + goto read_err; + } + fclose(inf); + return(0); +}