Пример #1
0
void McuMessageHandler::SendLEDState(int flag)
{
    int dir = GpsLocationMessageHandler::Instance()->GetDirection();
    if(dir == 0)
    {
        dir = 0x01;
    }
    else
    {
        dir = 0x02;
    }

    m_driving_state = flag;
    
    for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++)
    {
        if((LEDFLAG[i] & LED_FLAG_STATE) == 0)
        {
            continue;
        }
        
        StruMcuPacket *thepacket = CreatePacket();
        
        thepacket->data = MCU_PACKET_LED_SUBTYPE_STATE;
        
        McuPacket &packet = thepacket->packet;
        PushByte(packet, MCU_PACKET_LED_SUBTYPE_STATE);
        PushByte(packet, dir);
        PushByte(packet, flag);
        SendBody(thepacket, LEDID[i]);
    }
}
Пример #2
0
void McuMessageHandler::SendLEDStateByDir(int dir)
{
    int send_dir;
    if(dir == 0)
    {
        send_dir = 0x01;
    }
    else
    {
        send_dir = 0x02;
    }

    for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++)
    {
        if((LEDFLAG[i] & LED_FLAG_DIRECT) == 0)
        {
            continue;
        }
        
        StruMcuPacket *thepacket = CreatePacket();
        
        thepacket->data = MCU_PACKET_LED_SUBTYPE_STATE;
        
        McuPacket &packet = thepacket->packet;
        PushByte(packet, MCU_PACKET_LED_SUBTYPE_STATE);
        PushByte(packet, send_dir);
        PushByte(packet, 0x03);
        SendBody(thepacket, LEDID[i]);
    }
}
Пример #3
0
void McuMessageHandler::SendPOSStation(unsigned char station, unsigned char call_station, unsigned char dir)
{
    vector<unsigned char> vstation;
    vector<unsigned char> vcall_station;
    vector<unsigned char> vdir;
    
    GetCharSequenceFromInt(vstation, station, 2);
    GetCharSequenceFromInt(vcall_station, call_station, 1);
    GetCharSequenceFromInt(vdir, dir, 1);
    
    for(int i = 0; i < m_pos_num; i ++)
    {
        StruMcuPacket *thepacket = CreatePacket();
        McuPacket &packet = thepacket->packet;
        
        PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR));
        PushByte(packet, 0x07);
        PushByte(packet, MCU_POS_FLAG);
        PushByte(packet, 0x30);
        PushByte(packet, 0x31);
        PushSequence(packet, vstation, 2);
        PushSequence(packet, vcall_station, 1);
        PushSequence(packet, vdir, 1);
        SendBody(thepacket, MCU_POS_STATION);
    }
}
Пример #4
0
void ICACHE_FLASH_ATTR WebSocketSend( uint8_t * data, int size )
{
	START_PACK;
	PushByte( 0x81 );
	if( size >= 126 )
	{
		PushByte( 0x00 | 126 );
		PushByte( size>>8 );
		PushByte( size&0xff );
	}
Пример #5
0
void McuMessageHandler::SendPOSTime(time_t *timep)
{
    struct tm *t = localtime(timep);
    int year, month, day, hour, minute, second;
    vector<unsigned char> vyear, vmonth, vday, vhour, vminute, vsecond;
    
    year = 1900 + t->tm_year;
    month = t->tm_mon + 1;
    day = t->tm_mday;
    hour = t->tm_hour;
    minute = t->tm_min;
    second = t->tm_sec;
    
    GetCharSequenceFromInt(vyear, year, 4);
    GetCharSequenceFromInt(vmonth, month, 2);
    GetCharSequenceFromInt(vday, day, 2);
    GetCharSequenceFromInt(vhour, hour, 2);
    GetCharSequenceFromInt(vminute, minute, 2);
    GetCharSequenceFromInt(vsecond, second, 2);
    
    for(int i = 0; i < m_pos_num; i ++)
    {
        StruMcuPacket *thepacket = CreatePacket();
        McuPacket &packet = thepacket->packet;
        
        PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR));
        PushByte(packet, 0x11);
        PushByte(packet, MCU_POS_FLAG);
        PushByte(packet, 0x30);
        PushByte(packet, 0x32);
        PushSequence(packet, vyear, 4);
        PushSequence(packet, vmonth, 2);
        PushSequence(packet, vday, 2);
        PushSequence(packet, m_pos_pass, 6);
        SendBody(thepacket, MCU_POS_DATE);
    }

    for(int i = 0; i < m_pos_num; i ++)
    {
        StruMcuPacket *thepacket = CreatePacket();
        McuPacket &packet = thepacket->packet;
        
        PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR));
        PushByte(packet, 0x0F);
        PushByte(packet, MCU_POS_FLAG);
        PushByte(packet, 0x30);
        PushByte(packet, 0x33);
        PushSequence(packet, vhour, 2);
        PushSequence(packet, vmonth, 2);
        PushSequence(packet, vday, 2);
        PushSequence(packet, m_pos_pass, 6);
        SendBody(thepacket, MCU_POS_TIME);
    }
}
Пример #6
0
/*  escape: 1==on 0==off */
char *i18n_utf2numref(char *instr,int escape){

  char *ucs,*headofucs;
  size_t len;
  struct Push buff;
  char strbuf[10];

  INIT_PUSH(buff);

  if (!set_i18n){
    PushString(&buff,instr);
    RETURN_PUSH(buff);
  }

  headofucs=ucs=i18n_convstring(instr, "UTF-8", "UCS-2BE", &len);

  unsigned int p;
  int i = (int) len;
  for(; i > 0; i-=2){
    p=(unsigned char)*ucs*256+(unsigned char)*(ucs+1);
    if (p<128){
      /* keep ASCII characters human readable */
      if (escape){
        switch (p){
        case '<':
          PushString(&buff, "&#0060;");
          break;
        case '>':
          PushString(&buff, "&#0062;");
          break;
        case '&':
          PushString(&buff, "&#0038;");
          break;
        case '\"':
          PushString(&buff, "&#0034;");
          break;
        case '\'':
          PushString(&buff, "&#0039;");
          break;
        default:
          PushByte(&buff,p);
        }
      }else{
        PushByte(&buff,p);
      }
    }else{
      snprintf(strbuf,10,"&#%04d;",p);
      PushString(&buff,strbuf);
    }
    ucs+=2;
  }
  free(headofucs);
  RETURN_PUSH(buff);
}
Пример #7
0
static ICACHE_FLASH_ATTR void huge()
{
	uint8_t i = 0;

	START_PACK;
	do
	{
		PushByte( 0 );
		PushByte( i );
	} while( ++i ); //Tricky:  this will roll-over to 0, and thus only execute 256 times.

	EndTCPWrite( curhttp->socket );
}
Пример #8
0
void McuMessageHandler::SendGetPosPassengerRecord(int i, EnumRecordFlag clear)
{
    StruMcuPacket *thepacket = CreatePacket();
    McuPacket &packet = thepacket->packet;
    
    PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR));
    if(clear == CLEAR_RECORD)
    {
        PushByte(packet, 0x03);
    }
    else
    {
        PushByte(packet, 0x05);
    }
    PushByte(packet, MCU_POS_FLAG);
    PushByte(packet, 0x38);
    PushByte(packet, 0x30);
    if(clear != CLEAR_RECORD)
    {
        PushByte(packet, 0x30);
        PushByte(packet, 0x31);
    }
    thepacket->data = (int)clear;
    thepacket->data2 = i;
    SendBody(thepacket, MCU_POS_GET_PASSENGER);
}
Пример #9
0
void McuMessageHandler::SendGetCanData()
{
    StruMcuPacket *thepacket = CreatePacket();
    McuPacket &packet = thepacket->packet;

    PushByte(packet, 0x01);
    SendBody(thepacket, MCU_CAN_GET_DATA);
}
Пример #10
0
void McuMessageHandler::PacketCheckSumFlag(StruMcuPacket *thepacket)
{
    unsigned char checksum = 0;
    
    McuPacket &packet = thepacket->packet;
    
    for(McuPacket::iterator it = packet.begin(); it != packet.end(); it ++)
    {
        checksum ^= (*it);
    }
    
    PushByteFront(packet, checksum);

    for(McuPacket::iterator it = packet.begin(); it != packet.end(); it ++)
    {
        if(*it == MCU_PACKET_FLAG)
        {
            *it = MCU_PACKET_ESCAPE;
            if(++it == packet.end())
            {
                PushByte(packet, 0x02);
                break;
            }
            else
            {
                it = packet.insert(it, 0x02);
            }
            continue;
        }
        else if(*it == MCU_PACKET_ESCAPE)
        {
            if(++it == packet.end())
            {
                PushByte(packet, 0x01);
                break;
            }
            else
            {
                it = packet.insert(it, 0x01);
            }
        }
    }

    PushByteFront(packet, MCU_PACKET_FLAG);
    PushByte(packet, MCU_PACKET_FLAG);
}
Пример #11
0
static char *msgsperfolder_label(char *frmptr, int subdir_no)
{
    register char *aptr;
    char dtstr[DATESTRLEN];
    char c;

    struct Push buff;

    INIT_PUSH(buff);

    aptr = frmptr;

    while ((c = *aptr++)) {
	if (c == '%') {
	    switch (*aptr++) {
	    case '%':		/* Add the % character */
		PushByte(&buff, '%');
		continue;
	    case 'd':		/* directory number, starting at 0 */
		sprintf(dtstr, "%d", subdir_no);
		PushString(&buff, dtstr);
		continue;
	    case 'D':		/* directory number, starting with 1 */
				sprintf(dtstr, "%d", subdir_no + 1);
		PushString(&buff, dtstr);
		continue;
	    case 'm':		/* number of first message in directory */
				sprintf(dtstr, "%d", set_msgsperfolder * subdir_no);
		PushString(&buff, dtstr);
		continue;
	    case 'M':		/* number of last message possible */
				sprintf(dtstr, "%d", set_msgsperfolder * (subdir_no + 1) - 1);
		PushString(&buff, dtstr);
		continue;
	    default:
		PushString(&buff, "%?");
		continue;
	    }			/* end switch */
	}

	PushByte(&buff, c);
    }				/* end while */

    RETURN_PUSH(buff);
}
Пример #12
0
void extSbyte( uint8_t b )
{
	if( is_in_outcirc )
	{
		// printf( "Broadcast: %02x (%c)\n", b, b );
		outcirc[outcirchead & (OUTCIRCBUFFSIZE-1)] = b;
		outcirchead++;
	} else {
		PushByte( b );
	}
}
Пример #13
0
void McuMessageHandler::SendGPSData(unsigned char *buf, int len)
{
    StruMcuPacket *thepacket = CreatePacket();
    McuPacket &packet = thepacket->packet;
    buf[1] = 'G';
    buf[2] = 'P';
    printf("GSPData: %s\n",buf);
    for(int i = 0; i < len; i++){
	    PushByte(packet, buf[i]);
    }

    SendBody(thepacket, MCU_PACKET_STATION_REPORT);
}
Пример #14
0
/*
*	push a chunk header
*/
bool FileIO::PushHeader(char* header)
{
	//initialize other attributes
	mCurrentMode = BYTE_MODE;
	mRecordSize = 0;

	//check for errors
	if ((this->mFileHandle == NULL) || (!header))
		return false;

	//push single bytes
	for (unsigned char c=0; c<strlen(header); c++)
		PushByte(header[c]);

	return true;
}
Пример #15
0
uint8_t UnloadCircularBufferOnThisClient( uint16_t * whence )
{
	uint16_t i16;
	uint16_t w = *whence;
	i16 = GetRoomLeft();
	while( w != outcirchead && i16 )
	{
		// printf( "." );
		PushByte( outcirc[(w++)&(OUTCIRCBUFFSIZE-1)] );
		i16--;
	}
	*whence = w;
	if( !i16 )
		return 1;
	else
		return 0;
}
Пример #16
0
void McuMessageHandler::SendLEDTime(time_t *timep)
{
    struct tm *t = localtime(timep);
    int year, month, day, hour, minute, second, week;
    vector<unsigned char> vyear, vmonth, vday, vweek, vhour, vminute, vsecond;
    
    year = t->tm_year;
    month = t->tm_mon + 1;
    day = t->tm_mday;
    hour = t->tm_hour;
    minute = t->tm_min;
    second = t->tm_sec;
    week = t->tm_wday;
    
    IntToBcd(year % 100, vyear, 1);
    IntToBcd(month, vmonth, 1);
    IntToBcd(day, vday, 1);
    IntToBcd(hour, vhour, 1);
    IntToBcd(minute, vminute, 1);
    IntToBcd(second, vsecond, 1);
    IntToBcd(week, vweek, 1);

    for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++)
    {
        if((LEDFLAG[i] & LED_FLAG_TIME) == 0)
        {
            continue;
        }
        
        StruMcuPacket *thepacket = CreatePacket();
        
        thepacket->data = MCU_PACKET_LED_SUBTYPE_TIME;
        
        McuPacket &packet = thepacket->packet;
        PushByte(packet, MCU_PACKET_LED_SUBTYPE_TIME);
        PushSequence(packet, vyear, 1);
        PushSequence(packet, vmonth, 1);
        PushSequence(packet, vday, 1);
        PushSequence(packet, vweek, 1);
        PushSequence(packet, vhour, 1);
        PushSequence(packet, vminute, 1);
        PushSequence(packet, vsecond, 1);
        SendBody(thepacket, LEDID[i]);
    }
}
Пример #17
0
void McuMessageHandler::SendGetDealRecord(int i, EnumRecordFlag clear, int index)
{
    StruMcuPacket *thepacket = CreatePacket();
    McuPacket &packet = thepacket->packet;
    
    PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR));
    PushByte(packet, 0x06);
    PushByte(packet, MCU_POS_FLAG);
    PushByte(packet, 0x38);
    PushByte(packet, 0x31);
    if(clear == CLEAR_RECORD)
    {
        PushByte(packet, 0x02);
    }
    else
    {
        PushByte(packet, 0x01);
    }
    PushWord(packet, index);
    thepacket->data = (int)clear;
    thepacket->data2 = i;
    SendBody(thepacket, MCU_POS_GET_DEAL);
}
Пример #18
0
void McuMessageHandler::SendLEDStation(unsigned char mode, unsigned char speed, unsigned char times, unsigned char inout, unsigned char station)
{
    for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++)
    {
        if((LEDFLAG[i] & LED_FLAG_STATION) == 0)
        {
            continue;
        }
        
        StruMcuPacket *thepacket = CreatePacket();
        
        thepacket->data = MCU_PACKET_LED_SUBTYPE_STATION;
        
        McuPacket &packet = thepacket->packet;
        PushByte(packet, MCU_PACKET_LED_SUBTYPE_STATION);
        PushByte(packet, mode);
        PushByte(packet, speed);
        PushByte(packet, times);
        PushByte(packet, inout);
        PushByte(packet, station);
        SendBody(thepacket, LEDID[i]);
    }
}
Пример #19
0
char *unobfuscate_email_address(char *address){

#define uea_sbufsize 6
  int flag=0; /* 1==obfuscated string */
  int on_error=0; /* 1==something was happen */
  int on_hex=0; /* 1==base 16 */
  int at_mailbox=1; /* 1==mailbox part */
  char *sentinel;

  char sbuf[uea_sbufsize+1];
  int scount=0;
  struct Push buf;
  long c;

  INIT_PUSH(buf);

  sentinel=strlen(address)+address;


  while (address<sentinel){
    switch (*address){
    case '&':
      if(strncmp(address,"&#x40;",6)==0){
	PushByte(&buf, '@');
	address+=5;
	flag=0;
	on_error=0;
	on_hex=0;
	at_mailbox=0;
	break;
      }
      if (at_mailbox){
	PushByte(&buf, '&');
	break;
      }
      if (flag==0){
	flag=1;
      }else{
	/* broken string */
	on_error=1;
      }
      break;
    case ';':
      if (at_mailbox){
	PushByte(&buf, ';');
	break;
      }
      if (flag==1){
	flag=0;
	sbuf[scount]=0;
	if(on_hex==1){
	  c=strtol(sbuf,NULL,16);
	}else{
	  c=strtol(sbuf,NULL,10);
	}
	if (on_error==0){
	  PushByte(&buf, (unsigned char)c); /* should be ASCII */
	} /* simply discard error */
	scount=0;
	on_error=0;
      }else{
	/* broken string */
	on_error=1;
      }
      break;
    case '#':
      if (at_mailbox){
	PushByte(&buf, '#');
	break;
      }
      if (flag==1){
	if (*(address+1)=='x'){
	  on_hex=1;
	  address++;
	}else{
	  on_hex=0;
	}
	break;
      }else{
	PushByte(&buf, *address);
      }
      break;
    default:
      if (at_mailbox || flag==0){
	PushByte(&buf, *address);
      }else{
	sbuf[scount++]=*address;
	if (scount>=uea_sbufsize){
	  /* broken string */
	  scount=0;
	  on_error=1;
	}
      }
    }
    address++;
  }
  RETURN_PUSH(buf);
}
Пример #20
0
void PushString( const char * buffer )
{
	char c;
	while( c = *(buffer++) )
		PushByte( c );
}
Пример #21
0
void PushBlob( const uint8 * buffer, int len )
{
	int i;
	for( i = 0; i < len; i++ )
		PushByte( buffer[i] );
}
Пример #22
0
static char *url_replying_to(struct emailinfo *email, char *line1,	/* first line of quoted text, with html */
			     const char *line2,	/* first line of quoted text, w/o html */
			     const struct body *bp, int quote_num, int *quoting_msgnum, int count_quoted_lines, int maybe_reply)
{
    String_Match match_info;
    char *p;
    int subjmatch = 0;
    char *anchor;
    struct emailinfo *ep;
	int statusnum = hashreplynumlookup(*quoting_msgnum, email->inreplyto, email->subject,
			   &subjmatch);
    hashnumlookup(*quoting_msgnum, &ep);
    trio_asprintf(&anchor, "%.4dqlink%d", *quoting_msgnum, quote_num);
    if (statusnum != -1) {
	struct emailinfo *ep2;
	hashnumlookup(statusnum, &ep2);
		if (add_anchor(statusnum, *quoting_msgnum, quote_num, anchor, line1, 0, count_quoted_lines, NULL)) {
	    char *path = get_path(ep, ep2);
	    char *buf;
			trio_asprintf(&buf, "%s%.4d.%s#%s", path, statusnum, set_htmlsuffix, anchor);
	    if (maybe_reply)
		set_new_reply_to(statusnum, strlen(line2));
	    if (*path)
	        free(path);
	    free(anchor);
	    return buf;
	}
	if (strlen(line2) > 6 && (p = strstr(line2, "..."))) {
	    char *parsed;
	    char *tptr = (char *)emalloc(p - line2 + 1 + strlen(p + 3));	/* AUDIT biege: IOF unlikely */
	    strncpy(tptr, line2, p - line2);
	    strcpy(tptr + (p - line2), p + 3);
	    parsed = ConvURLsString(tptr, email->msgid, email->subject, email->charset);
	    free(tptr);
	    tptr = stripwhitespace(parsed ? parsed : "");
	    if (parsed)
		free(parsed);
			if (add_anchor(statusnum, *quoting_msgnum, quote_num, anchor, tptr, 1, count_quoted_lines, NULL)) {
	        char *path = get_path(ep, ep2);
		char *buf;
				trio_asprintf(&buf, "%s%.4d.%s#%s", path, statusnum, set_htmlsuffix, anchor);
		free(tptr);
		if (maybe_reply)
		    set_new_reply_to(statusnum, strlen(buf));
		if (*path)
	            free(path);
		free(anchor);
		return buf;
	    }
	    free(tptr);
	}
    }
    {
	int i;
	struct Push full_line;
	struct Push exact_line;
	INIT_PUSH(full_line);
	INIT_PUSH(exact_line);
	PushString(&full_line, p = stripwhitespace(line2));
	free(p);
	PushString(&exact_line, line2);
	for (i = 1; i < count_quoted_lines && (bp = bp->next); ++i) {
	    char *stripped = unquote_and_strip(bp->line);
	    PushByte(&full_line, '\n');
	    PushString(&full_line, p = stripwhitespace(stripped));
	    free(p);
	    free(stripped);
	    PushString(&exact_line, unquote(bp->line));
	}
		search_for_quote(PUSH_STRING(full_line), PUSH_STRING(exact_line), *quoting_msgnum, &match_info);
	free(PUSH_STRING(full_line));
	free(PUSH_STRING(exact_line));
    }
    if (match_info.msgnum >= 0) {
		char *parsed = ConvURLsString(match_info.last_matched_string, email->msgid,
			   email->subject, email->charset);
	if (parsed) {
	    char *parsed2 = stripwhitespace(parsed);
	    free(parsed);
	    if (add_anchor(match_info.msgnum, *quoting_msgnum, quote_num, anchor, parsed2, 1, count_quoted_lines, &match_info)) {
	        struct emailinfo *ep2;
                char *path;
                char *buf;
                hashnumlookup(match_info.msgnum, &ep2);
                path = get_path(ep, ep2);

		trio_asprintf(&buf, "%s%.4d.%s#%s", path, match_info.msgnum, set_htmlsuffix, anchor);
		set_new_reply_to(match_info.msgnum, match_info.match_len_bytes);
		free(parsed2);
		if (*path)
		    free(path);
		free(anchor);
		return buf;
	    }
	    free(parsed2);
	}
	if (match_info.last_matched_string)
	    free(match_info.last_matched_string);
    }
    if (count_quoted_lines < 3 && strcmp(get_quote_prefix(), ">")
	&& strcmp(get_quote_prefix(), " >"))	/* was quote_prefix guess shaky? */
	*quoting_msgnum = -1;	/* msg probably doesn't have any quotes */
    free(anchor);
    return NULL;
}
Пример #23
0
void check1dir(char *dir)
{
    struct stat sbuf;

    if (stat(dir, &sbuf)) {
    /*
    ** LCC only has the short mkdir().  Fortunately, we do a chmod
    ** immediately afterward, so it's a don't care.
    */
#ifdef __LCC__
	if (errno != ENOENT || mkdir(dir) < 0) {
#else
	if (errno != ENOENT || mkdir(dir, set_dirmode) < 0) {
#endif
            if (errno != EEXIST) {
		snprintf(errmsg, sizeof(errmsg), "%s \"%s\".", lang[MSG_CANNOT_CREATE_DIRECTORY], dir);
		progerr(errmsg);
	    }
	}
	if (set_showprogress)
			printf(" %s \"%s\", %s %o.\n", lang[MSG_CREATING_DIRECTORY], dir, lang[MSG_MODE], set_dirmode);

	if (chmod(dir, set_dirmode) == -1) {
			snprintf(errmsg, sizeof(errmsg), "%s \"%s\" to %o.", lang[MSG_CANNOT_CHMOD], dir, set_dirmode);
	    progerr(errmsg);
	}
    }
}

/*
** This tries to create and chmod a path to a directory.
*/

void checkdir(char *dir)
{
    register char *p;
    struct stat sbuf;
    int ch;

    p = dir;

    if (*p && *p == '/')	/* get off root */
	p++;

    for (;; ++p) {
	if (!*p || *p == '/') {
	    ch = *p;
	    *p = '\0';
	    if (stat(dir, &sbuf)) {
	    /* See comment in check1dir */
#ifdef __LCC__
		if (errno != ENOENT || mkdir(dir) < 0) {
#else
		if (errno != ENOENT || mkdir(dir, set_dirmode) < 0) {
#endif
		    if (errno != EEXIST) {
			snprintf(errmsg, sizeof(errmsg), "%s \"%s\".", lang[MSG_CANNOT_CREATE_DIRECTORY], dir);
			progerr(errmsg);
		    }
		}
		if (set_report_new_folder) {
		    printf("%s\n", dir);
		}
		if (set_showprogress)
		    printf(" %s \"%s\", %s %o.\n", lang[MSG_CREATING_DIRECTORY], dir, lang[MSG_MODE], set_dirmode);

		if (chmod(dir, set_dirmode) == -1) {
		    snprintf(errmsg, sizeof(errmsg), "%s \"%s\" to %o.", lang[MSG_CANNOT_CHMOD], dir, set_dirmode);
		    progerr(errmsg);
		}
	    }
	    *p = ch;
	    if (!*p)
		break;
	}
    }
}

char *getfilecontents(char *filename)
{
    FILE *infile;
    struct stat finfo;
    char *retval;

    if ((infile = fopen(filename, "r")) == NULL)
	return (NULL);

    if (fstat(fileno(infile), &finfo) != 0) {
	(void)fclose(infile);
	return (NULL);
    }

    if (finfo.st_size == 0) {
	(void)fclose(infile);
	return (NULL);
    }

    else {
	if (!(retval = (char *)malloc((unsigned)finfo.st_size + 1))) {
	    (void)fclose(infile);
	    return (NULL);
	}
	if (!fread(retval, (size_t) finfo.st_size, 1, infile)) {
	    (void)fclose(infile);
	    free(retval);
	    return (NULL);
	}
	*(retval + finfo.st_size) = '\0';
    }
    (void)fclose(infile);
    return (retval);
}


/*
** expand_path - fill in values substituting for magic cookies
**
**      Substitution cookies supported
**
**              %d - two digit day of month (1-28/30/31)
**              %D - three letter day of the week
**              %m - two digit month of year (1-12)
**              %M - three letter month of year (Jan, Feb, ..., Dec)
**              %y - four digit year (1990,..2001)
**
** Returns:   expanded path string
*/

char *dirpath(char *frmptr)
{
    register char *aptr;
    char dtstr[DATESTRLEN];
    char c;
    struct tm *now;
    time_t clk;

    struct Push buff;

    INIT_PUSH(buff);

    clk = time((time_t *)0);
    now = localtime(&clk);

    aptr = frmptr;

    while ((c = *aptr++)) {
	if (c == '%') {
	    switch (*aptr++) {
	    case '%':		/* Add the % character */
		PushByte(&buff, '%');
		continue;
	    case 'd':		/* two digit day of month (1-31) */
		sprintf(dtstr, "%.2d", now->tm_mday);
		PushString(&buff, dtstr);
		continue;
	    case 'D':		/* three letter day of week */
		PushString(&buff, days[now->tm_wday]);
		continue;
	    case 'j':		/* julian date */
		sprintf(dtstr, "%.3d", now->tm_yday);
		PushString(&buff, dtstr);
		continue;
	    case 'm':		/* two digit month of year (1-12) */
		sprintf(dtstr, "%.2d", now->tm_mon + 1);
		PushString(&buff, dtstr);
		continue;
	    case 'M':		/* three letter month of year */
		PushString(&buff, months[now->tm_mon]);
		continue;
	    case 'y':		/* 4 digit year */
		sprintf(dtstr, "%.4d", now->tm_year + 1900);
		PushString(&buff, dtstr);
		continue;
	    default:
		PushString(&buff, "%?");
		continue;
	    }			/* end switch */
	}

	PushByte(&buff, c);
    }				/* end while */

    RETURN_PUSH(buff);
}

/*
** Reads a configuration file if it exists and puts all the right
** values into the right variables.
*/

void readconfigs(char *path, int cmd_show_variables)
{
    if (path && path[0] == '~') {
    	char *ep; 
	char tmppath[MAXFILELEN]; /*AUDIT biege: pathname + filename is more then 4KB long on linux */
        struct passwd *pp;

#ifndef __LCC__
	/*
	** Getting password data from /etc/passwd is pretty silly in
	** Win9x systems since nearly everybody builds this file after
	** they set up $HOME.  Just skip this try at finding a default
	** location for the config file and go on to try $HOME.
	*/
        if ((pp = getpwuid(getuid())) != NULL) {
			snprintf(tmppath, sizeof(tmppath), "%s%s", pp->pw_dir, path + 1);	/* AUDIT biege: who gurantees that path+1 contains data? */
            ConfigInit(tmppath);
        } 
	else
#endif
            if ((ep = getenv("HOME")) != NULL) { /* AUDIT biege: possible BOF.. but it's not setuid.. so why to care? */
			snprintf(tmppath, sizeof(tmppath), "%s%s", ep, path + 1);	/* AUDIT biege: who gurantees that path+1 contains data? */
            ConfigInit(tmppath);
        }
        /* 
         * So what happens here if the above two conditions fail ????
         * Simply use the compiled in defaults ?
         */
    }    
    else {
	ConfigInit(path);

	if (set_showprogress && !cmd_show_variables)
	    printf("%s: %s\n", lang[MSG_PATH], path);
    }
}

void symlink_latest()
{
    /*
    ** Symlinks work so differently in Windows that I think we'll just
    ** skip that whole thing and ignore that option.
    */
#ifdef __LCC__
    snprintf(errmsg, sizeof(errmsg),
	"WARNING: latest_folder not supported in Win32 environment.\n");
    fprintf(stderr, "%s", errmsg);
#else
    char filename[MAXFILELEN];
    
    struct stat stbuf;

    if (!latest_folder_path)
	return;			/* haven't created new folder this time? */
    trio_snprintf(filename, MAXFILELEN, "%s%s", set_dir, set_latest_folder);

    if (!stat(filename, &stbuf) && unlink(filename)) {
		snprintf(errmsg, sizeof(errmsg), "%s \"%s\" (latest_folder option).", lang[MSG_CANNOT_UNLINK], filename);
	progerr(errmsg);
	return;
    }

    if (symlink(latest_folder_path, filename)) {
		snprintf(errmsg, sizeof(errmsg), "%s \"%s\" (latest_folder option).", lang[MSG_CANNOT_CREATE_SYMLINK], filename);
	progerr(errmsg);
	return;
    }
#endif
}