/*
 * mpe.c
 * 
 * Copyright 2025 osboxes <osboxes@osboxes>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * Programa Escritor en cola de mensaje, chat procesos independientes
 * forma de uso:
 $ ./mpe <tipo cola de mensaje escritura>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

typedef struct msg {
	long mtype;       /* message type, must be > 0 */
	char mtext[128];  /* message data */
} msg;

int main(int argc, char **argv) {
	if ( argc != 2 ) { 
		printf("Forma de Uso:\n./mpe <tipo cola mensaje escritura>\n");
		return 1;
	}
	long tcme = atol(argv[1]); // tipo escritura
	int msgid = msgget(0xA,0);
	printf("main(): msgid = %d escribo en: %ld\n",msgid,tcme);

	char linea[128];
	msg m;
	
	// proceso escritor
	m.mtype = tcme;
	do {
		printf(">>");
		memset(linea,0,128);
		fgets(linea,128,stdin);
		linea[strlen(linea)-1] = '\0';
		memcpy(m.mtext,linea,128);
		msgsnd(msgid,&m,128,0);
	} while(strncmp(linea,"fin",3) != 0);

	return 0;
}

