xref: /illumos-gate/usr/src/lib/libnsl/saf/checkver.c (revision 7c478bd9)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <rpc/trace.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(ver, fname)
53 int ver;
54 char *fname;
55 {
56 	FILE *fp;		/* file pointer for sactab */
57 	char line[BUFSIZ];	/* temp buffer for input */
58 	char *p;		/* working pointer */
59 	int version;		/* version number from sactab */
60 
61 	trace2(TR_check_version, 0, ver);
62 	if ((fp = fopen(fname, "r")) == NULL) {
63 		trace2(TR_check_version, 1, ver);
64 		return(2);
65 	}
66 	p = line;
67 	while (fgets(p, BUFSIZ, fp)) {
68 		if (!strncmp(p, VSTR, strlen(VSTR))) {
69 			p += strlen(VSTR);
70 			if (*p)
71 				version = atoi(p);
72 			else {
73 				trace2(TR_check_version, 1, ver);
74 				return(3);
75 			}
76 			(void) fclose(fp);
77 			trace2(TR_check_version, 1, ver);
78 			return((version != ver) ? 1 : 0);
79 		}
80 		p = line;
81 	}
82 	trace2(TR_check_version, 1, ver);
83 	return(3);
84 }
85