#include <stdio.h>      
#include <sys/file.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include "inet.h"

#define CR		'\015'
#define LF		'\012'
#define EOM             ".\r\n"
#define EOM_LEN         3
#define CRLF            "\015\012"
#define SOCKBUF		4096
#define FALSE           0
#define TRUE            1
#define INCR            100 /* how many nodes to do at a time */

char            outbuf[SOCKBUF];
char            inbuf[SOCKBUF];

char           *host, *port;
static int      ids[6], level, node, pipssock;
static char    *subj[6], *title, *line, *cp;


main(int argc, char **argv)
{				/* send the admin command to the server
				 for updating the last modified times */

    FILE           *fopen(), *ifp, *ofp;
    char           *malloc(),*passwd;
    int             len, i,start,end,rc = 1;
    title = malloc(80);
    line = malloc(120);
    /* establish connect with pips */
 
    if (argc < 3)
      {
	printf("need to have hostname and password as args\n");
	exit();
      }
       host = argv[1];
       passwd = argv[2];
    port = "9000";
printf ("host = %s\n",host);
    pipssock = inet_establish_connection(host, port, 0);
    if (pipssock < 0) {
	puts("Fatal error: could not establish connection.");
	exit(1);
    }
    get_msg(pipssock, inbuf, SOCKBUF);
printf("banner=%s\n",inbuf);
bzero(inbuf,strlen(inbuf));
    sprintf(line,"z:%s",passwd);
    send_msg(pipssock,line,strlen(line)); /* become an administrator */
    get_msg(pipssock, inbuf, SOCKBUF);
printf("msg=<%s>\n",inbuf);
bzero(inbuf,strlen(inbuf));
start = 0;
end = INCR;
while (rc) { /* keep going in intervals until finished */
    sprintf(line,"E:%d:%d",start,end);
    send_msg(pipssock,line,strlen(line)); /* stat the files */
    get_msg(pipssock, inbuf, SOCKBUF);
    rc = atoi(inbuf);
    start += INCR;
    end += INCR;
  }
printf("msg=<%s>\n",inbuf);
    close(pipssock);
}


get_msg(sock, buff, buffsize)	/* read message off socket */
    int             sock;
    char           *buff;
    int             buffsize;
{
    int             red;
    char           *cp,tempbuff[256];

*buff = '\0';   
while ((red = read(sock, tempbuff, buffsize)) > 0) {
  if (!strncmp(&tempbuff[red - 3], EOM, EOM_LEN)) {
    tempbuff[red-4] = '\0';
    strcat(buff,tempbuff); 
    if (strlen(buff) > 0)   
      break; }
  strncat(buff,tempbuff,red);
}
}

send_msg(sock,str,len)
int sock;
char *str;
int len;
{
int wrote;
char msg[150];
sprintf(msg,"%s%s",str,CRLF);
wrote = write(sock,str,len + 2);
/* printf("wrote %d\n",wrote);
 printf("wrote end %d\n",wrote); */
}


















