home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / mush5.7 / part02 / folders.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-16  |  4.8 KB  |  171 lines

  1. /* @(#)folders.c    (c) copyright 10/18/86 (Dan Heller) */
  2.  
  3. #include "mush.h"
  4.  
  5. /* folder %[user]  --new mailfile is the spool/mail/login file [user].
  6.  * folder #  --new mailfile is the folder previous to the current folder
  7.  * folder &  --new mailfile is ~/mbox (or whatever "mbox" is set to)
  8.  * folder +file --new mailfile is in the directory "folder"; name is 'file'
  9.  * folder "path" --full path name or the one in current working directory.
  10.  *
  11.  * in all cases, changes are updated unless a '!' is specified after the
  12.  * folder command (e.g. "f!", "folder !" "fo!" .. all permutations)
  13.  * as usual, if new mail has arrived before the file is copied back, then
  14.  * user will be notified beforehand.
  15.  *
  16.  * RETURN -1 on error -- else return 0. All bits in msg_list are set to true.
  17.  */
  18. folder(argc, argv, list)
  19. register char **argv, list[];
  20. {
  21.     int n, updating = !strcmp(*argv, "update"), do_read_only = 0, no_hdrs = 0;
  22.     static char oldfolder[256];
  23.     char *tmp, *newfolder = NULL, buf[256];
  24.  
  25.     if (ison(glob_flags, DO_PIPE)) {
  26.     print("You can't pipe to the folder command");
  27.     return -1;
  28.     }
  29.     while (*++argv && (**argv == '-' || **argv == '!'))
  30.     if (!strcmp(*argv, "-?"))
  31.         return help(0, "folder_help", cmd_help);
  32.     else if (!strcmp(*argv, "-N"))
  33.         no_hdrs = 1;
  34.     else if (!strcmp(*argv, "-r"))
  35.         do_read_only = 1;
  36.     else if (!strcmp(*argv, "!"))
  37.         turnoff(glob_flags, DO_UPDATE);
  38.  
  39.     if (updating)
  40.     (void) strcpy(buf, mailfile);
  41.     else {
  42.     if (!*argv) {
  43.         print("%s\n", mailfile);
  44.         return 0;
  45.     }
  46.     if (!strcmp(*argv, "#"))
  47.         if (!*oldfolder) {
  48.         print("No previous folder\n");
  49.         return -1;
  50.         } else
  51.         newfolder = oldfolder;
  52.     else if (!strcmp(*argv, "&")) {
  53.         if (!(newfolder = do_set(set_options, "mbox")) || !*newfolder)
  54.         newfolder = DEF_MBOX;
  55.     } else
  56.         newfolder = *argv;
  57.     n = 0;
  58.     tmp = getpath(newfolder, &n);
  59.     if (n == -1) {
  60.         print("%s: %s\n", newfolder, tmp);
  61.         return -1;
  62.     } else if (n == 1) {
  63.         print("%s: is a directory\n", tmp);
  64.         return -1;
  65.     }
  66.     /* strcpy so copyback() below (which calls getpath) doesn't change
  67.      * the data that tmp intended to point to.
  68.      */
  69.     (void) strcpy(buf, tmp);
  70.     }
  71.     if (access(buf, R_OK)) {
  72.     error("Can't open %s", buf);
  73.     return -1;
  74.     }
  75.     if (access(buf, W_OK))
  76.     do_read_only = 1;
  77. #ifdef SUNTOOL
  78.     if (istool) lock_cursors();
  79. #endif SUNTOOL
  80.     if (ison(glob_flags, DO_UPDATE) && !copyback()) {
  81. #ifdef SUNTOOL
  82.     if (istool) unlock_cursors();
  83. #endif SUNTOOL
  84.     /* could be an error, but new mail probably came in */
  85.     return -1;
  86.     }
  87.     (void) strcpy(oldfolder, mailfile);
  88.     strdup(mailfile, buf);
  89.     do_read_only? turnon(glob_flags,READ_ONLY) : turnoff(glob_flags,READ_ONLY);
  90.     last_size = spool_size = 0L;
  91.     msg_cnt = 0;
  92.     turnoff(glob_flags, CONT_PRNT);
  93.  
  94.     turnon(glob_flags, IGN_SIGS);
  95.     /* clear the tempfile */
  96.     fclose(tmpf);
  97.     if (!(tmpf = fopen(tempfile, "w"))) {
  98.     error("error truncating %s", tempfile);
  99.     turnoff(glob_flags, IGN_SIGS);
  100.     return -1;
  101.     }
  102.     getmail();
  103.     last_msg_cnt = msg_cnt;  /* for check_new_mail */
  104.     (void) mail_size();
  105. #ifdef SUNTOOL
  106.     if (istool) {
  107.     panel_set(next_scr, PANEL_SHOW_ITEM, FALSE, 0);
  108.     panel_set(prev_scr, PANEL_SHOW_ITEM, FALSE, 0);
  109.     pw_rop(hdr_win, 0,0, hdr_rect.r_width, hdr_rect.r_height,PIX_CLR,0,0,0);
  110.     }
  111. #endif SUNTOOL
  112.     current_msg = 0;
  113.     turnoff(glob_flags, IGN_SIGS);
  114.  
  115.     /* now sort messages according a user_defined default */
  116.     if (!updating && msg_cnt > 1 && is_spool(mailfile) &&
  117.         (tmp = do_set(set_options, "sort"))) {
  118.     (void) sprintf(buf, "sort %s", tmp);
  119.     if (argv = make_command(buf, TRPL_NULL, &argc)) {
  120.         /* msg_list can't be null for do_command and since we're not
  121.          * interested in the result, call sort directly
  122.          */
  123.         (void) sort(argc, argv, NULL);
  124.         free_vec(argv);
  125.     }
  126.     }
  127.     turnoff(glob_flags, DO_UPDATE);
  128.  
  129.     while (current_msg < msg_cnt && isoff(msg[current_msg].m_flags, UNREAD))
  130.     current_msg++;
  131.     if (current_msg == msg_cnt)
  132.     current_msg = 0;
  133.  
  134.     if (!istool && !iscurses)
  135.     mail_status(0);
  136.     /* be quite if we're piping */
  137.     if ((istool || !updating) && isoff(glob_flags, IS_PIPE) &&
  138.     (istool || !no_hdrs) && msg_cnt)
  139.     (void) cmd_line(sprintf(buf, "headers %d", current_msg+1), msg_list);
  140. #ifdef SUNTOOL
  141.     if (istool) {
  142.     if (!msg_cnt)
  143.         print("No Mail in %s\n", mailfile);
  144.     else if (isoff(glob_flags, IS_GETTING) && !getting_opts)
  145.         display_msg(current_msg, (long)0);
  146.     unlock_cursors();
  147.     }
  148. #endif SUNTOOL
  149.     if (list) {
  150.     clear_msg_list(list);
  151.     bitput(list, list, msg_cnt, =~) /* macro */
  152.     }
  153.     return 0;
  154. }
  155.  
  156. folders(argc, argv)
  157. register char **argv;
  158. {
  159.     register char *p;
  160.     char buf[128], unused[MAXMSGS_BITS];
  161.  
  162.     if (!(p = do_set(set_options, "folder")) || !*p) {
  163.     print("You have no folder variable set.\n");
  164.     return -1;
  165.     }
  166.     (void) sprintf(buf, "ls %s", p);
  167.     if (argv = make_command(buf, TRPL_NULL, &argc))
  168.     (void) do_command(argc, argv, unused);
  169.     return -1;
  170. }
  171.