summaryrefslogtreecommitdiff
path: root/libs.c
blob: e9ad7aec5ab7362741f9ee128c85680a7eab189d (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
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>

#include "cc.h"
#include "libs.h"

static char **libdirs = NULL;
static size_t nlibdirs = 0;

void addlibdir(char *path, int commandline)
{
	libdirs = realloc(libdirs, (nlibdirs + 1) * sizeof(*libdirs));
	if (!libdirs) {
		error(NULL, commandline, "out of memory adding '%s' to library search path\n", path);
	}
	libdirs[nlibdirs++] = path;
}

char *findlib(const char *lib)
{
	char path[PATH_MAX];
	for (size_t i = 0; i < nlibdirs; i++) {
		snprintf(path, sizeof(path), "-l%s/lib%s.a", libdirs[i], lib);
		printf("checking %s\n", path + 2);
		if (access(path + 2, F_OK) == 0) {
			printf("\tOK\n");
			return strdup(path);
		}
	}
	error(NULL, 1, "could not find library -l%s\n", lib);
	return NULL;
}