home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / src / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-11  |  11.8 KB  |  482 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: utils.c,v 5.10 1993/04/12 03:11:50 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.10 $   $State: Exp $
  6.  *
  7.  *            Copyright (c) 1988-1992 USENET Community Trust
  8.  *            Copyright (c) 1986,1987 Dave Taylor
  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: utils.c,v $
  17.  * Revision 5.10  1993/04/12  03:11:50  syd
  18.  * nameof() didn't check that the character after the common string was /, thus
  19.  * (if Mail is the folderdir) Maildir/x was made to be =dir/x.
  20.  * From: Jan Djarv <Jan.Djarv@sa.erisoft.se>
  21.  *
  22.  * Revision 5.9  1993/04/12  03:08:40  syd
  23.  * Added check if headers_per_page is zero in get_page().
  24.  * From: Jan Djarv <Jan.Djarv@sa.erisoft.se>
  25.  *
  26.  * Revision 5.8  1993/04/12  01:52:31  syd
  27.  * Initialize safe_malloc() failure trap just to play it safe.  Although
  28.  * Elm doesn't currently use these routines, do this just in case somebody
  29.  * someday adds a call to a library routine that does use them.
  30.  * From: chip@chinacat.unicom.com (Chip Rosenthal)
  31.  *
  32.  * Revision 5.7  1993/01/20  03:37:16  syd
  33.  * Nits and typos in the NLS messages and corresponding default messages.
  34.  * From: dwolfe@pffft.sps.mot.com (Dave Wolfe)
  35.  *
  36.  * Revision 5.6  1993/01/05  03:40:45  syd
  37.  * Protect TSTP for those systems without it.
  38.  * From: kevin@cfctech.cfc.com (Kevin Darcy)
  39.  *
  40.  * Revision 5.5  1992/12/24  22:05:11  syd
  41.  * Some OS's, especially ULTRIX create extra continue signals
  42.  * that confuse Elm on exit
  43.  * From: Syd via patch from Bob Mason
  44.  *
  45.  * Revision 5.4  1992/12/11  01:45:04  syd
  46.  * remove sys/types.h include, it is now included by defs.h
  47.  * and this routine includes defs.h or indirectly includes defs.h
  48.  * From: Syd
  49.  *
  50.  * Revision 5.3  1992/12/07  04:30:37  syd
  51.  * fix missing brace on do_cursor calls
  52.  * From: Syd
  53.  *
  54.  * Revision 5.2  1992/11/26  00:46:13  syd
  55.  * changes to first change screen back (Raw off) and then issue final
  56.  * error message.
  57.  * From: Syd
  58.  *
  59.  * Revision 5.1  1992/10/03  22:58:40  syd
  60.  * Initial checkin as of 2.4 Release at PL0
  61.  *
  62.  *
  63.  ******************************************************************************/
  64.  
  65. /** Utility routines for ELM
  66.  
  67. **/
  68.  
  69. #include "headers.h"
  70. #include "s_elm.h"
  71. #include <sys/stat.h>
  72. #include <ctype.h>
  73. #include <errno.h>
  74.  
  75. #ifdef BSD
  76. #undef tolower
  77. #endif
  78.  
  79. extern int errno;
  80.  
  81. create_new_folders()
  82. {
  83.     /* this creates a new folders directory */
  84.  
  85. #ifdef MKDIR
  86.     (void) mkdir(folders, 0700);
  87. #else
  88.     char com[SLEN];
  89.  
  90.     /** Some systems don't have a mkdir call - how inconvienient! **/
  91.  
  92.     sprintf(com, "mkdir %s", folders);
  93.     (void) system_call(com, 0);
  94.     sprintf(com, "chmod 700 %s", folders);
  95.     (void) system_call(com, 0);
  96. #endif /* MKDIR */
  97.  
  98.     chown(folders, userid, groupid);
  99. }
  100.  
  101. create_new_elmdir()
  102. {
  103.     /** this routine is just for allowing new users who don't have the
  104.         old elm files to create a new .elm directory **/
  105.  
  106.     char source[SLEN];
  107. #ifdef MKDIR
  108.     sprintf(source, "%s/.elm", home);
  109.     (void) mkdir(source, 0700);
  110. #else
  111.     char com[SLEN];
  112.  
  113.     /** Some systems don't have a mkdir call - how inconvienient! **/
  114.  
  115.     sprintf(com, "mkdir %s/.elm", home);
  116.     (void) system_call(com, 0);
  117.     sprintf(com, "chmod 700 %s/.elm", home);
  118.     (void) system_call(com, 0);
  119. #endif /* MKDIR */
  120.  
  121.     chown( source, userid, groupid);
  122. }
  123.  
  124. move_old_files_to_new()
  125. {
  126.     /** this routine is just for allowing people to transition from
  127.         the old Elm, where things are all kept in their $HOME dir,
  128.         to the new one where everything is in $HOME/.elm... **/
  129.  
  130.     char source[SLEN], dest[SLEN], temp[SLEN];
  131.     char com[SLEN];
  132.  
  133.     /** simply go through all the files... **/
  134.  
  135.     sprintf(source, "%s/.alias_text", home);
  136.     if (access(source, ACCESS_EXISTS) != -1) {
  137.       sprintf(dest,   "%s/%s", home, ALIAS_TEXT);
  138.       MCprintf(catgets(elm_msg_cat, ElmSet, ElmCopyingFromCopyingTo,
  139.         "\n\rCopying from: %s\n\rCopying to:   %s\n\r"),
  140.         source, dest);
  141.  
  142.       sprintf(temp, "/tmp/%d", getpid());
  143.       sprintf(com, "%s -e 's/:/=/g' %s > %s\n", sed_cmd, source, temp);
  144.       (void) system_call(com, 0);
  145.       sprintf(com, "%s %s %s\n", move_cmd, temp, dest);
  146.       (void) system_call(com, 0);
  147.       (void) system_call("newalias", 0);
  148.     }
  149.  
  150.     sprintf(source, "%s/.elmheaders", home);
  151.     if (access(source, ACCESS_EXISTS) != -1) {
  152.       sprintf(dest,   "%s/%s", home, mailheaders);
  153.       MCprintf(catgets(elm_msg_cat, ElmSet, ElmCopyingFromCopyingTo,
  154.         "\n\rCopying from: %s\n\rCopying to:   %s\n\r"),
  155.           source, dest);
  156.       copy(source, dest);
  157.     }
  158.  
  159.     sprintf(source, "%s/.elmrc", home);
  160.     if (access(source, ACCESS_EXISTS) != -1) {
  161.       sprintf(dest,   "%s/%s", home, elmrcfile);
  162.       MCprintf(catgets(elm_msg_cat, ElmSet, ElmCopyingFromCopyingTo,
  163.         "\n\rCopying from: %s\n\rCopying to:   %s\n\r"),
  164.           source, dest);
  165.       copy(source, dest);
  166.     }
  167.  
  168.     printf(catgets(elm_msg_cat, ElmSet, ElmWelcomeToNewElm,
  169.     "\n\rWelcome to the new version of ELM!\n\n\rHit return to continue."));
  170.     getchar();
  171. }
  172.  
  173.  
  174. /*
  175.  * The initialize() procedure sets the "xalloc_fail_handler" vector to
  176.  * point here in the event that xmalloc() or friends fail.
  177.  */
  178. /*ARGSUSED*/
  179. void malloc_failed_exit(proc, len)
  180. {
  181.     MoveCursor(LINES,0);
  182.     Raw(OFF);
  183.     fprintf(stderr, catgets(elm_msg_cat, ElmSet, ElmCouldntMallocBytes,
  184.     "\n\nCouldn't malloc %d bytes!!\n\n"), len);
  185.     emergency_exit();
  186. }
  187.  
  188.  
  189. emergency_exit()
  190. {
  191.     /** used in dramatic cases when we must leave without altering
  192.         ANYTHING about the system... **/
  193.     int do_cursor = RawState();
  194.  
  195.     char *mk_lockname();
  196.  
  197. /*
  198.  *    some OS's get extra cont signal, so once this far into the
  199.  *    exit, ignore those signals (Especially Ultrix)
  200.  */
  201. #ifdef SIGTSTP
  202.     signal(SIGTSTP,SIG_IGN);
  203. #endif
  204. #ifdef SIGSTOP
  205.     signal(SIGSTOP,SIG_IGN);
  206. #endif
  207. #ifdef SIGCONT
  208.     signal(SIGCONT,SIG_IGN);
  209. #endif
  210.  
  211.     dprint(1, (debugfile,
  212.      "\nERROR: Something dreadful is happening!  Taking emergency exit!!\n\n"));
  213.     dprint(1, (debugfile,
  214.          "  possibly leaving behind the following files;\n"));
  215.     dprint(1, (debugfile,
  216.          "     The mailbox temp file : %s\n", cur_tempfolder));
  217.     if(folder_type == SPOOL) dprint(1, (debugfile,
  218.          "     The mailbox lock file: %s\n", mk_lockname(cur_folder)));
  219.     dprint(1, (debugfile,
  220.          "     The composition file : %s%s%d\n", temp_dir, temp_file, getpid()));
  221.  
  222.     if (cursor_control)  transmit_functions(OFF);
  223.     if (hp_terminal)     softkeys_off();
  224.  
  225.     if (do_cursor) {
  226.       Raw(OFF);
  227.       MoveCursor(LINES, 0);
  228.       }
  229.  
  230.     printf(catgets(elm_msg_cat, ElmSet, ElmEmergencyExitTaken,
  231.         "\nEmergency exit taken! All temp files intact!\n\n"));
  232.  
  233.     exit(1);
  234. }
  235. rm_temps_exit()
  236. {
  237.     char buffer[SLEN];
  238.     int do_cursor = RawState();
  239.  
  240. /*
  241.  *    some OS's get extra cont signal, so once this far into the
  242.  *    exit, ignore those signals (Especially Ultrix)
  243.  */
  244. #ifdef SIGTSTP
  245.     signal(SIGTSTP,SIG_IGN);
  246. #endif
  247. #ifdef SIGSTOP
  248.     signal(SIGSTOP,SIG_IGN);
  249. #endif
  250. #ifdef SIGCONT
  251.     signal(SIGCONT,SIG_IGN);
  252. #endif
  253.  
  254.     PutLine0(LINES, 0, catgets(elm_msg_cat, ElmSet, ElmWriteFailedExitingIntact,
  255.      "\nWrite to temp file failed, exiting leaving mailbox intact!\n\n"));
  256.     dprint(2, (debugfile, "\nrm_temps_exit, deleteing temp files\n"));
  257.  
  258.     if (cursor_control)  transmit_functions(OFF);
  259.     if (hp_terminal)     softkeys_off();
  260.  
  261.     sprintf(buffer,"%s%d",temp_file, getpid());  /* editor buffer */
  262.     (void) unlink(buffer);
  263.  
  264.     if (folder_type == SPOOL) {
  265.         (void) unlink(cur_tempfolder);
  266.     }
  267.  
  268.     unlock();                               /* remove lock file if any */
  269.  
  270.     if(do_cursor) {
  271.         MoveCursor(LINES,0);
  272.         NewLine();
  273.         Raw(OFF);
  274.     }
  275.  
  276.     exit(1);
  277. }
  278.  
  279. /*ARGSUSED*/
  280. /*VARARGS0*/
  281.  
  282. leave(val)
  283. int val;    /* not used, placeholder for signal catching! */
  284. {
  285.     char buffer[SLEN];
  286.     int do_cursor = RawState();
  287.  
  288. /*
  289.  *    some OS's get extra cont signal, so once this far into the
  290.  *    exit, ignore those signals (Especially Ultrix)
  291.  */
  292. #ifdef SIGTSTP
  293.     signal(SIGTSTP,SIG_IGN);
  294. #endif
  295. #ifdef SIGSTOP
  296.     signal(SIGSTOP,SIG_IGN);
  297. #endif
  298. #ifdef SIGCONT
  299.     signal(SIGCONT,SIG_IGN);
  300. #endif
  301.  
  302.     dprint(2, (debugfile, "\nLeaving mailer normally (leave)\n"));
  303.  
  304.     if (cursor_control)  transmit_functions(OFF);
  305.     if (hp_terminal)     softkeys_off();
  306.  
  307.     sprintf(buffer,"%s%s%d", temp_dir, temp_file, getpid());  /* editor buffer */
  308.     (void) unlink(buffer);
  309.  
  310.     if (folder_type == SPOOL) {
  311.       (void) unlink(cur_tempfolder);
  312.     }
  313.  
  314.     unlock();                /* remove lock file if any */
  315.  
  316.     if (do_cursor) {
  317.       MoveCursor(LINES,0);
  318.       NewLine();
  319.       Raw(OFF);
  320.     }
  321.  
  322.     exit(0);
  323. }
  324.  
  325. silently_exit()
  326. {
  327.     /** This is the same as 'leave', but it doesn't remove any non-pid
  328.         files.  It's used when we notice that we're trying to create a
  329.         temp mail file and one already exists!!
  330.     **/
  331.     char buffer[SLEN];
  332.     int do_cursor = RawState();
  333.  
  334. /*
  335.  *    some OS's get extra cont signal, so once this far into the
  336.  *    exit, ignore those signals (Especially Ultrix)
  337.  */
  338. #ifdef SIGTSTP
  339.     signal(SIGTSTP,SIG_IGN);
  340. #endif
  341. #ifdef SIGSTOP
  342.     signal(SIGSTOP,SIG_IGN);
  343. #endif
  344. #ifdef SIGCONT
  345.     signal(SIGCONT,SIG_IGN);
  346. #endif
  347.  
  348.     dprint(2, (debugfile, "\nLeaving mailer quietly (silently_exit)\n"));
  349.  
  350.     if (cursor_control)  transmit_functions(OFF);
  351.     if (hp_terminal)     softkeys_off();
  352.  
  353.     sprintf(buffer,"%s%s%d", temp_dir, temp_file, getpid());  /* editor buffer */
  354.     (void) unlink(buffer);
  355.  
  356.     if (do_cursor) {
  357.       MoveCursor(LINES,0);
  358.       NewLine();
  359.       Raw(OFF);
  360.     }
  361.  
  362.     exit(0);
  363. }
  364.  
  365. /*ARGSUSED0*/
  366.  
  367. #ifndef REMOVE_AT_LAST
  368. leave_locked(val)
  369. int val;    /* not used, placeholder for signal catching! */
  370. {
  371.     /** same as leave routine, but don't disturb lock file **/
  372.  
  373.     char buffer[SLEN];
  374.         int do_cursor = RawState();
  375.  
  376. /*
  377.  *    some OS's get extra cont signal, so once this far into the
  378.  *    exit, ignore those signals (Especially Ultrix)
  379.  */
  380. #ifdef SIGTSTP
  381.     signal(SIGTSTP,SIG_IGN);
  382. #endif
  383. #ifdef SIGSTOP
  384.     signal(SIGSTOP,SIG_IGN);
  385. #endif
  386. #ifdef SIGCONT
  387.     signal(SIGCONT,SIG_IGN);
  388. #endif
  389.  
  390.         dprint(3, (debugfile,
  391.         "\nLeaving mailer due to presence of lock file (leave_locked)\n"));
  392.  
  393.     if (cursor_control)  transmit_functions(OFF);
  394.     if (hp_terminal)     softkeys_off();
  395.  
  396.     sprintf(buffer,"%s%s%d", temp_dir, temp_file, getpid());  /* editor buffer */
  397.     (void) unlink(buffer);
  398.  
  399.     (void) unlink(cur_tempfolder);            /* temp mailbox */
  400.  
  401.     if (do_cursor) {
  402.       MoveCursor(LINES,0);
  403.       NewLine();
  404.       Raw(OFF);
  405.     }
  406.  
  407.     exit(0);
  408. }
  409. #endif
  410.  
  411. int
  412. get_page(msg_pointer)
  413. int msg_pointer;
  414. {
  415.     /** Ensure that 'current' is on the displayed page,
  416.         returning NEW_PAGE iff the page changed! **/
  417.  
  418.     register int first_on_page, last_on_page;
  419.  
  420.     if (headers_per_page == 0)
  421.       return(SAME_PAGE); /* What else can I do ? */
  422.  
  423.     first_on_page = (header_page * headers_per_page) + 1;
  424.  
  425.     last_on_page = first_on_page + headers_per_page - 1;
  426.  
  427.     if (selected)    /* but what is it on the SCREEN??? */
  428.       msg_pointer = compute_visible(msg_pointer);
  429.  
  430.     if (selected && msg_pointer > selected)
  431.       return(SAME_PAGE);    /* too far - page can't change! */
  432.  
  433.     if (msg_pointer > last_on_page) {
  434.       header_page = (int) (msg_pointer-1)/ headers_per_page;
  435.       return(NEW_PAGE);
  436.     }
  437.     else if (msg_pointer < first_on_page) {
  438.       header_page = (int) (msg_pointer-1) / headers_per_page;
  439.       return(NEW_PAGE);
  440.     }
  441.     else
  442.       return(SAME_PAGE);
  443. }
  444.  
  445. char *nameof(filename)
  446. char *filename;
  447. {
  448.     /** checks to see if 'filename' has any common prefixes, if
  449.         so it returns a string that is the same filename, but
  450.         with '=' as the folder directory, or '~' as the home
  451.         directory..
  452.     **/
  453.  
  454.     static char buffer[STRING];
  455.     register int i = 0, iindex = 0, len;
  456.  
  457.     len = strlen(folders);
  458.     if (strncmp(filename, folders, len) == 0 &&
  459.         len > 0 && (filename[len] == '/' || filename[len] == '\0')) {
  460.       buffer[i++] = '=';
  461.       iindex = len;
  462.       if(filename[iindex] == '/')
  463.         iindex++;
  464.     }
  465.     else
  466.     {
  467.       len = strlen(home);
  468.       if (strncmp(filename, home, len) == 0 &&
  469.           len > 1 && (filename[len] == '/' || filename[len] == '\0')) {
  470.         buffer[i++] = '~';
  471.         iindex = len;
  472.       }
  473.       else iindex = 0;
  474.     }
  475.  
  476.     while (filename[iindex] != '\0')
  477.       buffer[i++] = filename[iindex++];
  478.     buffer[i] = '\0';
  479.  
  480.     return( (char *) buffer);
  481. }
  482.