dtcm: Resolve CID 87408
[oweals/cde.git] / cde / programs / dtcm / dtcm / fns.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 /*******************************************************************************
24 **
25 **  cmfns.c
26 **
27 **  $XConsortium: fns.c /main/3 1995/11/03 10:26:30 rswiston $
28 **
29 **  RESTRICTED CONFIDENTIAL INFORMATION:
30 **
31 **  The information in this document is subject to special
32 **  restrictions in a confidential disclosure agreement between
33 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
35 **  Sun's specific written approval.  This document and all copies
36 **  and derivative works thereof must be returned or destroyed at
37 **  Sun's request.
38 **
39 **  Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40 **
41 *******************************************************************************/
42
43 /*                                                                      *
44  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
45  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
46  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
47  * (c) Copyright 1993, 1994 Novell, Inc.                                *
48  */
49
50
51 #ifndef lint
52 static  char sccsid[] = "@(#)fns.c 1.3 94/11/07 Copyr 1993 Sun Microsystems, Inc.";
53 #endif
54
55 /*
56  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved
57  */
58
59 #ifdef FNS
60
61 #include <EUSCompat.h>
62 #include <sys/stat.h>
63 #include <unistd.h>
64 #include <stdio.h>
65 #include <fcntl.h>
66 #include <string.h>
67 #include <stdlib.h>
68 #ifdef POSIX_THREADS
69 #include <thread.h>
70 #endif
71
72 #include "debug.h"
73 #include "calendar.h"
74 #include "props.h"
75 #include "misc.h"
76 #include "dtfns.h"
77 #include "cmfns.h"
78
79 extern char *strdup(const char *);
80 extern FILE     *fdopen(int, const char *);
81
82 #ifdef POSIX_THREADS
83 /* Data used by registration thread */
84 typedef struct _Registration_data  {
85         char    *username;
86         char    *location;
87 } Registration_data;
88
89 static void *registration_thr(void *data);
90 #endif
91 static int  register_calendar(const char *username, const char *location);
92
93 /*
94  * Register a calendar location in FNS.
95  */
96 int
97 cm_register_calendar(const char *username, const char *location)
98
99 {
100 #ifdef POSIX_THREADS
101         int error;
102         Registration_data       *data;
103
104         data = (Registration_data *)malloc(sizeof(Registration_data));
105
106         if (data == NULL) {
107                 return -1;
108         }
109
110         data->username = strdup(username);
111         data->location = strdup(location);
112
113         error = thr_create(NULL, 0, registration_thr, data, THR_DETACHED, NULL);
114
115         if (error > 0) {
116                 printf("Could not create registration thread\n");
117                 return -1;
118         } else {
119                 return 0;
120         }
121 #else
122         return cmfns_register_calendar(username, location);
123 #endif
124 }
125
126 #ifdef POSIX_THREADS
127 static void *
128 registration_thr(void *data)
129
130 {
131         Registration_data *reg_data; 
132
133         DP(("registration_thr: THREAD ENTER\n"));
134         if (data == NULL) {
135                 return (void *)-1;
136         }
137
138         reg_data = (Registration_data *)data;
139
140         DP(("registration_thr: Registering calendar\n"));
141         cmfns_register_calendar(reg_data->username, reg_data->location);
142
143         DP(("registration_thr: Registration complete. Freeing data\n"));
144         free(reg_data->username);
145         free(reg_data->location);
146         free(reg_data);
147         DP(("registration_thr: THREAD EXIT\n"));
148         return (void *)1;
149 }
150 #endif
151
152 #ifdef FNS_DEMO
153
154 /*
155  * Extract the FNS name out of a file or directory. In the case of a
156  * file it should have the format of
157  *
158  *      # Zero or more
159  *      # comments
160  *      fns:name:for:the:fns:object
161  *
162  * In the case of a directory this routine looks for a file called
163  * ".FNSName" in the directory specified.  This file should have the
164  * format described above.
165  *
166  * Caller is responsible for providing the buffer to place the name in.
167  *
168  *      Returns
169  *              -1      Internal error or corrupted file
170  *              0       Could not access file/directory
171  *              1       Success.  'name' contains the FNS name
172  */
173 int
174 cmfns_name_from_file(
175         const char      *path,  /* Path to file to get name from */
176         char            *name,  /* Buffer to put name in */
177         int             len)    /* Length of buffer */
178
179 {
180         int     fd;
181         FILE    *fp;
182         char    *p;
183         struct stat     statb;
184
185         if ((fd = open(path, O_RDONLY)) < 0) {
186                 return 0;
187         }
188
189         if (fstat(fd, &statb) < 0) {
190                 close(fd);
191                 return 0;
192         }
193
194         if (S_ISDIR(statb.st_mode)) {
195                 char    *newpath;
196
197                 /* path points to a directory.  Look for .FNSName file */
198                 close(fd);
199                 newpath = (char *)malloc(strlen(path) + strlen(FNS_FILE) + 2);
200                 if (newpath == NULL) {
201                         return -1;
202                 }
203                 sprintf(newpath, "%s/%s", path, FNS_FILE);
204                 if ((fp = fopen(newpath, "r")) == NULL) {
205                         free(newpath);
206                         return 0;
207                 }
208                 free(newpath);
209         } else {
210                 if ((fp = fdopen(fd, "r")) == NULL) {
211                         return 0;
212                 }
213         }
214
215         do {
216                 /* Get line from file */
217                 if (fgets(name, len, fp) == NULL) {
218                         fclose(fp);
219                         return -1;
220                 }
221
222                 /* Make sure we got a full line */
223                 p = strrchr(name, '\n');
224
225                 /*
226                  * If it is a comment line or we got a partial line
227                  * try again
228                  */
229         } while (*name == '#' || p == NULL);
230
231         /* Should have the name.  Wipe out newline */
232         *p = '\0';
233
234         fclose(fp);
235         return 1;
236 }
237 #endif /* FNS_DEMO */
238
239 #endif /* FNS */