Skip to content

Commit

Permalink
don't compile regex in loop
Browse files Browse the repository at this point in the history
  • Loading branch information
spieglt committed Oct 1, 2020
1 parent ee98a26 commit 0cbb3a1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/attach.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ char read_status(pid_t pid)
if (!h_status) return 0;
read_file(str, 4096, h_status);

regex_t regex;
int err;
regmatch_t pmatch[2];
if (regcomp(&regex, "State:\\W+([A-Za-z])", REG_EXTENDED) != 0)
SYS_ERR("regex compilation error");
err = regexec(&regex, str->data, 2, pmatch, 0);
regfree(&regex);
if (err) {
DEBUG("failed to find regex match in /proc/%d/status file\n", pid);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/whatfiles.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <dirent.h>
#include <regex.h>
#include <signal.h>
#include <stddef.h>
#include <string.h>
Expand All @@ -14,6 +15,7 @@

FILE *Handle = (FILE*)NULL;
int Debug = 0;
regex_t regex;
LastSyscall_t LastSyscall;
DebugStats_t DebugStats;

Expand Down Expand Up @@ -92,6 +94,10 @@ int main(int argc, char* argv[])
HashMap hashmap = &hm;
init_hashmap(hashmap);

if (regcomp(&regex, "State:\\W+([A-Za-z])", REG_EXTENDED) != 0) {
SYS_ERR("regex compilation error");
}

int start_of_user_command = discover_flags(argc, argv);
char *user_filename = parse_flags(start_of_user_command, argv, &pid, &stdout_override, &attach);
if (start_of_user_command == argc && !attach) {
Expand Down Expand Up @@ -210,6 +216,7 @@ int main(int argc, char* argv[])
}
}

regfree(&regex);
err = destroy(hashmap);
HASH_ERR_CHECK(err, "tried to free null pointers in hashmap.\n")
fclose(Handle);
Expand Down
2 changes: 2 additions & 0 deletions src/whatfiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
#define WHATFILES_H

#include <errno.h>
#include <regex.h>
#include <stdbool.h>
#include <stdlib.h>

#include "hashmap.h"

extern int Debug;
extern FILE *Handle;
extern regex_t regex;

#define MODE_LEN 32
#define OUTPUT(...) fprintf(Handle, __VA_ARGS__)
Expand Down

0 comments on commit 0cbb3a1

Please sign in to comment.