system.c
资源名称:snmp.src.rar [点击查看]
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:7k
源码类别:
SNMP编程
开发平台:
C/C++
- /*
- * system.c
- */
- /***********************************************************
- Copyright 1992 by Carnegie Mellon University
- All Rights Reserved
- Permission to use, copy, modify, and distribute this software and its
- documentation for any purpose and without fee is hereby granted,
- provided that the above copyright notice appear in all copies and that
- both that copyright notice and this permission notice appear in
- supporting documentation, and that the name of CMU not be
- used in advertising or publicity pertaining to distribution of the
- software without specific, written prior permission.
- CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
- ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
- CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
- ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
- WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
- ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- SOFTWARE.
- ******************************************************************/
- /*
- * System dependent routines go here
- */
- #ifdef OS_VXWORKS
- #include <sys/times.h>
- #endif
- #include <ctype.h>
- #include <config.h>
- #include "asn1.h"
- #include "snmp_api.h"
- #include "tools.h"
- #include "system.h"
- #include "snmp_logging.h"
- #define NUM_NETWORKS 32 /* max number of interfaces to check */
- #ifndef IFF_LOOPBACK
- # define IFF_LOOPBACK 0
- #endif
- #define LOOPBACK 0x7f000001
- struct timeval starttime = {0};
- extern time_t time(time_t *);
- void init_start_time()
- {
- /* get current time (ie, the time the agent started) */
- gettimeofday(&starttime, NULL);
- /*
- starttime.tv_sec--;
- starttime.tv_usec += 1000000L;
- */
- }
- int gettimeofday(struct timeval *tp,struct timezone *tzp)
- {
- time_t t;
- unsigned long tickshi, tickslo;
- tm_getticks(&tickshi, &tickslo);
- time(&t);
- tp->tv_sec = t;
- tp->tv_usec = (tickslo % ticks_per_sec) * 1000000 / ticks_per_sec;
- return 0;
- }
- /*******************************************************************/
- /*
- * XXX What if we have multiple addresses?
- * XXX Could it be computed once then cached?
- */
- in_addr_t get_myaddr (void)
- {
- return 0;
- }
- /*
- * Returns uptime in centiseconds(!).
- */
- long get_uptime (void)
- {
- long long_return;
- struct timeval now, diff;
- gettimeofday(&now, NULL);
- now.tv_sec--;
- now.tv_usec += 1000000L;
- diff.tv_sec = now.tv_sec - starttime.tv_sec;
- diff.tv_usec = now.tv_usec - starttime.tv_usec;
- if (diff.tv_usec > 1000000L){
- diff.tv_usec -= 1000000L;
- diff.tv_sec++;
- }
- long_return = ((diff.tv_sec * 100) + (diff.tv_usec / 10000));
- return long_return;
- }
- /*******************************************************************/
- #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.
- */
- int strncasecmp(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);
- }
- int strcasecmp(const char *s1, const char *s2)
- {
- return strncasecmp(s1, s2, 1000000);
- }
- #endif /* HAVE_STRNCASECMP */
- #ifndef HAVE_STRDUP
- char *
- 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 */
- #ifdef UCD_SNMP
- #ifndef HAVE_SETENV
- int setenv(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);
- return ret;
- }
- #endif /* HAVE_SETENV */
- #endif /*UCD_SNMP*/
- int
- calculate_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;
- }
- #endif
- extern char *strdup (char*);
- int
- mkdirhier(const char *pathname, mode_t mode, int skiplast) {
- return 0;
- #ifdef UCD_SNMP
- struct stat sbuf;
- char *ourcopy = strdup(pathname);
- char *entry;
- char buf[SNMP_MAXPATH];
- entry = strtok( ourcopy, "/" );
- buf[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: %sn", buf);
- #ifdef WIN32
- CreateDirectory(buf, NULL);
- #else
- mkdir(buf, mode);
- #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;
- #endif
- }