Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtcm / server / cmsentry.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: cmsentry.c /main/4 1995/11/09 12:41:44 rswiston $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include <EUSCompat.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include "cmsentry.h"
36 #include "cmsdata.h"
37 #include "nametbl.h"
38 #include "attr.h"
39
40 /******************************************************************************
41  * forward declaration of static functions used within the file
42  ******************************************************************************/
43 static CSA_return_code
44 _ExtractEntryAttrsFromEntry(uint srcsize, cms_attribute *srcattrs,
45         uint *dstsize, cms_attribute **dstattrs, boolean_t time_only);
46
47 /*****************************************************************************
48  * extern functions
49  *****************************************************************************/
50
51 /*
52  * Given a hashed name table, initialize a new cms_entry with
53  * the given list of attribute values
54  */
55 extern CSA_return_code
56 _DtCmsMakeHashedEntry(
57         _DtCmsCalendar  *cal,
58         uint            num,
59         cms_attribute   *attrs,
60         cms_entry       **entry)
61 {
62         int             i, index;
63         cms_entry       *eptr;
64         CSA_return_code stat = CSA_SUCCESS;
65
66         if (cal == NULL || entry == NULL)
67                 return (CSA_E_INVALID_PARAMETER);
68
69         if ((eptr = _DtCm_make_cms_entry(cal->entry_tbl)) == NULL)
70                 return (CSA_E_INSUFFICIENT_MEMORY);
71
72         for (i = 0; i < num && stat == CSA_SUCCESS; i++) {
73                 index = _DtCm_get_index_from_table(cal->entry_tbl,
74                         attrs[i].name.name);
75
76                 if (index > 0) {
77                         /* check type */
78                         if (index > _DtCM_DEFINED_ENTRY_ATTR_SIZE &&
79                             attrs[i].value &&
80                             attrs[i].value->type != cal->types[index])
81                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
82                         else
83                                 stat = _DtCm_copy_cms_attr_val(attrs[i].value,
84                                         &eptr->attrs[index].value);
85                 } else if (attrs[i].value) {
86                         if ((stat = _DtCmExtendNameTable(attrs[i].name.name, 0,
87                             attrs[i].value->type, _DtCm_entry_name_tbl,
88                             _DtCM_DEFINED_ENTRY_ATTR_SIZE,
89                             _CSA_entry_attribute_names, &cal->entry_tbl,
90                             &cal->types)) == CSA_SUCCESS) {
91
92                                 attrs[i].name.num = cal->entry_tbl->size;
93
94                                 stat = _DtCmGrowAttrArray(&eptr->num_attrs,
95                                         &eptr->attrs, &attrs[i]);
96                         }
97                 }
98         }
99
100         if (stat != CSA_SUCCESS) {
101                 _DtCm_free_cms_entry(eptr);
102                 return (stat);
103         }
104
105         *entry = eptr;
106         return (CSA_SUCCESS);
107 }
108
109 extern void
110 _DtCmsFreeEntryAttrResItem(cms_get_entry_attr_res_item *elist)
111 {
112         cms_get_entry_attr_res_item *ptr;
113
114         while (elist) {
115                 ptr = elist->next;
116
117                 _DtCm_free_cms_attributes(elist->num_attrs, elist->attrs);
118                 free(elist);
119
120                 elist = ptr;
121         }
122 }
123
124 extern CSA_return_code
125 _DtCmsGetCmsEntryForClient(cms_entry *e, cms_entry **e_r, boolean_t time_only)
126 {
127         cms_entry *ptr;
128         CSA_return_code stat;
129
130         if (e == NULL || e_r == NULL)
131                 return (CSA_E_INVALID_PARAMETER);
132
133         if ((ptr = (cms_entry *)calloc(1, sizeof(cms_entry))) == NULL)
134                 return (CSA_E_INSUFFICIENT_MEMORY);
135
136         if ((stat = _ExtractEntryAttrsFromEntry(e->num_attrs, e->attrs,
137             &ptr->num_attrs, &ptr->attrs,time_only)) != CSA_SUCCESS) {
138                 free(ptr);
139                 return (stat);
140         } else {
141                 ptr->key = e->key;
142                 *e_r = ptr;
143                 return (CSA_SUCCESS);
144         }
145 }
146
147 /*****************************************************************************
148  * static functions used within the file
149  *****************************************************************************/
150
151 static CSA_return_code
152 _ExtractEntryAttrsFromEntry(
153         uint            srcsize,
154         cms_attribute   *srcattrs,
155         uint            *dstsize,
156         cms_attribute   **dstattrs,
157         boolean_t       time_only)
158 {
159         CSA_return_code stat;
160         int             i, j;
161         cms_attribute   *attrs;
162
163         if (dstsize == NULL || dstattrs == NULL)
164                 return (CSA_E_INVALID_PARAMETER);
165
166         *dstsize = 0;
167         *dstattrs = NULL;
168
169         if (srcsize == 0)
170                 return (CSA_SUCCESS);
171
172         if ((attrs = calloc(1, sizeof(cms_attribute) * srcsize)) == NULL)
173                 return (CSA_E_INSUFFICIENT_MEMORY);
174
175         for (i = 0, j = 0; i <= srcsize; i++) {
176                 if (srcattrs[i].value == NULL)
177                         continue;
178
179                 if ((stat = _DtCm_copy_cms_attribute(&attrs[j], &srcattrs[i],
180                     B_TRUE)) != CSA_SUCCESS)
181                         break;
182                 else {
183                         if ( (i == CSA_ENTRY_ATTR_SUMMARY_I) && (time_only == B_TRUE) ) {
184                                 if (attrs[j].value && attrs[j].value->item.string_value)
185                                         attrs[j].value->item.string_value[0] = '\0';
186                         }
187
188                         j++;
189                 }
190         }
191
192         if (stat == CSA_SUCCESS && j > 0) {
193                 *dstsize = j;
194                 *dstattrs = attrs;
195         } else {
196                 _DtCm_free_cms_attributes(j, attrs);
197                 free(attrs);
198         }
199
200         return (stat);
201 }
202
203