xref: /titanic_44/usr/src/cmd/ypcmd/getlist.c (revision bde3d612a7c090234c60e6e4578821237a5db135)
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 1992 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /*	  All Rights Reserved   */
28 
29 /*
30  * Portions of this source code were derived from Berkeley
31  * under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "ypsym.h"
40 
41 extern void free();
42 extern char *strdup();
43 
44 /*
45  * Add a name to the list
46  */
47 static listofnames *
48 newname(str)
49 char *str;
50 {
51 	listofnames *it;
52 	char *copy;
53 
54 	if (str == NULL)
55 		return (NULL);
56 	copy = strdup(str);
57 	if (copy == NULL)
58 		return (NULL);
59 	it = (listofnames *) malloc(sizeof (listofnames));
60 	if (it == NULL) {
61 		free(copy);
62 		return (NULL);
63 	}
64 	it->name = copy;
65 	it->nextname = NULL;
66 	return (it);
67 }
68 
69 /*
70  * Assemble the list of names
71  */
72 listofnames *
73 names(filename)
74 char *filename;
75 {
76 	listofnames *nameslist;
77 	listofnames *end;
78 	listofnames *nname;
79 	FILE *fyle;
80 	char line[256];
81 	char name[256];
82 
83 	fyle = fopen(filename, "r");
84 	if (fyle == NULL) {
85 		return (NULL);
86 	}
87 	nameslist = NULL;
88 	while (fgets(line, sizeof (line), fyle)) {
89 		if (line[0] == '#') continue;
90 		if (line[0] == '\0') continue;
91 		if (line[0] == '\n') continue;
92 		nname = newname(line);
93 		if (nname) {
94 			if (nameslist == NULL) {
95 					nameslist = nname;
96 					end = nname;
97 			} else {
98 				end->nextname = nname;
99 				end = nname;
100 			}
101 		} else
102 			fprintf(stderr,
103 		"file %s bad malloc %s\n", filename, name);
104 	}
105 	fclose(fyle);
106 	return (nameslist);
107 }
108 
109 void
110 free_listofnames(locallist)
111 listofnames *locallist;
112 {
113 	listofnames *next = (listofnames *)NULL;
114 
115 	for (; locallist; locallist = next) {
116 		next = locallist->nextname;
117 		if (locallist->name)
118 			free(locallist->name);
119 		free((char *)locallist);
120 	}
121 }
122 
123 
124 #ifdef MAIN
125 main(argc, argv)
126 char **argv;
127 {
128 	listofnames *list;
129 	list = names(argv[1]);
130 #ifdef DEBUG
131 	print_listofnames(list);
132 #endif
133 	free_listofnames(list);
134 #ifdef DEBUG
135 	printf("Done\n");
136 #endif
137 }
138 #endif
139 
140 #ifdef DEBUG
141 void
142 print_listofnames(list)
143 listofnames *list;
144 {
145 	if (list == NULL)
146 		printf("NULL\n");
147 	for (; list; list = list->nextname)
148 		printf("%s\n", list->name);
149 }
150 #endif
151