Link with C++ linker
[oweals/cde.git] / cde / programs / dtfile / Utils.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: Utils.c /main/6 1996/10/09 11:44:33 mustafa $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  *
27  *   FILE:           Utils.c
28  *
29  *   COMPONENT_NAME: Desktop File Manager (dtfile)
30  *
31  *   Description:    Contains utility routines.
32  *
33  *   FUNCTIONS: ResolveLocalPathName
34  *              ResolveTranslationString
35  *
36  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
37  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
38  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
39  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
40  *
41  ****************************************************************************
42  ************************************<+>*************************************/
43
44 #include <Dt/Connect.h>
45 #include <Tt/tttk.h>
46
47
48 /***********************************************************************
49 * FUNCTION NAME: ResolveLocalPathName
50
51 * PURPOSE: This function takes the given parameters and returns a path
52 *          (owned by the caller) suitable for passing to open(2). It
53 *          takes the internal cannonical file name kept by the File
54 *          Manager and remaps them into the standard CDE file name
55 *          mapping calls (tt_host_file_netfile, tt_netfile_file). The
56 *          returned pathname is valid only on the local host. See
57 *          ResolveRemotePathName if you need a pathname which is valid
58 *          on a different host.
59 *
60 * SYNOPSIS: path = ResolveLocalPathName(hostname, directory_name,  
61 *                                       file_name,local_hostname,
62 *                                       tt_status)
63 *
64 *       char *path;           The returned pathname. The memory is owned
65 *                             by the caller. NULL is returned on failure.
66 *  
67 *       char *hostname;       The host where the file is located.
68 *
69 *       char *directory_name; The directory portion of the pathname;
70 *
71 *       char *file_name;      The name of the file.          
72 *
73 *       char *local_hostname; The current host.
74 *
75 *       Tt_status *tt_status; Upon error, the tool talk error status will
76 *                             be returned for use by the caller. Use
77 *                             tt_status_message to get a printable string.
78
79 ********************************************************************************/
80
81 char *
82 ResolveLocalPathName(
83                      char *hostname,
84                      char *directory_name,
85                      char *file_name,
86                      char *local_hostname,
87                      Tt_status *tt_status)
88 {
89   char * fully_qualified_name;
90   char * cannon_name;
91   char * path;
92   char * tmp;
93   int len = strlen( directory_name );
94
95
96 #ifdef DEBUG
97   printf ("Home host name is %s  actual hostname is %s\n",
98           local_hostname, hostname);
99 #endif
100
101   /* construct full qualified filename
102   */
103   if( file_name )
104   {
105     if( len == 1 && *directory_name == '/' )
106     {
107       fully_qualified_name = (char *)XtCalloc ( 1, ( strlen (file_name) + 2 ) );
108       sprintf( fully_qualified_name, "/%s", file_name );
109     }
110     else
111     {
112       fully_qualified_name = (char *)XtCalloc ( 1, ( len + strlen (file_name) + 2 ) );
113       sprintf( fully_qualified_name, "%s/%s", directory_name, file_name );
114     }
115   }
116   else
117   {
118     if( len > 0 )
119     {
120       fully_qualified_name = (char *)XtMalloc ( len + 1 );
121       strcpy( fully_qualified_name, directory_name );
122     }
123     else
124       fully_qualified_name = NULL;
125   }
126
127   *tt_status=TT_OK;
128
129 #if defined(FILE_MAP_OPTIMIZE)
130   /* check if local host name = hostname  */
131   /* Do not do a tooltalk call if that is */ 
132   /* the case                             */
133   if (strcmp(hostname, local_hostname) == 0 )
134   {
135 #ifdef DEBUG
136     printf ("fully qualified name is %s\n", path);
137 #endif
138     return (fully_qualified_name);    
139   }
140
141   /* not on local host, make an rpc trip */
142 #endif
143
144   /* What about no hostname, then just send the file */
145   if (!hostname || (hostname[0] == '\0'))
146      return fully_qualified_name;
147
148   /* convert to network canonical name
149   */
150   cannon_name = (char *)tt_host_file_netfile (hostname, fully_qualified_name);
151   XtFree (fully_qualified_name);
152   if ( (*tt_status = tt_pointer_error(cannon_name)) != TT_OK)
153   {
154 #ifdef DEBUG
155     printf( "Tooltalk error message: %s\n", tt_status_message(*tt_status));
156 #endif
157     return (NULL);
158   }
159
160
161   /* resolve canonical name on local host
162   */
163   tmp = (char *) tt_netfile_file(cannon_name);
164   tt_free (cannon_name);
165
166   if ( (*tt_status = tt_pointer_error (tmp)) != TT_OK)
167   {
168 #ifdef DEBUG
169     printf( "Tooltalk error message: %s\n", tt_status_message(*tt_status));
170 #endif
171     return (NULL);
172   }
173
174   path = XtNewString( tmp );
175   tt_free( tmp );
176
177   return (path);
178 }
179
180
181
182 char *
183 ResolveTranslationString( char * originalString,
184                           char * address )
185
186 {
187   char   addressStr[20];
188   char * resolvedString = NULL;
189   int    i, j, k, total, length;
190
191 #ifdef __osf__
192   sprintf( addressStr, "%lx", address );
193 #else
194   sprintf( addressStr, "%p", address );
195 #endif
196
197   for( i = 0, total = 0; originalString[i] != '\0'; ++i )
198     if( originalString[i] == '@' )
199       ++total;
200
201   length = strlen( originalString );
202
203   if( total == 0 )
204   {
205     resolvedString = XtNewString( originalString );
206   }
207   else
208   {
209     resolvedString = (char *)XtCalloc( 1, length + ( ( strlen( addressStr ) )
210                                                      * total ) + 1 );
211     if( resolvedString != NULL )
212     {
213       i = 0;
214       j = 0;
215       while( originalString[i] != '\0' )
216       {
217         if( originalString[i] == '@' )
218         {
219           for( k = 0; addressStr[k] != '\0'; ++j, ++k )
220             resolvedString[ j ] = addressStr[k];
221           ++i;
222         }
223         else
224           resolvedString[ j++ ] = originalString[ i++ ];
225       }
226     }
227   }
228
229   return resolvedString;
230 }
231