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