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 >
Wrap
C/C++ Source or Header
|
1992-08-16
|
4KB
|
229 lines
/*
current.c : Copyright Paul Healy, EI9GL, 1992.
920706 : Created.
920808 : Cleaned up, automatically updates current.bmh on exit.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <ctype.h>
#include "rc.h"
#include "current.h"
static atexit_t
update(void)
{
char *filename = getrc(current),
*folder = currentfolder(NULL);
FILE *fp;
if ( (filename == NULL) || (fp=fopen(filename, "w")) == NULL)
return NULL;
fprintf(fp, "%s %d",
folder != NULL ? folder : getrc(username),
currentmsg(NULL) != -1 ? currentmsg(NULL) : 1);
fclose(fp);
/*
* compiler complains if nothing is returned here, so
*/
return NULL;
}
static int
postexit(void)
{
static int posted = 0;
if (posted)
return 0;
posted = 1;
return atexit( (atexit_t) update);
}
/* ------------------------------------------------------------------------ */
char *
news2dir(char *s)
{
char *p = s;
if (s == NULL)
return NULL;
while (*s) {
if (*s == '.')
*s = '/';
s++;
}
return p;
}
char *
dir2news(char *s)
{
char *p = s;
if (s == NULL)
return NULL;
while (*s) {
if (*s == '/')
*s = '.';
s++;
}
return p;
}
/* ------------------------------------------------------------------------ */
static int
readcurrent(void)
{
char s[256], *filename = getrc(current);
FILE *fp;
int msg;
if ( (filename == NULL) || (fp=fopen(filename, "r")) == NULL)
return -1;
if (fscanf(fp, "%s %d", s, &msg) != 2) {
fclose(fp);
return -1;
}
fclose(fp);
currentmsg(&msg);
return currentfolder(s) != NULL;
}
char *
currentfolder(char *f)
{
static char *curfolder = NULL;
if (f == NULL) {
if (curfolder == NULL)
readcurrent();
return curfolder;
}
if (curfolder != NULL) {
int msg = 1;
currentmsg(&msg);
free(curfolder);
postexit();
}
return curfolder = strdup(news2dir(f));
}
int
currentmsg(int *msg)
{
static int curmessage = -1;
if (msg == NULL) {
if (curmessage == -1)
readcurrent();
return curmessage;
}
else {
curmessage = *msg;
postexit();
}
return curmessage;
}
char *
newsfile(void)
{
static char name[40];
return dir2news(strcpy(name, currentfolder(NULL)));
}
/* ------------------------------------------------------------------------ */
/*
* called by the bmh n command
*/
int
setcurrent(char *folder, int curmsg)
{
currentmsg(&curmsg);
return currentfolder(folder) != NULL;
}
int
getcurrent(int argc, char *argv[], char **folder, int *curmsg)
{
int i;
char *currentbmh = getrc(current);
if ( currentbmh == NULL )
return -1;
if (access(currentbmh, 0) == -1) {
int msg = 1;
currentmsg(&msg);
currentfolder(getrc(username));
}
else
currentmsg(NULL); /* pull in current settings off disk */
for (i=1; i<argc; i++)
if (isdigit(argv[i][0])) {
int msg = atoi(argv[i]);
currentmsg(&msg);
break;
}
for (i=1; i<argc; i++)
if (argv[i][0] == '+') {
currentfolder(&argv[i][1]);
break;
}
*curmsg = currentmsg(NULL);
*folder = currentfolder(NULL);
return 0;
}
int
lastmsg(int argc, char *argv[])
{
int last = 0, i, this;
if (argc == 1)
return 0;
for (i=1; i<argc; i++)
if (isdigit(argv[i][0])) {
char *p = strchr(argv[i], '-');
if ( p != NULL)
this=atoi(p+1);
else
this=atoi(argv[i]);
if (this>last)
last = this;
}
if (last > 0)
currentmsg(&last);
return currentmsg(NULL);
}