const char * Xdg_DetectDesktopEnv(void) { static char *outbuf = NULL; if (outbuf == NULL) { FILE *cmdPipe = popen(xdgDetectDEExec, "r"); if (cmdPipe) { static const size_t maxSize = sizeof "TEHLONGISTDESKTOPENVEVAR"; size_t outLen; // Doesn't include NUL. int status; if ( StdIO_ReadNextLine(cmdPipe, &outbuf, maxSize, &outLen) == StdIO_Success) { int i; for (i = 0; i < outLen; i++) { if (!isalnum(outbuf[i])) { g_debug("%s: received malformed input\n", __func__); free(outbuf); outbuf = NULL; break; } } } status = pclose(cmdPipe); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { g_debug("%s: %s did not exit cleanly (%x/%x)\n", __func__, xdgDetectDEExec, status, WEXITSTATUS(status)); free(outbuf); outbuf = NULL; } } if (outbuf == NULL) { outbuf = ""; } } return outbuf; }
int DictLL_ReadLine(FILE *stream, // IN: stream to read char **line, // OUT: malloc()'d line or null pointer char **name, // OUT: malloc()'d name or null pointer char **value) // OUT: malloc()'d value or null pointer { char *myLine; size_t myLineLen; ASSERT(stream); ASSERT(line); ASSERT(name); ASSERT(value); *line = NULL; *name = NULL; *value = NULL; switch (StdIO_ReadNextLine(stream, &myLine, 0, &myLineLen)) { case StdIO_Error: return 0; case StdIO_EOF: return 1; case StdIO_Success: if (DictLL_UnmarshalLine(myLine, myLineLen, line, name, value) == NULL) { *line = BufDup("", 0); } free(myLine); return 2; default: NOT_IMPLEMENTED(); } NOT_REACHED(); }
uint64 System_Uptime(void) { uint64 uptime = -1; #ifdef USERWORLD { VmkuserStatus_Code status; uint64 sysUptime; status = VmkuserUptime_GetUptime(&sysUptime); if (VmkuserStatus_IsOK(status)) { uptime = sysUptime / 10000; } } #elif defined(__linux__) { FILE *procStream; char *buf = NULL; size_t bufSize; uint64 sec; unsigned int csec; if (((procStream = Posix_Fopen("/proc/uptime", "r")) != NULL) && (StdIO_ReadNextLine(procStream, &buf, 80, &bufSize) == StdIO_Success) && (sscanf(buf, "%"FMT64"u.%2u", &sec, &csec) == 2)) { uptime = sec * 100 + csec; } else { Warning("%s: Unable to parse /proc/uptime.\n", __func__); } free(buf); if (procStream) { fclose(procStream); } } #elif defined sun || defined __APPLE__ { struct utmpx *boot, tmp; tmp.ut_type = BOOT_TIME; if ((boot = getutxid(&tmp)) != NULL) { struct timeval now; struct timeval *boottime = &boot->ut_tv; gettimeofday(&now, NULL); uptime = (now.tv_sec * 100 + now.tv_usec / 10000) - (boottime->tv_sec * 100 + boottime->tv_usec / 10000); } else { Warning("%s: Unable to determine boot time.\n", __func__); } endutxent(); } #else // FreeBSD { /* * FreeBSD: src/usr.bin/w/w.c rev 1.59: * "Obtain true uptime through clock_gettime(CLOCK_MONOTONIC, * struct *timespec) instead of subtracting 'bootime' from 'now'." */ struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != -1) { uptime = ts.tv_sec * 100 + ts.tv_nsec / 10000000; } else { Warning("%s: clock_gettime: %d\n", __func__, errno); } } #endif return uptime; }