#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>

#define EXEC 1

void error(char *);

struct sockaddr_un serv_addr;

int main(int argc, char **argv)
{
char buf[40];
	if(argc<=2 || argc>4)
	{ 
    		printf("edwses latos orismata\n");
		exit(0);
	}
	
	int servlen,sd;
	
	sd = socket(AF_UNIX, SOCK_STREAM, 0);
	if(sd<0)
	{	error("sfalma sth dhmioyrgia toy socket");	}

	bzero((char *)&serv_addr,sizeof(serv_addr));
	 
	serv_addr.sun_family = AF_UNIX;

	strcpy(serv_addr.sun_path, "mysock");
	
	servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
	
	if (connect(sd, (struct sockaddr *) &serv_addr, servlen) < 0)
       	{	error("sfalma sundesis toy client me ton server");	}
	//connect(sd,(struct sockaddr*)&srv , sizeof(srv));
	int op = EXEC;
	strcpy(buf,(char *)op);
	printf("trexei1\n");
	write(sd,buf,strlen(buf));
	//write(sd,&op,sizeof(int));	//stelnw ston server tin praxi poy telw na kanei (dhl exec)
	printf("trexei2\n");
	write(sd,&argc,sizeof(int));	// auto to stelnw ston server gia naxerei poso(megetos) ta diabasei
	printf("trexei3\n");
	int i = 1;
	for (i = 1; i < argc; i++)
	{
		int size = strlen (argv[i]) + 1;
		write (sd, &size, sizeof(int));
		write (sd, argv[i], size);
	}
	printf("trexei4\n");
	//int status;
	//read (sd, &status, sizeof(int));
	//return status;

}



void error(char *msg)
{
    perror(msg);
    exit(0);
}
