summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2018-06-28 03:53:30 -0400
committermidipix <writeonce@midipix.org>2018-06-29 01:37:42 -0400
commit62ed2e6c2962f6012ede1584646bbdb73021b141 (patch)
tree132c88dbe8a06e588ca0fe557c292cb22d265235 /src
parenta2786588d2ccb01f96e92e039c2aaf18f77d5d5c (diff)
downloadslibtool-62ed2e6c2962f6012ede1584646bbdb73021b141.tar.bz2
slibtool-62ed2e6c2962f6012ede1584646bbdb73021b141.tar.xz
internals: added slbt_mapped_readline().
Diffstat (limited to 'src')
-rw-r--r--src/internal/slibtool_mapfile_impl.c40
-rw-r--r--src/internal/slibtool_mapfile_impl.h7
2 files changed, 47 insertions, 0 deletions
diff --git a/src/internal/slibtool_mapfile_impl.c b/src/internal/slibtool_mapfile_impl.c
index e5d40d1..a9c1d61 100644
--- a/src/internal/slibtool_mapfile_impl.c
+++ b/src/internal/slibtool_mapfile_impl.c
@@ -3,8 +3,11 @@
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
+
+#include "slibtool_errinfo_impl.h"
#include "slibtool_mapfile_impl.h"
static void slbt_munmap(void * addr, size_t size)
@@ -67,6 +70,8 @@ struct slbt_map_info * slbt_map_file(
mapinfo->addr = addr;
mapinfo->size = st.st_size;
+ mapinfo->mark = addr;
+ mapinfo->cap = &mapinfo->mark[st.st_size];
return mapinfo;
}
@@ -76,3 +81,38 @@ void slbt_unmap_file(struct slbt_map_info * mapinfo)
slbt_munmap(mapinfo->addr,mapinfo->size);
free(mapinfo);
}
+
+int slbt_mapped_readline(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_map_info * mapinfo,
+ char * buf,
+ size_t buflen)
+{
+ const char * ch;
+ const char * cap;
+ const char * mark;
+ const char * newline;
+ size_t len;
+
+ mark = mapinfo->mark;
+ cap = mapinfo->cap;
+
+ for (ch=mark, newline=0; ch<cap && !newline; ch++)
+ if (*ch == '\n')
+ newline = ch;
+
+ len = ch - mark;
+
+ if (newline && (buflen <= len))
+ return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_FLOW_ERROR);
+
+ if (!newline)
+ return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_BAD_DATA);
+
+ memcpy(buf,mark,len);
+ buf[len] = 0;
+
+ mapinfo->mark += len;
+
+ return 0;
+}
diff --git a/src/internal/slibtool_mapfile_impl.h b/src/internal/slibtool_mapfile_impl.h
index 567fc8f..4b96c11 100644
--- a/src/internal/slibtool_mapfile_impl.h
+++ b/src/internal/slibtool_mapfile_impl.h
@@ -7,6 +7,8 @@
struct slbt_map_info {
void * addr;
size_t size;
+ char * mark;
+ char * cap;
};
struct slbt_map_info * slbt_map_file(
@@ -15,4 +17,9 @@ struct slbt_map_info * slbt_map_file(
void slbt_unmap_file(struct slbt_map_info *);
+int slbt_mapped_readline(
+ const struct slbt_driver_ctx *,
+ struct slbt_map_info *,
+ char *, size_t);
+
#endif