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