date and time created 87/06/11 15:28:38 by minshall
[unix-history] / usr / src / usr.bin / tn3270 / api / api_bsd.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include "../api/api.h"
static int sock = -1;
api_open_api(string)
char *string; /* if non-zero, where to connect to */
{
struct sockaddr_in server;
struct hostent *hp;
char *getenv();
char thehostname[100];
int port;
if (string == 0) {
string = getenv("API3270"); /* Get API */
if (string == 0) {
fprintf(stderr,
"API3270 environmental variable not set - no API.\n");
return -1; /* Nothing */
}
}
if (sscanf(string, "%[^:]:%d", thehostname, &port) != 2) {
fprintf(stderr, "API3270 environmental variable has bad format.\n");
return -1;
}
/* Now, try to connect */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening API socket");
return -1;
}
server.sin_family = AF_INET;
hp = gethostbyname(thehostname);
if (hp == 0) {
fprintf(stderr, "%s specifies bad host name.\n", string);
return -1;
}
bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
server.sin_port = htons(port);
if (connect(sock, &server, sizeof server) < 0) {
perror("connecting to API server");
return -1;
}
/* YEAH */
return 0; /* Happiness! */
}
api_exch_api(regs, sregs)
union REGS *regs;
struct SREGS *sregs;
{
}