Revert "dtudcfonted, dtudcexch: delete from repository"
[oweals/cde.git] / cde / programs / dtudcexch / getbdffn.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: getbdffn.c /main/5 1996/10/14 14:44:32 barstow $ */
24 /*
25  *  (c) Copyright 1995 FUJITSU LIMITED
26  *  This is source code modified by FUJITSU LIMITED under the Joint
27  *  Development Agreement for the CDEnext PST.
28  *  This is unpublished proprietary source code of FUJITSU LIMITED
29  */
30
31 #include "excutil.h"
32 #include <Xm/FileSB.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 /*
38  * There is no public header file for this function (only an
39  * internal header XmStringI.h).
40  */
41 extern XtPointer _XmStringUngenerate (XmString string,
42                         XmStringTag tag,
43                         XmTextType tag_type,
44                         XmTextType output_type);
45
46 void filesbokCB();
47 void filesbcancelCB();
48
49 extern char *maintitle;
50
51 void getbdffn(Exc_data * ed)
52 {
53     Widget      filesb;
54     Arg         args[20];
55     Cardinal    n;
56     char        *selectlabel;
57     XmString    xms;
58
59     selectlabel = GETMESSAGE(2, 2, "BDF file selection");
60     n = 0;
61     XtSetArg(args[n], XmNtitle, maintitle); n++;
62     xms = XmStringCreateLocalized(selectlabel);
63     XtSetArg(args[n], XmNselectionLabelString, xms); n++;
64     filesb = XmCreateFileSelectionDialog(ed->toplevel, "filesb", args, n);
65     XtAddCallback(filesb, XmNokCallback,
66                   (XtCallbackProc) filesbokCB,
67                   (XtPointer) ed);
68     XtAddCallback(filesb, XmNcancelCallback,
69                   (XtCallbackProc) filesbcancelCB,
70                   (XtPointer) ed);
71     XtUnmanageChild(XmFileSelectionBoxGetChild(filesb, XmDIALOG_HELP_BUTTON));
72     XtManageChild(filesb);
73 }
74
75 int fopencheck(char *file, char *dir, char *mode)
76 /*
77  *  when mode = "r", if fopen can open the file with read only mode, return 0
78  *  when mode = "w", if fopen can open the file with write mode, return 0
79  *                   but if the file exists, return 1
80  *  otherwise return -1
81  */
82 {
83     struct stat buf;
84
85     if (strcmp(mode, "r") == 0) {
86         if (stat(file, &buf) == 0)
87             if ((buf.st_mode & S_IFMT) == S_IFREG)
88                 return 0; /* readable regular file */
89     } else if (strcmp(mode, "w") == 0) {
90         if (stat(file, &buf) == 0) {
91             if (((buf.st_mode & S_IFMT) == S_IFREG) &&
92                 (access(file, W_OK) == 0))
93                 return 1; /* writable existing file */
94         } else {
95             if (stat(dir, &buf) == 0) {
96                 if (((buf.st_mode & S_IFMT) == S_IFDIR) &&
97                     (access(dir, W_OK) == 0)) {
98                     return 0; /* writable new file */
99                 }
100             }
101         }
102     } else
103         fprintf(stderr, "Unanticipatable error occurred in fopencheck.\n");
104     return -1;
105 }
106
107 void filesbcancelCB(Widget widget, Exc_data * ed, XtPointer call_data)
108 {
109     excexit(ed);
110 }
111
112 void freeStrings(char * dir, char * file)
113 {
114     if (dir != NULL)
115         XtFree(dir);
116     if (file != NULL)
117         XtFree(file);
118 }
119
120 void filesbokCB(Widget widget, Exc_data * ed, XtPointer call_data)
121 {
122     XmFileSelectionBoxCallbackStruct    *ptr;
123     char                                *file = NULL, *dir = NULL, *tmpfile;
124     int                                 r, ans = 0;
125     char                                *msg1;
126     char                                *msg2;
127
128     msg1 = GETMESSAGE(2, 4, "The selected file exists. Overwrite?");
129     msg2 = GETMESSAGE(2, 6, "Failed to open the selected file.");
130
131     ptr = (XmFileSelectionBoxCallbackStruct *) call_data;
132
133     file = (char *)  _XmStringUngenerate((XmString) ptr->value, NULL,
134                                           XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
135     if (!file) {
136         return;
137     }
138
139     dir = (char *)  _XmStringUngenerate((XmString) ptr->dir, NULL,
140                                           XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
141     if (!dir) {
142         return;
143     } else {
144         if (*file != '/') {
145             if ((tmpfile = XtMalloc(strlen(dir) + 1 + strlen(file) + 1))
146                 == NULL) {
147                 excerror(ed, EXCERRMALLOC, "filesbokCB", "exit");
148             }
149             sprintf(tmpfile, "%s/%s", dir, file);
150             XtFree(file);
151             file = tmpfile;
152         }
153     }
154
155     r = fopencheck(file, dir, ed->bdfmode);
156     if (r == 0) {/* no problem */
157         /*fprintf(stderr, "%s will be opened\n", file);*/
158     } else if (r == 1) { /* file exist at export function */
159         AskUser(widget, ed, msg1, &ans, "warning");
160         if (ans != 1) { /* overwrite cancel */
161             freeStrings(dir, file);
162             return;
163         }
164     } else { /* file will not be opened */
165         AskUser(widget, ed, msg2, &ans, "error");
166         freeStrings(dir, file);
167         return;
168     }
169     ed->bdffile = (char *) malloc(strlen(file) + 1);
170     strcpy(ed->bdffile, file);
171     freeStrings(dir, file);
172     XtUnmanageChild(widget);
173     if (ed->function == EXPORT)
174     {
175         createbdf(ed);
176     } else if (ed->function == IMPORT)
177     {
178         PopupSelectXLFD(ed->toplevel);
179     }
180 }