home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / master12.zip / mastering / filetimes.c < prev    next >
C/C++ Source or Header  |  1992-08-19  |  12KB  |  389 lines

  1. /************************************************************************
  2.  *  File:  filetimes.c
  3.  ***********************************************************************/
  4.  
  5. /* information for 'what' */
  6. char ident1[]="@(#)filetimes Version A.00.16 19 August 1992";
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <time.h>
  12. #include <errno.h>
  13. #include <Xm/MainW.h>
  14. #include <Xm/CascadeB.h>
  15. #include <Xm/RowColumn.h>
  16. #include <Xm/Frame.h>
  17. #include <Xm/Label.h>
  18. #include <Xm/FileSB.h>
  19. #include <Xm/PushB.h>
  20. #include <Xm/MessageB.h>
  21.  
  22. typedef struct
  23. {
  24.     Widget   widget;
  25. } selectData;
  26.  
  27. void okCB ();
  28. Widget CreateHelp ();
  29. Widget CreateVersion ();
  30. void VersionCB ();
  31. void HelpCB ();
  32. void QuitCB ();
  33.  
  34. /*-------------------------------------------------------------
  35. ** Global Variables
  36. */
  37.  
  38. #define MAX_ARGS 20
  39.  
  40. static XmStringCharSet charset = (XmStringCharSet) XmFONTLIST_DEFAULT_TAG;
  41.  
  42. /*-------------------------------------------------------------
  43. **  HelpCB      - callback for help button
  44. */
  45. void HelpCB (w, client_data, call_data) 
  46. Widget     w;            /*  widget id              */
  47. XtPointer  client_data;  /*  data from application  */
  48. XtPointer  call_data;    /*  data from widget class */
  49. {
  50.   Widget   message_box;  /*  message box            */
  51.  
  52.   /*  Create help window.
  53.   */
  54.   message_box = CreateHelp (w);
  55.  
  56.   /*  Display help window.
  57.   */
  58.   XtManageChild (message_box);
  59. }
  60.  
  61. /*-------------------------------------------------------------
  62. **  CreateHelp    - create help window
  63. */
  64. Widget CreateHelp (parent) 
  65.   Widget       parent;          /*  parent widget   */
  66. {
  67.   Widget       button;
  68.   Widget       message_box;     /*  Message Dialog  */
  69.   Arg          args[MAX_ARGS];  /*  arg list        */
  70.   register     int  n;          /*  arg count       */
  71.  
  72.   static char  message[BUFSIZ]; /*  help text       */
  73.   XmString     title_string = NULL;
  74.   XmString     message_string = NULL;
  75.   XmString     button_string = NULL;
  76.  
  77.   /*  Generate message to display.
  78.   */
  79.   sprintf (message, "\
  80. The FileSelectionBox displays files in the current directory.  You \n\
  81. select a file by moving the pointer to that file name and clicking mouse \n\
  82. button 1.  Then move the pointer to the `ok' button and click mouse \n\
  83. button 1 again.  A message box will appear with the last file access time, \n\
  84. the last file modification time, and the last file change time.  Note \n\
  85. that writing to a file changes both modification time and change time. \n\
  86. See the man page for stat (2) and ctime (3C) for more information.\0");
  87.  
  88.   message_string = XmStringCreateLtoR (message, charset);
  89.   button_string = XmStringCreateLtoR ("Close", charset);
  90.   title_string = XmStringCreateLtoR ("filetimes help", charset);
  91.  
  92.   /*  Create message box dialog.
  93.   */
  94.   n = 0;
  95.   XtSetArg (args[n], XmNdialogTitle, title_string);  n++;
  96.   XtSetArg (args[n], XmNokLabelString, button_string);  n++;
  97.   XtSetArg (args[n], XmNmessageString, message_string);  n++;
  98.   message_box = XmCreateMessageDialog (parent, "helpbox", args, n);
  99.   button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
  100.   XtUnmanageChild (button);
  101.   button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
  102.   XtUnmanageChild (button);
  103.  
  104.   /*  Free strings and return message box.
  105.   */
  106.   if (title_string) XmStringFree (title_string);
  107.   if (message_string) XmStringFree (message_string);
  108.   if (button_string) XmStringFree (button_string);
  109.   return (message_box);
  110. }
  111.  
  112. /*-------------------------------------------------------------
  113. **  VersionCB      - callback for help button
  114. */
  115. void VersionCB (w, client_data, call_data) 
  116. Widget     w;            /*  widget id               */
  117. XtPointer  client_data;  /*  data from application   */
  118. XtPointer  call_data;    /*  data from widget class  */
  119. {
  120.   Widget   message_box;  /*  message box             */
  121.  
  122.   /*  Create message window. */
  123.   message_box = CreateVersion (w);
  124.  
  125.   /*  Display version window. */
  126.   XtManageChild (message_box);
  127. }
  128.  
  129. /*-------------------------------------------------------------
  130. **  CreateVersion    - create version window
  131. */
  132. Widget CreateVersion (parent) 
  133.   Widget       parent;           /*  parent widget  */
  134. {
  135.   Widget       button;
  136.   Widget       message_box;      /*  Message Dialog */
  137.   Arg          args[MAX_ARGS];   /*  arg list       */
  138.   register int n;                /*  arg count      */
  139.  
  140.   static char  message[BUFSIZ];  /*  help text      */
  141.   XmString     title_string = NULL;
  142.   XmString     message_string = NULL;
  143.   XmString     button_string = NULL;
  144.   char         text4[50];
  145.  
  146.   strcpy (text4, &ident1[4]);
  147.   /*  Generate message to display. */
  148.   sprintf (message, "This is %s \0",text4);
  149.  
  150.   /* Create the compound strings */
  151.   message_string = XmStringCreateLtoR (message, charset);
  152.   button_string = XmStringCreateLtoR ("Close", charset);
  153.   title_string = XmStringCreateLtoR ("diskuse help", charset);
  154.  
  155.   /*  Create message box dialog. */
  156.   n = 0;
  157.   XtSetArg (args[n], XmNdialogTitle, title_string);  n++;
  158.   XtSetArg (args[n], XmNokLabelString, button_string);  n++;
  159.   XtSetArg (args[n], XmNmessageString, message_string);  n++;
  160.   message_box = XmCreateMessageDialog (parent, "versionbox", args, n);
  161.  
  162.   button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
  163.   XtUnmanageChild (button);
  164.   button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
  165.   XtUnmanageChild (button);
  166.  
  167.   /*  Free strings and return message box. */
  168.   if (title_string) XmStringFree (title_string);
  169.   if (message_string) XmStringFree (message_string);
  170.   if (button_string) XmStringFree (button_string);
  171.   return (message_box);
  172. }
  173.  
  174. /*-------------------------------------------------------------
  175. **  QuitCB      - callback for quit button
  176. */
  177. void QuitCB (w, client_data, call_data) 
  178. Widget     w;            /*  widget id               */
  179. XtPointer  client_data;  /*  data from application   */
  180. XtPointer  call_data;    /*  data from widget class  */
  181. {
  182.  
  183.   /*  Terminate the application.
  184.   */
  185.   exit (0);
  186. }
  187.  
  188. /************************************************************************
  189.  *  main
  190.  ***********************************************************************/
  191.  
  192. main (argc, argv)
  193.     int argc;
  194.     char **argv;
  195. {
  196.     int         n;
  197.     Arg         args[10];
  198.     Widget      toplevel, main_window, menu_bar, menu_pane, button, fsbox;
  199.     Widget    cascade, temp;
  200.     XmString  str1, label_text;
  201.     XtAppContext  app_context;
  202.  
  203.    /*
  204.     *  Initialize the toolkit.
  205.     */
  206.   
  207.   toplevel = XtAppInitialize (&app_context, "Filetimes", NULL, 0, &argc,
  208.                               argv, NULL, args, 0);
  209.  
  210.  
  211.   /*  
  212.      *  Create main window.
  213.    */
  214.  
  215.   n = 0;
  216.   main_window = XmCreateMainWindow (toplevel, "main1", args, n);
  217.   XtManageChild (main_window);
  218.  
  219.   /*
  220.    *  Create menu bar in main window.
  221.    */
  222.  
  223.   n = 0;
  224.   menu_bar = XmCreateMenuBar (main_window, "menu_bar", args, n); 
  225.   XtManageChild (menu_bar);
  226.  
  227.   /*
  228.    *  Create "File" pulldown menu.
  229.    */
  230.  
  231.   n = 0;
  232.   menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane", args, n);
  233.  
  234.   n = 0;
  235.   button = XmCreatePushButton (menu_pane, "Exit", args, n);
  236.   XtManageChild (button);
  237.   XtAddCallback (button, XmNactivateCallback, QuitCB, NULL);
  238.  
  239.   n = 0;
  240.   XtSetArg (args[n], XmNsubMenuId, menu_pane);  n++;
  241.   cascade = XmCreateCascadeButton (menu_bar, "File", args, n);
  242.   XtManageChild (cascade);
  243.  
  244.   /*  
  245.    *  Create "Help" pulldown menu.
  246.    */
  247.  
  248.   n = 0;
  249.   menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane", args, n);
  250.  
  251.   n = 0;
  252.   button = XmCreatePushButton (menu_pane, "Help", args, n);
  253.   XtManageChild (button);
  254.   XtAddCallback (button, XmNactivateCallback, HelpCB, NULL);
  255.  
  256.   n = 0;
  257.   button = XmCreatePushButton (menu_pane, "Program Version", args, n);
  258.   XtManageChild (button);
  259.   XtAddCallback (button, XmNactivateCallback, VersionCB, NULL);
  260.  
  261.   n = 0;
  262.   XtSetArg (args[n], XmNsubMenuId, menu_pane);  n++;
  263.   cascade = XmCreateCascadeButton (menu_bar, "Help", args, n);
  264.   XtManageChild (cascade);
  265.  
  266.   n = 0;
  267.   XtSetArg (args[n], XmNmenuHelpWidget, cascade);  n++;
  268.   XtSetValues (menu_bar, args, n);
  269.  
  270.     /*
  271.      *  Create the file selection box dialog
  272.      */
  273.  
  274.   str1 = XmStringCreateLtoR ("Files", XmFONTLIST_DEFAULT_TAG);
  275.  
  276.   n = 0;
  277.   XtSetArg (args[n], XmNlistLabelString, str1); n++;
  278.   fsbox = XmCreateFileSelectionBox (main_window, "filesb", args, n);
  279.   XtManageChild (fsbox);
  280.   XmStringFree (str1);
  281.  
  282.   XtAddCallback (fsbox, XmNokCallback, okCB, NULL); 
  283.  
  284.   temp = XmFileSelectionBoxGetChild (fsbox, XmDIALOG_CANCEL_BUTTON);
  285.   XtUnmanageChild (temp);
  286.   temp = XmFileSelectionBoxGetChild (fsbox, XmDIALOG_HELP_BUTTON);
  287.   XtUnmanageChild (temp);
  288.  
  289.     /*
  290.      *  Set main window areas.
  291.      */
  292.  
  293.   XmMainWindowSetAreas (main_window, menu_bar, NULL, NULL, NULL, fsbox);
  294.  
  295.     /*
  296.      *  Realize the widget hierarchy and enter the main loop.
  297.      */
  298.  
  299.   XtRealizeWidget (toplevel);
  300.   XtAppMainLoop (app_context);
  301. }
  302.  
  303. /************************************************************************
  304.  *  okCB
  305.  ***********************************************************************/
  306.  
  307. void
  308. okCB (w, client_data, call_data)
  309.     Widget                w;
  310.     XtPointer               client_data;
  311.     XmFileSelectionBoxCallbackStruct  *call_data;
  312. {
  313.     Arg         args[10];
  314.   Widget    button, message_box;
  315.   XmString  title_string = NULL;
  316.   XmString  message_string = NULL;
  317.   XmString  message_string1 = NULL;
  318.   XmString  final_message_string = NULL;
  319.   XmString  button_string = NULL;
  320.   XmString  access_msg = NULL;
  321.   XmString   access_time = NULL;
  322.   XmString  mod_msg = NULL;
  323.   XmString   mod_time = NULL;
  324.   XmString  chg_msg = NULL;
  325.   XmString   chg_time = NULL;
  326.   XmString  accstring = NULL;
  327.   XmString  modstring = NULL;
  328.   XmString  chgstring = NULL;
  329.   XmString  pathstring = NULL;
  330.   XmString  carr_ret = NULL;
  331.  
  332.     int     n, stat_val;
  333.  
  334.     static XmStringCharSet   charset = (XmStringCharSet) XmFONTLIST_DEFAULT_TAG;
  335.     char  *path;
  336.  
  337.     struct stat bufr;
  338.     char *acctimptr, *modtimptr, *chgtimptr;
  339.     char *text, *text1, *text2;
  340.     Boolean testit, testit1, testit2;
  341.  
  342.     n = 0;
  343.  
  344.   carr_ret = XmStringCreateLtoR ("\012\012", charset);
  345.   pathstring = XmStringConcat (call_data->value, carr_ret);
  346.   testit = XmStringGetLtoR (call_data->value, charset, &path); 
  347.  
  348.    stat_val = stat (path, &bufr);
  349.  
  350.    acctimptr = ctime(&bufr.st_atime);
  351.   access_time = XmStringCreateLtoR (acctimptr, charset);
  352.   access_msg = XmStringCreateLtoR ("Last access time = ", charset);
  353.     accstring = XmStringConcat (access_msg, access_time);
  354.  
  355.    modtimptr = ctime(&bufr.st_mtime);
  356.   mod_time = XmStringCreateLtoR (modtimptr, charset);
  357.   mod_msg = XmStringCreateLtoR ("Last modification time = ", charset);
  358.     modstring = XmStringConcat (mod_msg, mod_time);
  359.  
  360.    chgtimptr = ctime(&bufr.st_ctime);
  361.   chg_time = XmStringCreateLtoR (chgtimptr, charset);
  362.   chg_msg = XmStringCreateLtoR ("Last change time = ", charset);
  363.     chgstring = XmStringConcat (chg_msg, chg_time);
  364.  
  365.     message_string = XmStringConcat (accstring, modstring);
  366.     message_string1 = XmStringConcat (message_string, chgstring);
  367.     final_message_string = XmStringConcat (pathstring, message_string1);
  368.  
  369.   if (stat_val != 0)       /*  Error condition from stat */
  370.     final_message_string = XmStringCreateLtoR ("Invalid file or directory.", charset);
  371.  
  372.   /* Create compound strings for message box button labels */
  373.   button_string = XmStringCreateLtoR ("Close", charset);
  374.   title_string = XmStringCreateLtoR ("File Times", charset);
  375.  
  376.   /*  Create message box dialog. */
  377.     n = 0;
  378.     XtSetArg (args[n], XmNmessageString, final_message_string); n++;
  379.   XtSetArg (args[n], XmNdialogTitle, title_string);  n++;
  380.   XtSetArg (args[n], XmNokLabelString, button_string);  n++;
  381.   message_box = XmCreateMessageDialog (w, "helpbox", args, n);
  382.         XtManageChild (message_box);
  383.   button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
  384.   XtUnmanageChild (button);
  385.   button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
  386.   XtUnmanageChild (button);
  387.  
  388. }
  389.