OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [getnetent.c] - Diff between revs 1325 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1325 Rev 1765
/*
/*
 * Copyright (c) 1983 Regents of the University of California.
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms are permitted
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
 */
 
 
#define __FORCE_GLIBC
#define __FORCE_GLIBC
#include <features.h>
#include <features.h>
#include <stdio.h>
#include <stdio.h>
#include <netdb.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
 
 
 
 
#ifdef __UCLIBC_HAS_THREADS__
#ifdef __UCLIBC_HAS_THREADS__
#include <pthread.h>
#include <pthread.h>
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
# define LOCK   __pthread_mutex_lock(&mylock)
# define LOCK   __pthread_mutex_lock(&mylock)
# define UNLOCK __pthread_mutex_unlock(&mylock);
# define UNLOCK __pthread_mutex_unlock(&mylock);
#else
#else
# define LOCK
# define LOCK
# define UNLOCK
# define UNLOCK
#endif
#endif
 
 
 
 
 
 
#define MAXALIASES      35
#define MAXALIASES      35
static const char NETDB[] = _PATH_NETWORKS;
static const char NETDB[] = _PATH_NETWORKS;
static FILE *netf = NULL;
static FILE *netf = NULL;
static char line[BUFSIZ+1];
static char line[BUFSIZ+1];
static struct netent net;
static struct netent net;
static char *net_aliases[MAXALIASES];
static char *net_aliases[MAXALIASES];
 
 
int _net_stayopen;
int _net_stayopen;
 
 
void setnetent(int f)
void setnetent(int f)
{
{
    LOCK;
    LOCK;
    if (netf == NULL)
    if (netf == NULL)
        netf = fopen(NETDB, "r" );
        netf = fopen(NETDB, "r" );
    else
    else
        rewind(netf);
        rewind(netf);
    _net_stayopen |= f;
    _net_stayopen |= f;
    UNLOCK;
    UNLOCK;
    return;
    return;
}
}
 
 
void endnetent(void)
void endnetent(void)
{
{
    LOCK;
    LOCK;
    if (netf) {
    if (netf) {
        fclose(netf);
        fclose(netf);
        netf = NULL;
        netf = NULL;
    }
    }
    _net_stayopen = 0;
    _net_stayopen = 0;
    UNLOCK;
    UNLOCK;
}
}
 
 
static char * any(register char *cp, char *match)
static char * any(register char *cp, char *match)
{
{
    register char *mp, c;
    register char *mp, c;
 
 
    while ((c = *cp)) {
    while ((c = *cp)) {
        for (mp = match; *mp; mp++)
        for (mp = match; *mp; mp++)
            if (*mp == c)
            if (*mp == c)
                return (cp);
                return (cp);
        cp++;
        cp++;
    }
    }
    return ((char *)0);
    return ((char *)0);
}
}
 
 
struct netent * getnetent(void)
struct netent * getnetent(void)
{
{
    char *p;
    char *p;
    register char *cp, **q;
    register char *cp, **q;
 
 
    LOCK;
    LOCK;
    if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
    if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
        UNLOCK;
        UNLOCK;
        return (NULL);
        return (NULL);
    }
    }
again:
again:
    p = fgets(line, BUFSIZ, netf);
    p = fgets(line, BUFSIZ, netf);
    if (p == NULL) {
    if (p == NULL) {
        UNLOCK;
        UNLOCK;
        return (NULL);
        return (NULL);
    }
    }
    if (*p == '#')
    if (*p == '#')
        goto again;
        goto again;
    cp = any(p, "#\n");
    cp = any(p, "#\n");
    if (cp == NULL)
    if (cp == NULL)
        goto again;
        goto again;
    *cp = '\0';
    *cp = '\0';
    net.n_name = p;
    net.n_name = p;
    cp = any(p, " \t");
    cp = any(p, " \t");
    if (cp == NULL)
    if (cp == NULL)
        goto again;
        goto again;
    *cp++ = '\0';
    *cp++ = '\0';
    while (*cp == ' ' || *cp == '\t')
    while (*cp == ' ' || *cp == '\t')
        cp++;
        cp++;
    p = any(cp, " \t");
    p = any(cp, " \t");
    if (p != NULL)
    if (p != NULL)
        *p++ = '\0';
        *p++ = '\0';
    net.n_net = inet_network(cp);
    net.n_net = inet_network(cp);
    net.n_addrtype = AF_INET;
    net.n_addrtype = AF_INET;
    q = net.n_aliases = net_aliases;
    q = net.n_aliases = net_aliases;
    if (p != NULL)
    if (p != NULL)
        cp = p;
        cp = p;
    while (cp && *cp) {
    while (cp && *cp) {
        if (*cp == ' ' || *cp == '\t') {
        if (*cp == ' ' || *cp == '\t') {
            cp++;
            cp++;
            continue;
            continue;
        }
        }
        if (q < &net_aliases[MAXALIASES - 1])
        if (q < &net_aliases[MAXALIASES - 1])
            *q++ = cp;
            *q++ = cp;
        cp = any(cp, " \t");
        cp = any(cp, " \t");
        if (cp != NULL)
        if (cp != NULL)
            *cp++ = '\0';
            *cp++ = '\0';
    }
    }
    *q = NULL;
    *q = NULL;
    UNLOCK;
    UNLOCK;
    return (&net);
    return (&net);
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.