summaryrefslogtreecommitdiff
path: root/src/internal/slibtool_objlist_impl.c
blob: 9f25529cbc65608ef0b534ba429487a33a5ec5a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************/
/*  slibtool: a strong libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include "slibtool_driver_impl.h"
#include "slibtool_errinfo_impl.h"
#include "slibtool_mapfile_impl.h"
#include "slibtool_objlist_impl.h"
#include "slibtool_visibility_impl.h"

slbt_hidden int slbt_objlist_read(
	int                     fdcwd,
	struct slbt_obj_list *  objlist)
{
	struct slbt_map_info *	mapinfo;
	int			objc;
	char **			objv;
	char *			src;
	char *			dst;
	char *			mark;
	int			skip;

	/* temporarily map the object list */
	if (!(mapinfo = slbt_map_file(fdcwd,objlist->name,SLBT_MAP_INPUT)))
		return -1;

	/* object list, cautionary null termination, vector null termination */
	objlist->size = mapinfo->size;
	objlist->size++;
	objlist->size++;

	if (!(objlist->addr = calloc(1,objlist->size))) {
		slbt_unmap_file(mapinfo);
		return -1;
	}

	/* object list file to normalized object strings */
	objc = 0;
	skip = true;

	for (src=mapinfo->addr,dst=objlist->addr; src<mapinfo->cap; src++) {
		if (!*src || (*src==' ') || (*src=='\n') || (*src=='\r')) {
			if (!skip)
				*dst++ = 0;

			skip = true;
		} else {
			*dst++ = *src;

			objc += !!skip;
			skip  = false;
		}
	}

	/* object vector */
	objlist->objc = objc;

	if (!(objlist->objv = calloc(++objc,sizeof(char *)))) {
		free(objlist->addr);
		slbt_unmap_file(mapinfo);
		return -1;
	}

	for (objv=objlist->objv,mark=objlist->addr; *mark; objv++) {
		*objv = mark;
		mark += strlen(mark) + 1;
	}

	slbt_unmap_file(mapinfo);

	return 0;
}