1/*
2 * Copyright (c) 1998-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include "vsdb.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31static FILE *_vs_fp;
32static struct vsdb _vs_vsdb;
33
34static int
35vsdbscan()
36{
37	char *cp, *p;
38#define	MAXLINELENGTH	1024
39	static char line[MAXLINELENGTH];
40
41	for (;;) {
42
43		if (!(p = fgets(line, sizeof(line), _vs_fp)))
44			return(0);
45		if (!(cp = strsep(&p, ":")) || *cp == '\0')
46			continue;
47		_vs_vsdb.vs_spec = cp;
48		if (!(cp = strsep(&p, "\n")) || *cp == '\0')
49			continue;
50		_vs_vsdb.vs_ops = strtol(cp, &p, 16);
51		if (*p == '\0')
52			return(1);
53	}
54	/* NOTREACHED */
55}
56
57struct vsdb *
58getvsent()
59{
60	if ((!_vs_fp && !setvsent()) || !vsdbscan())
61		return((struct vsdb *)NULL);
62	return(&_vs_vsdb);
63}
64
65struct vsdb *
66getvsspec(name)
67	const char *name;
68{
69	if (setvsent())
70		while (vsdbscan())
71			if (!strcmp(_vs_vsdb.vs_spec, name))
72				return(&_vs_vsdb);
73	return((struct vsdb *)NULL);
74}
75
76int
77setvsent()
78{
79	if (_vs_fp) {
80		rewind(_vs_fp);
81		return(1);
82	}
83	if ((_vs_fp = fopen(_PATH_VSDB, "r")) != NULL) {
84		return(1);
85	}
86	return(0);
87}
88
89void
90endvsent()
91{
92	if (_vs_fp) {
93		(void)fclose(_vs_fp);
94		_vs_fp = NULL;
95	}
96}
97