summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libuuconf/addstr.c
blob: f95d528fddc9c7383465c380613266c15827cf51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* addstr.c
   Add a string to a list of strings.

   Copyright (C) 1992 Ian Lance Taylor

   This file is part of the Taylor UUCP uuconf library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucnfi.h"

#if USE_RCS_ID
const char _uuconf_addstr_rcsid[] = "$FreeBSD$";
#endif

#include <errno.h>

/* When setting system information, we need to be able to distinguish
   between a value that is not set and a value that has been set to
   NULL.  We do this by initializing the value to point to the
   variable _uuconf_unset, and then correcting it in the function
   _uuconf_isystem_basic_default.  This variable is declared in this
   file because some linkers will apparently not pull in an object
   file which merely declarates a variable.  This functions happens to
   be pulled in by almost everything.  */

char *_uuconf_unset;

/* Add a string to a list of strings.  The list is maintained as an
   array of elements ending in NULL.  The total number of available
   slots is always a multiple of CSLOTS, so by counting the current
   number of elements we can tell whether a new slot is needed.  If
   the fcopy argument is TRUE, the new string is duplicated into
   memory.  If the fcheck argument is TRUE, this does not add a string
   that is already in the list.  The pblock argument may be used to do
   the allocations within a memory block.  This returns a standard
   uuconf error code.  */

#define CSLOTS (8)

int
_uuconf_iadd_string (qglobal, zadd, fcopy, fcheck, ppzstrings, pblock)
     struct sglobal *qglobal;
     char *zadd;
     boolean fcopy;
     boolean fcheck;
     char ***ppzstrings;
     pointer pblock;
{
  char **pz;
  size_t c;

  if (fcheck && *ppzstrings != NULL)
    {
      for (pz = *ppzstrings; *pz != NULL; pz++)
	if (strcmp (zadd, *pz) == 0)
	  return UUCONF_SUCCESS;
    }

  if (fcopy)
    {
      size_t clen;
      char *znew;

      clen = strlen (zadd) + 1;
      znew = (char *) uuconf_malloc (pblock, clen);
      if (znew == NULL)
	{
	  if (qglobal != NULL)
	    qglobal->ierrno = errno;
	  return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	}
      memcpy ((pointer) znew, (pointer) zadd, clen);
      zadd = znew;
    }

  pz = *ppzstrings;
  if (pz == NULL || pz == (char **) &_uuconf_unset)
    {
      pz = (char **) uuconf_malloc (pblock, CSLOTS * sizeof (char *));
      if (pz == NULL)
	{
	  if (qglobal != NULL)
	    qglobal->ierrno = errno;
	  return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	}
      *ppzstrings = pz;
    }
  else
    {
      c = 0;
      while (*pz != NULL)
	{
	  ++pz;
	  ++c;
	}

      if ((c + 1) % CSLOTS == 0)
	{
	  char **pznew;

	  pznew = (char **) uuconf_malloc (pblock,
					   ((c + 1 + CSLOTS)
					    * sizeof (char *)));
	  if (pznew == NULL)
	    {
	      if (qglobal != NULL)
		qglobal->ierrno = errno;
	      return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	    }
	  memcpy ((pointer) pznew, (pointer) *ppzstrings,
		  c * sizeof (char *));
	  uuconf_free (pblock, *ppzstrings);
	  *ppzstrings = pznew;
	  pz = pznew + c;
	}
    }

  pz[0] = zadd;
  pz[1] = NULL;

  return UUCONF_SUCCESS;
}
OpenPOWER on IntegriCloud