htinet.c
来自「www工具包」· C语言 代码 · 共 663 行 · 第 1/2 页
C
663 行
while (*domainstr == ' ' || *domainstr == '\t') domainstr++; end = domainstr; while (*end && !isspace((int) *end)) end++; *end = '\0'; if (*domainstr) { StrAllocCat(hostname, "."); StrAllocCat(hostname, domainstr); fqdn = 2; break; } } } fclose(fp); } }#endif /* RESOLV_CONF */#ifdef HAVE_GETDOMAINNAME /* If everything else has failed then try getdomainname */ if (fqdn==1) { if (getdomainname(name, MAXHOSTNAMELEN)) { HTTRACE(CORE_TRACE, "HostName.... Can't get domain name\n"); StrAllocCopy(hostname, ""); return NULL; } /* If the host name and the first part of the domain name are different then use the former as it is more exact (I guess) */ if (strncmp(name, hostname, (int) strlen(hostname))) { char *domain = strchr(name, '.'); if (!domain) domain = name; StrAllocCat(hostname, domain); } }#endif /* HAVE_GETDOMAINNAME */ if (hostname) { char *strptr = hostname; while (*strptr) { *strptr = TOLOWER(*strptr); strptr++; } if (*(hostname+strlen(hostname)-1) == '.') /* Remove trailing dot */ *(hostname+strlen(hostname)-1) = '\0'; HTTRACE(CORE_TRACE, "HostName.... FQDN is `%s\'\n" _ hostname); } return hostname;}/* HTGetMailAddress**** Get the mail address of the current user on the current host. The** domain name used is the one initialized in HTSetHostName or** HTGetHostName. The login name is determined using (ordered):**** getlogin** getpwuid(getuid())**** The weakness about the last attempt is if the user has multiple** login names each with the same user ID. If this fails as well then:**** LOGNAME environment variable** USER environment variable**** Returns NULL or string to be freed by caller*/PUBLIC char * HTGetMailAddress (void){#ifdef HT_REENTRANT char name[HT_LOGNAME_MAX]; /* For getlogin_r or getUserName */#endif#ifdef WWW_MSWINDOWS/* what was the plan for this under windows? - EGP */ char name[256]; /* For getlogin_r or getUserName */ unsigned int bufSize = sizeof(name);#endif#ifdef HAVE_PWD_H struct passwd * pw_info = NULL;#endif char * login = NULL;#ifdef WWW_MSWINDOWS if (!login && GetUserName(name, &bufSize) != TRUE) HTTRACE(CORE_TRACE, "MailAddress. GetUsername returns NO\n");#endif /* WWW_MSWINDOWS */#ifdef HAVE_CUSERID if (!login && (login = (char *) cuserid(NULL)) == NULL) HTTRACE(CORE_TRACE, "MailAddress. cuserid returns NULL\n");#endif /* HAVE_CUSERID */#ifdef HAVE_GETLOGIN#ifdef HT_REENTRANT if (!login && (login = (char *) getlogin_r(name, HT_LOGNAME_MAX)) == NULL)#else if (!login && (login = (char *) getlogin()) == NULL)#endif /* HT_REENTRANT */ HTTRACE(CORE_TRACE, "MailAddress. getlogin returns NULL\n");#endif /* HAVE_GETLOGIN */#ifdef HAVE_PWD_H if (!login && (pw_info = getpwuid(getuid())) != NULL) login = pw_info->pw_name;#endif /* HAVE_PWD_H */ if (!login && (login = getenv("LOGNAME")) == NULL) HTTRACE(CORE_TRACE, "MailAddress. LOGNAME not found\n"); if (!login && (login = getenv("USER")) == NULL) HTTRACE(CORE_TRACE, "MailAddress. USER not found\n"); if (!login) login = HT_DEFAULT_LOGIN; if (login) { char * domain = NULL; char * mailaddress = NULL; StrAllocCopy(mailaddress, login); StrAllocCat(mailaddress, "@"); if ((domain = HTGetHostName()) != NULL) { StrAllocCat(mailaddress, domain); HT_FREE(domain); } return mailaddress; } return NULL;}/*** Except on the NeXT, we pick up the NewsHost name from**** 1. Environment variable NNTPSERVER** 2. File SERVER_FILE** 3. Compilation time macro DEFAULT_NEWS_HOST**** On the NeXT, we pick up the NewsHost name from, in order:**** 1. WorldWideWeb default "NewsHost"** 2. News default "NewsHost"** 3. Compilation time macro DEFAULT_NEWS_HOST**** Returns NULL or string to be freed by caller*/PUBLIC char * HTGetNewsServer (void){ char * newshost = NULL; char buffer[80];#ifdef NeXTStep if ((newshost = NXGetDefaultValue("WorldWideWeb","NewsHost")) == 0) if ((newshost = NXGetDefaultValue("News","NewsHost")) == 0) newshost = DEFAULT_NEWS_HOST;#else if ((newshost = (char *) getenv("NNTPSERVER")) == NULL) { FILE *fp = fopen(SERVER_FILE, "r"); *(buffer+79) = '\0'; if (fp) { if (fgets(buffer, 79, fp)) { char *end; newshost = buffer; while (*newshost == ' ' || *newshost == '\t') newshost++; end = newshost; while (*end && !isspace((int) *end)) end++; *end = '\0'; } fclose(fp); } }#endif /* NestStep */ /* Last resort */ if (!newshost || !*newshost) newshost = DEFAULT_NEWS_HOST; /* Canonicalize host name */ { char * result = NULL; StrAllocCopy(result, newshost); { char * strptr = result; while (*strptr) { *strptr = TOLOWER(*strptr); strptr++; } } return result; }}/* Timezone Offset** ---------------** Calculates the offset from GMT in seconds*/PUBLIC time_t HTGetTimeZoneOffset (void){ static time_t HTTimeZone = -1; /* Invalid timezone offset */ if (HTTimeZone != -1) return HTTimeZone; /* Already done */#ifdef HAVE_TIMEZONE { time_t cur_t = time(NULL);#ifdef HT_REENTRANT struct tm loctime; struct tm *local = (struct tm *) localtime_r(&cur_t, &loctime);#else struct tm *local = localtime(&cur_t);#endif /* HT_REENTRANT */#ifdef HAVE_DAYLIGHT if (daylight && local->tm_isdst>0) { /* daylight time? */#else if (local->tm_isdst>0) { /* daylight time? */#endif /* HAVE_DAYLIGHT */#ifdef HAVE_ALTZONE HTTimeZone = altzone;#else /* Assumes a fixed DST offset of 1 hour, which is probably wrong */#ifdef __CYGWIN__ HTTimeZone = _timezone - 3600;#else HTTimeZone = timezone - 3600;#endif#endif /* HAVE_ALTZONE */ } else { /* no */#ifdef __CYGWIN__ HTTimeZone = _timezone;#else HTTimeZone = timezone;#endif } HTTimeZone = -HTTimeZone; HTTRACE(CORE_TRACE, "TimeZone.... GMT + (%02d) hours (including DST)\n" _ (int) HTTimeZone/3600); }#else#ifdef HAVE_TM_GMTOFF { time_t cur_t = time(NULL);#ifdef HT_REENTRANT struct tm loctime; localtime_r(&cur_t, &loctime);#else struct tm * local = localtime(&cur_t);#endif /* HT_REENTRANT */ HTTimeZone = local->tm_gmtoff; HTTRACE(CORE_TRACE, "TimeZone.... GMT + (%02d) hours (including DST)\n" _ (int)local->tm_gmtoff / 3600); }#else HTTRACE(CORE_TRACE, "TimeZone.... Not defined\n");#endif /* HAVE_TM_GMTOFF */#endif /* HAVE_TIMEZONE */ return HTTimeZone;}/*** Finds a temporary name in in the directory given. If the directory** is NULL then don't prepend anything.** If success, the result must be freed by caller, else we return NULL*/PUBLIC char * HTGetTmpFileName (const char * abs_dir){#ifdef HAVE_TEMPNAM#ifdef __CYGWIN__ return tempnam(abs_dir, "");#else return tempnam(abs_dir, NULL);#endif /* __CYGWIN__ */#else /* ** This is only approx. as we don't know if this file exists or not. ** Hopefully, tempnam() exists on enough platforms so that this is not ** a problem. */ char * result = NULL; char * offset = NULL; if (!(result = (char *) HT_MALLOC((abs_dir ? strlen(abs_dir) : 0) + HT_MAX_TMPNAM + 2))) HT_OUTOFMEM("HTGetTmpFileName");#ifdef WWW_MSWINDOWS if (abs_dir) {#else if (abs_dir && *abs_dir==DIR_SEPARATOR_CHAR) {#endif /* WWW_MSWINDOWS */ strcpy(result, abs_dir); offset = result+strlen(result); if (*(offset-1) != DIR_SEPARATOR_CHAR) *offset++ = DIR_SEPARATOR_CHAR;#ifdef HT_REENTRANT tmpnam_r(offset);#else tmpnam(offset);#endif { char * orig = strrchr(offset, DIR_SEPARATOR_CHAR); char * dest = offset; if (orig++) while ((*dest++ = *orig++)); } } else { offset = result;#ifdef HT_REENTRANT tmpnam_r(offset);#else tmpnam(offset);#endif offset = result; } return result;#endif /* HAVE_TEMPNAM */}/*** Copied from X utilities*/PUBLIC ms_t HTGetTimeInMillis (void){#ifdef WWW_MSWINDOWS return GetTickCount();#else /* WWW_MSWINDOWS */#ifdef HAVE_GETTIMEOFDAY struct timeval tp; gettimeofday(&tp, NULL); return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);#else return((ms_t) 0);#endif#endif /* !WWW_MSWINDOWS */}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?