xref: /illumos-gate/usr/src/cmd/ypcmd/getlist.c (revision 1725e09f9fca72291210d9663e76ea00fb8290bd)
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 #include <stdio.h>
36 #include <stdlib.h>
37 #include "ypsym.h"
38 
39 extern void free();
40 extern char *strdup();
41 
42 /*
43  * Add a name to the list
44  */
45 static listofnames *
46 newname(str)
47 char *str;
48 {
49 	listofnames *it;
50 	char *copy;
51 
52 	if (str == NULL)
53 		return (NULL);
54 	copy = strdup(str);
55 	if (copy == NULL)
56 		return (NULL);
57 	it = (listofnames *) malloc(sizeof (listofnames));
58 	if (it == NULL) {
59 		free(copy);
60 		return (NULL);
61 	}
62 	it->name = copy;
63 	it->nextname = NULL;
64 	return (it);
65 }
66 
67 /*
68  * Assemble the list of names
69  */
70 listofnames *
71 names(filename)
72 char *filename;
73 {
74 	listofnames *nameslist;
75 	listofnames *end;
76 	listofnames *nname;
77 	FILE *fyle;
78 	char line[256];
79 	char name[256];
80 
81 	fyle = fopen(filename, "r");
82 	if (fyle == NULL) {
83 		return (NULL);
84 	}
85 	nameslist = NULL;
86 	while (fgets(line, sizeof (line), fyle)) {
87 		if (line[0] == '#') continue;
88 		if (line[0] == '\0') continue;
89 		if (line[0] == '\n') continue;
90 		nname = newname(line);
91 		if (nname) {
92 			if (nameslist == NULL) {
93 					nameslist = nname;
94 					end = nname;
95 			} else {
96 				end->nextname = nname;
97 				end = nname;
98 			}
99 		} else
100 			fprintf(stderr,
101 		"file %s bad malloc %s\n", filename, name);
102 	}
103 	fclose(fyle);
104 	return (nameslist);
105 }
106 
107 void
108 free_listofnames(locallist)
109 listofnames *locallist;
110 {
111 	listofnames *next = (listofnames *)NULL;
112 
113 	for (; locallist; locallist = next) {
114 		next = locallist->nextname;
115 		if (locallist->name)
116 			free(locallist->name);
117 		free((char *)locallist);
118 	}
119 }
120 
121 
122 #ifdef MAIN
123 main(argc, argv)
124 char **argv;
125 {
126 	listofnames *list;
127 	list = names(argv[1]);
128 #ifdef DEBUG
129 	print_listofnames(list);
130 #endif
131 	free_listofnames(list);
132 #ifdef DEBUG
133 	printf("Done\n");
134 #endif
135 }
136 #endif
137 
138 #ifdef DEBUG
139 void
140 print_listofnames(list)
141 listofnames *list;
142 {
143 	if (list == NULL)
144 		printf("NULL\n");
145 	for (; list; list = list->nextname)
146 		printf("%s\n", list->name);
147 }
148 #endif
149