ftp.nice.ch/pub/next/connectivity/infosystems/Gopher.1.13.s.tar.gz#/Gopher_1.13_Source/Utilities.c

This is Utilities.c in view mode; [Download] [Up]

// Utilities.c
//
// All the code in this file is taken from the Unix client
// written at the University of Minnesota.

#import "Utilities.h"

///////////////////////////////////////////////////////////////
// Creates a socket to host hostname at port port.
// Returns the socket, or :
// -1 : Unknown host
// -2 : Socket call failed
// -3 : Connect call failed
// It is the responsibility of the caller to close the socket
// when it is over with it.

int create_socket(char *hostname, int port)
{
int 	sock;
struct sockaddr_in server;
struct hostent *hp;

if ((server.sin_addr.s_addr = inet_addr(hostname)) == -1)
	{
	if (hp = gethostbyname(hostname))
		{
		bzero((char *) &server, sizeof(server));
		bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);
		server.sin_family = hp->h_addrtype;
	  	}
	else
		return(-1);
	}
else
	server.sin_family = AF_INET;

server.sin_port = (unsigned short) htons(port);

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	return(-2);

setsockopt(sock, SOL_SOCKET, ~SO_LINGER, 0, 0); 
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 0, 0);
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 0, 0);
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
	{
	close(sock);
	return(-3);
	}

return(sock);
}

////////////////////////////////////////////////////
// Write "n" bytes to a descriptor.
// Use in place of write() when fd is a stream socket
//
// We return the number of bytes written

int writen(int fd, char *ptr, int nbytes)
{
int nleft, nwritten;

nleft = nbytes;
while(nleft > 0)
	{
	nwritten = write(fd, ptr, nleft);
	if (nwritten <= 0)
		return(nwritten);	/* error */

	nleft	-= nwritten;
	ptr	+= nwritten;
	}
return(nbytes - nleft);
}

////////////////////////////////////////////////////
// Writestring uses the writen and strlen calls to write a
// string to the file descriptor fd.  If the write fails
// a -1 is returned. Otherwise zero is returned.

int writestring(int fd, char *stringptr)
{
int length;

length = strlen(stringptr);
if (writen(fd, stringptr, length) != length)
	return(-1);
else
	return(0);
}

////////////////////////////////////////////////////
// Read a line from a descriptor.  Read the line one byte at a time,
// looking for the newline.  We store the newline in the buffer,
// then follow it with a null (the same as fgets(3)).
// We return the number of characters up to, but not including,
// the null (the same as strlen(3))

int readline(int fd, char *ptr, int maxlen)
{
int n;
int rc;
char c;

for (n=1; n < maxlen; n++)
	{
	if ( (rc = read(fd, &c, 1)) == 1)
		{
		*ptr++ = c;
		if (c == '\n')
			break;
		}
	else if (rc == 0)
		{
		if (n == 1)
			return(0);	/* EOF, no data read */
		else
			break;		/* EOF, some data was read */
		}
	else 
		return(-1);		/* error */
	}

*ptr = 0; 				/* Tack a NULL on the end */
return(n);
}

////////////////////////////////////////////////////
// This strips off  CR's and LF's leaving us with a nice pure string.

void ZapCRLF(char *inputline)
{
char *cp;

cp = index(inputline, '\r');    /* Zap CR-LF */
if (cp != NULL)
	*cp = '\0';
else
	{
	cp = index(inputline, '\n');
	if (cp != NULL)
		*cp = '\0';
	}
}


////////////////////////////////////////////////////////////////////////////////
// This does a case independent str search
// No it's not really efficent.
//

char *strcasestr(char *inputline, char *match)
{
     int matchlen=0;
     int i, inlen;

     matchlen = strlen(match);
     inlen = strlen(inputline);

     for(i=0; i<inlen; i++) {
          if (strncasecmp(inputline+i, match, matchlen)==0)
               return(inputline+i);
     }

     return(NULL);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.