Blame src/internal/slibtool_objlist_impl.c

a126a7
/*******************************************************************/
a126a7
/*  slibtool: a skinny libtool implementation, written in C        */
a126a7
/*  Copyright (C) 2016--2023  SysDeer Technologies, LLC            */
a126a7
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
a126a7
/*******************************************************************/
a126a7
a126a7
#include <fcntl.h>
a126a7
#include <errno.h>
a126a7
#include <unistd.h>
a126a7
#include <stdint.h>
a126a7
#include <stdlib.h>
a126a7
#include <string.h>
a126a7
#include <sys/mman.h>
a126a7
#include <sys/stat.h>
a126a7
a126a7
#include "slibtool_driver_impl.h"
a126a7
#include "slibtool_errinfo_impl.h"
a126a7
#include "slibtool_mapfile_impl.h"
a126a7
#include "slibtool_objlist_impl.h"
a126a7
a126a7
int slbt_objlist_read(
a126a7
	int                     fdcwd,
a126a7
	struct slbt_obj_list *  objlist)
a126a7
{
a126a7
	struct slbt_map_info *	mapinfo;
a126a7
	int			objc;
a126a7
	char **			objv;
a126a7
	char *			src;
a126a7
	char *			dst;
a126a7
	char *			mark;
a126a7
	int			skip;
a126a7
a126a7
	/* temporarily map the object list */
a126a7
	if (!(mapinfo = slbt_map_file(fdcwd,objlist->name,SLBT_MAP_INPUT)))
a126a7
		return -1;
a126a7
a126a7
	/* object list, cautionary null termination, vector null termination */
a126a7
	objlist->size = mapinfo->size;
a126a7
	objlist->size++;
a126a7
	objlist->size++;
a126a7
a126a7
	if (!(objlist->addr = calloc(1,objlist->size))) {
a126a7
		slbt_unmap_file(mapinfo);
a126a7
		return -1;
a126a7
	}
a126a7
a126a7
	/* object list file to normalized object strings */
a126a7
	objc = 0;
a126a7
	skip = true;
a126a7
a126a7
	for (src=mapinfo->addr,dst=objlist->addr; src<mapinfo->cap; src++) {
a126a7
		if (!*src || (*src==' ') || (*src=='\n') || (*src=='\r')) {
a126a7
			if (!skip)
a126a7
				*dst++ = 0;
a126a7
a126a7
			skip = true;
a126a7
		} else {
a126a7
			*dst++ = *src;
a126a7
a126a7
			objc += !!skip;
a126a7
			skip  = false;
a126a7
		}
a126a7
	}
a126a7
a126a7
	/* object vector */
a126a7
	objlist->objc = objc;
a126a7
a126a7
	if (!(objlist->objv = calloc(++objc,sizeof(char *)))) {
a126a7
		free(objlist->addr);
a126a7
		slbt_unmap_file(mapinfo);
a126a7
		return -1;
a126a7
	}
a126a7
a126a7
	for (objv=objlist->objv,mark=objlist->addr; *mark; objv++) {
a126a7
		*objv = mark;
a126a7
		mark += strlen(mark) + 1;
a126a7
	}
a126a7
a126a7
	slbt_unmap_file(mapinfo);
a126a7
a126a7
	return 0;
a126a7
}