1 /* hsnams.c
2    Get all known system names from the HDB configuration files.
3 
4    Copyright (C) 1992 Ian Lance Taylor
5 
6    This file is part of the Taylor UUCP uuconf library.
7 
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Library General Public License
10    as published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12 
13    This library is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Library General Public License for more details.
17 
18    You should have received a copy of the GNU Library General Public
19    License along with this library; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21 
22    The author of the program may be contacted at ian@airs.com.
23    */
24 
25 #include "uucnfi.h"
26 
27 #if USE_RCS_ID
28 const char _uuconf_hsnams_rcsid[] = "$FreeBSD$";
29 #endif
30 
31 #include <errno.h>
32 #include <ctype.h>
33 
34 /* Get all the system names from the HDB Systems file.  We have to
35    read the Permissions file in order to support aliases.  */
36 
37 int
uuconf_hdb_system_names(pglobal,ppzsystems,falias)38 uuconf_hdb_system_names (pglobal, ppzsystems, falias)
39      pointer pglobal;
40      char ***ppzsystems;
41      int falias;
42 {
43   struct sglobal *qglobal = (struct sglobal *) pglobal;
44   int iret;
45   char *zline;
46   size_t cline;
47   char **pz;
48 
49   *ppzsystems = NULL;
50 
51   iret = UUCONF_SUCCESS;
52 
53   zline = NULL;
54   cline = 0;
55 
56   for (pz = qglobal->qprocess->pzhdb_systems; *pz != NULL; pz++)
57     {
58       FILE *e;
59 
60       e = fopen (*pz, "r");
61       if (e == NULL)
62 	{
63 	  if (FNO_SUCH_FILE ())
64 	    continue;
65 	  qglobal->ierrno = errno;
66 	  iret = UUCONF_FOPEN_FAILED | UUCONF_ERROR_ERRNO;
67 	  break;
68 	}
69 
70       qglobal->ilineno = 0;
71 
72       while (_uuconf_getline (qglobal, &zline, &cline, e) > 0)
73 	{
74 	  ++qglobal->ilineno;
75 
76 	  /* Lines beginning with whitespace are treated as comments.
77 	     No system name can contain a '#', which is another
78 	     comment character, so eliminating the first '#' does no
79 	     harm and catches comments.  */
80 	  zline[strcspn (zline, " \t#\n")] = '\0';
81 	  if (*zline == '\0')
82 	    continue;
83 
84 	  iret = _uuconf_iadd_string (qglobal, zline, TRUE, TRUE,
85 				      ppzsystems, (pointer) NULL);
86 	  if (iret != UUCONF_SUCCESS)
87 	    {
88 	      iret |= UUCONF_ERROR_LINENO;
89 	      break;
90 	    }
91 	}
92 
93       (void) fclose (e);
94     }
95 
96   if (zline != NULL)
97     free ((pointer) zline);
98 
99   if (iret != UUCONF_SUCCESS)
100     {
101       qglobal->zfilename = *pz;
102       return iret | UUCONF_ERROR_FILENAME;
103     }
104 
105   /* If we are supposed to return aliases, we must read the
106      Permissions file.  */
107   if (falias)
108     {
109       struct shpermissions *q;
110 
111       if (! qglobal->qprocess->fhdb_read_permissions)
112 	{
113 	  iret = _uuconf_ihread_permissions (qglobal);
114 	  if (iret != UUCONF_SUCCESS)
115 	    return iret;
116 	}
117 
118       for (q = qglobal->qprocess->qhdb_permissions;
119 	   q != NULL;
120 	   q = q->qnext)
121 	{
122 	  pz = q->pzalias;
123 	  if (pz == NULL || pz == (char **) &_uuconf_unset)
124 	    continue;
125 
126 	  for (; *pz != NULL; pz++)
127 	    {
128 	      iret = _uuconf_iadd_string (qglobal, *pz, TRUE, TRUE,
129 					  ppzsystems, (pointer) NULL);
130 	      if (iret != UUCONF_SUCCESS)
131 		return iret;
132 	    }
133 	}
134     }
135 
136   if (*ppzsystems == NULL)
137     iret = _uuconf_iadd_string (qglobal, (char *) NULL, FALSE, FALSE,
138 				ppzsystems, (pointer) NULL);
139 
140   return iret;
141 }
142