home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / CURRENT.C < prev    next >
C/C++ Source or Header  |  1992-08-16  |  4KB  |  229 lines

  1. /* 
  2.    current.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    920706 : Created.
  5.    920808 : Cleaned up, automatically updates current.bmh on exit.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <io.h>
  11. #include <ctype.h>
  12. #include "rc.h"
  13. #include "current.h"
  14.  
  15. static atexit_t
  16. update(void)
  17. {
  18.    char *filename = getrc(current),
  19.         *folder = currentfolder(NULL);
  20.    FILE *fp;
  21.  
  22.    if ( (filename == NULL) || (fp=fopen(filename, "w")) == NULL)
  23.       return NULL;
  24.  
  25.    fprintf(fp, "%s %d",
  26.             folder != NULL ? folder : getrc(username),
  27.             currentmsg(NULL) != -1 ? currentmsg(NULL) : 1); 
  28.  
  29.    fclose(fp);
  30.  
  31.    /*
  32.     *  compiler complains if nothing is returned here, so
  33.     */
  34.    return NULL; 
  35. }
  36.  
  37. static int
  38. postexit(void)
  39. {
  40.    static int posted = 0;
  41.  
  42.    if (posted)
  43.       return 0;
  44.  
  45.    posted = 1;
  46.  
  47.    return atexit( (atexit_t) update);
  48. }
  49.  
  50. /* ------------------------------------------------------------------------ */
  51.  
  52. char *
  53. news2dir(char *s)
  54. {
  55.    char *p = s;
  56.  
  57.    if (s == NULL)
  58.       return NULL;
  59.  
  60.    while (*s) {
  61.       if (*s == '.')
  62.          *s = '/';
  63.       s++;
  64.       }
  65.    return p;
  66. }
  67.  
  68. char *
  69. dir2news(char *s)
  70. {
  71.    char *p = s;
  72.  
  73.    if (s == NULL)
  74.       return NULL;
  75.  
  76.    while (*s) {
  77.       if (*s == '/')
  78.          *s = '.';
  79.       s++;
  80.       }
  81.    return p;
  82. }
  83.  
  84. /* ------------------------------------------------------------------------ */
  85.  
  86. static int
  87. readcurrent(void)
  88. {
  89.    char s[256], *filename = getrc(current);
  90.    FILE *fp;
  91.    int msg;
  92.  
  93.    if ( (filename == NULL) || (fp=fopen(filename, "r")) == NULL)
  94.       return -1;
  95.    if (fscanf(fp, "%s %d", s, &msg) != 2) {
  96.       fclose(fp);
  97.       return -1;
  98.       }
  99.    fclose(fp);
  100.  
  101.    currentmsg(&msg);
  102.  
  103.    return currentfolder(s) != NULL;
  104. }
  105.  
  106. char *
  107. currentfolder(char *f)
  108. {
  109.    static char *curfolder = NULL;
  110.  
  111.    if (f == NULL) {
  112.       if (curfolder == NULL)
  113.          readcurrent();
  114.       return curfolder;
  115.       }
  116.    if (curfolder != NULL) {
  117.       int msg = 1;
  118.  
  119.       currentmsg(&msg);
  120.       free(curfolder);
  121.       postexit();
  122.       }
  123.  
  124.    return curfolder = strdup(news2dir(f));
  125. }
  126.  
  127. int
  128. currentmsg(int *msg)
  129. {
  130.    static int curmessage = -1;
  131.  
  132.    if (msg == NULL) {
  133.       if (curmessage == -1)
  134.          readcurrent();
  135.       return curmessage;
  136.       }
  137.    else {
  138.       curmessage = *msg;
  139.       postexit();
  140.       }
  141.  
  142.    return curmessage;
  143. }
  144.  
  145. char *
  146. newsfile(void)
  147. {
  148.    static char name[40];
  149.  
  150.    return dir2news(strcpy(name, currentfolder(NULL)));
  151. }
  152.  
  153. /* ------------------------------------------------------------------------ */
  154.  
  155. /*
  156.  * called by the bmh n command
  157.  */
  158. int
  159. setcurrent(char *folder, int curmsg)
  160. {
  161.    currentmsg(&curmsg);
  162.  
  163.    return currentfolder(folder) != NULL;
  164. }
  165.  
  166. int
  167. getcurrent(int argc, char *argv[], char **folder, int *curmsg)
  168. {
  169.    int i;
  170.    char *currentbmh = getrc(current);
  171.  
  172.    if ( currentbmh == NULL )
  173.       return -1;
  174.  
  175.    if (access(currentbmh, 0) == -1) {
  176.       int msg = 1;
  177.  
  178.       currentmsg(&msg);
  179.       currentfolder(getrc(username));
  180.       }
  181.    else 
  182.       currentmsg(NULL);  /* pull in current settings off disk */
  183.  
  184.    for (i=1; i<argc; i++)
  185.       if (isdigit(argv[i][0])) {
  186.          int msg = atoi(argv[i]);
  187.          currentmsg(&msg);
  188.          break;
  189.          }
  190.  
  191.    for (i=1; i<argc; i++)
  192.       if (argv[i][0] == '+') {
  193.          currentfolder(&argv[i][1]);
  194.          break;
  195.          }
  196.  
  197.    *curmsg = currentmsg(NULL);
  198.    *folder = currentfolder(NULL);
  199.  
  200.    return 0;
  201. }
  202.  
  203. int
  204. lastmsg(int argc, char *argv[])
  205. {
  206.    int last = 0, i, this;
  207.  
  208.    if (argc == 1)
  209.       return 0;
  210.  
  211.    for (i=1; i<argc; i++)
  212.       if (isdigit(argv[i][0])) {
  213.          char *p = strchr(argv[i], '-');
  214.  
  215.          if ( p != NULL)
  216.             this=atoi(p+1);
  217.          else
  218.             this=atoi(argv[i]);
  219.  
  220.          if (this>last)
  221.             last = this;
  222.          }
  223.  
  224.    if (last > 0)
  225.       currentmsg(&last);
  226.  
  227.    return currentmsg(NULL);
  228. }
  229.