/*
 * Beans Common Source
 *
 * Tom Coppeto
 * MIT Network Group
 * 25 December 1997
 *
 * $Source: /u01/home/oracon3/tom/src/cgi/resume/RCS/util.c,v $
 * $Author: oracon3 $
 * $Locker: oracon3 $
 */

#ifndef lint
static char *rcsid="$Header: /u01/home/oracon3/tom/src/cgi/resume/RCS/util.c,v 1.3 1996/12/31 18:47:16 oracon3 Exp oracon3 $";
#endif /* lint */

#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>

#include <sys/time.h>

#include <beans.h>

    
int copy_cgi_text(char *text, char **s) {
	int len, c;
	char *sp;
	char *textp;
	char hex[5];

	len = strlen(text) + 1;
	if(*s)
		*s = (char *) realloc(*s, len * sizeof(char));
	else
		*s = (char *) malloc(len * sizeof(char));
	if(!*s) {
		logmsg(LOG_ERR, strerror(errno), "cannot allocate data");
		return(FATAL);
	}
	
	/*
	 * copy the text, trapping the cgi-isms + and %
	 */
	
	sp = *s;
	textp = text;
	while(*textp){
		if(*textp == '+')
			*sp = ' ';
		else if(*textp == '%') {
			++textp;
			hex[0] = *textp++;
			hex[1] = *textp;
			hex[2] = '\0';
			sscanf(hex, "%x", &c);
			*sp = (char) c;
		} else
			*sp = *textp;
		++sp;
		++textp;
	}
	*sp = '\0';
	return(OK);
}



    
int copy_text(char *text, char **s) {
	int len;
	
	if(!text)
		*s = (char *) NULL;
	else {
		len = strlen(text) + 1;
		if(*s)
			*s = (char *) realloc(*s, len * sizeof(char));
		else
			*s = (char *) malloc(len * sizeof(char));
		if(!*s) {
			logmsg(LOG_ERR, strerror(errno), "cannot allocate data");
			return(FATAL);
		}
		
		strcpy(*s, text);
	}
	return(OK);
}




char *get_next_word(char *line, char **ret) {
	char *c;
	char *d;
	
	if(!line || !*line)
		return((char *) NULL);

	c = line;
	while(isspace(*c))
		++c;
	if(!*c)
		return((char *) NULL);
	d = c;
	while(!isspace(*d))
		++d;
	if(!*d)
		return((char *) NULL);
	*d++ = '\0';
	*ret = c;
	return(d);
}


char *lcase(char *s) {
	char *sp;

	if(!s)
		return(s);
	
	sp = s;
	while(*sp)  {
		if(isupper(*sp))
			*sp = tolower(*sp);
		++sp;
	}

	return(s);
}


char *ucase(char *s) {
	char *sp;

	if(!s)
		return(s);
	
	sp = s;
	while(*sp)  {
		if(islower(*sp))
			*sp = toupper(*sp);
		++sp;
	}

	return(s);
}



char *whoami() {
	struct passwd *pwd;
	static char buf[16];

	if(pwd = getpwuid(getuid()))
		return(pwd->pw_name);

	sprintf(buf, "unknown(%d)", getuid());
	return(buf);
}


struct tm *get_gmtoff(int *tz) {
	time_t tt = time(NULL);
	struct tm gmt;
	struct tm *t;
	int days, hours, minutes;
 
	/* Assume we are never more than 24 hours away. */
	gmt = *gmtime(&tt); 
	t = localtime(&tt); /* buffer... so be careful */
	days = t->tm_yday - gmt.tm_yday;
	hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24)
                 + t->tm_hour - gmt.tm_hour);
	minutes = hours * 60 + t->tm_min - gmt.tm_min;
	*tz = minutes;
	return t;
}

		   
int month(char *s) {
	if(!s || !*s)
		return(-1);

	if(!(strcasecmp(s, "jan") && strcasecmp(s, "january")))
		return(0);
	if(!(strcasecmp(s, "feb") && strcasecmp(s, "february")))
		return(1);
	if(!(strcasecmp(s, "mar") && strcasecmp(s, "march")))
		return(2);
	if(!(strcasecmp(s, "apr") && strcasecmp(s, "april")))
		return(3);
	if(!(strcasecmp(s, "may") && strcasecmp(s, "may")))
		return(4);
	if(!(strcasecmp(s, "jun") && strcasecmp(s, "june")))
		return(5);
	if(!(strcasecmp(s, "jul") && strcasecmp(s, "july")))
		return(6);
	if(!(strcasecmp(s, "aug") && strcasecmp(s, "august")))
		return(7);
	if(!(strcasecmp(s, "sep") && strcasecmp(s, "september")))
		return(8);
	if(!(strcasecmp(s, "oct") && strcasecmp(s, "october")))
		return(9);
	if(!(strcasecmp(s, "nov") && strcasecmp(s, "november")))
		return(10);
	if(!(strcasecmp(s, "dec") && strcasecmp(s, "december")))
		return(11);
	return(-1);
}



void freeHit(struct hit *h) {
	struct hit *hp;

	if(!h) 
		return;

	hp = h->next;
	while(hp) {
		free(h);
		h = hp;
		hp = hp->next;
	}
	return;
}


void sfree(char *s) {
	if(s)
		free(s);
	return;
}



/*
 * Local Variables:
 * c-indent-level: 8
 * c-argdecl-indent: 8
 * c-continued-statement-offset: 8
 * c-continued-brace-offset: -8
 * c-label-offset: -8
 * c-brace-offset: -8
 * End:
 */
