Skip to content

Commit

Permalink
Use 'fp' name for local 'FILE*' variables.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Explorer09 authored and BenBE committed Apr 17, 2024
1 parent 6f0adfa commit a46b3f0
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 87 deletions.
8 changes: 4 additions & 4 deletions OpenFilesScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
74 changes: 37 additions & 37 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions TraceScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions XUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -202,7 +202,7 @@ char* String_readLine(FILE* fd) {
*newLine = '\0';
return buffer;
} else {
if (feof(fd)) {
if (feof(fp)) {
return buffer;
}
}
Expand Down
2 changes: 1 addition & 1 deletion XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions generic/uname.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ 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;
}

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=\"")) {
Expand All @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions linux/LinuxProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -755,19 +755,19 @@ 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;
process->m_swap = 0;
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;
}
Expand All @@ -784,7 +784,7 @@ static bool LinuxProcessTable_readSmapsFile(LinuxProcess* process, openat_arg_t
}
}

fclose(f);
fclose(fp);
return true;
}

Expand Down
Loading

0 comments on commit a46b3f0

Please sign in to comment.