home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
b
/
bmh02src.zip
/
FOLDERS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-16
|
6KB
|
234 lines
/*
folders.c : Copyright Paul Healy, EI9GL, 1992.
Derived from bm.
Copyright 1986 Bdale Garbee, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
911222 : Added this header.
920601 : Added test program to display info on folders - define FOLDER_PROG
920604 : folders.exe, release v0.1
920610 : release v0.2, stars public areas
920616 : release v0.3, simpler date, flag locked folders
920708 : v0.5, cleanup for release
920715 : Cleanup bmh v0.1 release, fixed month display
920719 : Added recursive search of mail directory for news files
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <io.h>
#include <time.h>
#include "pc.h"
#include "rc.h"
#include "lock.h"
#include "misc.h"
#include "help.h"
#include "pager.h"
#ifdef BMH
#define main folders_main
#endif
/* undef this to make dates appear in the form <Month>/<Day>/<Year>
This should go into bm.rc
*/
#define DAY_FIRST
static const char PublicMark = '*';
static const int MaxAreas = 25;
static const int AreaLength = 20;
static char *areas = NULL;
static int numareas = 0;
static int listfolders_long(char *maildir, char *prefix);
static int
readareas(char *dir)
{
char s[256], *p, *cp;
FILE *fp;
if ( (dir == NULL) || (areas != NULL) )
return -1;
if ( (areas = (char *) malloc(sizeof(char)*AreaLength*MaxAreas)) == NULL) {
fprintf(stderr, "folders: warning no room to hold public areas\n");
return -1;
}
sprintf(s, "%s/areas", dir);
if ((fp=fopen(s, "r")) == NULL) {
free (areas);
areas = NULL;
return -1;
}
numareas = 0;
p = areas;
while (fgets(s, sizeof(s), fp) != NULL) {
if ( (*s=='\0') || (*s==' ') || (*s=='\n') || (*s=='\t') )
continue;
s[9] = '\0';
cp = s;
while (*cp)
if ( (*cp == ' ') || (*cp == '\t') )
*cp = '\0';
else
cp++;
strncpy(p, s, AreaLength);
p[AreaLength-1] = '\0';
numareas++;
p+=AreaLength;
}
fclose(fp);
qsort(areas, numareas, AreaLength, strcmp);
return 0;
}
static int
ispublic(char *s)
{
return bsearch(s, areas, numareas, AreaLength, strcmp) != 0;
}
static void
listfolders_short(char *maildir)
{
char buf[256], *cp, *cp1;
Directory d;
sprintf(buf, "%s/*%s", maildir, EXT);
initdir(&d);
while( ( cp1 = readdir(&d, buf) ) != NULL ) {
cp = strchr(cp1,'.');
*cp = '\0';
printf("%10s", cp1);
}
}
static char *
bmtime(const time_t *t, char *s)
{
struct tm *tmp = localtime(t);
sprintf(s, "%2d/%02d/%2d",
#ifdef DAY_FIRST
tmp->tm_mday, tmp->tm_mon+1,
#else
tmp->tm_mon+1, tmp->tm_mday,
#endif
tmp->tm_year);
return s;
}
static int
listnews(char *maildir, char *prefix)
{
char buf[256], *cp1, newmaildir[256], newprefix[256];
Directory d;
if ( listfolders_long(maildir, prefix) == -1)
return -1;
sprintf(buf, "%s/*. ", maildir);
initdir(&d);
while( ( cp1 = readdir(&d, buf) ) != NULL ) {
struct stat sbuf;
sprintf(buf, "%s/%s", maildir, cp1);
if ( stat(buf, &sbuf) == -1) {
fprintf(stderr, "\nfolders: warning can't stat %s\n", buf);
return -1;
}
if (!(sbuf.st_mode & S_IFDIR) || (*cp1 == '.'))
continue;
sprintf(newmaildir, "%s/%s", maildir, cp1);
sprintf(newprefix, "%s%s.", prefix, cp1);
if ( listnews(newmaildir, newprefix) == -1)
return -1;
}
return 0;
}
static int
listfolders_long(char *maildir, char *prefix)
{
char buf[256], *cp, *cp1, date[10], line[256], name[256];
Directory d;
sprintf(buf, "%s/*%s", maildir, EXT);
page_setup();
initdir(&d);
while( ( cp1 = readdir(&d, buf) ) != NULL ) {
struct stat sbuf;
sprintf(buf, "%s/%s", maildir, cp1);
if ( stat(buf, &sbuf) == -1) {
fprintf(stderr, "\nfolders: warning can't stat %s\n", buf);
return -1;
}
if ( (cp = strchr(cp1, '.')) != NULL)
*cp = '\0';
if (prefix[0] == '\0')
strcpy(name, cp1);
else
sprintf(name, "%s%s", prefix, cp1);
sprintf(buf, "%s/%s", maildir, cp1);
sprintf(line, " %c %s%-*s%8ld %s %c",
ispublic(name) == 0 ? ' ' : PublicMark,
prefix, 20 - strlen(prefix), cp1,
sbuf.st_size, bmtime(&sbuf.st_atime, date),
islocked(buf) ? 'L' : ' ');
if (page_puts(line) == -1)
return -1;
}
return 0;
}
int
main(int argc, char *argv[])
{
dohelp(argc, argv, "folders [-short - mail]");
if (setupbm()==-1)
return -1;
(void) readareas(getrc(spooldir));
if (argc > 1) {
if (strcmp(argv[1], "-short") == 0)
listfolders_short(getrc(maildir));
else if (strcmp(argv[1], "-mail") == 0)
(void) listfolders_long(getrc(maildir), "");
else
fprintf(stderr, "folders: unknown option %s\n", argv[1]);
}
else
(void) listnews(getrc(maildir), "");
return 0;
}