}
else { /* reason == FILE_OPEN */
/* make sure the file is a regular text file and open it */
if (stat (filename, &statb) == −1 ||
(statb.st_mode & S_IFMT) != S_IFREG ||
!(fp = fopen (filename, "r"))) {
perror (filename);
sprintf (buf, "Can't read %s.", filename);
XmTextSetString (text_output, buf);
XtFree (filename);
return;
}
/* put the contents of the file in the Text widget by
* allocating enough space for the entire file, reading the
* file into the space, and using XmTextSetString() to show
* the file.
*/
len = statb.st_size;
if (!(text = XtMalloc ((unsigned)(len+1)))) /* +1 for NULL */
sprintf (buf, "%s: XtMalloc(%ld) failed", len, filename);
else {
if (fread (text, sizeof (char), len, fp) != len)
sprintf (buf, "Warning: did not read entire file!");
else
sprintf (buf, "Loaded %ld bytes from %s.", len, filename);
text[len] = 0; /* NULL−terminate */
XmTextSetString (text_edit, text);
}
}
XmTextSetString (text_output, buf); /* purge output message */
/* free all allocated space. */
XtFree (text);
XtFree (filename);
fclose (fp);
XtUnmanageChild (dialog);
}
/* popdown_cb() −− callback routine for "Cancel" button. */
void
popdown_cb (w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
XtUnmanageChild (w);
}
/* file_cb() −− a menu item from the "File" pulldown menu was selected */
void
file_cb(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
static Widget open_dialog, save_dialog;
Widget dialog = NULL;
XmString ...