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