Skip to content

Commit

Permalink
Add const qualifiers to be more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthäus Wininger committed Dec 18, 2023
1 parent 7ff2abb commit a5d22b4
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/zps.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static void silence(FILE *stream)
if (!stream) {
return;
}
int fd = open("/dev/null", O_WRONLY);
const int fd = open("/dev/null", O_WRONLY);
if (fd != -1) {
dup2(fd, fileno(stream));
close(fd);
Expand Down Expand Up @@ -271,7 +271,7 @@ static ssize_t read_file(char *buf, size_t bufsiz, const char *format, ...)
vsnprintf(path, sizeof(path), format, vargs);
va_end(vargs);

int fd = open(path, O_RDONLY);
const int fd = open(path, O_RDONLY);
if (fd == -1) {
return -1;
}
Expand Down Expand Up @@ -304,17 +304,16 @@ static int parse_stat_content(char *stat_buf, struct proc_stats *proc_stats)
}

/* Pointer bounds for `comm` in the buffer */
char *begin = strchr(stat_buf, '(');
const char *begin = strchr(stat_buf, '(');
if (!begin) {
return -1;
}
char *end = strrchr(begin, ')');
char *const end = strrchr(begin, ')');
if (!end || end[1] == '\0') {
return -1;
}
*end = '\0';
++begin;
char *last_fields = end + 2;
const char *const last_fields = end + 2;
/*
begin:
%d (...) %c %d
Expand All @@ -333,7 +332,8 @@ static int parse_stat_content(char *stat_buf, struct proc_stats *proc_stats)
}

/* Limit `comm_strlen` */
size_t comm_strlen = strnlen(begin, sizeof(proc_stats->name) - 1);
*end = '\0';
const size_t comm_strlen = strnlen(begin, sizeof(proc_stats->name) - 1);
/* Extract the process name (limited by `comm_strlen`) */
memcpy(proc_stats->name, begin, comm_strlen);
/* Make sure string ends here */
Expand Down Expand Up @@ -372,8 +372,9 @@ static int get_proc_stats(const char *pid, struct proc_stats *proc_stats)
}

/* Read the `"/proc/<pid>/cmdline"` file */
ssize_t cmd_len = read_file(proc_stats->cmd, sizeof(proc_stats->cmd),
"%s/%s/%s", PROC_FILESYSTEM, pid, CMD_FILE);
const ssize_t cmd_len = read_file(proc_stats->cmd, sizeof(proc_stats->cmd),
"%s/%s/%s", PROC_FILESYSTEM, pid,
CMD_FILE);
if (cmd_len == -1) {
return -1;
}
Expand Down Expand Up @@ -505,8 +506,7 @@ static void proc_iter(struct proc_vec *defunct_procs,
}

for (;;) {
struct dirent *d;
d = readdir(dir);
struct dirent *d = readdir(dir);
if (d == NULL) {
break;
}
Expand Down

0 comments on commit a5d22b4

Please sign in to comment.