Subject: Re: Building gcc-2.5.8
To: current-users <current-users@sun-lamp.cs.berkeley.edu>
From: Manuel Bouyer <bouyer@ensta.fr>
List: current-users
Date: 05/13/1994 18:37:19
>   What about incorporating a more up-to-date gcc than gcc-2.4.5 into
>   the source tree?
>
>Is there something *wrong* with the one currently in the tree?  It seems
>to work just fine for everything I've built.

I think there is a bug in gcc2.4.5 optimisation. The folloing code works for
me (pc 486 dx2 66, tar-balls from 16 april) whith cc or cc -g, but not
whith cc -O.
Compile whith cc -o rdate rdate.c, and then rdate <host>. repeat whith cc -O
and see.

rdate.c:

/*
	Ce programme est l'equivalent de rdate (Sun).
	Ecrit par P.Chiccourat.
	Usage: rdate <host>
*/

#include <stdio.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <netinet/in.h>


#define OFFSET	0x83aa7e80

int length[] = {
	31, 28, 31, 30, 31, 30,
	31, 31, 30, 31, 30, 31
};

main(argc,argv)
int argc;
char **argv;
{	
	unsigned char s[1024], *t;
	union time {
		u_long l;
		unsigned char c[sizeof(u_long)];
	} clock;
	int fd, pid, l, index;
	union wait status;
	struct tm *tm;
	int fildes[2];
	struct timeval timeval;
	struct timezone timezon;

	gettimeofday(&timeval,&timezon);
	tm = localtime(&timeval.tv_sec);

	if (argc != 2) {
		fprintf(stderr,"Use: getdate <host>\n");
		exit(3);
	}

	if ((fd = c_clientbyname(argv[1],"time")) == -1) {
		printf("Error when creating socket\n");
		exit (1);
	}
	pipe(fildes);
	if (!(pid = fork())) {
		close(fildes[0]);
		while ((l = read(fd,s,1024)) > 0) {
				write(fildes[1],s,l);
#ifdef DEBUG
				t = s;
				while (t - s < l)
					fprintf(stderr,"> %02x",*t++);
#endif
			}
		close(fildes[1]);
		exit(0);
	} else {
		close(fildes[1]);
		while ((l = read(fildes[0],s,1024)) > 0) {
#ifdef DEBUG
			fprintf(stderr,"Read: %d\n",l);
#endif
			t = s;
			while ((t - s < l) && (index < sizeof(u_long))) {
#ifdef DEBUG
				fprintf(stderr,"< %02x",*t);
#endif
				clock.c[index++] = *t++;
			}
		}
		wait(&status);
		clock.l = ntohl(clock.l) - OFFSET;
		if (index != sizeof(u_long)) {
			fprintf(stderr,"getdate: Unable to get a correct answer\n");
			exit(1);
		}
		gettimeofday(&timeval,&timezon);
#ifdef DEBUG
		fprintf(stderr,"Was %lx, now %lx\n",timeval.tv_sec, clock.l);
#endif
		timeval.tv_sec = clock.l;
		timeval.tv_usec = 0;
#ifndef SGI
		if (settimeofday(&timeval,&timezon) == -1) {
#else
		if (stime(&timeval.tv_sec) == -1) {
#endif
			perror("getdate");
#ifdef DEBUG
			exit(2);
#endif
		}
		gettimeofday(&timeval,&timezon);
		tm = localtime(&clock.l);
		printf("Date set to: %02d/%02d/%02d %02d:%02d:%02d (from %s)\n",
			tm->tm_mday, tm->tm_mon+1, tm->tm_year,
			tm->tm_hour, tm->tm_min, tm->tm_sec, argv[1]);
		exit(0);
	}
}


/*
 *
 *
 *	Routines standard de creation de sockets cote client.
 *
 *	Permet de creer des socket tcp sur un  service donne).
 *
 */

#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>


#ifndef u_short
#define u_short ushort
#endif u_short
/*
 *
 *	Cree une socket vers un hote donne, sur un service donne,
 *  Hote et service peuvent etre des numeros.
 *
 *	Ex : sock = c_clientbyname("aneth","courier")
 *	     sock = c_clientbyname("129.199.98.20","213")
 *
 *	Renvoie comme resultat :
 *		- dans le cas tcp : la socket prete a l'emploie (sur laquelle on
 *							peut faire des read ou des write).
 *
 */

c_clientbyname(host,name)
char *host,*name;
{
	int sock, i, num;
	struct sockaddr_in addr;
	struct servent *sv;
	struct hostent *hp, hp_entry;
	char host_number[4], *p;
	int host_bis[4];

	/* Recupere l'adresse de l'hote cible */
	if ((*host >= '0') && (*host <= '9')) {
		hp_entry.h_addrtype = AF_INET;
		hp_entry.h_length = 4;
		p = host_number;
		hp_entry.h_addr_list = &p;
		sscanf(host,"%d.%d.%d.%d",host_bis,host_bis+1,host_bis+2,host_bis+3);
		host_number[0] = host_bis[0];
		host_number[1] = host_bis[1];
		host_number[2] = host_bis[2];
		host_number[3] = host_bis[3];
		hp = &hp_entry;
	} else if (!(hp = gethostbyname(host))) {
		perror("gethostbyname");
		return (-1);
	}

	/* Description du service recherche (essentiellement, le numero de port) */
	if ((*name >= '0') && (*name <= '9')) {
		num = htons(atoi(name));
	} else if (!(sv = getservbyname(name,"tcp"))) {
		perror("getservbyname");
		return(-1);
	} else num = sv->s_port;

	/* Et on cree la socket */
	if ((sock = socket(AF_INET, SOCK_STREAM,0)) == -1) {
		perror("socket");
		return (-1);
	}

	/* Preparons l'adresse pour le connect */
	/* Le type d'adresse */
	/* la macro htons est un peut bidon : elle est la pour assurer */
	/* la compatibilite avec le vax qui inverse les bits entre le local */
	/* et ethernet .... */

	addr.sin_family = hp->h_addrtype;
	/* Le port (recupere dans le getservbyname) */
	addr.sin_port = num;
	/* L'adresse de l'hote cible */
#ifdef hpux
	memcpy( (caddr_t)&addr.sin_addr, hp->h_addr,hp->h_length);
#else
	bcopy(hp->h_addr, (caddr_t)&addr.sin_addr, hp->h_length);
#endif hpux

	/* Et zoup : on se connecte */
	if ( connect(sock,(struct sockaddr *)&addr, sizeof(addr)) == -1){
		perror("connect");
		return (-1);
	}

	/* Ok ! : on a une socket en tcp de bon gout */
	return (sock);
}
/*
 *
 *	Cree une socket vers un hote donne, sur un service donne,
 *
 *	Ex : sock = c_clientbyport("aneth",port)
 *
 *	Renvoie comme resultat :
 *		- dans le cas tcp : la socket prete a l'emploie (sur laquelle on
 *							peut faire des read ou des write).
 *
 */

c_clientbyport(host,port)
char *host;
int port;
{
	int sock;
	struct sockaddr_in addr;
	struct hostent *hp;
	/* Recupere l'adresse de l'hote cible */
	if (!(hp = gethostbyname(host))){
		perror("gethostbyname");
		return (-1);
	}
	/* Et on cree la socket */
	if ((sock = socket(AF_INET, SOCK_STREAM,0)) == -1){
		perror("socket");
		return (-1);
	}
	/* Preparons l'adresse pour le connect */
	/* Le type d'adresse */
	/* la macro htons est un peut bidon : elle est la pour assurer */
	/* la compatibilite avec le vax qui inverse les bits entre le local */
	/* et ethernet .... */
	addr.sin_family = hp->h_addrtype;
	/* Le port (recupere dans le getservbyname) */
	addr.sin_port = port;
	/* L'adresse de l'hote cible */
#ifdef hpux
	memcpy( (caddr_t)&addr.sin_addr, hp->h_addr,hp->h_length);
#else
	bcopy(hp->h_addr, (caddr_t)&addr.sin_addr, hp->h_length);
#endif hpux
	/* Et zoup : on se connecte */
	if ( connect(sock,(struct sockaddr *)&addr, sizeof(addr)) == -1){
		perror("connect");
		return (-1);
	}
	/* Ok ! : on a une socket en tcp de bon gout */
	return (sock);
}



--
Manuel Bouyer, Ecole Nationale Superieure de Techniques Avancees, Paris
email: bouyer@ensta.fr
--

------------------------------------------------------------------------------