Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / csa / appt4.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: appt4.c /main/1 1996/04/21 19:21:34 drk $ */
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 /*
32  * this file contains allocate and free routines for v4 data structures
33  */
34
35 #include <EUSCompat.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "appt4.h"
40
41 #define FAUX_STRING     "Appointment"
42
43 /*
44  * forward declaration of static functions used within the file
45  */
46 static void free_tag4(Tag_4 *t);
47 static Attribute_4 *copy_attr4(Attribute_4 *a4);
48 static Tag_4 *copy_tag4(Tag_4 *t4);
49
50 /*****************************************************************************
51  * extern functions
52  *****************************************************************************/
53
54 extern Appt_4 *
55 _DtCm_make_appt4(boolean_t alloc)
56 {
57         Appt_4 *a;
58         
59         if ((a = (Appt_4 *)calloc(1, sizeof(Appt_4))) == NULL)
60                 return NULL;
61
62         if ((a->tag = (Tag_4 *)calloc(1, sizeof(Tag_4))) == NULL) {
63                 free(a);
64                 return NULL;
65         }
66         a->tag->tag = appointment_4;
67         a->tag->next = NULL;
68         a->tag->showtime = B_TRUE;
69
70         a->period.period = single_4;
71         a->appt_status = active_4;
72         a->privacy = public_4;
73
74         if (alloc) {
75                 a->what = calloc(1, sizeof(1));
76                 a->author = calloc(1, sizeof(1));
77                 a->client_data = calloc(1, sizeof(1));
78         }
79
80         a->next = NULL;
81         return(a);
82 }
83
84 extern Appt_4 *
85 _DtCm_copy_one_appt4(Appt_4 *a4)
86 {
87         Appt_4 *ptr, *copy;
88
89         if (a4 == NULL)
90                 return NULL;
91
92         ptr = a4->next;
93         a4->next = NULL;
94
95         copy = _DtCm_copy_appt4(a4);
96
97         a4->next = ptr;
98         return (copy);
99 }
100
101 extern Appt_4 *
102 _DtCm_copy_appt4(Appt_4 *a4)
103 {
104         Appt_4          *a, *head, *prev;
105         boolean_t       cleanup = B_FALSE;
106
107         prev = head = NULL;
108         while (a4 != NULL) {
109                 if ((a = (Appt_4 *)calloc(1, sizeof(Appt_4))) == NULL) {
110                         cleanup = B_TRUE;
111                         break;
112                 }
113
114                 a->appt_id.tick = a4->appt_id.tick;
115                 a->appt_id.key = a4->appt_id.key;
116
117                 a->duration = a4->duration;
118                 a->ntimes = a4->ntimes;
119
120                 if (a4->what) {
121                         if ((a->what = strdup(a4->what)) == NULL) {
122                                 _DtCm_free_appt4(a);
123                                 cleanup = B_TRUE;
124                                 break;
125                         }
126                 } else if ((a->what = (char *)calloc(1, 1)) == NULL) {
127                         _DtCm_free_appt4(a);
128                         cleanup = B_TRUE;
129                         break;
130                 }
131
132                 a->period.period = a4->period.period;
133                 a->period.nth = a4->period.nth;
134                 a->period.enddate = a4->period.enddate;
135
136                 if (a4->author) {
137                         if ((a->author = strdup(a4->author)) == NULL) {
138                                 _DtCm_free_appt4(a);
139                                 cleanup = B_TRUE;
140                                 break;
141                         }
142                 } else if ((a->author = (char *)calloc(1, 1)) == NULL) {
143                         _DtCm_free_appt4(a);
144                         cleanup = B_TRUE;
145                         break;
146                 }
147
148                 if (a4->client_data) {
149                         if ((a->client_data = strdup(a4->client_data)) == NULL) {
150                                 _DtCm_free_appt4(a);
151                                 cleanup = B_TRUE;
152                                 break;
153                         }
154                 } else if ((a->client_data = (char *)calloc(1, 1)) == NULL) {
155                         _DtCm_free_appt4(a);
156                         cleanup = B_TRUE;
157                         break;
158                 }
159
160                 if (a4->exception &&
161                     (a->exception = _DtCm_copy_excpt4(a4->exception)) == NULL) {
162                         _DtCm_free_appt4(a);
163                         cleanup = B_TRUE;
164                         break;
165                 }
166
167                 if (a4->attr && (a->attr = copy_attr4(a4->attr)) == NULL) {
168                         _DtCm_free_appt4(a);
169                         cleanup = B_TRUE;
170                         break;
171                 }
172
173                 if (a4->tag) {
174                         if ((a->tag = copy_tag4(a4->tag)) == NULL) {
175                                 free(a);
176                                 cleanup = B_TRUE;
177                                 break;
178                         }
179                 } else if ((a->tag = (Tag_4 *)calloc(1, sizeof(Tag_4))) == NULL) {
180                         free(a);
181                         cleanup = B_TRUE;
182                         break;
183                 } else {
184                         a->tag->tag = appointment_4;
185                         a->tag->showtime = B_TRUE;
186                         a->tag->next = NULL;
187                 }
188
189                 a->appt_status = a4->appt_status;
190                 a->privacy = a4->privacy;
191                 a->next = NULL;
192
193                 if (head == NULL)
194                         head = a;
195                 else
196                         prev->next = a;
197                 prev = a;
198
199                 a4 = a4->next;
200         }
201
202         if (cleanup == B_TRUE) {
203                 _DtCm_free_appt4(head);
204                 head = NULL;
205         }
206
207         return (head);
208 }
209
210 /*
211  * Copy one appointment.
212  * The what field is set to "Appointment".
213  */
214 extern Appt_4 *
215 _DtCm_copy_semiprivate_appt4(Appt_4 *original)
216 {
217         Appt_4 *temp, *copy = NULL;
218
219         if (original != NULL) {
220                 temp = original->next;
221                 original->next = NULL;
222
223                 if ((copy = _DtCm_copy_appt4(original)) != NULL) {
224                         if (copy->what != NULL)
225                                 free(copy->what);
226                         if ((copy->what = (char *)strdup(FAUX_STRING)) == NULL)
227                         {
228                                 _DtCm_free_appt4(copy);
229                                 copy = NULL;
230                         }
231                 }
232
233                 original->next = temp;
234         }
235         return(copy);
236 }
237
238 extern Abb_Appt_4 *
239 _DtCm_appt_to_abbrev4(Appt_4 *original)
240 {
241         Abb_Appt_4 *new = NULL;
242
243         if (original != NULL) {
244                 if ((new = (Abb_Appt_4 *)calloc(1, sizeof(Abb_Appt_4))) == NULL)
245                         return NULL;
246
247                 if ((new->tag = copy_tag4(original->tag)) == NULL) {
248                         free(new);
249                         return NULL;
250                 }
251
252                 new->appt_id.tick = original->appt_id.tick;
253                 new->appt_id.key = original->appt_id.key;
254
255                 if ((new->what = (char *)strdup(original->what)) == NULL) {
256                         _DtCm_free_abbrev_appt4(new);
257                         return NULL;
258                 }
259
260                 new->duration = original->duration;
261                 new->period.period = original->period.period;
262                 new->period.nth = original->period.nth;
263                 new->period.enddate = original->period.enddate;
264
265                 new->appt_status = original->appt_status;
266                 new->privacy = original->privacy;
267                 new->next=NULL;
268         }
269         return(new);
270 }
271
272 /*
273  * The what field is set to "Appointment".
274  */
275 extern Abb_Appt_4 *
276 _DtCm_appt_to_semiprivate_abbrev4(Appt_4 *original)
277 {
278         Abb_Appt_4 *new = _DtCm_appt_to_abbrev4(original);
279         if (new != NULL) {
280                 if (new->what != NULL)
281                         free(new->what);
282                 if ((new->what = strdup(FAUX_STRING)) == NULL) {
283                         _DtCm_free_abbrev_appt4(new);
284                         new = NULL;
285                 }
286         }
287         return(new);
288 }
289
290 extern void
291 _DtCm_free_appt4(Appt_4 *a)
292 {
293         Appt_4 *ptr;
294
295         while (a != NULL) {
296                 ptr = a->next;
297
298                 if (a->what != NULL)
299                         free(a->what);
300
301                 if (a->author != NULL)
302                         free(a->author);
303
304                 if (a->tag != NULL)
305                         free_tag4(a->tag);
306
307                 if (a->attr != NULL)
308                         _DtCm_free_attr4(a->attr);
309
310                 if (a->exception != NULL)
311                         _DtCm_free_excpt4(a->exception);
312
313                 free(a);
314
315                 a = ptr;
316         }
317 }
318
319 extern void
320 _DtCm_free_abbrev_appt4(Abb_Appt_4 *a)
321 {
322         Abb_Appt_4 *ptr;
323
324         while (a != NULL) {
325                 ptr = a->next;
326
327                 if (a->what != NULL) {
328                         free(a->what);
329                 }
330
331                 if (a->tag != NULL) {
332                         free_tag4(a->tag);
333                 }
334
335                 free(a);
336
337                 a = ptr;
338         }
339 }
340
341 Attribute_4 *
342 _DtCm_make_attr4()
343 {
344         Attribute_4 *a;
345         
346         if ((a = (Attribute_4 *)calloc(1, sizeof(Attribute_4))) == NULL)
347                 return NULL;
348
349         if ((a->attr = (char *)calloc(1, 1)) == NULL) {
350                 free(a);
351                 return NULL;
352         }
353
354         if ((a->value = (char *)calloc(1, 1)) == NULL) {
355                 _DtCm_free_attr4(a);
356                 return NULL;
357         }
358
359         if ((a->clientdata = (char *)calloc(1, 1)) == NULL) {
360                 _DtCm_free_attr4(a);
361                 return NULL;
362         }
363
364         return(a);
365 }
366
367 extern void
368 _DtCm_free_attr4(Attribute_4 *a)
369 {
370         Attribute_4 *ptr;
371
372         while (a != NULL) {
373                 ptr = a->next;
374                 if (a->attr != NULL)
375                         free(a->attr);
376                 if (a->value != NULL)
377                         free(a->value);
378                 if (a->clientdata != NULL)
379                         free(a->clientdata);
380                 free(a);
381
382                 a = ptr;
383         }
384 }
385
386 extern Reminder_4 *
387 _DtCm_copy_reminder4(Reminder_4 *r4)
388 {
389         Reminder_4 *rem, *head, *prev;
390
391         prev = head = NULL;
392         while (r4 != NULL) {
393                 rem = (Reminder_4 *)calloc(1, sizeof(Reminder_4));
394                 rem->appt_id = r4->appt_id;
395                 rem->tick = r4->tick;
396                 rem->attr.next = NULL;
397                 rem->attr.attr = strdup(r4->attr.attr);
398                 rem->attr.value = strdup(r4->attr.value);
399                 rem->attr.clientdata = strdup(r4->attr.clientdata);
400                 rem->next = NULL;
401
402                 if (head == NULL)
403                         head = rem;
404                 else
405                         prev->next = rem;
406                 prev = rem;
407
408                 r4 = r4->next;
409         }
410         return(head);
411 }
412       
413 extern void
414 _DtCm_free_reminder4(Reminder_4 *r)
415 {
416         Reminder_4 *ptr;
417
418         while (r != NULL) {
419                 ptr = r->next;
420
421                 if (r->attr.attr != NULL)
422                         free (r->attr.attr);
423                 if (r->attr.value != NULL)
424                         free (r->attr.value);
425                 if (r->attr.clientdata != NULL)
426                         free (r->attr.clientdata);
427                 free (r);
428
429                 r = ptr;
430         }
431 }
432
433 extern void
434 _DtCm_free_keyentry4(Uid_4 *k)
435 {
436         Uid_4 *ptr;
437
438         while (k != NULL) {
439                 ptr = k->next;
440                 free(k);
441                 k = ptr;
442         }
443 }
444
445 extern Access_Entry_4 *
446 _DtCm_make_access_entry4(char *who, int perms)
447 {
448         Access_Entry_4 *e;
449
450         if (who==NULL) return((Access_Entry_4 *)NULL);
451         if ((e = (Access_Entry_4 *)calloc(1, sizeof(Access_Entry_4))) == NULL)
452                 return NULL;
453
454         if ((e->who = strdup(who)) == NULL) {
455                 free(e);
456                 return NULL;
457         }
458
459         e->access_type = perms;
460         e->next = NULL;
461         return(e);
462 }
463
464 extern void
465 _DtCm_free_access_list4(Access_Entry_4 *e)
466 {
467         Access_Entry_4 *ptr;
468
469         while (e != NULL) {
470                 ptr = e->next;
471
472                 if (e->who != NULL)
473                         free(e->who);
474                 free(e);
475
476                 e = ptr;
477         }
478 }
479
480 extern void
481 _DtCm_free_excpt4(Except_4 *e)
482 {
483         Except_4 *ptr;
484
485         while (e != NULL) {
486                 ptr = e->next;
487                 free(e);
488                 e = ptr;
489         }
490 }
491
492 extern Except_4 *
493 _DtCm_copy_excpt4(Except_4 *e4)
494 {
495         Except_4        *e, *head, *prev;
496         boolean_t       cleanup = B_FALSE;
497
498         prev = head = NULL;
499         while (e4 != NULL) {
500                 if ((e = (Except_4 *)calloc(1, sizeof(Except_4))) == NULL) {
501                         cleanup = B_TRUE;
502                         break;
503                 }
504
505                 e->ordinal = e4->ordinal;
506                 e->next = NULL;
507
508                 if (head == NULL)
509                         head = e;
510                 else
511                         prev->next = e;
512                 prev = e;
513
514                 e4 = e4->next;
515         }
516
517         if (cleanup == B_TRUE) {
518                 _DtCm_free_excpt4(head);
519                 head = NULL;
520         }
521
522         return (head);
523 }
524
525 /******************************************************************************
526  * static functions used within the file
527  ******************************************************************************/
528
529 static void
530 free_tag4(Tag_4 *t)
531 {
532         Tag_4 *ptr;
533
534         while (t != NULL) {
535                 ptr = t->next;
536                 free(t);
537                 t = ptr;
538         }
539 }
540
541 /*      Copy an attribute list recursively */
542 static Attribute_4 *
543 copy_attr4(Attribute_4 *a4)
544 {
545         Attribute_4     *a, *head, *prev;
546         boolean_t       cleanup = B_FALSE;
547
548         prev = head = NULL;
549         while (a4 != NULL) {
550                 if ((a = (Attribute_4 *)calloc(1, sizeof(Attribute_4)))
551                     == NULL) {
552                         cleanup = B_TRUE;
553                         break;
554                 }
555
556                 if ((a->attr = strdup(a4->attr)) == NULL) {
557                         free(a);
558                         cleanup = B_TRUE;
559                         break;
560                 }
561         
562                 if ((a->value = strdup(a4->value)) == NULL) {
563                         _DtCm_free_attr4(a);
564                         cleanup = B_TRUE;
565                         break;
566                 }
567
568                 if ((a->clientdata = strdup(a4->clientdata)) == NULL) {
569                         _DtCm_free_attr4(a);
570                         cleanup = B_TRUE;
571                         break;
572                 }
573
574                 if (head == NULL)
575                         head = a;
576                 else
577                         prev->next = a;
578                 prev = a;
579
580                 a4 = a4->next;
581         }
582
583         if (cleanup == B_TRUE) {
584                 _DtCm_free_attr4(head);
585                 head = NULL;
586         }
587
588         return (head);
589 }
590
591 static Tag_4 *
592 copy_tag4(Tag_4 *t4)
593 {
594         Tag_4           *t, *head, *prev;
595         boolean_t       cleanup = B_FALSE;
596
597         prev = head = NULL;
598         while (t4 != NULL) {
599                 if ((t = (Tag_4 *)calloc(1, sizeof(Tag_4))) == NULL) {
600                         cleanup = B_TRUE;
601                         break;
602                 }
603
604                 t->tag = t4->tag;
605                 t->showtime = t4->showtime;
606                 t->next = NULL;
607
608                 if (head == NULL)
609                         head = t;
610                 else
611                         prev->next = t;
612                 prev = t;
613
614                 t4 = t4->next;
615         }
616
617         if (cleanup == B_TRUE) {
618                 free_tag4(head);
619                 head = NULL;
620         }
621
622         return (head);
623 }
624