home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / ldstate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-02  |  4.5 KB  |  157 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: ldstate.c,v 5.5 1993/02/03 15:26:13 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.5 $   $State: Exp $
  6.  *
  7.  *            Copyright (c) 1992 USENET Community Trust
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: ldstate.c,v $
  16.  * Revision 5.5  1993/02/03  15:26:13  syd
  17.  * protect atol in ifndef __STDC__ as some make it a macro, and its in stdlib.h
  18.  *
  19.  * Revision 5.4  1992/12/24  21:48:07  syd
  20.  * Make fgetline elm_fgetline, as BSD 4.4 now has such a routine, and
  21.  * it causes compile problems.
  22.  * From: Syd
  23.  *
  24.  * Revision 5.3  1992/12/11  01:45:04  syd
  25.  * remove sys/types.h include, it is now included by defs.h
  26.  * and this routine includes defs.h or indirectly includes defs.h
  27.  * From: Syd
  28.  *
  29.  * Revision 5.2  1992/12/07  04:51:34  syd
  30.  * add sys/types.h for time_t declaration
  31.  *
  32.  * Revision 5.1  1992/10/03  22:41:36  syd
  33.  * Initial checkin as of 2.4 Release at PL0
  34.  *
  35.  *
  36.  ******************************************************************************/
  37.  
  38. #include <stdio.h>
  39. #include "defs.h"
  40.  
  41. /*
  42.  * Retrieve Elm folder state.
  43.  *
  44.  * The SY_DUMPSTATE option to "system_call()" causes Elm to dump the
  45.  * current folder state before spawning a shell.  This allows programs
  46.  * running as an Elm subprocess (e.g. "readmsg") to obtain information
  47.  * on the folder.  (See the "system_call()" code for additional info
  48.  * on the format of the state file.)
  49.  *
  50.  * This procedure returns -1 on the event of an error (corrupt state
  51.  * file or malloc failed).
  52.  *
  53.  * A zero return does NOT necessarily mean that folder state information
  54.  * was retrieved.  On a zero return, inspect the "folder_name" element.
  55.  * If it was NULL then there was no state file found.  If it is non-NULL
  56.  * then valid folder state information was found and loaded into
  57.  * the (struct folder_state) record.
  58.  */
  59.  
  60. #ifndef __STDC__ /* avoid problemswith systems that declare atol as a macro */
  61.     extern long atol();
  62. #endif
  63.  
  64. static char *elm_fgetline(buf, buflen, fp)
  65. char *buf;
  66. unsigned buflen;
  67. FILE *fp;
  68. {
  69.     if (fgets(buf, buflen, fp) == NULL)
  70.     return (char *) NULL;
  71.     buf[strlen(buf)-1] = '\0';
  72.     return buf;
  73. }
  74.  
  75.  
  76. int load_folder_state_file(fst)
  77. struct folder_state *fst;
  78. {
  79.     char buf[SLEN], *state_fname;
  80.     int status, i;
  81.     FILE *fp;
  82.  
  83.     /* clear out the folder status record */
  84.     fst->folder_name = NULL;
  85.     fst->num_mssgs = -1;
  86.     fst->idx_list = NULL;
  87.     fst->num_sel = -1;
  88.     fst->sel_list = NULL;
  89.  
  90.     /* see if we can find a state file */
  91.     if ((state_fname = getenv(FOLDER_STATE_ENV)) == NULL)
  92.     return 0;
  93.     if ((fp = fopen(state_fname, "r")) == NULL)
  94.     return 0;
  95.  
  96.     /* initialize status to failure */
  97.     status = -1;
  98.  
  99.     /* retrieve pathname of the folder */
  100.     if (elm_fgetline(buf, sizeof(buf), fp) == NULL || buf[0] != 'F')
  101.     goto done;
  102.     if ((fst->folder_name = malloc(strlen(buf+1) + 1)) == NULL)
  103.     goto done;
  104.     (void) strcpy(fst->folder_name, buf+1);
  105.  
  106.     /* retrieve number of messages in the folder */
  107.     if (elm_fgetline(buf, sizeof(buf), fp) == NULL || buf[0] != 'N')
  108.     goto done;
  109.     fst->num_mssgs = atoi(buf+1);
  110.  
  111.     /* allocate space to hold the indices */
  112.     fst->idx_list = (long *) malloc(fst->num_mssgs * sizeof(long));
  113.     if (fst->idx_list == NULL)
  114.     goto done;
  115.  
  116.     /* load in the indices of the messages */
  117.     for (i = 0 ; i < fst->num_mssgs ; ++i) {
  118.     if (elm_fgetline(buf, sizeof(buf), fp) == NULL || buf[0] != 'I')
  119.         goto done;
  120.     fst->idx_list[i] = atol(buf+1);
  121.     }
  122.  
  123.     /* load in the number of messages selected */
  124.     if (elm_fgetline(buf, sizeof(buf), fp) == NULL || buf[0] != 'C')
  125.     goto done;
  126.     fst->num_sel = atoi(buf+1);
  127.  
  128.     /* it is possible that there are no selections */
  129.     if (fst->num_sel > 0) {
  130.  
  131.     /* allocate space to hold the list of selected messages */
  132.     fst->sel_list = (int *) malloc(fst->num_sel * sizeof(int));
  133.     if (fst->sel_list == NULL)
  134.         goto done;
  135.  
  136.     /* load in the list of selected messages */
  137.     for (i = 0 ; i < fst->num_sel ; ++i) {
  138.         if (elm_fgetline(buf, sizeof(buf), fp) == NULL || buf[0] != 'S')
  139.         goto done;
  140.         fst->sel_list[i] = atoi(buf+1);
  141.     }
  142.  
  143.     }
  144.  
  145.     /* that should be the end of the file */
  146.     if (elm_fgetline(buf, sizeof(buf), fp) != NULL)
  147.     goto done;
  148.  
  149.     /* success */
  150.     status = 0;
  151.  
  152. done:
  153.     (void) fclose(fp);
  154.     return status;
  155. }
  156.  
  157.