Blame src/output/slbt_output_config.c

97bc5c
/*******************************************************************/
97bc5c
/*  slibtool: a skinny libtool implementation, written in C        */
97bc5c
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
97bc5c
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
97bc5c
/*******************************************************************/
97bc5c
97bc5c
#include <stdio.h>
97bc5c
#include <string.h>
97bc5c
#include <stdbool.h>
97bc5c
97bc5c
#include <slibtool/slibtool.h>
97bc5c
#include "slibtool_driver_impl.h"
97bc5c
#include "slibtool_dprintf_impl.h"
97bc5c
#include "slibtool_errinfo_impl.h"
97bc5c
97bc5c
static const char lconf_begin[] = "# ### BEGIN LIBTOOL CONFIG\n";
97bc5c
static const char lconf_end  [] = "# ### END LIBTOOL CONFIG\n";
97bc5c
97bc5c
static int slbt_output_config_lconf(
97bc5c
	const struct slbt_driver_ctx * dctx,
97bc5c
	const struct slbt_map_info *   lconf)
97bc5c
{
97bc5c
	const char *    ch;
97bc5c
	const char *    cfg_begin;
97bc5c
	const char *    cfg_end;
97bc5c
	const char *    map_cap;
97bc5c
	size_t          cmp_len;
97bc5c
	size_t          end_len;
97bc5c
	size_t          min_len;
97bc5c
	size_t          nbytes;
97bc5c
	ssize_t         written;
97bc5c
	int             fdout;
97bc5c
97bc5c
	cmp_len = strlen(lconf_begin);
97bc5c
	end_len = strlen(lconf_end);
97bc5c
	min_len = cmp_len + end_len;
97bc5c
97bc5c
	if (lconf->size < min_len)
97bc5c
		return SLBT_CUSTOM_ERROR(
97bc5c
			dctx,
97bc5c
			SLBT_ERR_FLOW_ERROR);
97bc5c
97bc5c
	map_cap  = lconf->addr;
97bc5c
	map_cap += lconf->size;
97bc5c
	map_cap -= strlen(lconf_end);
97bc5c
	map_cap -= strlen(lconf_begin);
97bc5c
97bc5c
	cfg_begin = cfg_end = 0;
97bc5c
97bc5c
	for (ch=lconf->addr; !cfg_begin && (ch < map_cap); ch++)
97bc5c
		if (!strncmp(ch,lconf_begin,cmp_len))
97bc5c
			cfg_begin = ch;
97bc5c
97bc5c
	if (!cfg_begin)
97bc5c
		return SLBT_CUSTOM_ERROR(
97bc5c
			dctx,
97bc5c
			SLBT_ERR_FLOW_ERROR);
97bc5c
97bc5c
	for (++ch; !cfg_end && (ch < map_cap); ch++)
97bc5c
		if (!strncmp(ch,lconf_end,end_len))
97bc5c
			cfg_end = ch;
97bc5c
97bc5c
	if (!cfg_end)
97bc5c
		return SLBT_CUSTOM_ERROR(
97bc5c
			dctx,
97bc5c
			SLBT_ERR_FLOW_ERROR);
97bc5c
97bc5c
	fdout  = slbt_driver_fdout(dctx);
97bc5c
	nbytes = cfg_end - cfg_begin - cmp_len;
97bc5c
97bc5c
	for (ch=&cfg_begin[cmp_len]; nbytes; ) {
97bc5c
		written = write(fdout,ch,nbytes);
97bc5c
97bc5c
		while ((written < 0) && (errno == EINTR))
97bc5c
			written = write(fdout,ch,nbytes);
97bc5c
97bc5c
		if (written < 0)
97bc5c
			return SLBT_SYSTEM_ERROR(dctx,0);
97bc5c
97bc5c
		nbytes -= written;
97bc5c
		ch     += written;
97bc5c
	}
97bc5c
97bc5c
	return 0;
97bc5c
}
97bc5c
97bc5c
int slbt_output_config(const struct slbt_driver_ctx * dctx)
97bc5c
{
97bc5c
	struct slbt_driver_ctx_impl *   ictx;
97bc5c
	const struct slbt_map_info *    lconf;
97bc5c
97bc5c
	ictx  = slbt_get_driver_ictx(dctx);
97bc5c
	lconf = &ictx->lconf;
97bc5c
97bc5c
	if (lconf->addr)
97bc5c
		return slbt_output_config_lconf(
97bc5c
			dctx,lconf);
97bc5c
97bc5c
	return 0;
97bc5c
}