home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
3
/
3138
/
anne.misc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-03-26
|
5KB
|
282 lines
/*
* anne.jones - anne.misc.c - 03/26/91
*
* Miscellaneous routines used by anne.jones
*/
#include <stdio.h>
#include <string.h>
#ifndef AIX /* AIX doesn't have malloc.h. Oh well. */
#include <malloc.h>
#endif
#include <sys/types.h>
#include <time.h>
#include <sys/timeb.h>
#include "anne.h"
char *legaldate(), *fixgmt(), *fix_date(), *fix_expire();
extern char *safemalloc(), *saferealloc(), *get_a_line();
FILE *
trypath(route, name)
register char *route, *name;
{
register char *s;
if ((s = safemalloc(strlen(route) + strlen(name) + 2)) == (char *) NULL) {
perror("trypath safemalloc");
exit(1);
}
strcpy(s, route);
strcat(s, "/");
strcat(s, name);
return (fopen(s, "r"));
}
char *
squeeze(str)
char *str;
{
register char *s, *t, *u;
#ifdef DEBUG
fprintf(debug, "squeeze got ->%s<-\n", str);
#endif
s = str;
/*
worst case it'll be as long as s .. never longer
*/
t = u = safemalloc(strlen(s) + 1);
/*
fixed by fk@u222.rci.dk (Flemming Kraglund); it stored one too many
\0's, which can become a problem if the length of the string is a
multiple of the memory allocation size
*/
for (; *s; ++s) if (*s != ' ' && *s != '\t') *t++ = *s;
*t = '\0';
#ifdef DEBUG
fprintf(debug, "squeeze gave back ->%s<-\n", u);
#endif
return (u);
}
void
squeeze2(s)
register char *s;
{
register char *t, *u, *orig;
t = u = safemalloc(strlen(s) + 8);
#ifdef DEBUG
fprintf(debug, "squeeze2 got ->%s<-\n", s);
#endif
orig = s;
do {
if (!((*s < '\040') &&
((*s >= '\015') || (*s == '\013') || (*s < '\010'))
)) {
*(t++) = *s;
}
} while (*(++s));
*t = '\0';
strcpy(orig, u);
#ifdef DEBUG
fprintf(debug, "squeeze2 put ->%s<-\n", orig);
#endif
free(u);
}
char *
getmname(f)
FILE *f;
{
char *s, *t;
s = safemalloc(NAMESIZE + 1);
if ((get_a_line(s, NAMESIZE, f)) == (char *) NULL) {
perror("getmname");
exit(1);
}
fclose(f);
if (*(t = (s + strlen(s) - 1)) == '\n')
*t = '\0';
return (s);
}
int
know_head(s)
char *s;
{
register char *t;
if ((t = strstr(allheads, s)) != (char *) NULL)
return (((t - allheads) / 13) + 1);
else
return (NOTKNOWN);
}
char **
buildbook(dim)
int dim;
{
register int i;
register char *pages, **book;
pages = safemalloc(dim * dim);
if (pages == (char *)NULL) {
fprintf(stderr, "No heap space for header book items!\n");
exit(1);
}
book = (char **) calloc(dim, sizeof(char *));
if (book == (char **)NULL) {
fprintf(stderr, "No heap space for header book!\n");
exit(1);
}
for (i = 0; i < dim; i++) {
*(book + i) = pages;
pages += dim;
}
return (book);
}
void
termin()
{
creat("/tmp/termin", 0644);
exit(0);
}
/* This was ripped from rn 4.3 */
#ifndef HAVESTRSTR
/* return ptr to little string in big string, NULL if not found */
char *
strstr(big, little)
char *big, *little;
{
register char *t, *s, *x;
for (t = big; *t; t++) {
for (x = t, s = little; *s; x++, s++) {
if (!*x)
return ((char *) NULL);
if (*s != *x)
break;
}
if (!*s)
return (t);
}
return ((char *) NULL);
}
#endif /* HAVESTRSTR */
#ifdef MINIMALIST
/*
* The following is based on a program apparently written by Jon Zeeff
* (zeeff@b-tech.ann-arbor.mi.us). Palkovic@linac.fnal.gov, 3/22/91.
*
* A string of some valid message id characters
*/
void
rand_id(s)
char *s;
{
static char string[] = "!#._+-=ABCDFGHJKLMNPQRSTVWXYZ1234567890";
register int size = sizeof(string) - 1;
long num;
num = (time((long *)0) - 658216800) / 60;
do {
*s++ = string[num % size];
num /= size;
} while (num);
num = (long) getpid();
do {
*s++ = string[num % size];
num /= size;
} while (num);
*s = '\0';
}
#endif /* MINIMALIST */
static struct timeb epoch;
char *
fix_date(s)
char *s;
{
time_t absdate;
ftime(&epoch);
if ((absdate = getabsdate(s, &epoch)) < 0)
drop("Date:");
else
return(fixgmt(absdate, s, "Date:"));
}
/*
* in anne.jones, there's a note that this will use getreldate instead
* at some point in the future
*/
char *
fix_expire(s)
char *s;
{
time_t absdate;
ftime(&epoch);
if ((absdate = getdate(s, &epoch)) < (time_t)0)
drop("Expires:");
else
return(fixgmt(absdate, s, "Expires:"));
}
drop(s)
char *s;
{
fprintf(stderr, "anne.jones: bad %s header\n", s);
exit(1);
}
char *
fixgmt(t, s, head)
time_t t;
char *s;
char *head;
{
register struct tm *prstime = gmtime(&t);
return (legaldate(prstime, s, head));
}
char *mon[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec",
};
char *
legaldate(tm, s, head)
struct tm *tm;
char *s;
char *head;
{
static char *dayname[8] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat", "Sun",
};
char *_tmp = safemalloc(29 + strlen(head));
sprintf(_tmp,
"%s %s, %02d %s %02d %02d:%02d:%02d GMT",
head,
*(dayname + tm->tm_wday), tm->tm_mday,
*(mon + tm->tm_mon), tm->tm_year,
tm->tm_hour, tm->tm_min, tm->tm_sec);
free(s);
return(_tmp);
}