1159720Syar/*-
2159720Syar * Copyright (c) 2002, 2003 Greg Lehey
3159720Syar * All rights reserved.
4159720Syar *
5159720Syar * Redistribution and use in source and binary forms, with or without
6159720Syar * modification, are permitted provided that the following conditions
7159720Syar * are met:
8159720Syar * 1. Redistributions of source code must retain the above copyright
9159720Syar *    notice, this list of conditions and the following disclaimer.
10159720Syar * 2. Redistributions in binary form must reproduce the above copyright
11159720Syar *    notice, this list of conditions and the following disclaimer in the
12159720Syar *    documentation and/or other materials provided with the distribution.
13159720Syar *
14159720Syar * This software is provided by the author ``as is'' and any express
15159720Syar * or implied warranties, including, but not limited to, the implied
16159720Syar * warranties of merchantability and fitness for a particular purpose
17159720Syar * are disclaimed.  In no event shall the author be liable for any
18159720Syar * direct, indirect, incidental, special, exemplary, or consequential
19159720Syar * damages (including, but not limited to, procurement of substitute
20159720Syar * goods or services; loss of use, data, or profits; or business
21159720Syar * interruption) however caused and on any theory of liability,
22159720Syar * whether in contract, strict liability, or tort (including
23159720Syar * negligence or otherwise) arising in any way out of the use of this
24159720Syar * software, even if advised of the possibility of such damage.
25159720Syar */
26159720Syar
27159720Syar#include <sys/cdefs.h>
28159720Syar__FBSDID("$FreeBSD: releng/11.0/usr.sbin/asf/asf_prog.c 159720 2006-06-18 11:14:40Z yar $");
29159720Syar
30159720Syar#include <sys/types.h>
31159720Syar#include <err.h>
32159720Syar#include <inttypes.h>
33159720Syar#include <limits.h>
34159720Syar#include <stdio.h>
35159720Syar#include <string.h>
36159720Syar
37159720Syar#include "asf.h"
38159720Syar
39159720Syar/*
40159720Syar * Get the linker file list from kldstat(8) output.
41159720Syar * The "run" flag tells if kldstat(8) should run now.
42159720Syar * Of course, kldstat(1) can run in a live system only, but its output
43159720Syar * can be saved beforehand and fed to this function later via stdin.
44159720Syar */
45159720Syarvoid
46159720Syarasf_prog(int run)
47159720Syar{
48159720Syar	char	buf[LINE_MAX];
49159720Syar	char   *token[MAXTOKEN];
50159720Syar	char   *endp;
51159720Syar	FILE   *kldstat;
52159720Syar	caddr_t	base;
53159720Syar	int	tokens;
54159720Syar
55159720Syar	if (run) {
56159720Syar		if ((kldstat = popen("kldstat", "r")) == NULL)
57159720Syar			err(2, "can't start kldstat");
58159720Syar	} else
59159720Syar		kldstat = stdin;
60159720Syar
61159720Syar	while (fgets(buf, sizeof(buf), kldstat)) {
62159720Syar		/* Skip header line and main kernel file */
63159720Syar		if (buf[0] == 'I' || strstr(buf, KERNFILE))
64159720Syar			continue;
65159720Syar		tokens = tokenize(buf, token, MAXTOKEN);
66159720Syar		if (tokens < 4)
67159720Syar			continue;
68159720Syar		base = (caddr_t)(uintptr_t)strtoumax(token[2], &endp, 16);
69159720Syar		if (endp == NULL || *endp != '\0')
70159720Syar			continue;
71159720Syar		kfile_add(token[4], base);
72159720Syar	}
73159720Syar}
74