ftp.nice.ch/pub/next/developer/resources/libraries/eni.a.tar.gz#/utils/eniutils.c

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

#include <stdio.h>
#include <strings.h>
#include "eniutils.h"


/*
    Convert an ethernet address to a string.
*/

char *addr_to_hex(unsigned char *eaddr, char str[ADDR_STR_LEN])
{
    char htab[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		   'a', 'b', 'c', 'd', 'e', 'f'} ;
    int i ;


    for(i = 0 ; i < 6 ; i++)
    {
	str[2*i] = htab[(*(eaddr + i) >> 4)] ;
	str[2*i+1] = htab[(*(eaddr + i) & 0xf)] ;
    }

    str[ADDR_STR_LEN - 1] = '\0' ;
    return &str[0] ;
}



/*
    Convert a hex string to an ethernet address.

    Return 1 if successful, 0 otherwise.
*/

int hex_to_addr(char *str, unsigned char *addr)
{
    int i ;


    for(i = 0 ; i < 6 ; i++)
    {
	unsigned char n ;

	
	n = *(str + 2 * i) ;
	if(n >= '0' && n <= '9')
	{
	    addr[i] = (n - '0') << 4 ;
	}
	else if(n >= 'a' && n <= 'f')
	{
	    addr[i] = (n - 'a' + 10) << 4 ;
	}
	else
	{
	    return 0 ;
	}

    	n = *(str + 2 * i + 1) ;
	if(n >= '0' && n <= '9')
	{
	    addr[i] += (n - '0') ;
	}
	else if(n >= 'a' && n <= 'f')
	{
	    addr[i] += (n - 'a' + 10) ;
	}
	else if(n >= 'A' && n <= 'F')
	{
	    addr[i] += (n - 'A' + 10) ;
	}
	else
	{
	    return 0 ;
	}
    }

    return 1 ;
}


/*
 *
 *		Truly miscellaneus routines.
 *
 */

int get_ether_addr(char *host, unsigned char eaddr[], char *etherfile)
{
    FILE *fp ;
    char e[64] ;
    char h[64] ;
    char line[128] ;

    
    if((fp = fopen(etherfile, "r")) == (FILE *)0)
    {
	return 0 ;
    }
    else
    {
	while(fgets(line, sizeof line, fp) != (char *)0)
	{
	    if(sscanf(line, "%s %s", h, e) != 2)
	    {
		fprintf(stderr, "get_ether_addr: bad input line [%s]\n", line) ;
	    }
	    else
	    {
		if(strcmp(h, host) == 0)
		{
		    return hex_to_addr(e, eaddr) ;
		}
	    }
	}

	fprintf(stderr, "get_ether_addr: address for %s not found.\n", host) ;
	return 0 ;
    }
}


void nuke_newline(char *s)
{
    char *n ;


    if((n = rindex(s, '\n')) != (char *)0)
    {
	*n = '\0' ;
    }
}


char *tail(char *path)
{
    char *p ;


    return (p = rindex(path, '/')) == (char *)0 ? path : ++p ;
}

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