home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / elm.lzh / ELM / SRC / LEAVEMBOX.C < prev    next >
Text File  |  1991-01-11  |  22KB  |  737 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: leavembox.c,v 4.1.1.4 90/08/15 21:00:07 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.4 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    leavembox.c,v $
  17.  * Revision 4.1.1.4  90/08/15  21:00:07  syd
  18.  * Change elm to not delete empty folders on a resync
  19.  * From: Syd
  20.  * 
  21.  * Revision 4.1.1.3  90/06/21  22:51:52  syd
  22.  * Add time.h to includes as some OSs include needed substructure only
  23.  * from time.h
  24.  * From: Syd
  25.  * 
  26.  * Revision 4.1.1.2  90/06/21  22:48:14  syd
  27.  * patch to fix up the Log headers.
  28.  * From: pdc%lunch.wpd@sgi.com (Paul Close)
  29.  * 
  30.  * Revision 4.1.1.1  90/06/09  21:33:23  syd
  31.  * Some flock()s refuse to exclusively lock a fd open for read-only access.
  32.  * From: pdc%lunch.wpd@sgi.com (Paul Close)
  33.  * 
  34.  * Revision 4.1  90/04/28  22:43:18  syd
  35.  * checkin of Elm 2.3 as of Release PL0
  36.  * 
  37.  *
  38.  ******************************************************************************/
  39.  
  40. /** leave current folder, updating etc. as needed...
  41.   
  42. **/
  43.  
  44. #include "headers.h"
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #ifdef LOCK_BY_FLOCK
  48. #include <sys/file.h>
  49. #endif
  50. #include <errno.h>
  51. #ifdef I_TIME
  52. #  include <time.h>
  53. #endif
  54. #ifdef I_SYSTIME
  55. #  include <sys/time.h>
  56. #endif
  57.  
  58.  
  59. #define EEXIST E_CEF
  60.  
  61. /**********
  62.    Since a number of machines don't seem to bother to define the utimbuf
  63.    structure for some *very* obscure reason.... 
  64.  
  65.    Suprise, though, BSD has a different utime() entirely...*sigh*
  66. **********/
  67.  
  68. #ifndef BSD
  69. # ifdef NOUTIMBUF
  70.  
  71. struct utimbuf {
  72.     time_t    actime;        /** access time       **/ 
  73.     time_t    modtime;    /** modification time **/
  74.        };
  75.  
  76.  
  77. # endif /* NOUTIMBUF */
  78. #endif /* BSD */
  79.  
  80. extern int errno;
  81.  
  82. char *error_name(), *error_description(), *strcpy(), *rindex();
  83. unsigned short getegid();
  84. #ifndef    _POSIX_SOURCE
  85. unsigned long sleep();
  86. #endif
  87.  
  88. int
  89. leave_mbox(resyncing, quitting, prompt)
  90. int resyncing, quitting, prompt;
  91. {
  92.     /** Close folder, deleting some messages, storing others in mbox,
  93.         and keeping others, as directed by user input and elmrc options.
  94.  
  95.         Return    1    Folder altered
  96.             0    Folder not altered
  97.             -1    New mail arrived during the process and
  98.                     closing was aborted.
  99.         If "resyncing" we are just writing out folder to reopen it. We
  100.         therefore only consider deletes and keeps, not stores to mbox.
  101.         Also we don't remove NEW status so that it can be preserved
  102.         across the resync.
  103.  
  104.         If "quitting" and "prompt" is false, then no prompting is done.
  105.         Otherwise prompting is dependent upon the variable
  106.         question_me, as set by an elmrc option.  This behavior makes
  107.         the 'q' command prompt just like 'c' and '$', while
  108.         retaining the 'Q' command for a quick exit that never
  109.         prompts.
  110.     **/
  111.  
  112.     FILE *temp;
  113.     char temp_keep_file[SLEN], buffer[SLEN];
  114.     struct stat    buf;        /* stat command  */
  115. #ifdef BSD
  116.     time_t utime_buffer[2];        /* utime command */
  117. #else
  118.     struct utimbuf utime_buffer;    /* utime command */
  119. #endif
  120.     register int to_delete = 0, to_store = 0, to_keep = 0, i,
  121.              marked_deleted, marked_read, marked_unread,
  122.              last_sortby, ask_questions,  asked_storage_q,
  123.              num_chgd_status, need_to_copy;
  124.     char answer;
  125.     long bytes();
  126.  
  127.     dprint(1, (debugfile, "\n\n-- leaving folder --\n\n"));
  128.  
  129.     if (message_count == 0)
  130.       return(0);    /* nothing changed */
  131.  
  132.     ask_questions = ((quitting && !prompt) ? FALSE : question_me);
  133.  
  134.     /* YES or NO on softkeys */
  135.     if (hp_softkeys && ask_questions) {
  136.       define_softkeys(YESNO);
  137.       softkeys_on();
  138.     }
  139.  
  140.     /* Clear the exit dispositions of all messages, just in case
  141.      * they were left set by a previous call to this function
  142.      * that was interrupted by the receipt of new mail.
  143.      */
  144.     for(i = 0; i < message_count; i++)
  145.       headers[i]->exit_disposition = UNSET;
  146.       
  147.     /* Determine if deleted messages are really to be deleted */
  148.  
  149.     /* we need to know if there are none, or one, or more to delete */
  150.     for (marked_deleted=0, i=0; i<message_count && marked_deleted<2; i++)
  151.       if (ison(headers[i]->status, DELETED))
  152.         marked_deleted++;
  153.  
  154.         if(marked_deleted) {
  155.       answer = (always_del ? 'y' : 'n');    /* default answer */
  156.       if(ask_questions) {
  157.         sprintf(buffer, "Delete message%s? (y/n) ", plural(marked_deleted));
  158.         answer = want_to(buffer, answer);
  159.       }
  160.  
  161.       if(answer == 'y') {
  162.         for (i = 0; i < message_count; i++) {
  163.           if (ison(headers[i]->status, DELETED)) {
  164.         headers[i]->exit_disposition = DELETE;
  165.         to_delete++;
  166.           }
  167.         }
  168.       }
  169.     }
  170.     dprint(3, (debugfile, "Messages to delete: %d\n", to_delete));
  171.  
  172.     /* If this is a non spool file, or if we are merely resyncing,
  173.      * all messages with an unset disposition (i.e. not slated for
  174.      * deletion) are to be kept.
  175.      * Otherwise, we need to determine if read and unread messages
  176.      * are to be stored or kept.
  177.      */
  178.     if(folder_type == NON_SPOOL || resyncing) {
  179.       to_store = 0;
  180.       for (i = 0; i < message_count; i++) {
  181.         if(headers[i]->exit_disposition == UNSET) {
  182.           headers[i]->exit_disposition = KEEP;
  183.           to_keep++;
  184.         }
  185.       }
  186.     } else {
  187.  
  188.       /* Let's first see if user wants to store read messages 
  189.        * that aren't slated for deletion */
  190.  
  191.       asked_storage_q = FALSE;
  192.  
  193.       /* we need to know if there are none, or one, or more marked read */
  194.       for (marked_read=0, i=0; i < message_count && marked_read < 2; i++) {
  195.         if((isoff(headers[i]->status, UNREAD))
  196.           && (headers[i]->exit_disposition == UNSET))
  197.         marked_read++;
  198.       }
  199.       if(marked_read) {
  200.         answer = (always_store ? 'y' : 'n');    /* default answer */
  201.         if(ask_questions) {
  202.           sprintf(buffer, "Move read message%s to \"received\" folder? (y/n) ",
  203.             plural(marked_read));
  204.           answer = want_to(buffer, answer);
  205.           asked_storage_q = TRUE;
  206.         }
  207.  
  208.         for (i = 0; i < message_count; i++) {
  209.           if((isoff(headers[i]->status, UNREAD)) 
  210.         && (headers[i]->exit_disposition == UNSET)) {
  211.  
  212.           if(answer == 'y') {
  213.             headers[i]->exit_disposition = STORE;
  214.             to_store++;
  215.           } else {
  216.             headers[i]->exit_disposition = KEEP;
  217.             to_keep++;
  218.           }
  219.           }
  220.         } 
  221.       }
  222.  
  223.       /* If we asked the user if read messages should be stored,
  224.        * and if the user wanted them kept instead, then certainly the
  225.        * user would want the unread messages kept as well.
  226.        */
  227.       if(asked_storage_q && answer == 'n') {
  228.  
  229.         for (i = 0; i < message_count; i++) {
  230.           if((ison(headers[i]->status, UNREAD))
  231.         && (headers[i]->exit_disposition == UNSET)) {
  232.           headers[i]->exit_disposition = KEEP;
  233.           to_keep++;
  234.           }
  235.         }
  236.  
  237.       } else {
  238.  
  239.         /* Determine if unread messages are to be kept */
  240.  
  241.         /* we need to know if there are none, or one, or more unread */
  242.         for (marked_unread=0, i=0; i<message_count && marked_unread<2; i++)
  243.           if((ison(headers[i]->status, UNREAD))
  244.         && (headers[i]->exit_disposition == UNSET))
  245.           marked_unread++;
  246.  
  247.         if(marked_unread) {
  248.           answer = (always_keep ? 'y' : 'n');    /* default answer */
  249.           if(ask_questions) {
  250.         sprintf(buffer,
  251.           "Keep unread message%s in incoming mailbox? (y/n) ",
  252.           plural(marked_unread));
  253.         answer = want_to(buffer, answer);
  254.           }
  255.  
  256.           for (i = 0; i < message_count; i++) {
  257.         if((ison(headers[i]->status, UNREAD))
  258.           && (headers[i]->exit_disposition == UNSET)) {
  259.  
  260.             if(answer == 'n') {
  261.               headers[i]->exit_disposition = STORE;
  262.               to_store++;
  263.             } else {
  264.               headers[i]->exit_disposition = KEEP;
  265.               to_keep++;
  266.             }
  267.           
  268.         }
  269.           }
  270.         }
  271.       }
  272.     }
  273.  
  274.     dprint(3, (debugfile, "Messages to store: %d\n", to_store));
  275.     dprint(3, (debugfile, "Messages to keep: %d\n", to_keep));
  276.  
  277.     if(to_delete + to_store + to_keep != message_count) {
  278.       dprint(1, (debugfile,
  279.       "Error: %d to delete + %d to store + %d to keep != %d message cnt\n",
  280.         to_delete, to_store, to_keep, message_count));
  281.       error("Something wrong in message counts! Folder unchanged.");
  282.       emergency_exit();
  283.     }
  284.       
  285.  
  286.     /* If we are not resyncing, we are leaving the mailfile and
  287.      * the new messages are new no longer. Note that this changes
  288.      * their status.
  289.      */
  290.     if(!resyncing) {
  291.       for (i = 0; i < message_count; i++) {
  292.         if (ison(headers[i]->status, NEW)) {
  293.           clearit(headers[i]->status, NEW);
  294.           headers[i]->status_chgd = TRUE;
  295.         }
  296.       }
  297.     }
  298.  
  299.     /* If all messages are to be kept and none have changed status
  300.      * we don't need to do anything because the current folder won't
  301.      * be changed by our writing it out - unless we are resyncing, in
  302.      * which case we force the writing out of the mailfile.
  303.      */
  304.  
  305.     for (num_chgd_status = 0, i = 0; i < message_count; i++)
  306.       if(headers[i]->status_chgd == TRUE)
  307.         num_chgd_status++;
  308.     
  309.     if(!to_delete && !to_store && !num_chgd_status && !resyncing) {
  310.       dprint(3, (debugfile, "Folder keep as is!\n"));
  311.       error("Folder unchanged.");
  312.       return(0);
  313.     }
  314.  
  315.     /** we have to check to see what the sorting order was...so that
  316.         the order in which we write messages is the same as the order
  317.         of the messages originally.
  318.         We only need to do this if there are any messages to be
  319.         written out (either to keep or to store). **/
  320.  
  321.     if ((to_keep || to_store ) && sortby != MAILBOX_ORDER) {
  322.       last_sortby = sortby;
  323.       sortby = MAILBOX_ORDER;
  324.       sort_mailbox(message_count, FALSE);
  325.       sortby = last_sortby;
  326.     }
  327.  
  328.     /* Formulate message as to number of keeps, stores, and deletes.
  329.      * This is only complex so that the message is good English.
  330.      */
  331.     if (to_keep > 0) {
  332.       if (to_store > 0) {
  333.         if (to_delete > 0)
  334.           sprintf(buffer,
  335.                 "[Keeping %d message%s, storing %d, and deleting %d.]", 
  336.             to_keep, plural(to_keep), to_store, to_delete);
  337.         else
  338.           sprintf(buffer, "[Keeping %d message%s and storing %d.]", 
  339.             to_keep, plural(to_keep), to_store);
  340.       } else {
  341.         if (to_delete > 0)
  342.           sprintf(buffer, "[Keeping %d message%s and deleting %d.]", 
  343.             to_keep, plural(to_keep), to_delete);
  344.         else
  345.           sprintf(buffer, "[Keeping %s.]",
  346.             to_keep > 1 ? "all messages" : "message");
  347.       }
  348.     } else if (to_store > 0) {
  349.       if (to_delete > 0)
  350.         sprintf(buffer, "[Storing %d message%s and deleting %d.]", 
  351.           to_store, plural(to_store), to_delete);
  352.       else 
  353.         sprintf(buffer, "[Storing %s.]",
  354.           to_store > 1? "all messages" : "message");
  355.  
  356.     } else {
  357.       if (to_delete > 0)
  358.         sprintf(buffer, "[Deleting all messages.]");
  359.       else
  360.         buffer[0] = '\0';
  361.     }
  362.     /* NOTE: don't use variable "buffer" till message is output later */
  363.  
  364.     /** next, let's lock the file up and make one last size check **/
  365.  
  366.     if (folder_type == SPOOL)
  367.       lock(OUTGOING);
  368.     
  369.     if (mailfile_size != bytes(cur_folder)) {
  370.       unlock();
  371.       error("New mail has just arrived. Resynchronizing...");
  372.       return(-1);
  373.     }
  374.     
  375.     /* Everything's GO - so ouput that user message and go to it. */
  376.  
  377.     dprint(2, (debugfile, "Action: %s\n", buffer));
  378.     error(buffer);
  379.  
  380.     /* Store messages slated for storage in received mail folder */
  381.     if (to_store > 0) {
  382.       if ((errno = can_open(recvd_mail, "a"))) {
  383.         error1(
  384.           "Permission to append to %s denied!  Leaving folder intact.\n",
  385.           recvd_mail);
  386.         dprint(1, (debugfile,
  387.           "Error: Permission to append to folder %s denied!! (%s)\n",
  388.           recvd_mail, "leavembox"));
  389.         dprint(1, (debugfile, "** %s - %s **\n", error_name(errno),
  390.           error_description(errno)));
  391.         unlock();
  392.         return(0);
  393.       }
  394.       if ((temp = fopen(recvd_mail,"a")) == NULL) {
  395.         unlock();
  396.         dprint(1, (debugfile, "Error: could not append to file %s\n", 
  397.           recvd_mail));
  398.         dprint(1, (debugfile, "** %s - %s **\n", error_name(errno),
  399.           error_description(errno)));
  400.         sprintf(buffer, "Could not append to folder %s!", recvd_mail);
  401.         Centerline(LINES-1, buffer);
  402.         emergency_exit();
  403.       }
  404.       dprint(2, (debugfile, "Storing message%s ", plural(to_store)));
  405.       for (i = 0; i < message_count; i++) {
  406.         if(headers[i]->exit_disposition == STORE) {
  407.           current = i+1;
  408.           dprint(2, (debugfile, "#%d, ", current));
  409.           copy_message("", temp, FALSE, FALSE, TRUE, FALSE);
  410.         }
  411.       }
  412.       fclose(temp);
  413.       dprint(2, (debugfile, "\n\n"));
  414.       ochown(recvd_mail, userid, groupid);
  415.     }
  416.  
  417.     /* If there are any messages to keep, first copy them to a
  418.      * temp file, then remove original and copy whole temp file over.
  419.      */
  420.     if (to_keep > 0) {
  421.       sprintf(temp_keep_file, "%s%s%d", temp_dir, temp_file, getpid());
  422.       if ((errno = can_open(temp_keep_file, "w"))) {
  423.         error1(
  424. "Permission to create temp file %s for writing denied! Leaving folder intact.",
  425.           temp_keep_file);
  426.         dprint(1, (debugfile,
  427.           "Error: Permission to create temp file %s denied!! (%s)\n",
  428.           temp_keep_file, "leavembox"));
  429.         dprint(1, (debugfile, "** %s - %s **\n", error_name(errno),
  430.           error_description(errno)));
  431.         unlock();
  432.         return(0);
  433.       }
  434.       if ((temp = fopen(temp_keep_file,"w")) == NULL) {
  435.         unlock();
  436.         dprint(1, (debugfile, "Error: could not create file %s\n", 
  437.           temp_keep_file));
  438.         dprint(1, (debugfile, "** %s - %s **\n", error_name(errno),
  439.           error_description(errno)));
  440.         sprintf(buffer, "Could not create temp file %s!", temp_keep_file);
  441.         Centerline(LINES-1, buffer);
  442.         emergency_exit();
  443.       }
  444.       dprint(2, (debugfile, "Copying to temp file message%s to be kept ",
  445.         plural(to_keep)));
  446.       for (i = 0; i < message_count; i++) {
  447.         if(headers[i]->exit_disposition == KEEP) {
  448.           current = i+1;
  449.           dprint(2, (debugfile, "#%d, ", current));
  450.           copy_message("", temp, FALSE, FALSE, TRUE, FALSE);
  451.         }
  452.       }
  453.       if ( fclose(temp) == EOF ) {
  454.         Write_to_screen("\l\rClose failed on temp keep file in leavembox\l\r", 0);
  455.         perror(temp_keep_file);
  456.         dprint(2, (debugfile, "\l\rfclose err on temp_keep_file - leavembox\l\r"));
  457.         rm_temps_exit();
  458.       }
  459.       dprint(2, (debugfile, "\n\n"));
  460.  
  461.     } else if (folder_type == NON_SPOOL && !keep_empty_files && !resyncing) {
  462.  
  463.       /* i.e. if no messages were to be kept and this is not a spool
  464.        * folder and we aren't keeping empty non-spool folders,
  465.        * simply remove the old original folder and that's it!
  466.        */
  467.       fclose(mailfile);           /* -hm (hint from hp...) */
  468.       (void)unlink(cur_folder);
  469.       return(1);
  470.     }
  471.  
  472.     /* Otherwise we have some work left to do! */
  473.  
  474.     /* Get original permissions and access time of the original
  475.      * mail folder before we remove it.
  476.      */
  477.     if(save_file_stats(cur_folder) != 0) {
  478.       error1("Problems saving permissions of folder %s!", cur_folder);
  479.       sleep(2);
  480.     }
  481.       
  482.         if (stat(cur_folder, &buf) != 0) {
  483.       dprint(1, (debugfile, "Error: errno %s attempting to stat file %s\n", 
  484.              error_name(errno), cur_folder));
  485.           error3("Error %s (%s) on stat(%s).", error_name(errno), 
  486.         error_description(errno), cur_folder);
  487.     }
  488.  
  489.     /* Close and remove the original folder.
  490.      * However, if we are going to copy a temp file of kept messages
  491.      * to it, and this is a locked (spool) mailbox, we need to keep
  492.      * it locked during this process. Unfortunately,
  493.      * if we did our LOCK_BY_FLOCK, unlinking the original will kill the
  494.      * lock, so we have to resort to copying the temp file to the original
  495.      * file while keeping the original open.
  496.      * Also, if the file has a link count > 1, then it has links, so to
  497.      * prevent destroying the links, we do a copy back, even though its
  498.      * slower.
  499.      */
  500.  
  501.     fclose(mailfile);
  502.  
  503.     if(to_keep) {
  504.  
  505.       need_to_copy = TRUE;        /* OSK */
  506.  
  507.       if(need_to_copy) {
  508.  
  509.         if (copy(temp_keep_file, cur_folder) != 0) {
  510.  
  511.           /* copy to cur_folder failed - try to copy to special file */
  512.           dprint(1, (debugfile, "leavembox: copy(%s, %s) failed;",
  513.               temp_keep_file, cur_folder));
  514.           dprint(1, (debugfile, "** %s - %s **\n", error_name(errno),
  515.            error_description(errno)));
  516.           error("Couldn't modify folder!");
  517.           sleep(1);
  518.           sprintf(cur_folder,"%s/%s", home, unedited_mail);
  519.           if (copy(temp_keep_file, cur_folder) != 0) {
  520.  
  521.         /* couldn't copy to special file either */
  522.         dprint(1, (debugfile, 
  523.             "leavembox: couldn't copy to %s either!!  Help;", 
  524.             cur_folder));
  525.         dprint(1, (debugfile, "** %s - %s **\n", error_name(errno),
  526.             error_description(errno)));
  527.         error("Can't copy mailbox, system trouble!!!");
  528.         unlock();
  529.         emergency_exit();
  530.           } else {
  531.         dprint(1, (debugfile,
  532.             "\nWoah! Confused - Saved mail in %s (leavembox)\n", 
  533.             cur_folder));
  534.         error1("Saved mail in %s.", cur_folder);
  535.         sleep(1);
  536.           }
  537.         }
  538.       }
  539.  
  540.       /* link or copy complete - remove temp keep file */
  541.       unlink(temp_keep_file);
  542.  
  543.     } else if(folder_type == SPOOL || keep_empty_files || resyncing) {
  544.  
  545.       /* if this is an empty spool file, or if this is an empty non spool 
  546.        * file and we keep empty non spool files (we always keep empty
  547.        * spool files), create an empty file */
  548.  
  549.       if(folder_type == NON_SPOOL)
  550.         error1("Keeping empty folder '%s'.", cur_folder);
  551.       temp = fopen(cur_folder, "w");
  552.       fclose(temp);
  553.     }
  554.  
  555.     /* restore permissions and access times of folder */
  556.  
  557.     if(restore_file_stats(cur_folder) != 1) {
  558.       error1("Problems restoring permissions of folder %s!", cur_folder);
  559.       sleep(2);
  560.     }
  561.  
  562. #ifdef BSD
  563.     utime_buffer[0]     = buf.st_atime;
  564.     utime_buffer[1]     = buf.st_mtime;
  565. #else
  566.     utime_buffer.actime = buf.st_atime;
  567.     utime_buffer.modtime= buf.st_mtime;
  568. #endif
  569.  
  570. #ifdef BSD
  571.     if (utime(cur_folder, utime_buffer) != 0) {
  572. #else
  573.     if (utime(cur_folder, &utime_buffer) != 0) {
  574. #endif
  575.       dprint(1, (debugfile, 
  576.          "Error: encountered error doing utime (leavmbox)\n"));
  577.       dprint(1, (debugfile, "** %s - %s **\n", error_name(errno), 
  578.            error_description(errno)));
  579.       error2("Error %s trying to change file %s access time.", 
  580.            error_name(errno), cur_folder);
  581.     }
  582.  
  583.  
  584.     mailfile_size = bytes(cur_folder);
  585.     unlock();    /* remove the lock on the file ASAP! */
  586.  
  587.     return(1);    
  588. }
  589.  
  590. static int  lock_state = OFF;
  591.  
  592. static char lock_name[SLEN];
  593.  
  594. char *
  595. mk_lockname(file_to_lock)
  596. char *file_to_lock;
  597. {
  598.     /** Create the proper name of the lock file for file_to_lock,
  599.         which is presumed to be a spool file full path (see
  600.         get_folder_type()), and put it in the static area lock_name.
  601.         Return lock_name for informational purposes.
  602.      **/
  603.  
  604.     char *slptr;    /* pointer to last slash */
  605.  
  606.     if(((slptr = rindex(file_to_lock,'/')) != NULL) && \
  607.        ((info_str("MAIL.LOCK",lock_name,SLEN)) != NULL))
  608.     {
  609.         /* lock file is <MAIL.LOCK>/<username> */
  610.         strcat(lock_name,slptr);
  611.     }
  612.     else
  613.     {     
  614.         /* lock is [file_to_lock].elm */
  615.         sprintf(lock_name, "%s.lock", file_to_lock);
  616.     }
  617.     return(lock_name);
  618. }
  619.  
  620. lock(direction)
  621. int direction;
  622. {
  623.       /** Create lock file to ensure that we don't get any mail 
  624.       while altering the folder contents!
  625.       If it already exists sit and spin until 
  626.          either the lock file is removed...indicating new mail
  627.       or
  628.          we have iterated MAX_ATTEMPTS times, in which case we
  629.          either fail or remove it and make our own (determined
  630.          by if REMOVE_AT_LAST is defined in header file
  631.  
  632.       If direction == INCOMING then DON'T remove the lock file
  633.       on the way out!  (It'd mess up whatever created it!).
  634.  
  635.       But if that succeeds and if we are also locking by flock(),
  636.       follow a similar algorithm. Now if we can't lock by flock(),
  637.       we DO need to remove the lock file, since if we got this far,
  638.       we DID create it, not another process.
  639.       **/
  640.  
  641.       FILE *locklock;
  642.       
  643.       register int create_iteration = 0,
  644.            flock_iteration = 0;
  645.  
  646.       /* formulate lock file name */
  647.       mk_lockname(cur_folder);
  648.  
  649.       /* try to assert create lock file MAX_ATTEMPTS times */
  650.       do {
  651.  
  652.     errno = 0;
  653.  
  654.     if((access(lock_name,0)) == -1)
  655.     {
  656.         if((locklock = fopen(lock_name,"w")) != 0 )
  657.         {
  658.             errno = 0;
  659.             fclose(locklock);            
  660.             break;
  661.         }
  662.     }
  663.     dprint(2, (debugfile,"File '%s' already exists!  Waiting...(lock)\n", 
  664.       lock_name));
  665.     error1(
  666.       "Waiting to read mailbox while mail is being received: attempt #%d",
  667.       create_iteration);
  668.     sleep(5);
  669.       } while (create_iteration++ < MAX_ATTEMPTS);
  670.  
  671.       clear_error();
  672.  
  673.       if(errno != 0) {
  674.     
  675.     /* we weren't able to create the lock file */
  676.  
  677.     /* Okay...we die and leave, not updating the mailfile mbox or
  678.        any of those! */
  679.  
  680.     if (direction == INCOMING) {
  681.       PutLine1(LINES, 0, "\l\r\l\rGiving up after %d iterations.\l\r", 
  682.         create_iteration);
  683.       PutLine0(LINES, 0, 
  684.       "\l\rPlease try to read your mail again in a few minutes.\l\r\l\r");
  685.       dprint(1, (debugfile, 
  686.         "Warning: bailing out after %d iterations...(lock)\n",
  687.         create_iteration));
  688.       leave_locked(0);
  689.     } else {
  690.       dprint(1, (debugfile, 
  691.        "Warning: after %d iterations, timed out! (lock)\n",
  692.        create_iteration));
  693.       leave(error("Timed out on locking mailbox.  Leaving program."));
  694.     }
  695.       }
  696.  
  697.       /* If we're here we successfully created the lock file */
  698.       dprint(5,
  699.     (debugfile, "lock():Lock %s %s for file %s on.\n", lock_name,
  700.     (direction == INCOMING ? "incoming" : "outgoing"), cur_folder));
  701.  
  702. /*      (void)close(create_fd); */
  703.  
  704.       lock_state = ON;
  705.       return(0);
  706. }
  707.  
  708. int
  709. unlock()
  710. {
  711.     /** Remove the lock file!    This must be part of the interrupt
  712.         processing routine to ensure that the lock file is NEVER
  713.         left sitting in the mailhome directory!
  714.      **/
  715.  
  716.     int retcode = 0;
  717.  
  718.     dprint(5,
  719.       (debugfile, "unlock():Lock %s for file %s %s off.\n",
  720.         (*lock_name ? lock_name : "none"), cur_folder,
  721.         (lock_state == ON ? "going" : "already")));
  722.  
  723.     if(lock_state == ON) {
  724.  
  725.       if((retcode = unlink(lock_name)) == 0) {    /* remove lock file */
  726.         *lock_name = '\0';        /* null lock file name */
  727.         lock_state = OFF;        /* indicate we don't have a lock on */
  728.       } else {
  729.         dprint(1, (debugfile,
  730.           "unlock(): Error %s (%s)\n\ttrying to unlink file %s (%s)\n", 
  731.           error_name(errno), error_description(errno), lock_name,"unlock"));
  732.           error1("Couldn't remove my own lock file %s!", lock_name);
  733.       }
  734.     }
  735.     return(retcode);
  736. }
  737.