home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume14 / mush6.0 / part02 / folders.c < prev    next >
C/C++ Source or Header  |  1988-04-12  |  6KB  |  193 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[MAXPATHLEN];
  23.     char *tmp, *newfolder = NULL, buf[MAXPATHLEN];
  24.     struct stat statbuf;
  25.  
  26.     if (ison(glob_flags, DO_PIPE)) {
  27.     print("You can't pipe to the folder command.\n");
  28.     return -1;
  29.     }
  30.     while (*++argv && (**argv == '-' || **argv == '!'))
  31.     if (!strcmp(*argv, "-?"))
  32.         return help(0, "folder_help", cmd_help);
  33.     else if (!strcmp(*argv, "-N"))
  34.         no_hdrs = 1;
  35.     else if (!strcmp(*argv, "-r"))
  36.         do_read_only = 1;
  37.     else if (!strcmp(*argv, "!"))
  38.         turnoff(glob_flags, DO_UPDATE);
  39.  
  40.     if (updating) {
  41.     (void) strcpy(buf, mailfile);
  42.     if (ison(glob_flags, READ_ONLY))
  43.         do_read_only = 1;
  44.     } else {
  45.     if (!*argv) {
  46.         mail_status(0);
  47.         return 0;
  48.     }
  49.     if (!strcmp(*argv, "#"))
  50.         if (!*oldfolder) {
  51.         print("No previous folder\n");
  52.         return -1;
  53.         } else
  54.         newfolder = oldfolder;
  55.     else if (!strcmp(*argv, "&")) {
  56.         if (!(newfolder = do_set(set_options, "mbox")) || !*newfolder)
  57.         newfolder = DEF_MBOX;
  58.     } else
  59.         newfolder = *argv;
  60.     n = 0;
  61.     tmp = getpath(newfolder, &n);
  62.     if (n == -1) {
  63.         print("%s: %s\n", newfolder, tmp);
  64.         return -1;
  65.     } else if (n == 1) {
  66.         print("%s: is a directory\n", tmp);
  67.         return -1;
  68.     }
  69.     /* strcpy so copyback() below (which calls getpath) doesn't change
  70.      * the data that tmp intended to point to.
  71.      */
  72.     (void) strcpy(buf, tmp);
  73.     }
  74.     if (stat(buf, &statbuf) == -1 || !(statbuf.st_mode & 0400)) {
  75.     error("Unable to read %s", buf);
  76.     return -1;
  77.     }
  78.     /* If the file can't be opened for writing, autoset READ_ONLY */
  79.     if (!(statbuf.st_mode & 0200))
  80.     do_read_only = 1;
  81. #ifdef SUNTOOL
  82.     if (istool) lock_cursors();
  83. #endif /* SUNTOOL */
  84.     if (ison(glob_flags, DO_UPDATE) && !copyback()) {
  85. #ifdef SUNTOOL
  86.     if (istool) unlock_cursors();
  87. #endif /* SUNTOOL */
  88.     /* could be an error, but new mail probably came in */
  89.     return -1;
  90.     }
  91.     if (!updating)
  92.     (void) strcpy(oldfolder, mailfile);
  93.     strdup(mailfile, buf);
  94.     do_read_only? turnon(glob_flags,READ_ONLY) : turnoff(glob_flags,READ_ONLY);
  95.     last_size = spool_size = 0L;
  96.     msg_cnt = 0;
  97.     turnoff(glob_flags, CONT_PRNT);
  98.  
  99.     turnon(glob_flags, IGN_SIGS);
  100.     /* clear the tempfile */
  101.     fclose(tmpf);
  102.     if (!do_read_only) {
  103.     if (!(tmpf = fopen(tempfile, "w"))) {
  104.         error("error truncating %s", tempfile);
  105.         turnoff(glob_flags, IGN_SIGS);
  106.         return -1;
  107.     }
  108.     } else if (!(tmpf = fopen(mailfile, "r"))) {
  109.     error(mailfile);
  110.     return -1;
  111.     }
  112.     getmail();
  113.     last_msg_cnt = msg_cnt;  /* for check_new_mail */
  114.     (void) mail_size();
  115. #ifdef SUNTOOL
  116.     if (istool) {
  117.     panel_set(next_scr, PANEL_SHOW_ITEM, FALSE, 0);
  118.     panel_set(prev_scr, PANEL_SHOW_ITEM, FALSE, 0);
  119.     pw_rop(hdr_win, 0,0, hdr_rect.r_width, hdr_rect.r_height,PIX_CLR,0,0,0);
  120.     if (!msg_cnt) {
  121.         add_folder_to_menu(folder_item, 3);
  122.         add_folder_to_menu(save_item, 1);
  123.     }
  124.     }
  125. #endif /* SUNTOOL */
  126.     current_msg = 0;
  127.     turnoff(glob_flags, IGN_SIGS);
  128.  
  129.     /* now sort messages according a user_defined default */
  130.     if (!updating && msg_cnt > 1 && !strcmp(mailfile, spoolfile) &&
  131.         (tmp = do_set(set_options, "sort"))) {
  132.     (void) sprintf(buf, "sort %s", tmp);
  133.     if (argv = make_command(buf, TRPL_NULL, &argc)) {
  134.         /* msg_list can't be null for do_command and since we're not
  135.          * interested in the result, call sort directly
  136.          */
  137.         (void) sort(argc, argv, NULL);
  138.         free_vec(argv);
  139.     }
  140.     }
  141.     turnoff(glob_flags, DO_UPDATE);
  142.  
  143.     /* go to first NEW message */
  144.     while (current_msg < msg_cnt && ison(msg[current_msg].m_flags, OLD))
  145.     current_msg++;
  146.     if (current_msg == msg_cnt)
  147.     /* no new message found -- try first unread message */
  148.     for (current_msg = 0;
  149.          current_msg < msg_cnt && isoff(msg[current_msg].m_flags, UNREAD);
  150.          current_msg++)
  151.          ;
  152.     if (current_msg == msg_cnt)
  153.     current_msg = 0;
  154.  
  155.     if ((!istool || istool && !msg_cnt) && !iscurses)
  156.     mail_status(0);
  157.     /* be quite if we're piping */
  158.     if ((istool || !updating) && isoff(glob_flags, IS_PIPE) &&
  159.     (istool || !no_hdrs) && msg_cnt)
  160.     (void) cmd_line(sprintf(buf, "headers %d", current_msg+1), msg_list);
  161. #ifdef SUNTOOL
  162.     if (istool) {
  163.     if (!msg_cnt)
  164.         print("No Mail in %s\n", mailfile);
  165.     if (!getting_opts)
  166.         if (msg_cnt && isoff(glob_flags, IS_GETTING))
  167.         display_msg(current_msg, (long)0);
  168.         else
  169.         do_clear();
  170.     unlock_cursors();
  171.     }
  172. #endif /* SUNTOOL */
  173.     if (list) {
  174.     clear_msg_list(list);
  175.     bitput(list, list, msg_cnt, =~) /* macro */
  176.     }
  177.     return 0;
  178. }
  179.  
  180. folders(argc, argv)
  181. register char **argv;
  182. {
  183.     register char *p;
  184.     char buf[128], unused[MAXMSGS_BITS];
  185.  
  186.     if (!(p = do_set(set_options, "folder")) || !*p)
  187.     p = DEF_FOLDER;
  188.     (void) sprintf(buf, "ls %s", p);
  189.     if (argv = make_command(buf, TRPL_NULL, &argc))
  190.     (void) do_command(argc, argv, unused);
  191.     return -1;
  192. }
  193.