home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / pcmail / part03 / file.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  7KB  |  229 lines

  1. /*++
  2. /* NAME
  3. /*      file 3
  4. /* SUMMARY
  5. /*      manipulate ordinary files
  6. /* PROJECT
  7. /*      pc-mail
  8. /* PACKAGE
  9. /*      mailsh
  10. /* SYNOPSIS
  11. /*      int file()
  12. /*
  13. /*    int junk_file()
  14. /* DESCRIPTION
  15. /*      file() is called when the user selected the "file" option
  16. /*    in the main menu. The pager displays a directory listing on the
  17. /*      screen. The user may then walk through the file display and
  18. /*    select a file or directory. 
  19. /*
  20. /*    In case a regular file is selected the user may specify
  21. /*    an action (send file as mail, delete file, display
  22. /*    file, print file, copy file to workfile for subsequent
  23. /*    editing etc.).
  24. /*
  25. /*    junk_file() should be invoked when the file display is no longer
  26. /*    valid.
  27. /* FILES
  28. /*      By selecting directory files the user can walk through the whole
  29. /*      file system.
  30. /* SEE ALSO
  31. /*      kbdinp(3), pager(3)
  32. /* BUGS
  33. /*    On some MS-DOS systems, we never get the ".." entry.
  34. /*    This is an unsolved problem.
  35. /*
  36. /*    Changing the current directory may cause problems if
  37. /*    the MAILDIR and EDITOR environment variables hold
  38. /*    relative path names.
  39. /*
  40. /*    No provision to change default drive on MS-DOS systems.
  41. /* AUTHOR(S)
  42. /*      W.Z. Venema
  43. /*      Eindhoven University of Technology
  44. /*      Department of Mathematics and Computer Science
  45. /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  46. /* CREATION DATE
  47. /*      Sun Apr  5 17:18:06 GMT+1:00 1987
  48. /* LAST MODIFICATION
  49. /*    Mon Apr  4 23:40:13 MET 1988
  50. /* VERSION/RELEASE
  51. /*    1.3
  52. /*--*/
  53.  
  54. #include <time.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include <ctype.h>
  58. #include "defs.h"
  59. #include "screen.h"
  60. #include "pager.h"
  61. #include "mailsh.h"
  62. #include "dir.h"
  63. #include "path.h"
  64. #include "status.h"
  65. #include "window.h"
  66.  
  67. extern struct tm *localtime();          /* std C library */
  68.  
  69. hidden File *bldlist();            /* forward declaration */
  70. hidden int show_list();
  71. hidden int pick_file();
  72. hidden int delfile();
  73. hidden int show_file();
  74. hidden int rmfile();
  75.  
  76. hidden File *dfile = 0;            /* file listing */
  77. hidden File *dirfile = 0;               /* directory listing */
  78.  
  79. /* file - called from keyboard interpreter, call keyboard driver */
  80.  
  81. public int file()
  82. {
  83.     static Screen screen[] = {
  84.     'C',    "Close",        0,      initscreen,
  85.     'P',    "Print",        print,    "Print file listing",
  86.     'S',    "Save",         save,    "Save file listing to ordinary file",
  87.     PGUP,    PgUp,        pu_pager,pageup,
  88.     PGDN,    PgDn,        pd_pager,pagedn,
  89.     UP,    "Up",           up_pager,csrup,
  90.     DOWN,    "Down",         dn_pager,csrdn,
  91.     ENTER,    "Enter",        pick_file,"Display file at cursor",
  92.     0,    0,              show_list,
  93.     "To display a file, select it with cursor keys, then press ENTER",
  94.     };
  95.  
  96.     kbdinp(screen);            /* set up dialogue */
  97.     close_pager(dirfile);        /* deallocate pager file */
  98.     dirfile = 0;            /* say it's gone */
  99.     return(S_REDRAW);            /* force screen update */
  100. }
  101.  
  102. /* show_list - create or refresh file listing */
  103.  
  104. hidden int show_list()
  105. {
  106.     if (dirfile == 0) {            /* no pager file? */
  107.     patience();
  108.     dirfile = bldlist();        /* create one */
  109.     } else {
  110.     set_pager(dirfile);        /* select existing pager file */
  111.     }
  112.     ds_pager();                /* display the thing */
  113.     return(0);                /* say screen is up-to-date */
  114. }
  115.  
  116. /* bldlist - build file listing display */
  117.  
  118. hidden File *bldlist()
  119. {
  120.     register int dd = opendir(THISDIR);    /* start directory search */
  121.     register File *pp = open_pager();    /* allocate pager file */
  122.     register char *f;            /* file name pointer */
  123.  
  124.     while (f = readdir(dd)) {        /* read next file name */
  125.     struct stat s;
  126.     if (stat(f,&s) == 0) {        /* get file info (size, date) */
  127.         app_pager(pp,strcons("%-20s %9ld %s",f,
  128.         s.st_size,tstamp(&s.st_mtime)));
  129.     } else {
  130.         app_pager(pp,f);
  131.     }
  132.     }
  133.     sort_pager(pp,FORW_SORT);        /* sort by file name */
  134.     closedir(dd);            /* directory search done */
  135.     return(pp);                /* current pager file */
  136. }
  137.  
  138. /* pick_file - display selected file or change directory */
  139.  
  140. hidden int pick_file()
  141. {
  142.     static char botline[80];
  143.     static Screen screen[] = {
  144.     'C',    "Close",        0,      "Return to file listing",
  145.     'D',    "Delete",    delfile,"Delete this file",
  146.     'M',    "Mail",        mailfile,"Mail a copy of this file",
  147.     'P',    "Print",        print,    "Print this file",
  148.     'S',    "Save",         save,    "Save a copy to an ordinary file",
  149.     'W',    "Work",        makework,"Save a copy to a work file",
  150.     PGUP,    PgUp,        pu_pager,pageup,
  151.     PGDN,    PgDn,        pd_pager,pagedn,
  152.     UP,    "Up",           up_pager,csrup,
  153.     DOWN,    "Down",         dn_pager,csrdn,
  154.     0,    0,              show_file,
  155.     botline,
  156.     };
  157.     struct stat s;
  158.  
  159.     if (scan_pager(dirfile,"%s",message) == 0) {    /* cannot read name */
  160.     beep();                        /* "notify" user */
  161.     return(0);                    /* nothing happened */
  162.     } else if (access(message,04) || stat(message,&s)) {/* cannot read file */
  163.     beep();                        /* "notify" user */
  164.     return(0);                    /* nothing happened */
  165.     } else if ((s.st_mode&S_IFMT) == S_IFDIR) {         /* directory file */
  166.     chdir(message);                    /* change directory */
  167.     close_pager(dirfile);                /* purge display */
  168.     dirfile = bldlist();                /* new file display */
  169.     return(S_REDRAW);                /* update display */
  170.     } else {                                            /* ordinary file */
  171.      sprintf(botline,"(Reading file \"%s\")",message);
  172.     kbdinp(screen);                    /* look at screen */
  173.     close_pager(dfile);                /* and forget it */
  174.     dfile = 0;                    /* say it's gone */
  175.     return(S_REDRAW);                               /* force redrawing */
  176.     }
  177. }
  178.  
  179. /* show_file - create or refresh display of selected file */
  180.  
  181. hidden int show_file()
  182. {
  183.     if (dfile) {
  184.     set_pager(dfile);                /* select dispay */
  185.     } else {
  186.     if (rd_pager(dfile = open_pager(),message))    /* try to read file */
  187.         mesg_pager(dfile,m_msgread);        /* read error */
  188.     }
  189.     ds_pager();                        /* rewrite screen */
  190.     return(0);                        /* screen up-to-date */
  191. }
  192.  
  193. /* delfile - ask confirmation before removing a file */
  194.  
  195. hidden int delfile()
  196. {
  197.     static Screen screen[] = {
  198.     ESCCR,    0,        rmfile,int_error,
  199.     0,    0,        0,
  200.     "Press ESC to cancel. Confirm with ENTER",
  201.     };
  202.  
  203.     return(kbdinp(screen)|S_REDRAW);        /* "are you sure?" */
  204. }
  205.  
  206. /* rmfile - actually remove file */
  207.  
  208. hidden int rmfile()
  209. {
  210.     if (unlink(message)) {            /* try to remove file */
  211.     errdisp(E_UNLINK);            /* notify the user */
  212.     return(S_BREAK|S_REDRAW);        /* redisplay, stop caller */
  213.     } else {
  214.     junk_file();                /* say file display outdated */
  215.     return(S_BREAK);            /* terminate caller */
  216.     }
  217. }
  218.  
  219. /* junk_file - directory listing is no longer up to date */
  220.  
  221. public int junk_file()
  222. {
  223.     if (dirfile) {
  224.     close_pager(dirfile);            /* close pager file */
  225.     dirfile = 0;                /* say it is gone */
  226.     }
  227.     return(0);                    /* in case we need that */
  228. }
  229.