/* Converts a timestamp into a recent date string ("May 27 06:33"), or an * old (or future) date string (i.e. "Oct 27 1996"). */ void LsDate(char *dstr, size_t dsiz, time_t ts) { struct tm t; if (ts == kModTimeUnknown) { (void) Strncpy(dstr, " ", dsiz); return; } if (Localtime(ts, &t) == NULL) { (void) Strncpy(dstr, "Jan 0 1900", dsiz); return; } if ((ts > gNowPlus1Hr) || (ts < gNowMinus6Mon)) { #ifdef HAVE_SNPRINTF (void) snprintf(dstr, dsiz, #else (void) sprintf(dstr, #endif "%s %2d %4d", gLsMon[t.tm_mon], t.tm_mday, t.tm_year + 1900 ); } else {
/* Creates the ls monthname abbreviation array, so we don't have to * re-calculate them each time. */ void InitLsMonths(void) { struct tm lt; int i; memset(gLsMon, 0, sizeof(gLsMon)); (void) Localtime(0, <); lt.tm_mday = 15; lt.tm_hour = 12; for (i=0; i<12; i++) { lt.tm_mon = i; (void) strftime(gLsMon[i], sizeof(gLsMon[i]) - 1, "%b", <); } (void) strcpy(gLsMon[i], "BUG"); } /* InitLsMonths */
/* * _newline_output * * Output metric data, one node per line */ static void _newline_output(List l) { struct node_metric_data *data = NULL; ListIterator litr = NULL; assert(l); litr = List_iterator_create(l); while ((data = list_next(litr))) { char vbuf[CEREBRO_STAT_BUFLEN]; memset(vbuf, '\0', CEREBRO_STAT_BUFLEN); _metric_value_str(data->metric_value_type, data->metric_value_len, data->metric_value, vbuf, CEREBRO_STAT_BUFLEN); if (metric_received_time_flag) { char tbuf[CEREBRO_STAT_BUFLEN]; memset(tbuf, '\0', CEREBRO_STAT_BUFLEN); if (data->metric_value_received_time) { time_t t = (time_t)data->metric_value_received_time; struct tm *tm = Localtime(&t); strftime(tbuf, CEREBRO_STAT_BUFLEN, "%F %I:%M:%S%P", tm); } else snprintf(tbuf, CEREBRO_STAT_BUFLEN, CEREBRO_STAT_UNKNOWN_STRING); fprintf(stdout, "%s(%s): %s\n", data->nodename, tbuf, vbuf); } else fprintf(stdout, "%s: %s\n", data->nodename, vbuf); } /* No need to destroy list iterator, caller will destroy List */ }
int UnLslR(const FTPCIPtr cip, FTPFileInfoListPtr filp, FTPLineListPtr llp, int serverType) { char curdir[512]; char line[512]; int len; size_t curdirlen = 0; char fname[256]; char linkto[256]; char *cp; longest_int fsize; int ftype; time_t ftime, now; int thisyear; struct tm nowtm; int rc; FTPLinePtr lp; FTPFileInfo fi; int linesread = 0; int linesconverted = 0; size_t maxFileLen = 0; size_t maxPlugLen = 0; size_t fileLen; int plugend; int skipdir = 0; size_t cwdlen; if (Localtime(time(&now), &nowtm) == NULL) thisyear = 1970; /* should never happen */ else thisyear = nowtm.tm_year + 1900; curdir[0] = '\0'; cip->buf[0] = '\0'; cwdlen = 0; InitFileInfoList(filp); for (lp = llp->first; lp != NULL; lp = lp->next) { len = (int) strlen(STRNCPY(line, lp->line)); for (cp = line; ; cp++) { if ((*cp == '\0') || (!isspace((int) *cp))) break; } if (*cp == '\0') { /* Entire line was blank. */ /* separator line between dirs */ continue; } if (serverType == kServerTypeMicrosoftFTP) { /* IIS runs on WinNT only, and NT * can use / as a separator rather * that \. We will convert them * here. */ for (cp = line; *cp != '\0'; cp++) { if (*cp == '\\') *cp = '/'; } } linesread++; rc = UnLslRLine(line, curdir, curdirlen, fname, sizeof(fname), linkto, sizeof(linkto), &ftype, &fsize, &ftime, now, thisyear, &plugend); if ((rc < 0) && (serverType == kServerTypeMicrosoftFTP)) { rc = UnDosLine(line, curdir, curdirlen, fname, sizeof(fname), &ftype, &fsize, &ftime); if (rc == 0) { *linkto = '\0'; plugend = 0; } } if (rc == 0) { if (skipdir != 0) continue; cp = fname + curdirlen; if ((cp[0] == '.') && ((cp[1] == '\0') || ((cp[1] == '.') && (cp[2] == '\0')))) continue; /* ignore . and .. */ linesconverted++; cp = fname; if ((cp[0] == '.') && ((cp[1] == '/') || (cp[1] == '\\'))) cp += 2; if ((*cp == '/') || (*cp == '\\')) { /* Absolute pathnames not allowed unless * they are in the cwd. */ if (cip->buf[0] == '\0') { (void) FTPGetCWD(cip, cip->buf, cip->bufSize); cwdlen = strlen(cip->buf); } /* In root directory (cwdlen == 1), paths * from root are OK. */ if (cwdlen > 1) { if (memcmp(cp, cip->buf, cwdlen) == 0) { /* Abs path prefixed with cwd */ cp += cwdlen; if ((*cp == '/') || (*cp == '\\')) cp++; } else { /* Abs path not prefixed with cwd */ continue; } } } if (PathContainsIntermediateDotDotSubDir(cp)) continue; fileLen = strlen(cp); if (fileLen > maxFileLen) maxFileLen = fileLen; fi.relnameLen = fileLen; fi.relname = StrDup(cp); fi.rname = NULL; fi.lname = NULL; fi.rlinkto = (linkto[0] == '\0') ? NULL : StrDup(linkto); fi.mdtm = ftime; fi.size = (longest_int) fsize; fi.type = ftype; fi.mode = -1; if (plugend > 0) { fi.plug = (char *) malloc((size_t) plugend + 1); if (fi.plug != NULL) { (void) memcpy(fi.plug, line, (size_t) plugend); fi.plug[plugend] = '\0'; if ((size_t) plugend > maxPlugLen) maxPlugLen = (size_t) plugend; } } else { fi.plug = (char *) malloc(32 + 1); if (fi.plug != NULL) { (void) strcpy(fi.plug, "---------- 1 ftpuser ftpusers"); fi.plug[0] = (char) ftype; if (32 > maxPlugLen) maxPlugLen = (size_t) 32; } } (void) AddFileInfo(filp, &fi); } else if ((rc < 0) && (line[len - 1] == ':')) { if ((line[0] == '.') && (line[1] == '/')) { line[len - 1] = '/'; (void) memcpy(curdir, line + 2, (size_t) len + 1 - 2); curdirlen = (size_t) (len - 2); } else if ((line[0] == '.') && (line[1] == '\\')) { line[len - 1] = '\\'; (void) memcpy(curdir, line + 2, (size_t) len + 1 - 2); curdirlen = (size_t) (len - 2); } else { line[len - 1] = '/'; (void) memcpy(curdir, line, (size_t) len + 1); curdirlen = (size_t) len; } skipdir = 0; cp = curdir; if ((*cp == '/') || (*cp == '\\')) { skipdir = 1; /* absolute pathnames not allowed */ if (cip->buf[0] == '\0') { (void) FTPGetCWD(cip, cip->buf, cip->bufSize); cwdlen = strlen(cip->buf); } if (cwdlen == 1) { /* In root directory, so paths * from root are OK. */ skipdir = 0; } else { if (memcmp(cp, cip->buf, cwdlen) == 0) { cp += cwdlen; if ((*cp == '/') || (*cp == '\\')) cp++; memmove(curdir, cp, strlen(cp) + 1); skipdir = 0; cp = curdir; } } } if (PathContainsIntermediateDotDotSubDir(cp)) { skipdir = 1; } } } filp->maxFileLen = maxFileLen; filp->maxPlugLen = maxPlugLen; if (linesread == 0) return (0); return ((linesconverted > 0) ? linesconverted : (-1)); } /* UnLslR */
bool readTimeZoneFile(const char* zonefile, struct TimeZone::Data* data) { File f(zonefile); if (f.valid()) { try { string head = f.readBytes(4); if (head != "TZif"); { throw logic_error("bad head"); } string version = f.readBytes(1); f.readBytes(15); int32_t isgmtcnt = f.readInt32(); int32_t isstdcnt = f.readInt32(); int32_t leapcnt = f.readInt32(); int32_t timecnt = f.readInt32(); int32_t typecnt = f.readInt32(); int32_t charcnt = f.readInt32(); vector<int32_t> trans; vector<int> localtimes; trans.reserve(timecnt); for (int i = 0; i < timecnt; i++) { trans.push_back(f.readInt32()); } for (int i = 0; i < timecnt; i++) { uint8_t local = f.readUInt8(); localtimes.push_back(local); } for (int i = 0; i < typecnt; i++) { int32_t gmtoff = f.readInt32(); uint8_t isdst = f.readUInt8(); uint8_t abbrind = f.readUInt8(); data->localtimes.push_back(Localtime(gmtoff, isdst, abbrind)); } for (int i = 0; i < timecnt; i++) { int localIdx = localtimes[i]; time_t localtime = trans[i] + data->localtimes[localIdx].gmtOffset; data->transitions.push_back(Transition(trans[i], localtime, localIdx)); } data->abbreviation = f.readBytes(charcnt); // leapcnt for (int i = 0; i < leapcnt; i++) { // int32_t leaptime = f.readInt32(); // int32_t cumleap = f.readInt32(); } // FIXME (void) isstdcnt; (void) isgmtcnt; } catch (logic_error& e) { fprintf(stderr, "%s\n", e.what()); } } return true; }
/* * _event_data * * Monitor event data */ static void _event_data(void) { const char *func = __FUNCTION__; int fd; if ((fd = cerebro_event_register(handle, event_name)) < 0) { char *msg = cerebro_strerror(cerebro_errnum(handle)); _clean_err_exit(cerebro_errnum(handle)); err_exit("%s: cerebro_event_register: %s", func, msg); } while (1) { struct pollfd pfd; int n; pfd.fd = fd; pfd.events = POLLIN; pfd.revents = 0; n = Poll(&pfd, 1, -1); if (n && pfd.revents & POLLIN) { char vbuf[CEREBRO_STAT_BUFLEN]; char tbuf[CEREBRO_STAT_BUFLEN]; char *nodename; unsigned int event_value_type; unsigned int event_value_len; void *event_value; time_t t; struct tm *tm; if (cerebro_event_parse(handle, fd, &nodename, &event_value_type, &event_value_len, &event_value) < 0) { char *msg = cerebro_strerror(cerebro_errnum(handle)); _clean_err_exit(cerebro_errnum(handle)); err_exit("%s: cerebro_event_parse: %s", func, msg); } memset(vbuf, '\0', CEREBRO_STAT_BUFLEN); _metric_value_str(event_value_type, event_value_len, event_value, vbuf, CEREBRO_STAT_BUFLEN); memset(tbuf, '\0', CEREBRO_STAT_BUFLEN); t = time(NULL); tm = Localtime(&t); strftime(tbuf, CEREBRO_STAT_BUFLEN, "%F %I:%M:%S%P", tm); fprintf(stdout, "%s(%s): %s\n", nodename, tbuf, vbuf); free(nodename); free(event_value); } } (void)cerebro_event_unregister(handle, fd); }
/**************************************** TrafficInformation ****************************************/ void TrafficInformation::DrawInfo(InfoItem* it,time_t tm) { static char str[MAX_PATH+1]; double db; int cn; time_t tmp; struct tm *ltm; int len; switch(it->Type) { //00 " " ---- Space case 00: break; //01 "SrcPathname" ---- Source filename case 01: FTP_Info->StrCpy(str,SrcFileName,ARRAYSIZE(str)); break; //02 "SrcPath", case 02: FTP_Info->StrCpy(str,SrcFileName,ARRAYSIZE(str)); FTP_Info->PointToName(str)[0] = 0; break; //03 "SrcName", case 03: FTP_Info->StrCpy(str,FTP_Info->PointToName(SrcFileName),ARRAYSIZE(str)); break; //04 "DestPathname" ---- Target filename case 04: FTP_Info->StrCpy(str,DestFileName,ARRAYSIZE(str)); break; //05 "DestPath", case 05: FTP_Info->StrCpy(str,DestFileName,ARRAYSIZE(str)); FTP_Info->PointToName(str)[0] = 0; break; //06 "DestName", case 06: FTP_Info->StrCpy(str,FTP_Info->PointToName(DestFileName),ARRAYSIZE(str)); break; //07 "CurSize" ---- Current processed size case 07: FTP_Info->FDigit(str,CurrentSz(),-1); break; //08 "CurESize" ---- Full size of current file //09 "CurFSize", case 8: case 9: FTP_Info->FDigit(str,FullFileSize,-1); break; //10 "CurRSize" ---- Remain size of current file case 10: FTP_Info->FDigit(str,CurrentRemain(),-1); break; //11 "CurTime" ---- Time from start of current file case 11: cn = (int)(tm - FileStartTime); _snprintf(str,ARRAYSIZE(str), "%02d:%02d:%02d", cn/3600, (cn/60)%60, cn%60); break; //12 "CurFTime" ---- Full time to process current file case 12: db = Cps; if(db > 0) cn = (int)(CurrentDoRemain() / db); else cn = 0; cn = Max(cn,0); _snprintf(str,ARRAYSIZE(str), "%02d:%02d:%02d", cn/3600, (cn/60)%60, cn%60); break; //13 "CurRTime" ---- Remain time to process current file case 13: db = Cps; if(db > 0) cn = (int)(CurrentRemain() / db); else cn = 0; cn = Max(cn,0); _snprintf(str,ARRAYSIZE(str), "%02d:%02d:%02d", cn/3600, (cn/60)%60, cn%60); break; //14 "CurETime" ---- End time to process current file case 14: db = Cps; if(db > 0) tmp = tm + (time_t)(CurrentRemain() / db); else tmp = tm; tmp = Max(tmp,tm); ltm = Localtime(&tmp); StrTTime(str,ltm); break; //15 "CurEDate" ---- End date to process current file case 15: db = Cps; if(db > 0) tmp = tm + (time_t)(CurrentRemain() / db); else tmp = tm; tmp = Max(tmp,tm); ltm = Localtime(&tmp); StrYTime(str,ltm); break; //16 "TotSize" ---- Current processed size case 16: FTP_Info->FDigit(str,TotalSz(),-1); break; //17 "TotESize" ---- Full size of current file //18 "TotFSize", case 17: case 18: FTP_Info->FDigit(str,TotalFullSize,-1); break; //19 "TotRSize" ---- Remain size of current file case 19: FTP_Info->FDigit(str,TotalRemain(),-1); break; //20 "TotTime" ---- Time from start of current file case 20: cn = (int)(tm - TotalStartTime); _snprintf(str,ARRAYSIZE(str), "%02d:%02d:%02d", cn/3600, (cn/60)%60, cn%60); break; //21 "TotFTime" ---- Full time to process current file case 21: db = (AvCps[0] + AvCps[1] + AvCps[2])/3; if(db > 0) cn = (int)(TotalDoRemain() / db); else cn = 0; cn = Max(cn,0); _snprintf(str,ARRAYSIZE(str), "%02d:%02d:%02d", cn/3600, (cn/60)%60, cn%60); break; //22 "TotRTime" ----- Remain time to process current file case 22: db = (AvCps[0] + AvCps[1] + AvCps[2])/3; if(db > 0) cn = (int)(TotalRemain() / db); else cn = 0; cn = Max(cn,0); _snprintf(str,ARRAYSIZE(str), "%02d:%02d:%02d", cn/3600, (cn/60)%60, cn%60); break; //23 "TotETime" ---- End time to process all files case 23: db = TotalCps; if(db > 0) tmp = tm + (time_t)(TotalRemain() / db); else tmp = tm; ltm = Localtime(&tmp); StrTTime(str,ltm); break; //24 "TotEDate" ----- End date to process current file case 24: db = TotalCps; if(db > 0) tmp = tm + (time_t)(TotalRemain() / db); else tmp = tm; ltm = Localtime(&tmp); StrYTime(str,ltm); break; //25 "CurCPS" ----- CP of current file case 25: FTP_Info->FCps(str,Cps); break; //26 "CPS" ----- Summary CPS case 26: FTP_Info->FCps(str,(AvCps[0] + AvCps[1] + AvCps[2])/3); break; //27 "TotCPS" ----- Total CPS case 27: FTP_Info->FCps(str,TotalCps); break; //28 "Progress" ----- Current progress bar //42 "Pg" ----- ALIAS `Progress` case 42: case 28: PPercent(str,0,it->Size-1,(int)ToPercent(CurrentSz(),FullFileSize)); break; //29 "FProgress"----- Total progress bar //43 "FPg" ----- ALIAS `FProgress` case 43: case 29: PPercent(str,0,it->Size-1,(int)ToPercent(TotalSz(),TotalFullSize)); break; //40 "Pc" ----- ALIAS `Percent` //30 "PerCent" ----- Current "99.9%" percent case 40: case 30: db = ToPercent(CurrentSz(),FullFileSize); if(((int)db) == 100) FTP_Info->StrCpy(str,"100%",-1); else sprintf(str,"%2.1lf%%",db); break; //41 "FPc" ---- ALIAS `FPercent` //31 "FPerCent" ----- Total "99.9%" percent case 41: case 31: db = ToPercent(TotalSz(),TotalFullSize); if(((int)db) == 100) FTP_Info->StrCpy(str,"100%",-1); else sprintf(str,"%2.1lf%%",db); break; //32 "Cn" ---- Complete count case 32: FTP_Info->FDigit(str,TotalComplete,-1); break; //33 "CnSkip" ---- Skipped count case 33: FTP_Info->FDigit(str,TotalSkipped,-1); break; //34 "CnR" ---- Remain count case 34: FTP_Info->FDigit(str,TotalFiles-TotalComplete-TotalSkipped,-1); break; //35 "CnF" ---- Count case 35: FTP_Info->FDigit(str,TotalFiles,-1); break; //36 "CurPc" ---- Current percent case 36: db = ToPercent(TotalComplete,TotalFiles); if(((int)db) == 100) FTP_Info->StrCpy(str,"100%",-1); else sprintf(str,"%2.1lf%%",db); break; //37 "SkipPc" ---- Skipped percent case 37: db = ToPercent(TotalSkipped,TotalFiles); if(((int)db) == 100) FTP_Info->StrCpy(str,"100%",-1); else sprintf(str,"%2.1lf%%",db); break; //38 "CurPg" ---- Current percent progress case 38: PPercent(str,0,it->Size-1,(int)ToPercent(TotalComplete,TotalFiles)); break; //39 "SkipPg" ---- Skipped percent progress case 39: PPercent(str,0,it->Size-1,(int)ToPercent(TotalSkipped,TotalFiles)); break; //44 "CurCPS4" ----- CP of current file case 44: FCps4(str,Cps); break; //45 "CPS4" ----- Summary CPS case 45: FCps4(str,(AvCps[0] + AvCps[1] + AvCps[2])/3); break; //46 "TotCPS4" ----- Total CPS case 46: FCps4(str,TotalCps); break; //47 "IPc" ----- Current "99%" percent case 47: db = ToPercent(CurrentSz(),FullFileSize); if(((int)db) == 100) FTP_Info->StrCpy(str,"100%",-1); else sprintf(str,"%d%%",(int)db); break; //48 "IFPc" ----- Total "99%" percent case 48: db = ToPercent(TotalSz(),TotalFullSize); if(((int)db) == 100) FTP_Info->StrCpy(str,"100%",-1); else sprintf(str,"%d%%",(int)db); break; } if(it->Type) { len = static_cast<int>(strlen(str)); if(!it->Size && it->Align == tCenter) it->Pos -= len/2; if(!it->Size) it->Size = len; if(len > it->Size && it->Align == tCenter) it->Align = tLeft; if(it->Pos+it->Size > MAX_TRAF_WIDTH) return; if(len > it->Size) { if(it->Align == tLeft) { memmove(Lines[it->Line]+it->Pos,str+len-it->Size,it->Size); Lines[it->Line][it->Pos] = FAR_LEFT_CHAR; } else { memmove(Lines[it->Line]+it->Pos,str,it->Size); Lines[it->Line][it->Pos+it->Size] = FAR_RIGHT_CHAR; } } else { //Clear if(it->Align != tCenter) memset(Lines[it->Line]+it->Pos,it->Fill,it->Size); //Draw if(it->Align == tLeft) memmove(Lines[it->Line]+it->Pos, str, len); else if(it->Align == tRight) memmove(Lines[it->Line]+it->Pos + it->Size - len, str, len); else memmove(Lines[it->Line]+it->Pos + it->Size/2 - len/2, str, len); } } else { memset(Lines[it->Line]+it->Pos,it->Fill,it->Size); } }