From a46b3f0a98c801b94df08598be0127e4d7db2aca Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Wed, 17 Apr 2024 16:45:46 +0800 Subject: Use 'fp' name for local 'FILE*' variables. It is inappropriate to use the 'fd' name for 'FILE*' variables. POSIX file descriptiors are of type 'int' and are distinguished from ISO C stream pointers (type 'FILE*'). Rename these variables to 'fp', which is a preferred naming in POSIX. (Note that ISO C preferred the 'stream' name for the variables.) No code changes. --- OpenFilesScreen.c | 8 ++--- Settings.c | 74 +++++++++++++++++++++++------------------------ TraceScreen.c | 6 ++-- XUtils.c | 6 ++-- XUtils.h | 2 +- generic/uname.c | 10 +++---- linux/LinuxProcessTable.c | 16 +++++----- linux/Platform.c | 52 ++++++++++++++++----------------- 8 files changed, 87 insertions(+), 87 deletions(-) diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index d6d663c0..08b3b3d6 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -138,13 +138,13 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { OpenFiles_FileData* fdata = NULL; bool lsofIncludesFileSize = false; - FILE* fd = fdopen(fdpair[0], "r"); - if (!fd) { + FILE* fp = fdopen(fdpair[0], "r"); + if (!fp) { pdata->error = 1; return pdata; } for (;;) { - char* line = String_readLine(fd); + char* line = String_readLine(fp); if (!line) { break; } @@ -212,7 +212,7 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { free(line); } - fclose(fd); + fclose(fp); int wstatus; while (waitpid(child, &wstatus, 0) == -1) diff --git a/Settings.c b/Settings.c index 95c30305..a689b42b 100644 --- a/Settings.c +++ b/Settings.c @@ -62,13 +62,13 @@ static char** readQuotedList(char* line) { return list; } -static void writeQuotedList(FILE* fd, char** list) { +static void writeQuotedList(FILE* fp, char** list) { const char* sep = ""; for (int i = 0; list[i]; i++) { - fprintf(fd, "%s\"%s\"", sep, list[i]); + fprintf(fp, "%s\"%s\"", sep, list[i]); sep = " "; } - fprintf(fd, "\n"); + fprintf(fp, "\n"); } */ @@ -600,60 +600,60 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini typedef ATTR_FORMAT(printf, 2, 3) int (*OutputFunc)(FILE*, const char*,...); -static void writeFields(OutputFunc of, FILE* fd, +static void writeFields(OutputFunc of, FILE* fp, const ProcessField* fields, Hashtable* columns, bool byName, char separator) { const char* sep = ""; for (unsigned int i = 0; fields[i]; i++) { if (fields[i] < LAST_PROCESSFIELD && byName) { const char* pName = toFieldName(columns, fields[i], NULL); - of(fd, "%s%s", sep, pName); + of(fp, "%s%s", sep, pName); } else if (fields[i] >= LAST_PROCESSFIELD && byName) { bool enabled; const char* pName = toFieldName(columns, fields[i], &enabled); if (enabled) - of(fd, "%sDynamic(%s)", sep, pName); + of(fp, "%sDynamic(%s)", sep, pName); } else { // This "-1" is for compatibility with the older enum format. - of(fd, "%s%d", sep, (int) fields[i] - 1); + of(fp, "%s%d", sep, (int) fields[i] - 1); } sep = " "; } - of(fd, "%c", separator); + of(fp, "%c", separator); } -static void writeList(OutputFunc of, FILE* fd, +static void writeList(OutputFunc of, FILE* fp, char** list, int len, char separator) { const char* sep = ""; for (int i = 0; i < len; i++) { - of(fd, "%s%s", sep, list[i]); + of(fp, "%s%s", sep, list[i]); sep = " "; } - of(fd, "%c", separator); + of(fp, "%c", separator); } static void writeMeters(const Settings* this, OutputFunc of, - FILE* fd, char separator, unsigned int column) { + FILE* fp, char separator, unsigned int column) { if (this->hColumns[column].len) { - writeList(of, fd, this->hColumns[column].names, this->hColumns[column].len, separator); + writeList(of, fp, this->hColumns[column].names, this->hColumns[column].len, separator); } else { - of(fd, "!%c", separator); + of(fp, "!%c", separator); } } static void writeMeterModes(const Settings* this, OutputFunc of, - FILE* fd, char separator, unsigned int column) { + FILE* fp, char separator, unsigned int column) { if (this->hColumns[column].len) { const char* sep = ""; for (size_t i = 0; i < this->hColumns[column].len; i++) { - of(fd, "%s%d", sep, this->hColumns[column].modes[i]); + of(fp, "%s%d", sep, this->hColumns[column].modes[i]); sep = " "; } } else { - of(fd, "!"); + of(fp, "!"); } - of(fd, "%c", separator); + of(fp, "%c", separator); } ATTR_FORMAT(printf, 2, 3) @@ -672,12 +672,12 @@ static int signal_safe_fprintf(FILE* stream, const char* fmt, ...) { } int Settings_write(const Settings* this, bool onCrash) { - FILE* fd; + FILE* fp; char separator; char* tmpFilename = NULL; OutputFunc of; if (onCrash) { - fd = stderr; + fp = stderr; separator = ';'; of = signal_safe_fprintf; } else if (!this->writeConfig) { @@ -690,25 +690,25 @@ int Settings_write(const Settings* this, bool onCrash) { umask(cur_umask); if (fdtmp == -1) return -errno; - fd = fdopen(fdtmp, "w"); - if (fd == NULL) + fp = fdopen(fdtmp, "w"); + if (!fp) return -errno; separator = '\n'; of = fprintf; } #define printSettingInteger(setting_, value_) \ - of(fd, setting_ "=%d%c", (int) (value_), separator) + of(fp, setting_ "=%d%c", (int) (value_), separator) #define printSettingString(setting_, value_) \ - of(fd, setting_ "=%s%c", value_, separator) + of(fp, setting_ "=%s%c", value_, separator) if (!onCrash) { - of(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n"); - of(fd, "# The parser is also very primitive, and not human-friendly.\n"); + of(fp, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n"); + of(fp, "# The parser is also very primitive, and not human-friendly.\n"); } printSettingString("htop_version", VERSION); printSettingInteger("config_reader_min_version", CONFIG_READER_MIN_VERSION); - of(fd, "fields="); writeFields(of, fd, this->screens[0]->fields, this->dynamicColumns, false, separator); + of(fp, "fields="); writeFields(of, fp, this->screens[0]->fields, this->dynamicColumns, false, separator); printSettingInteger("hide_kernel_threads", this->hideKernelThreads); printSettingInteger("hide_userland_threads", this->hideUserlandThreads); printSettingInteger("hide_running_in_container", this->hideRunningInContainer); @@ -749,10 +749,10 @@ int Settings_write(const Settings* this, bool onCrash) { printSettingString("header_layout", HeaderLayout_getName(this->hLayout)); for (unsigned int i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) { - of(fd, "column_meters_%u=", i); - writeMeters(this, of, fd, separator, i); - of(fd, "column_meter_modes_%u=", i); - writeMeterModes(this, of, fd, separator, i); + of(fp, "column_meters_%u=", i); + writeMeters(this, of, fp, separator, i); + of(fp, "column_meter_modes_%u=", i); + writeMeterModes(this, of, fp, separator, i); } // Legacy compatibility with older versions of htop @@ -770,14 +770,14 @@ int Settings_write(const Settings* this, bool onCrash) { const char* sortKey = toFieldName(this->dynamicColumns, ss->sortKey, NULL); const char* treeSortKey = toFieldName(this->dynamicColumns, ss->treeSortKey, NULL); - of(fd, "screen:%s=", ss->heading); - writeFields(of, fd, ss->fields, this->dynamicColumns, true, separator); + of(fp, "screen:%s=", ss->heading); + writeFields(of, fp, ss->fields, this->dynamicColumns, true, separator); if (ss->dynamic) { printSettingString(".dynamic", ss->dynamic); if (ss->sortKey && ss->sortKey != PID) - of(fd, "%s=Dynamic(%s)%c", ".sort_key", sortKey, separator); + of(fp, "%s=Dynamic(%s)%c", ".sort_key", sortKey, separator); if (ss->treeSortKey && ss->treeSortKey != PID) - of(fd, "%s=Dynamic(%s)%c", ".tree_sort_key", treeSortKey, separator); + of(fp, "%s=Dynamic(%s)%c", ".tree_sort_key", treeSortKey, separator); } else { printSettingString(".sort_key", sortKey); printSettingString(".tree_sort_key", treeSortKey); @@ -797,10 +797,10 @@ int Settings_write(const Settings* this, bool onCrash) { int r = 0; - if (ferror(fd) != 0) + if (ferror(fp) != 0) r = (errno != 0) ? -errno : -EBADF; - if (fclose(fd) != 0) + if (fclose(fp) != 0) r = r ? r : -errno; if (r == 0) diff --git a/TraceScreen.c b/TraceScreen.c index 8a69b678..d421d0eb 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -113,14 +113,14 @@ bool TraceScreen_forkTracer(TraceScreen* this) { exit(127); } - FILE* fd = fdopen(fdpair[0], "r"); - if (!fd) + FILE* fp = fdopen(fdpair[0], "r"); + if (!fp) goto err; close(fdpair[1]); this->child = child; - this->strace = fd; + this->strace = fp; return true; err: diff --git a/XUtils.c b/XUtils.c index bd4fe141..ec6cce4a 100644 --- a/XUtils.c +++ b/XUtils.c @@ -186,13 +186,13 @@ void String_freeArray(char** s) { free(s); } -char* String_readLine(FILE* fd) { +char* String_readLine(FILE* fp) { const size_t step = 1024; size_t bufSize = step; char* buffer = xMalloc(step + 1); char* at = buffer; for (;;) { - const char* ok = fgets(at, step + 1, fd); + const char* ok = fgets(at, step + 1, fp); if (!ok) { free(buffer); return NULL; @@ -202,7 +202,7 @@ char* String_readLine(FILE* fd) { *newLine = '\0'; return buffer; } else { - if (feof(fd)) { + if (feof(fp)) { return buffer; } } diff --git a/XUtils.h b/XUtils.h index 3703f8bc..093f5005 100644 --- a/XUtils.h +++ b/XUtils.h @@ -76,7 +76,7 @@ char** String_split(const char* s, char sep, size_t* n); void String_freeArray(char** s); ATTR_NONNULL -char* String_readLine(FILE* fd) ATTR_MALLOC; +char* String_readLine(FILE* fp) ATTR_MALLOC; ATTR_NONNULL static inline char* String_strchrnul(const char* s, int c) { diff --git a/generic/uname.c b/generic/uname.c index b5bb5834..d8293b3a 100644 --- a/generic/uname.c +++ b/generic/uname.c @@ -26,8 +26,8 @@ in the source distribution for its full text. #endif static void parseOSRelease(char* buffer, size_t bufferLen) { - FILE* stream = fopen(OSRELEASEFILE, "r"); - if (!stream) { + FILE* fp = fopen(OSRELEASEFILE, "r"); + if (!fp) { xSnprintf(buffer, bufferLen, "No OS Release"); return; } @@ -35,14 +35,14 @@ static void parseOSRelease(char* buffer, size_t bufferLen) { char name[64] = {'\0'}; char version[64] = {'\0'}; char lineBuffer[256]; - while (fgets(lineBuffer, sizeof(lineBuffer), stream)) { + while (fgets(lineBuffer, sizeof(lineBuffer), fp)) { if (String_startsWith(lineBuffer, "PRETTY_NAME=\"")) { const char* start = lineBuffer + strlen("PRETTY_NAME=\""); const char* stop = strrchr(lineBuffer, '"'); if (!stop || stop <= start) continue; String_safeStrncpy(buffer, start, MINIMUM(bufferLen, (size_t)(stop - start + 1))); - fclose(stream); + fclose(fp); return; } if (String_startsWith(lineBuffer, "NAME=\"")) { @@ -62,7 +62,7 @@ static void parseOSRelease(char* buffer, size_t bufferLen) { continue; } } - fclose(stream); + fclose(fp); snprintf(buffer, bufferLen, "%s%s%s", name[0] ? name : "", name[0] && version[0] ? " " : "", version); } diff --git a/linux/LinuxProcessTable.c b/linux/LinuxProcessTable.c index f8adf9ad..1e7c041d 100644 --- a/linux/LinuxProcessTable.c +++ b/linux/LinuxProcessTable.c @@ -71,11 +71,11 @@ static FILE* fopenat(openat_arg_t openatArg, const char* pathname, const char* m if (fd < 0) return NULL; - FILE* stream = fdopen(fd, mode); - if (!stream) + FILE* fp = fdopen(fd, mode); + if (!fp) close(fd); - return stream; + return fp; } static inline uint64_t fast_strtoull_dec(char** str, int maxlen) { @@ -755,8 +755,8 @@ static bool LinuxProcessTable_readStatmFile(LinuxProcess* process, openat_arg_t static bool LinuxProcessTable_readSmapsFile(LinuxProcess* process, openat_arg_t procFd, bool haveSmapsRollup) { //http://elixir.free-electrons.com/linux/v4.10/source/fs/proc/task_mmu.c#L719 //kernel will return data in chunks of size PAGE_SIZE or less. - FILE* f = fopenat(procFd, haveSmapsRollup ? "smaps_rollup" : "smaps", "r"); - if (!f) + FILE* fp = fopenat(procFd, haveSmapsRollup ? "smaps_rollup" : "smaps", "r"); + if (!fp) return false; process->m_pss = 0; @@ -764,10 +764,10 @@ static bool LinuxProcessTable_readSmapsFile(LinuxProcess* process, openat_arg_t process->m_psswp = 0; char buffer[256]; - while (fgets(buffer, sizeof(buffer), f)) { + while (fgets(buffer, sizeof(buffer), fp)) { if (!strchr(buffer, '\n')) { // Partial line, skip to end of this line - while (fgets(buffer, sizeof(buffer), f)) { + while (fgets(buffer, sizeof(buffer), fp)) { if (strchr(buffer, '\n')) { break; } @@ -784,7 +784,7 @@ static bool LinuxProcessTable_readSmapsFile(LinuxProcess* process, openat_arg_t } } - fclose(f); + fclose(fp); return true; } diff --git a/linux/Platform.c b/linux/Platform.c index 59fe7056..3316d1db 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -450,8 +450,8 @@ void Platform_setZfsCompressedArcValues(Meter* this) { char* Platform_getProcessEnv(pid_t pid) { char procname[128]; xSnprintf(procname, sizeof(procname), PROCDIR "/%d/environ", pid); - FILE* fd = fopen(procname, "r"); - if (!fd) + FILE* fp = fopen(procname, "r"); + if (!fp) return NULL; char* env = NULL; @@ -464,9 +464,9 @@ char* Platform_getProcessEnv(pid_t pid) { size += bytes; capacity += 4096; env = xRealloc(env, capacity); - } while ((bytes = fread(env + size, 1, capacity - size, fd)) > 0); + } while ((bytes = fread(env + size, 1, capacity - size, fp)) > 0); - fclose(fd); + fclose(fp); if (bytes < 0) { free(env); @@ -515,13 +515,13 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { int fd = openat(dfd, de->d_name, O_RDONLY | O_CLOEXEC); if (fd == -1) continue; - FILE* f = fdopen(fd, "r"); - if (!f) { + FILE* fp = fdopen(fd, "r"); + if (!fp) { close(fd); continue; } - for (char buffer[1024]; fgets(buffer, sizeof(buffer), f); ) { + for (char buffer[1024]; fgets(buffer, sizeof(buffer), fp); ) { if (!strchr(buffer, '\n')) continue; @@ -559,7 +559,7 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { data_ref = &(*data_ref)->next; } - fclose(f); + fclose(fp); } closedir(dirp); @@ -574,18 +574,18 @@ void Platform_getPressureStall(const char* file, bool some, double* ten, double* *ten = *sixty = *threehundred = 0; char procname[128]; xSnprintf(procname, sizeof(procname), PROCDIR "/pressure/%s", file); - FILE* fd = fopen(procname, "r"); - if (!fd) { + FILE* fp = fopen(procname, "r"); + if (!fp) { *ten = *sixty = *threehundred = NAN; return; } - int total = fscanf(fd, "some avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); + int total = fscanf(fp, "some avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); if (!some) { - total = fscanf(fd, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); + total = fscanf(fp, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); } (void) total; assert(total == 3); - fclose(fd); + fclose(fp); } void Platform_getFileDescriptors(double* used, double* max) { @@ -607,15 +607,15 @@ void Platform_getFileDescriptors(double* used, double* max) { } bool Platform_getDiskIO(DiskIOData* data) { - FILE* fd = fopen(PROCDIR "/diskstats", "r"); - if (!fd) + FILE* fp = fopen(PROCDIR "/diskstats", "r"); + if (!fp) return false; char lastTopDisk[32] = { '\0' }; unsigned long long int read_sum = 0, write_sum = 0, timeSpend_sum = 0; char lineBuffer[256]; - while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { + while (fgets(lineBuffer, sizeof(lineBuffer), fp)) { char diskname[32]; unsigned long long int read_tmp, write_tmp, timeSpend_tmp; if (sscanf(lineBuffer, "%*d %*d %31s %*u %*u %llu %*u %*u %*u %llu %*u %*u %llu", diskname, &read_tmp, &write_tmp, &timeSpend_tmp) == 4) { @@ -637,7 +637,7 @@ bool Platform_getDiskIO(DiskIOData* data) { timeSpend_sum += timeSpend_tmp; } } - fclose(fd); + fclose(fp); /* multiply with sector size */ data->totalBytesRead = 512 * read_sum; data->totalBytesWritten = 512 * write_sum; @@ -646,13 +646,13 @@ bool Platform_getDiskIO(DiskIOData* data) { } bool Platform_getNetworkIO(NetworkIOData* data) { - FILE* fd = fopen(PROCDIR "/net/dev", "r"); - if (!fd) + FILE* fp = fopen(PROCDIR "/net/dev", "r"); + if (!fp) return false; memset(data, 0, sizeof(NetworkIOData)); char lineBuffer[512]; - while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { + while (fgets(lineBuffer, sizeof(lineBuffer), fp)) { char interfaceName[32]; unsigned long long int bytesReceived, packetsReceived, bytesTransmitted, packetsTransmitted; if (sscanf(lineBuffer, "%31s %llu %llu %*u %*u %*u %*u %*u %*u %llu %llu", @@ -672,7 +672,7 @@ bool Platform_getNetworkIO(NetworkIOData* data) { data->packetsTransmitted += packetsTransmitted; } - fclose(fd); + fclose(fp); return true; } @@ -1079,18 +1079,18 @@ bool Platform_init(void) { } } - FILE* fd = fopen(PROCDIR "/1/mounts", "r"); - if (fd) { + FILE* fp = fopen(PROCDIR "/1/mounts", "r"); + if (fp) { char lineBuffer[256]; - while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { + while (fgets(lineBuffer, sizeof(lineBuffer), fp)) { // detect lxc or overlayfs and guess that this means we are running containerized if (String_startsWith(lineBuffer, "lxcfs /proc") || String_startsWith(lineBuffer, "overlay / overlay")) { Running_containerized = true; break; } } - fclose(fd); - } // if (fd) + fclose(fp); + } return true; } -- cgit v1.2.3