libDtSearch: Coverity 86579
[oweals/cde.git] / cde / programs / dtcreate / cmnrtns.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 libraries 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: cmnrtns.c /main/4 1995/11/01 16:11:08 rswiston $ */
24 /*****************************************************************************/
25 /*                                                                           */
26 /*  cmnrnts.c                                                                */
27 /*                                                                           */
28 /*   Common routines                                                         */
29 /*                                                                           */
30 /*****************************************************************************/
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <Dt/Icon.h>
35 #include <X11/cursorfont.h>
36 #include "UxXt.h"
37 #include <libgen.h>
38 #include <Dt/HourGlass.h>
39
40 #include "dtcreate.h"
41 #include "CreateActionAppShell.h"
42 #include "fileio.h"
43 #include "cmnrtns.h"
44
45 /*****************************************************************************/
46 /*                                                                           */
47 /*  ReplaceSpaces                                                            */
48 /*                                                                           */
49 /*****************************************************************************/
50 char * ReplaceSpaces(char *pszName)
51 {
52   char *newName;
53   char *ptr;
54
55   newName = (char *)XtMalloc(strlen(pszName) + 1);
56   memset(newName, 0, strlen(pszName) + 1);
57   strcpy(newName, pszName);
58   ptr = strrchr(newName, ' ');
59   if (ptr) {
60      ptr = newName;
61      while ((*ptr) && (ptr = strchr(ptr, ' '))) {
62        *ptr = '_';
63        ptr++;
64      }
65   }
66   return(newName);
67 }
68
69 #if 0
70 /*****************************************************************************/
71 /*                                                                           */
72 /*  GetExtName                                                               */
73 /*                                                                           */
74 /*****************************************************************************/
75 char * GetExtName(char *pszFileName)
76 {
77   char *type;
78
79   type = strrchr(pszFileName, '.');
80   if (type) {
81      type = strtok(type, ".");
82      return(type);
83   } else {
84      return(NULL);
85   }
86 }
87 #endif
88
89 #if 0
90 /*****************************************************************************/
91 /*                                                                           */
92 /*  GetPathName                                                              */
93 /*                                                                           */
94 /*****************************************************************************/
95 char * GetPathName(char *pszFileName)
96 {
97   char *ptr;
98   char *pszPath;
99
100   pszPath = (char *)malloc(strlen(pszFileName) + 1);
101   strcpy(pszPath, pszFileName);
102   ptr = strrchr(pszPath, '/');
103   if (ptr) {
104      *ptr = '\0';
105   } else {
106      free(pszPath);
107      pszPath = (char *)NULL;
108   }
109   return(pszPath);
110 }
111 #endif
112
113 #if 0
114 /*****************************************************************************/
115 /*                                                                           */
116 /*  Change_IconGadget_IconType                                               */
117 /*                                                                           */
118 /*****************************************************************************/
119 void Change_IconGadget_IconType(Widget widIconGadget, char *pszNewType)
120 {
121   char   *pszOldName;
122   char   pszNewName[MAXBUFSIZE];
123   char   *tmpptr;
124   ushort rc;
125
126   XtVaGetValues(widIconGadget, XmNimageName, &pszOldName, NULL);
127   if (pszOldName) {
128      tmpptr = strrchr(pszOldName, '.');
129      *tmpptr = '\0';
130      sprintf(pszNewName, "%s.%s", pszOldName, pszNewType);
131 #ifdef DEBUG
132      printf("monochrome name is '%s'\n", pszNewName); /* debug */
133 #endif
134      if (check_file_exists(pszNewName)) {
135         SET_ICONGADGET_ICON(widIconGadget, pszNewName);
136      } else {
137         SET_ICONGADGET_ICON(widIconGadget, "");
138      }
139   }
140 }
141 #endif
142
143 #if 0
144 /*****************************************************************************/
145 /*                                                                           */
146 /*  Change_IconName_IconType                                                 */
147 /*                                                                           */
148 /*****************************************************************************/
149 char * Change_IconName_IconType(char *pszOldName, char *pszNewType)
150 {
151   char   *tmpptr;
152   ushort rc;
153   char   *pszNewName = (char *)NULL;
154
155   if (pszOldName) {
156      pszNewName = (char *)malloc(strlen(pszOldName) + 10);
157      tmpptr = strrchr(pszOldName, '.');
158      *tmpptr = '\0';
159      sprintf(pszNewName, "%s.%s", pszOldName, pszNewType);
160 #ifdef DEBUG
161      printf("new icon name is '%s'\n", pszNewName);
162 #endif
163      if (!check_file_exists(pszNewName)) {
164         free(pszNewName);
165         pszNewName = (char *)NULL;
166      }
167   }
168   return(pszNewName);
169 }
170 #endif
171
172 /******************************************************************************/
173 /*                                                                            */
174 /* GetCoreName                                                                */
175 /*                                                                            */
176 /******************************************************************************/
177 char * GetCoreName(char *pszFullName)
178 {
179   char *name;
180   char *ptr;
181   char *newName;
182
183   name = strrchr(pszFullName, '/');
184   if (name) {
185      name = strtok(name, "/");
186   } else {
187      name = pszFullName;
188   }
189   newName = (char *)malloc(strlen(name) + 1);
190   memset(newName, 0, strlen(name) + 1);
191   strcpy(newName, name);
192   ptr = strrchr(newName, '.');
193   if (ptr) {
194     *ptr = '\0';
195   }
196   return(newName);
197 }
198
199 /******************************************************************************/
200 /*                                                                            */
201 /* load_icons - puts selected icons into the appropriate icon gadget.         */
202 /*                                                                            */
203 /* INPUT: Widget wid - OK button on Open File dialog,                         */
204 /*        XtPointer client_data                                               */
205 /*        XmFileSelectionBoxCallbackStruct *cbs                               */
206 /* OUTPUT: none                                                               */
207 /*                                                                            */
208 /******************************************************************************/
209 void load_icons (Widget wid, XtPointer client_data,
210                  XmFileSelectionBoxCallbackStruct *cbs)
211 {
212   char          *full_name, *path_and_base_name, *type_name, *size_name;
213   char          *base_name;
214   char          *ptr;
215   XtArgVal /* int */     iSource;
216   FiletypeData  *pFtD;
217
218   /*****************************************/
219   /* Get icon name and separate into parts */
220   /*****************************************/
221   full_name = (char *)client_data;
222   path_and_base_name = (char *)malloc(strlen(full_name)+1);
223   strcpy(path_and_base_name, full_name);
224
225   /*****************************************/
226   /* Strip off icon type extension.        */
227   /*****************************************/
228   ptr = strrchr(path_and_base_name, '.');
229   if (ptr) {
230      type_name = strtok(ptr, ".");
231      *ptr = '\0';
232   } else {
233      type_name = (char *)NULL;
234   }
235
236   /*****************************************/
237   /* Get size extention.                   */
238   /*****************************************/
239   ptr = strrchr(path_and_base_name, '.');
240   if (ptr) {
241      size_name = strtok(ptr, ".");
242      *ptr = '\0';
243   } else {
244      size_name = (char *)NULL;
245   }
246
247 #ifdef DEBUG
248     printf("path&base = %s\n", path_and_base_name); /* debug */
249     printf("type      = %s\n", type_name); /* debug */
250     printf("size      = %s\n", size_name); /* debug */
251 #endif
252
253   /* ***** cmvc 6715 *****
254   if ((!path_and_base_name) || (!type_name) || (!size_name)) {
255     printf ("'%s' is not a proper icon file name!\n", full_name);
256   }
257   else
258   */
259     {
260     XtVaGetValues(IconSelector, XmNuserData, &iSource, NULL);
261     base_name = basename(path_and_base_name);
262     ptr = XtMalloc(strlen(base_name) + 1);
263     strcpy(ptr, base_name);
264     switch (iSource) {
265       case CA_ACTION_ICONS:
266            AD.pszIcon = ptr;
267            SetIconData(CA_LRG_IconGadget, path_and_base_name, Large_Icon);
268            SetIconData(CA_MED_IconGadget, path_and_base_name, Medium_Icon);
269            SetIconData(CA_TINY_IconGadget, path_and_base_name, Tiny_Icon);
270            break;
271       case CA_FILETYPE_ICONS:
272            XtVaGetValues(AddFiletype, XmNuserData, &pFtD, NULL);
273            pFtD->pszIcon = ptr;
274            SetIconData(AF_MED_IconGadget, path_and_base_name, Medium_Icon);
275            SetIconData(AF_TINY_IconGadget, path_and_base_name, Tiny_Icon);
276            break;
277     }
278   }
279   free(path_and_base_name);
280   return;
281 }
282
283 /******************************************************************************/
284 /*                                                                            */
285 /* GetWidgetTextString                                                        */
286 /*                                                                            */
287 /* INPUT: Widget wid - TextField widget to get string from.                   */
288 /*        Pointer to variable to store string                                 */
289 /*                                                                            */
290 /* OUTPUT: none                                                               */
291 /*                                                                            */
292 /******************************************************************************/
293 void GetWidgetTextString (Widget wid, char **ppszText)
294 {
295   char *pszTmp = NULL;
296
297   if (*ppszText) {
298      XtFree(*ppszText);
299      *ppszText = (char *)NULL;
300   }
301   if (XmIsTextField(wid)) {
302      pszTmp = XmTextFieldGetString (wid);
303   } else if (XmIsText(wid)) {
304      pszTmp = XmTextGetString (wid);
305   }
306
307   if (pszTmp) {
308      if (!strcmp(pszTmp, "")) {
309         XtFree(pszTmp);
310         pszTmp = (char *)NULL;
311      }
312      *ppszText = pszTmp;
313   }
314 }
315
316 /******************************************************************************/
317 /*                                                                            */
318 /* PutWidgetTextString                                                        */
319 /*                                                                            */
320 /* INPUT: Widget wid - Widget whose text string is being set                  */
321 /*        Pointer to text string.                                             */
322 /*                                                                            */
323 /* OUTPUT: none                                                               */
324 /*                                                                            */
325 /******************************************************************************/
326 void PutWidgetTextString (Widget wid, char *pszText)
327 {
328   if (pszText) {
329      if (XmIsTextField(wid)) {
330         XmTextFieldSetString (wid, pszText);
331      } else if (XmIsText(wid)) {
332         XmTextSetString (wid, pszText);
333      }
334   }
335 }
336
337 /******************************************************************************/
338 /*                                                                            */
339 /* GetIconSearchPathList                                                      */
340 /*                                                                            */
341 /* INPUT:  none                                                               */
342 /*                                                                            */
343 /* OUTPUT: none                                                               */
344 /*                                                                            */
345 /******************************************************************************/
346 char **GetIconSearchPathList(void)
347 {
348   char    *iconpath = (char *)NULL;
349   char    *ptr;
350   char    *tmpptr;
351   char    *strip;
352   char    *path;
353   int     i;
354   int     count;
355   int     size;
356   char    **pplist;
357   char    *lang;
358   int     langsize;
359   static  char *default_list1[] = {"~/.dt/icons", "/etc/dt/appconfig/icons/C", "/usr/dt/appconfig/icons/C"};
360   static  char *default_list2[] = {"/etc/dt/appconfig/icons/C", "/usr/dt/appconfig/icons/C"};
361   char    **default_list;
362   Boolean bFound;
363   char    *pszEnvVar;
364
365   /**************************************************************************/
366   /* Get contents of icon search path environment variable.                 */
367   /**************************************************************************/
368   pszEnvVar = getenv("XMICONSEARCHPATH");
369   if ( pszEnvVar && strlen(pszEnvVar) ) {
370      iconpath = (char *)malloc(strlen(pszEnvVar) + 1);
371      strcpy(iconpath, pszEnvVar);
372   }
373
374   /**************************************************************************/
375   /* If no iconpath then return an appropriate default.                     */
376   /**************************************************************************/
377   if (!iconpath) {
378      pszEnvVar = getenv("HOME");
379      if (pszEnvVar && strlen(pszEnvVar)) {
380         default_list = default_list1;
381         count = sizeof(default_list1)/sizeof(void *);
382      } else {
383         default_list = default_list2;
384         count = sizeof(default_list2)/sizeof(void *);
385      }
386      pplist = (char **)calloc((count + 1), sizeof(void *));
387      for (i=0; i < count; i++) {
388         if (strchr(default_list[i], '~')) {
389            if (pszEnvVar && strlen(pszEnvVar)) {
390               pplist[i] = calloc(strlen(default_list[i]) + strlen(pszEnvVar) + 1, sizeof(char));
391               strcpy(pplist[i], pszEnvVar);
392            } else {
393               pplist[i] = calloc(strlen(default_list[i]) + 1, sizeof(char));
394            }
395            strcat(pplist[i], &(default_list[i][1]));
396         } else {
397            pplist[i] = calloc(strlen(default_list[i]) + 1,sizeof(char));
398            strcpy(pplist[i], default_list[i]);
399         }
400      }
401      return(pplist);
402   }
403
404 #ifdef DEBUG
405   printf("Path = %s\n", iconpath);
406 #endif
407
408   /**************************************************************************/
409   /* Iterate through the search path once to get total count of individual  */
410   /* paths within the search path.                                          */
411   /**************************************************************************/
412   count = 0;
413   ptr = iconpath;
414   while (ptr) {
415     tmpptr = ptr;
416     ptr = strchr(ptr, ':');
417     /*printf("ptr = %s\n", ptr);*/
418     if (tmpptr != ptr) {
419        count++;
420        if ((ptr) && (ptr[1] != '\0')) {
421           for (; ptr[0] == ':'; ptr++);
422        } else {
423           ptr = (char *)NULL;
424        }
425     }
426   }
427
428   /**************************************************************************/
429   /* Debug information.                                                     */
430   /**************************************************************************/
431 #ifdef DEBUG
432   printf("IconSearchPath = %s\n", iconpath);
433   printf("# of paths = %d\n", count);
434 #endif
435
436   /**************************************************************************/
437   /* Get contents of lang environment variable.                             */
438   /**************************************************************************/
439   lang = getenv("LANG");
440   if (lang) {
441      langsize = strlen(lang);
442   } else {
443      langsize = 0;
444   }
445
446   /**************************************************************************/
447   /* Allocate the array of pointers to store the individual path strings.   */
448   /**************************************************************************/
449   pplist = (char **)calloc((count+1), sizeof(void *));
450
451   /**************************************************************************/
452   /* Iterate through again to allocate space for each individual path and   */
453   /* store that path.                                                       */
454   /**************************************************************************/
455   count = 0;
456   ptr = iconpath;
457   while (ptr) {
458     tmpptr = ptr;
459     ptr = strchr(ptr, ':');
460     if (tmpptr != ptr) {
461        /*********************************************************************/
462        /* Make tmpptr the path.  Also move to the next path in the search   */
463        /* path.                                                             */
464        /*********************************************************************/
465        strip = ptr;
466        if ((ptr) && (ptr[1] != '\0')) {
467           for (; ptr[0] == ':'; ptr++);
468           *strip = '\0';
469        } else {
470           if (ptr) {
471              *strip = '\0';
472           }
473           ptr = (char *)NULL;
474        }
475
476        /*********************************************************************/
477        /* If %L in path, then add size of lang variable to it when          */
478        /* allocating array for path.                                        */
479        /*********************************************************************/
480        if (strip = strstr(tmpptr, "%L")) {
481          path = malloc(strlen(tmpptr) + langsize + 1);
482        } else {
483          path = malloc(strlen(tmpptr) + 1);
484        }
485        strcpy(path, tmpptr);
486
487        /*********************************************************************/
488        /* Strip off the /%B... stuff off of the path if there is some.      */
489        /*********************************************************************/
490        if (strip = strstr(path, "%B")) {
491           *strip = '\0';
492        }
493
494        /*********************************************************************/
495        /* Now replace %L with lang variable.                                */
496        /*********************************************************************/
497        if (strip = strstr(path, "%L")) {
498           *strip = '\0';
499           if (langsize) {
500              strcat(path, lang);
501           }
502        }
503
504        /*********************************************************************/
505        /* Remove slash from end of path if there is one.                    */
506        /*********************************************************************/
507        size = strlen(path);
508        if (size > 0) {
509           if (path[size - 1] == '/') {
510              path[size - 1] = '\0';
511           }
512
513 #ifdef DEBUG
514           printf("new path = %s\n", path);
515 #endif
516
517           /***************************************************************/
518           /* See if path is already in our list.                         */
519           /***************************************************************/
520           bFound = FALSE;
521           for (i=0; (i < count) && (!bFound); i++) {
522              if (!(strcmp(pplist[i], path))) {
523                 bFound = TRUE;
524              }
525           }
526           /***************************************************************/
527           /* If not in list, then add to list.                           */
528           /***************************************************************/
529           if (!bFound) {
530              pplist[count] = path;
531              count++;
532           /***************************************************************/
533           /* Else, just free resources.                                  */
534           /***************************************************************/
535           } else {
536              free(path);
537           }
538        }
539
540 #ifdef DEBUG
541        printf("Path%d = %s\n", count, pplist[count]);
542 #endif
543
544 #if 0
545        **** moving this up a bit in the code ****
546
547        /*********************************************************************/
548        /* Get the next path in the icon search path.                        */
549        /*********************************************************************/
550        if ((ptr) && (ptr[1] != '\0')) {
551           for (; ptr[0] == ':'; ptr++);
552        } else {
553           ptr = (char *)NULL;
554        }
555 #endif
556     }
557   }
558   free(iconpath);
559   return(pplist);
560 }
561
562 /******************************************************************************/
563 /*                                                                            */
564 /* FreeIconSearchPathList                                                     */
565 /*                                                                            */
566 /* INPUT:  none                                                               */
567 /*                                                                            */
568 /* OUTPUT: none                                                               */
569 /*                                                                            */
570 /******************************************************************************/
571 void FreeIconSearchPathList(char **pplist)
572 {
573   char *ptr;
574   int  i;
575
576   /**************************************************************************/
577   /* Iterate through the search path once to get total count of individual  */
578   /* paths within the search path.                                          */
579   /**************************************************************************/
580   if (pplist) {
581      for (i = 0; pplist[i]; free(pplist[i]), i++);
582      /*
583      i = 0;
584      while (pplist[i]) {
585        free(pplist[i]);
586      }
587      */
588      free(pplist);
589   }
590 }
591 /******************************************************************************/
592 /*                                                                            */
593 /* TurnOnHourGlassAllWindows                                                  */
594 /*                                                                            */
595 /* INPUT:  none                                                               */
596 /*                                                                            */
597 /* OUTPUT: none                                                               */
598 /*                                                                            */
599 /******************************************************************************/
600 void TurnOnHourGlassAllWindows(void)
601 {
602   _DtTurnOnHourGlass(CreateActionAppShell);
603   if ( (AddFiletype) &&
604        (XtIsRealized(AddFiletype)) &&
605        (XtIsManaged(AddFiletype)) ) {
606      _DtTurnOnHourGlass(AddFiletype);
607   }
608   if ( (FileCharacteristics) &&
609        (XtIsRealized(FileCharacteristics)) &&
610        (XtIsManaged(FileCharacteristics)) ) {
611      _DtTurnOnHourGlass(FileCharacteristics);
612   }
613   if ( (IconSelector) &&
614        (XtIsRealized(IconSelector)) &&
615        (XtIsManaged(IconSelector)) ) {
616      _DtTurnOnHourGlass(IconSelector);
617   }
618   if ( (OpenFile) &&
619        (XtIsRealized(OpenFile)) &&
620        (XtIsManaged(OpenFile)) ) {
621      _DtTurnOnHourGlass(OpenFile);
622   }
623 }
624 /******************************************************************************/
625 /*                                                                            */
626 /* TurnOffHourGlassAllWindows                                                 */
627 /*                                                                            */
628 /* INPUT:  none                                                               */
629 /*                                                                            */
630 /* OUTPUT: none                                                               */
631 /*                                                                            */
632 /******************************************************************************/
633 void TurnOffHourGlassAllWindows(void)
634 {
635   _DtTurnOffHourGlass(CreateActionAppShell);
636   if ( (AddFiletype) &&
637        (XtIsRealized(AddFiletype)) &&
638        (XtIsManaged(AddFiletype)) ) {
639      _DtTurnOffHourGlass(AddFiletype);
640   }
641   if ( (FileCharacteristics) &&
642        (XtIsRealized(FileCharacteristics)) &&
643        (XtIsManaged(FileCharacteristics)) ) {
644      _DtTurnOffHourGlass(FileCharacteristics);
645   }
646   if ( (IconSelector) &&
647        (XtIsRealized(IconSelector)) &&
648        (XtIsManaged(IconSelector)) ) {
649      _DtTurnOffHourGlass(IconSelector);
650   }
651   if ( (OpenFile) &&
652        (XtIsRealized(OpenFile)) &&
653        (XtIsManaged(OpenFile)) ) {
654      _DtTurnOffHourGlass(OpenFile);
655   }
656 }
657 /******************************************************************************/
658 /*                                                                            */
659 /* SetIconData                                                                */
660 /*                                                                            */
661 /* INPUT:  icon gadget widget id                                              */
662 /*         icon file base name                                                */
663 /*                                                                            */
664 /* OUTPUT: none                                                               */
665 /*                                                                            */
666 /******************************************************************************/
667 void SetIconData(Widget wid, char *pszIconFile, enum icon_size_range enumIconSize)
668 {
669   char      pmFileName[MAXBUFSIZE];
670   char      bmFileName[MAXBUFSIZE];
671   char      pszSize[MAX_EXT_SIZE];
672   IconData  *pIconData;
673   char      *pszName;
674
675 #if 0
676   switch (enumIconSize) {
677     case Large_Icon :
678                strcpy(pszSize, LARGE_EXT);
679                break;
680     case Medium_Icon :
681                strcpy(pszSize, MEDIUM_EXT);
682                break;
683     case Tiny_Icon :
684                strcpy(pszSize, TINY_EXT);
685                break;
686   }
687
688   sprintf(pmFileName, "%s%s%s", pszIconFile, pszSize, PIXMAP_EXT );
689   sprintf(bmFileName, "%s%s%s", pszIconFile, pszSize, BITMAP_EXT );
690 #endif
691
692   pszName = CreateIconName((char *)NULL, pszIconFile, enumIconSize, PIXMAP_EXT, FALSE);
693   snprintf(pmFileName, sizeof(pmFileName), "%s", pszName);
694   XtFree(pszName);
695
696   pszName = CreateIconName((char *)NULL, pszIconFile, enumIconSize, BITMAP_EXT, FALSE);
697   snprintf(bmFileName, sizeof(bmFileName), "%s", pszName);
698   XtFree(pszName);
699
700   pIconData = GetIconDataFromWid(wid);
701   if (pIconData) {
702      if ( (pIconData->pmDirtyBit) &&
703           (pIconData->pmFileName) &&
704           (strlen(pIconData->pmFileName)) ) {
705 #ifdef DEBUG
706         printf("SetIconData: unlink '%s'\n", pIconData->pmFileName);  /* debug */
707 #endif
708         unlink(pIconData->pmFileName);
709         pIconData->pmDirtyBit = False;
710      }
711      strcpy(pIconData->pmFileName, pmFileName);
712
713      if ( (pIconData->bmDirtyBit) &&
714           (strlen(pIconData->bmFileName)) ) {
715 #ifdef DEBUG
716         printf("SetIconData: unlink '%s'\n", pIconData->bmFileName);  /* debug */
717 #endif
718         unlink(pIconData->bmFileName);
719         pIconData->bmDirtyBit = False;
720      }
721      strcpy(pIconData->bmFileName, bmFileName);
722
723      if (bShowPixmaps) {
724         SET_ICONGADGET_ICON(wid, pmFileName);
725      } else {
726         SET_ICONGADGET_ICON(wid, bmFileName);
727      }
728   }
729 }
730 /******************************************************************************/
731 /*                                                                            */
732 /* GetCorrectIconType                                                         */
733 /*                                                                            */
734 /* INPUT:  icon file name                                                     */
735 /*                                                                            */
736 /* OUTPUT: correct icon type of icon file name passed                         */
737 /*                                                                            */
738 /******************************************************************************/
739 char * GetCorrectIconType(char *pszIconFile)
740 {
741   char  *pszTmp;
742   char  *ptr;
743   char  pszNewType[MAX_EXT_SIZE];
744
745   if (bShowPixmaps) {
746      strcpy(pszNewType, PIXMAP_EXT);
747   } else {
748      strcpy(pszNewType, BITMAP_EXT);
749   }
750
751   if (pszIconFile) {
752      pszTmp = XtMalloc(strlen(pszIconFile) + strlen(pszNewType) + 1);
753      if (!pszTmp) return((char *)NULL);
754      strcpy(pszTmp, pszIconFile);
755      ptr = strrchr(pszTmp, '.');
756      if (ptr) {
757         strcpy(ptr, pszNewType);
758      }
759   } else {
760      pszTmp = (char *)NULL;
761   }
762   return (pszTmp);
763 }
764 /******************************************************************************/
765 /*                                                                            */
766 /* CreateMaskName                                                             */
767 /*                                                                            */
768 /* INPUT:  icon file name                                                     */
769 /*                                                                            */
770 /* OUTPUT: mask file name for icon name passed in                             */
771 /*                                                                            */
772 /******************************************************************************/
773 char * CreateMaskName(char *pszIconName)
774 {
775   char *pszTmpName;
776   char *ptr;
777   char *pszNewName;
778   char *type_name;
779   char *size_name;
780   char type_ext[MAX_EXT_SIZE + 2];
781   char size_ext[MAX_EXT_SIZE + 2];
782   int  bytesneeded = 0;
783
784   /***************************************************************/
785   /* initialize temp arrays                                      */
786   /***************************************************************/
787   type_ext[0] = '\0';
788   size_ext[0] = '\0';
789
790   /***************************************************************/
791   /* alloc memory for temporary name                             */
792   /***************************************************************/
793   pszTmpName = (char *)XtMalloc(strlen(pszIconName) + 1);
794   if (pszTmpName) {
795      strcpy(pszTmpName, pszIconName);
796   } else {
797      return((char *)NULL);
798   }
799
800   /*****************************************/
801   /* Strip off icon type extension.        */
802   /*****************************************/
803   ptr = strrchr(pszTmpName, '.');
804   if (ptr) {
805      type_name = strtok(ptr, ".");
806      *ptr = '\0';
807   } else {
808      type_name = (char *)NULL;
809   }
810
811   /*****************************************/
812   /* Get size extention.                   */
813   /*****************************************/
814   ptr = strrchr(pszTmpName, '.');
815   if (ptr) {
816      size_name = strtok(ptr, ".");
817      *ptr = '\0';
818   } else {
819      size_name = (char *)NULL;
820   }
821
822   /*****************************************/
823   /* Alloc the storage for the new name    */
824   /*****************************************/
825   bytesneeded += ((pszTmpName) ? strlen(pszTmpName) : 0);
826   bytesneeded += strlen("_m..");
827   bytesneeded += ((size_name) ? strlen(size_name) : 0);
828   bytesneeded += ((type_name) ? strlen(type_name) : 0);
829   pszNewName = (char *)XtMalloc(bytesneeded + 1);
830
831   /*****************************************/
832   /* Create extension names                */
833   /*****************************************/
834   if (size_name) {
835      sprintf(size_ext, ".%s", size_name);
836   }
837   if (type_name) {
838      sprintf(type_ext, ".%s", type_name);
839   }
840   /*****************************************/
841   /* And construct the new name from pieces*/
842   /*****************************************/
843   if (pszNewName) {
844      if (size_name) {
845         sprintf(pszNewName, "%s%s_m%s", pszTmpName, size_ext, type_ext);
846      } else {
847         sprintf(pszNewName, "%s_m%s%s", pszTmpName, size_ext, type_ext);
848      }
849   }
850   if (pszTmpName) XtFree(pszTmpName);
851
852 #ifdef DEBUG
853   printf("Mask file name = '%s'\n", pszNewName); /* debug */
854 #endif
855
856   return(pszNewName);
857 }
858