xref: /illumos-gate/usr/src/lib/libnsl/saf/checkver.c (revision 7f7322febbcfe774b7270abc3b191c094bfcc517)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "mt.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 
39 #define	VSTR	"# VERSION="
40 
41 
42 /*
43  * check_version - check to make sure designated file is the correct version
44  *		returns : 0 - version correct
45  *			  1 - version incorrect
46  *			  2 - could not open file
47  *			  3 - corrupt file
48  */
49 
50 
51 int
52 check_version(int ver, char *fname)
53 {
54 	FILE *fp;		/* file pointer for sactab */
55 	char line[BUFSIZ];	/* temp buffer for input */
56 	char *p;		/* working pointer */
57 	int version;		/* version number from sactab */
58 
59 	if ((fp = fopen(fname, "r")) == NULL)
60 		return (2);
61 	p = line;
62 	while (fgets(p, BUFSIZ, fp)) {
63 		if (strncmp(p, VSTR, strlen(VSTR)) == 0) {
64 			p += strlen(VSTR);
65 			if (*p)
66 				version = atoi(p);
67 			else
68 				return (3);
69 			(void) fclose(fp);
70 			return ((version != ver) ? 1 : 0);
71 		}
72 		p = line;
73 	}
74 	return (3);
75 }
76