home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / src / utils.c < prev    next >
C/C++ Source or Header  |  1992-04-06  |  9KB  |  348 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: utils.c,v 4.1 90/04/28 22:44:19 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $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:    utils.c,v $
  17.  * Revision 4.1  90/04/28  22:44:19  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  *
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** Utility routines for ELM
  24.  
  25. **/
  26.  
  27. #include "headers.h"
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32.  
  33. #ifdef BSD
  34. #undef tolower
  35. #endif
  36.  
  37. #include <signal.h>
  38.  
  39. #ifndef OS2
  40. extern int errno;
  41. #endif
  42.  
  43. char *error_name();
  44. void   exit();
  45.  
  46. create_new_folders()
  47. {
  48.     /* this creates a new folders directory */
  49.  
  50. #ifdef MKDIR
  51.     (void) mkdir(folders, 0700);
  52. #else
  53.     char com[SLEN];
  54.  
  55.     /** Some systems don't have a mkdir call - how inconvienient! **/
  56.  
  57.     sprintf(com, "mkdir %s", folders);
  58.     system_call(com, SH, FALSE, FALSE);
  59.     sprintf(com, "chmod 700 %s", folders);
  60.     system_call(com, SH, FALSE, FALSE);
  61. #endif /* MKDIR */
  62.  
  63.     chown(folders, userid, groupid);
  64. }
  65.  
  66. create_new_elmdir()
  67. {
  68.     /** this routine is just for allowing new users who don't have the
  69.         old elm files to create a new .elm directory **/
  70.  
  71.     char source[SLEN];
  72. #ifdef MKDIR
  73.     sprintf(source, "%s/%s", home, dotelm);
  74.     (void) mkdir(source, 0700);
  75. #else
  76.     char com[SLEN];
  77.  
  78.     /** Some systems don't have a mkdir call - how inconvienient! **/
  79.  
  80.     sprintf(com, "mkdir %s/%s", home, dotelm);
  81.     system_call(com, SH, FALSE, FALSE);
  82.     sprintf(com, "chmod 700 %s/%s", home, dotelm);
  83.     system_call(com, SH, FALSE, FALSE);
  84. #endif /* MKDIR */
  85.  
  86.     chown( source, userid, groupid);
  87. }
  88.  
  89. move_old_files_to_new()
  90. {
  91.     /** this routine is just for allowing people to transition from
  92.         the old Elm, where things are all kept in their $HOME dir,
  93.         to the new one where everything is in $HOME/.elm... **/
  94.  
  95.     char source[SLEN], dest[SLEN], temp[SLEN];
  96.     char com[SLEN];
  97.  
  98.     /** simply go through all the files... **/
  99.  
  100.     sprintf(source, "%s/.alias_text", home);
  101.     if (access(source, ACCESS_EXISTS) != -1) {
  102.       sprintf(dest,   "%s/%s", home, ALIAS_TEXT);
  103.       printf("\n\rCopying from: %s\n\rCopying to:   %s\n\r", source, dest);
  104.  
  105.       sprintf(temp, "/tmp/%d", getpid());
  106.       sprintf(com, "%s -e 's/:/=/g' %s > %s\n", sed_cmd, source, temp);
  107.       (void) system_call(com, SH, FALSE, FALSE);
  108.       sprintf(com, "%s %s %s\n", move_cmd, temp, dest);
  109.       (void) system_call(com, SH, FALSE, FALSE);
  110.       (void) system_call("newalias", SH, FALSE, FALSE);
  111.     }
  112.  
  113.     sprintf(source, "%s/%sheaders", home, dotelm);
  114.     if (access(source, ACCESS_EXISTS) != -1) {
  115.       sprintf(dest,   "%s/%s", home, mailheaders);
  116.       printf("\n\rCopying from: %s\n\rCopying to:   %s\n\r", source, dest);
  117.       copy(source, dest);
  118.     }
  119.  
  120.     sprintf(source, "%s/%src", home, dotelm);
  121.     if (access(source, ACCESS_EXISTS) != -1) {
  122.       sprintf(dest,   "%s/%s", home, elmrcfile);
  123.       printf("\n\rCopying from: %s\n\rCopying to:   %s\n\r", source, dest);
  124.       copy(source, dest);
  125.     }
  126.  
  127.     printf(
  128.     "\n\rWelcome to the new version of ELM!\n\n\rHit return to continue.");
  129.     getchar();
  130. }
  131.  
  132. emergency_exit()
  133. {
  134.     /** used in dramatic cases when we must leave without altering
  135.         ANYTHING about the system... **/
  136.     char *mk_lockname();
  137.  
  138.     dprint(1, (debugfile,
  139.      "\nERROR: Something dreadful is happening!  Taking emergency exit!!\n\n"));
  140.     dprint(1, (debugfile,
  141.          "  possibly leaving behind the following files;\n"));
  142.     dprint(1, (debugfile,
  143.          "     The mailbox tempfile : %s\n", cur_tempfolder));
  144.     if(folder_type == SPOOL) dprint(1, (debugfile,
  145.          "     The mailbox lock file: %s\n", mk_lockname(cur_folder)));
  146.     dprint(1, (debugfile,
  147.          "     The composition file : %s%d%s\n", temp_dir, getpid(), temp_file));
  148.     dprint(1, (debugfile,
  149.          "     The readmsg data file: %s/%s\n", home, readmsg_file));
  150.  
  151.     Raw(OFF);
  152.     if (cursor_control)  transmit_functions(OFF);
  153.     if (hp_terminal)     softkeys_off();
  154.  
  155.     if (cursor_control)
  156.       MoveCursor(LINES-1, 0);
  157.  
  158.     PutLine0(LINES,0,
  159.         "\nEmergency exit taken! All temp files intact!\n\n");
  160.  
  161.         ExitScreen();
  162.     exit(1);
  163. }
  164. rm_temps_exit()
  165. {
  166.       char buffer[SLEN];
  167.       PutLine0(LINES,0,
  168.          "\nWrite to temp file failed, exiting leaving mailbox intact!\n\n");
  169.       dprint(2, (debugfile, "\nrm_temps_exit, deleteing temp files\n"));
  170.       Raw(OFF);
  171.       if (cursor_control)  transmit_functions(OFF);
  172.       if (hp_terminal)     softkeys_off();
  173.       sprintf(buffer,"%d%s", getpid(), temp_file);  /* editor buffer */
  174.       (void) unlink(buffer);
  175.       if (folder_type == SPOOL) {
  176.         if (mailfile)
  177.           fclose(mailfile);
  178.         (void) unlink(cur_tempfolder);
  179.       }
  180.       sprintf(buffer,"%s/%s", home, readmsg_file);  /* readmsg temp */
  181.       (void) unlink(buffer);
  182.       unlock();                               /* remove lock file if any */
  183.       if(!batch_only) {
  184.         MoveCursor(LINES-1,0);
  185.         NewLine();
  186.       }
  187.       ExitScreen();
  188.       exit(1);
  189. }
  190.  
  191. /*ARGSUSED*/
  192. /*VARARGS0*/
  193.  
  194. leave(val)
  195. int val;    /* not used, placeholder for signal catching! */
  196. {
  197.     char buffer[SLEN];
  198.  
  199.     dprint(2, (debugfile, "\nLeaving mailer normally (leave)\n"));
  200.  
  201.     Raw(OFF);
  202.     if (cursor_control)  transmit_functions(OFF);
  203.     if (hp_terminal)     softkeys_off();
  204.  
  205.     sprintf(buffer,"%s%d%s", temp_dir, getpid(), temp_file);  /* editor buffer */
  206.     (void) unlink(buffer);
  207.  
  208.     if (folder_type == SPOOL) {
  209.           if (mailfile)
  210.             fclose(mailfile);
  211.       (void) unlink(cur_tempfolder);
  212.     }
  213.  
  214.     sprintf(buffer,"%s/%s", home, readmsg_file);  /* readmsg temp */
  215.     (void) unlink(buffer);
  216.  
  217.     unlock();                /* remove lock file if any */
  218.  
  219.     if(!batch_only) {
  220.       MoveCursor(LINES-1,0);
  221.       NewLine();
  222.     }
  223.  
  224.     ExitScreen();
  225.     exit(0);
  226. }
  227.  
  228. silently_exit()
  229. {
  230.     /** This is the same as 'leave', but it doesn't remove any non-pid
  231.         files.  It's used when we notice that we're trying to create a
  232.         temp mail file and one already exists!!
  233.     **/
  234.     char buffer[SLEN];
  235.  
  236.     dprint(2, (debugfile, "\nLeaving mailer quietly (silently_exit)\n"));
  237.  
  238.     Raw(OFF);
  239.     if (cursor_control)  transmit_functions(OFF);
  240.     if (hp_terminal)     softkeys_off();
  241.  
  242.     sprintf(buffer,"%s%d%s", temp_dir, getpid(), temp_file);  /* editor buffer */
  243.     (void) unlink(buffer);
  244.  
  245.     MoveCursor(LINES-1,0);
  246.     NewLine();
  247.  
  248.     ExitScreen();
  249.     exit(0);
  250. }
  251.  
  252. /*ARGSUSED0*/
  253.  
  254. #ifndef REMOVE_AT_LAST
  255. leave_locked(val)
  256. int val;    /* not used, placeholder for signal catching! */
  257. {
  258.     /** same as leave routine, but don't disturb lock file **/
  259.  
  260.     char buffer[SLEN];
  261.  
  262.         dprint(3, (debugfile,
  263.         "\nLeaving mailer due to presence of lock file (leave_locked)\n"));
  264.  
  265.     Raw(OFF);
  266.     if (cursor_control)  transmit_functions(OFF);
  267.     if (hp_terminal)     softkeys_off();
  268.  
  269.     sprintf(buffer,"%s%d%s", temp_dir, getpid(), temp_file);  /* editor buffer */
  270.     (void) unlink(buffer);
  271.  
  272.         if (mailfile)
  273.           fclose(mailfile);
  274.     (void) unlink(cur_tempfolder);            /* temp mailbox */
  275.  
  276.     MoveCursor(LINES-1,0);
  277.     NewLine();
  278.     ExitScreen();
  279.     exit(0);
  280. }
  281. #endif
  282.  
  283. int
  284. get_page(msg_pointer)
  285. int msg_pointer;
  286. {
  287.     /** Ensure that 'current' is on the displayed page,
  288.         returning NEW_PAGE iff the page changed! **/
  289.  
  290.     register int first_on_page, last_on_page;
  291.  
  292.     first_on_page = (header_page * headers_per_page) + 1;
  293.  
  294.     last_on_page = first_on_page + headers_per_page - 1;
  295.  
  296.     if (selected)    /* but what is it on the SCREEN??? */
  297.       msg_pointer = compute_visible(msg_pointer);
  298.  
  299.     if (selected && msg_pointer > selected)
  300.       return(SAME_PAGE);    /* too far - page can't change! */
  301.  
  302.     if (msg_pointer > last_on_page) {
  303.       header_page = (int) (msg_pointer-1)/ headers_per_page;
  304.       return(NEW_PAGE);
  305.     }
  306.     else if (msg_pointer < first_on_page) {
  307.       header_page = (int) (msg_pointer-1) / headers_per_page;
  308.       return(NEW_PAGE);
  309.     }
  310.     else
  311.       return(SAME_PAGE);
  312. }
  313.  
  314. char *nameof(filename)
  315. char *filename;
  316. {
  317.     /** checks to see if 'filename' has any common prefixes, if
  318.         so it returns a string that is the same filename, but
  319.         with '=' as the folder directory, or '~' as the home
  320.         directory..
  321.     **/
  322.  
  323.     static char buffer[STRING];
  324.     register int i = 0, iindex = 0;
  325.  
  326.     if (strncmp(filename, folders, strlen(folders)) == 0) {
  327.       if (strlen(folders) > 0) {
  328.         buffer[i++] = '=';
  329.         iindex = strlen(folders);
  330.         if(filename[iindex] == '/')
  331.           iindex++;
  332.       }
  333.     }
  334.     else if (strncmp(filename, home, strlen(home)) == 0) {
  335.       if (strlen(home) > 1) {
  336.         buffer[i++] = '~';
  337.         iindex = strlen(home);
  338.       }
  339.     }
  340.     else iindex = 0;
  341.  
  342.     while (filename[iindex] != '\0')
  343.       buffer[i++] = filename[iindex++];
  344.     buffer[i] = '\0';
  345.  
  346.     return( (char *) buffer);
  347. }
  348.