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