⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 system.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 2 页
字号:
}#else                           /* ! WIN32 *//*******************************************************************//* * XXX  What if we have multiple addresses?  Or no addresses for that matter? * XXX  Could it be computed once then cached?  Probably not worth it (not *                                                           used very often). */in_addr_tget_myaddr(void){    int             sd, i, lastlen = 0;    struct ifconf   ifc;    struct ifreq   *ifrp = NULL;    in_addr_t       addr;    char           *buf = NULL;    if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {        return 0;    }    /*     * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF on     * some platforms; see W. R. Stevens, ``Unix Network Programming Volume     * I'', p.435.       */    for (i = 8;; i += 8) {        buf = (char *) calloc(i, sizeof(struct ifreq));        if (buf == NULL) {            close(sd);            return 0;        }        ifc.ifc_len = i * sizeof(struct ifreq);        ifc.ifc_buf = (caddr_t) buf;        if (ioctl(sd, SIOCGIFCONF, (char *) &ifc) < 0) {            if (errno != EINVAL || lastlen != 0) {                /*                 * Something has gone genuinely wrong.                   */                free(buf);                close(sd);                return 0;            }            /*             * Otherwise, it could just be that the buffer is too small.               */        } else {            if (ifc.ifc_len == lastlen) {                /*                 * The length is the same as the last time; we're done.                   */                break;            }            lastlen = ifc.ifc_len;        }        free(buf);    }    for (ifrp = ifc.ifc_req;        (char *)ifrp < (char *)ifc.ifc_req + ifc.ifc_len;#ifdef STRUCT_SOCKADDR_HAS_SA_LEN        ifrp = (struct ifreq *)(((char *) ifrp) +                                sizeof(ifrp->ifr_name) +                                ifrp->ifr_addr.sa_len)#else        ifrp++#endif        ) {        if (ifrp->ifr_addr.sa_family != AF_INET) {            continue;        }        addr = ((struct sockaddr_in *) &(ifrp->ifr_addr))->sin_addr.s_addr;        if (ioctl(sd, SIOCGIFFLAGS, (char *) ifrp) < 0) {            continue;        }        if ((ifrp->ifr_flags & IFF_UP)#ifdef IFF_RUNNING            && (ifrp->ifr_flags & IFF_RUNNING)#endif                          /* IFF_RUNNING */            && !(ifrp->ifr_flags & IFF_LOOPBACK)            && addr != LOOPBACK) {            /*             * I *really* don't understand why this is necessary.  Perhaps for             * some broken platform?  Leave it for now.  JBPN               */#ifdef SYS_IOCTL_H_HAS_SIOCGIFADDR            if (ioctl(sd, SIOCGIFADDR, (char *) ifrp) < 0) {                continue;            }            addr =                ((struct sockaddr_in *) &(ifrp->ifr_addr))->sin_addr.                s_addr;#endif            free(buf);            close(sd);            return addr;        }    }    free(buf);    close(sd);    return 0;}#if !defined(solaris2) && !defined(linux) && !defined(cygwin)/* * Returns boottime in centiseconds(!). *      Caches this for future use. */longget_boottime(void){    static long     boottime_csecs = 0;    struct timeval  boottime;#ifdef	CAN_USE_SYSCTL    int             mib[2];    size_t          len;#else    int             kmem;    static struct nlist nl[] = {#if !defined(hpux)        {(char *) "_boottime"},#else        {(char *) "boottime"},#endif        {(char *) ""}    };#endif    if (boottime_csecs != 0)        return (boottime_csecs);#ifdef CAN_USE_SYSCTL    mib[0] = CTL_KERN;    mib[1] = KERN_BOOTTIME;    len = sizeof(boottime);    sysctl(mib, 2, &boottime, &len, NULL, NULL);    boottime_csecs = (boottime.tv_sec * 100) + (boottime.tv_usec / 10000);#else                           /* CAN_USE_SYSCTL */    if ((kmem = open("/dev/kmem", 0)) < 0)        return 0;    nlist(KERNEL_LOC, nl);    if (nl[0].n_type == 0) {        close(kmem);        return 0;    }    lseek(kmem, (long) nl[0].n_value, L_SET);    read(kmem, &boottime, sizeof(boottime));    close(kmem);    boottime_csecs = (boottime.tv_sec * 100) + (boottime.tv_usec / 10000);#endif                          /* CAN_USE_SYSCTL */    return (boottime_csecs);}#endif/* * Returns uptime in centiseconds(!). */longget_uptime(void){#if !defined(solaris2) && !defined(linux) && !defined(cygwin)    struct timeval  now;    long            boottime_csecs, nowtime_csecs;    boottime_csecs = get_boottime();    if (boottime_csecs == 0)        return 0;    gettimeofday(&now, (struct timezone *) 0);    nowtime_csecs = (now.tv_sec * 100) + (now.tv_usec / 10000);    return (nowtime_csecs - boottime_csecs);#endif#ifdef solaris2    kstat_ctl_t    *ksc = kstat_open();    kstat_t        *ks;    kid_t           kid;    kstat_named_t  *named;    u_long          lbolt = 0;    if (ksc) {        ks = kstat_lookup(ksc, "unix", -1, "system_misc");        if (ks) {            kid = kstat_read(ksc, ks, NULL);            if (kid != -1) {                named = kstat_data_lookup(ks, "lbolt");                if (named) {#ifdef KSTAT_DATA_INT32                    lbolt = named->value.ul;#else                    lbolt = named->value.ul;#endif                }            }        }        kstat_close(ksc);    }    return lbolt;#endif                          /* solaris2 */#ifdef linux    FILE           *in = fopen("/proc/uptime", "r");    long            uptim = 0, a, b;    if (in) {        if (2 == fscanf(in, "%ld.%ld", &a, &b))            uptim = a * 100 + b;        fclose(in);    }    return uptim;#endif                          /* linux */#ifdef cygwin    return (0);                 /* not implemented */#endif}#endif                          /* ! WIN32 *//*******************************************************************/#ifndef HAVE_STRNCASECMP/* * test for NULL pointers before and NULL characters after * * comparing possibly non-NULL strings. * * WARNING: This function does NOT check for array overflow. */intstrncasecmp(const char *s1, const char *s2, size_t nch){    size_t          ii;    int             res = -1;    if (!s1) {        if (!s2)            return 0;        return (-1);    }    if (!s2)        return (1);    for (ii = 0; (ii < nch) && *s1 && *s2; ii++, s1++, s2++) {        res = (int) (tolower(*s1) - tolower(*s2));        if (res != 0)            break;    }    if (ii == nch) {        s1--;        s2--;    }    if (!*s1) {        if (!*s2)            return 0;        return (-1);    }    if (!*s2)        return (1);    return (res);}intstrcasecmp(const char *s1, const char *s2){    return strncasecmp(s1, s2, 1000000);}#endif                          /* HAVE_STRNCASECMP */#ifndef HAVE_STRDUPchar           *strdup(const char *src){    int             len;    char           *dst;    len = strlen(src) + 1;    if ((dst = (char *) malloc(len)) == NULL)        return (NULL);    strcpy(dst, src);    return (dst);}#endif                          /* HAVE_STRDUP */#ifndef HAVE_SETENVintsetenv(const char *name, const char *value, int overwrite){    char           *cp;    int             ret;    if (overwrite == 0) {        if (getenv(name))            return 0;    }    cp = (char *) malloc(strlen(name) + strlen(value) + 2);    if (cp == NULL)        return -1;    sprintf(cp, "%s=%s", name, value);    ret = putenv(cp);#ifdef WIN32    free(cp);#endif    return ret;}#endif                          /* HAVE_SETENV */intcalculate_time_diff(struct timeval *now, struct timeval *then){    struct timeval  tmp, diff;    memcpy(&tmp, now, sizeof(struct timeval));    tmp.tv_sec--;    tmp.tv_usec += 1000000L;    diff.tv_sec = tmp.tv_sec - then->tv_sec;    diff.tv_usec = tmp.tv_usec - then->tv_usec;    if (diff.tv_usec > 1000000L) {        diff.tv_usec -= 1000000L;        diff.tv_sec++;    }    return ((diff.tv_sec * 100) + (diff.tv_usec / 10000));}#ifndef HAVE_STRCASESTR/* * only glibc2 has this. */char           *strcasestr(const char *haystack, const char *needle){    const char     *cp1 = haystack, *cp2 = needle;    const char     *cx;    int             tstch1, tstch2;    /*     * printf("looking for '%s' in '%s'\n", needle, haystack);      */    if (cp1 && cp2 && *cp1 && *cp2)        for (cp1 = haystack, cp2 = needle; *cp1;) {            cx = cp1;            cp2 = needle;            do {                /*                 * printf("T'%c' ", *cp1);                  */                if (!*cp2) {    /* found the needle */                    /*                     * printf("\nfound '%s' in '%s'\n", needle, cx);                      */                    return (char *) cx;                }                if (!*cp1)                    break;                tstch1 = toupper(*cp1);                tstch2 = toupper(*cp2);                if (tstch1 != tstch2)                    break;                /*                 * printf("M'%c' ", *cp1);                  */                cp1++;                cp2++;            }            while (1);            if (*cp1)                cp1++;        }    /*     * printf("\n");      */    if (cp1 && *cp1)        return (char *) cp1;    return NULL;}#endifintmkdirhier(const char *pathname, mode_t mode, int skiplast){    struct stat     sbuf;    char           *ourcopy = strdup(pathname);    char           *entry;    char            buf[SNMP_MAXPATH];    entry = strtok(ourcopy, "/");    buf[0] = '\0';    /*     * check to see if filename is a directory      */    while (entry) {        strcat(buf, "/");        strcat(buf, entry);        entry = strtok(NULL, "/");        if (entry == NULL && skiplast)            break;        if (stat(buf, &sbuf) < 0) {            /*             * DNE, make it              */            snmp_log(LOG_INFO, "Creating directory: %s\n", buf);#ifdef WIN32            CreateDirectory(buf, NULL);#else            if (mkdir(buf, mode) == -1) {                free(ourcopy);                return SNMPERR_GENERR;            }#endif        } else {            /*             * exists, is it a file?              */            if ((sbuf.st_mode & S_IFDIR) == 0) {                /*                 * ack! can't make a directory on top of a file                  */                free(ourcopy);                return SNMPERR_GENERR;            }        }    }    free(ourcopy);    return SNMPERR_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -