getbdffn.c 5.1 KB

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