add clang-format
This commit is contained in:
8
.clang-format
Normal file
8
.clang-format
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
IndentWidth: 4
|
||||||
|
UseTab: Never
|
||||||
|
BreakBeforeBraces: Linux
|
||||||
|
AllowShortIfStatementsOnASingleLine: false
|
||||||
|
IndentCaseLabels: false
|
||||||
|
---
|
||||||
@@ -13,19 +13,19 @@
|
|||||||
|
|
||||||
#define BUFFER_LEN 4096
|
#define BUFFER_LEN 4096
|
||||||
|
|
||||||
static void handle_event(int inotify_instance, int *watched, char **filenames, int *counter, size_t watched_len);
|
static void handle_event(int inotify_instance, int *watched, char **filenames,
|
||||||
|
int *counter, size_t watched_len);
|
||||||
static char **filenames_in_dir(char *path, size_t *return_len);
|
static char **filenames_in_dir(char *path, size_t *return_len);
|
||||||
static char **filenames_in_path_envvar(size_t *return_len);
|
static char **filenames_in_path_envvar(size_t *return_len);
|
||||||
static void free_null_terminated_pointer_array(char **ptr);
|
static void free_null_terminated_pointer_array(char **ptr);
|
||||||
static void print_event(uint32_t event);
|
static void print_event(uint32_t event);
|
||||||
static void save_result(char **filenames, int *counter, size_t arr_len);
|
static void save_result(char **filenames, int *counter, size_t arr_len);
|
||||||
|
|
||||||
|
|
||||||
/* This code is a bit over-commented in general */
|
/* This code is a bit over-commented in general */
|
||||||
int
|
int main(int argc, char *const argv[])
|
||||||
main(int argc, char * const argv[])
|
|
||||||
{
|
{
|
||||||
/* Get null terminated list of files to watch, create counter of same size */
|
/* Get null terminated list of files to watch, create counter of same size
|
||||||
|
*/
|
||||||
|
|
||||||
char **file_names;
|
char **file_names;
|
||||||
int *use_counter, *watched;
|
int *use_counter, *watched;
|
||||||
@@ -39,7 +39,10 @@ main(int argc, char * const argv[])
|
|||||||
file_names = filenames_in_dir(argv[1], &files_len);
|
file_names = filenames_in_dir(argv[1], &files_len);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
errx(EXIT_FAILURE, "Usage: `%s <dir>`. If dir is empty, directories in PATH will be monitored", argv[0]);
|
errx(EXIT_FAILURE,
|
||||||
|
"Usage: `%s <dir>`. If dir is empty, directories in PATH will be "
|
||||||
|
"monitored",
|
||||||
|
argv[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_names == NULL)
|
if (file_names == NULL)
|
||||||
@@ -48,8 +51,8 @@ main(int argc, char * const argv[])
|
|||||||
use_counter = calloc(files_len, sizeof(int));
|
use_counter = calloc(files_len, sizeof(int));
|
||||||
|
|
||||||
if (use_counter == NULL)
|
if (use_counter == NULL)
|
||||||
err(EXIT_FAILURE, "Failed to allocate %zu bytes for use counter", files_len*sizeof(int));
|
err(EXIT_FAILURE, "Failed to allocate %zu bytes for use counter",
|
||||||
|
files_len * sizeof(int));
|
||||||
|
|
||||||
/* Create file descriptor for accessing the inotify API */
|
/* Create file descriptor for accessing the inotify API */
|
||||||
|
|
||||||
@@ -58,22 +61,22 @@ main(int argc, char * const argv[])
|
|||||||
if (inotify_instance == -1)
|
if (inotify_instance == -1)
|
||||||
err(EXIT_FAILURE, "Failed to initialize inotify instance");
|
err(EXIT_FAILURE, "Failed to initialize inotify instance");
|
||||||
|
|
||||||
|
|
||||||
/* Mark files for events */
|
/* Mark files for events */
|
||||||
|
|
||||||
watched = calloc(files_len, sizeof(int));
|
watched = calloc(files_len, sizeof(int));
|
||||||
|
|
||||||
if (watched == NULL)
|
if (watched == NULL)
|
||||||
err(EXIT_FAILURE, "Failed to allocate %zu bytes for watch descriptors", files_len*sizeof(int));
|
err(EXIT_FAILURE, "Failed to allocate %zu bytes for watch descriptors",
|
||||||
|
files_len * sizeof(int));
|
||||||
|
|
||||||
for (size_t i = 0; file_names[i] != NULL; i++) {
|
for (size_t i = 0; file_names[i] != NULL; i++) {
|
||||||
watched[i] = inotify_add_watch(inotify_instance, file_names[i], IN_ACCESS);
|
watched[i] =
|
||||||
|
inotify_add_watch(inotify_instance, file_names[i], IN_ACCESS);
|
||||||
|
|
||||||
if (watched[i] == -1)
|
if (watched[i] == -1)
|
||||||
err(EXIT_FAILURE, "Cannot watch %s", file_names[i]);
|
err(EXIT_FAILURE, "Cannot watch %s", file_names[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Prepare for polling */
|
/* Prepare for polling */
|
||||||
|
|
||||||
struct pollfd poll_fds[2];
|
struct pollfd poll_fds[2];
|
||||||
@@ -85,7 +88,6 @@ main(int argc, char * const argv[])
|
|||||||
poll_fds[1].fd = inotify_instance;
|
poll_fds[1].fd = inotify_instance;
|
||||||
poll_fds[1].events = POLLIN;
|
poll_fds[1].events = POLLIN;
|
||||||
|
|
||||||
|
|
||||||
/* Wait for events and/or terminal input */
|
/* Wait for events and/or terminal input */
|
||||||
|
|
||||||
int poll_num;
|
int poll_num;
|
||||||
@@ -110,11 +112,11 @@ main(int argc, char * const argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (poll_fds[1].revents & POLLIN) {
|
if (poll_fds[1].revents & POLLIN) {
|
||||||
handle_event(inotify_instance, watched, file_names, use_counter, files_len);
|
handle_event(inotify_instance, watched, file_names, use_counter,
|
||||||
|
files_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
close(inotify_instance);
|
close(inotify_instance);
|
||||||
|
|
||||||
free_null_terminated_pointer_array(file_names);
|
free_null_terminated_pointer_array(file_names);
|
||||||
@@ -124,11 +126,9 @@ main(int argc, char * const argv[])
|
|||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Free allocated memory pointed to by pointers
|
/* Free allocated memory pointed to by pointers
|
||||||
in 'ptr', then free 'ptr' itself */
|
in 'ptr', then free 'ptr' itself */
|
||||||
static void
|
static void free_null_terminated_pointer_array(char **ptr)
|
||||||
free_null_terminated_pointer_array(char **ptr)
|
|
||||||
{
|
{
|
||||||
for (char **p = ptr; *p != NULL; p++) {
|
for (char **p = ptr; *p != NULL; p++) {
|
||||||
free(*p);
|
free(*p);
|
||||||
@@ -137,11 +137,10 @@ free_null_terminated_pointer_array(char **ptr)
|
|||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Returns a null terminated array of strings
|
/* Returns a null terminated array of strings
|
||||||
for every file in PATH */
|
for every file in PATH */
|
||||||
static char**
|
static char **filenames_in_path_envvar(size_t *return_len)
|
||||||
filenames_in_path_envvar(size_t *return_len) {
|
{
|
||||||
size_t n_files, i, output_size;
|
size_t n_files, i, output_size;
|
||||||
char **filenames, **output, *saveptr, *path, *token;
|
char **filenames, **output, *saveptr, *path, *token;
|
||||||
void *tmp;
|
void *tmp;
|
||||||
@@ -178,7 +177,8 @@ filenames_in_path_envvar(size_t *return_len) {
|
|||||||
tmp = realloc(output, output_size * sizeof(char *));
|
tmp = realloc(output, output_size * sizeof(char *));
|
||||||
|
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
warn("failed to reallocate %zu bytes", output_size * sizeof(char*));
|
warn("failed to reallocate %zu bytes",
|
||||||
|
output_size * sizeof(char *));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,11 +208,9 @@ filenames_in_path_envvar(size_t *return_len) {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Returns null terminated array
|
/* Returns null terminated array
|
||||||
of filepaths in directory */
|
of filepaths in directory */
|
||||||
static char**
|
static char **filenames_in_dir(char *path, size_t *return_len)
|
||||||
filenames_in_dir(char *path, size_t *return_len)
|
|
||||||
{
|
{
|
||||||
size_t result_size, n_items = 0;
|
size_t result_size, n_items = 0;
|
||||||
char **result;
|
char **result;
|
||||||
@@ -245,7 +243,8 @@ filenames_in_dir(char *path, size_t *return_len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (dir_entity->d_type == DT_REG) {
|
if (dir_entity->d_type == DT_REG) {
|
||||||
result[n_items] = calloc(strlen(path) + strlen(dir_entity->d_name) + 2, sizeof(char));
|
result[n_items] = calloc(
|
||||||
|
strlen(path) + strlen(dir_entity->d_name) + 2, sizeof(char));
|
||||||
result[n_items] = strcat(result[n_items], path);
|
result[n_items] = strcat(result[n_items], path);
|
||||||
result[n_items] = strcat(result[n_items], "/");
|
result[n_items] = strcat(result[n_items], "/");
|
||||||
result[n_items] = strcat(result[n_items], dir_entity->d_name);
|
result[n_items] = strcat(result[n_items], dir_entity->d_name);
|
||||||
@@ -292,11 +291,10 @@ fail:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Read all available inotify events from
|
/* Read all available inotify events from
|
||||||
file descriptor 'inotify_instance' */
|
file descriptor 'inotify_instance' */
|
||||||
static void
|
static void handle_event(int inotify_instance, int *watched, char **filenames,
|
||||||
handle_event(int inotify_instance, int *watched, char **filenames, int *counter, size_t watched_len)
|
int *counter, size_t watched_len)
|
||||||
{
|
{
|
||||||
ssize_t length;
|
ssize_t length;
|
||||||
const struct inotify_event *event;
|
const struct inotify_event *event;
|
||||||
@@ -314,7 +312,8 @@ handle_event(int inotify_instance, int *watched, char **filenames, int *counter,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (char *p = buffer; p < buffer+length; p += sizeof(struct inotify_event) + event->len) {
|
for (char *p = buffer; p < buffer + length;
|
||||||
|
p += sizeof(struct inotify_event) + event->len) {
|
||||||
event = (struct inotify_event *)p;
|
event = (struct inotify_event *)p;
|
||||||
|
|
||||||
for (int i = 0; i < watched_len; i++) {
|
for (int i = 0; i < watched_len; i++) {
|
||||||
@@ -327,10 +326,8 @@ handle_event(int inotify_instance, int *watched, char **filenames, int *counter,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Prints event mask values in a human-readable format */
|
/* Prints event mask values in a human-readable format */
|
||||||
static void
|
static void print_event(uint32_t event)
|
||||||
print_event(uint32_t event)
|
|
||||||
{
|
{
|
||||||
if (event & IN_ACCESS) {
|
if (event & IN_ACCESS) {
|
||||||
fprintf(stderr, "IN_ACCESS ");
|
fprintf(stderr, "IN_ACCESS ");
|
||||||
@@ -367,11 +364,9 @@ print_event(uint32_t event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Saves number of recorded events for each
|
/* Saves number of recorded events for each
|
||||||
executable t 'uses.log' */
|
executable t 'uses.log' */
|
||||||
static void
|
static void save_result(char **filenames, int *counter, size_t arr_len)
|
||||||
save_result(char **filenames, int* counter, size_t arr_len)
|
|
||||||
{
|
{
|
||||||
FILE *savefile;
|
FILE *savefile;
|
||||||
|
|
||||||
@@ -386,9 +381,8 @@ save_result(char **filenames, int* counter, size_t arr_len)
|
|||||||
if (counter[i] == 0)
|
if (counter[i] == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
fprintf(stdout, "%d %s\n", counter[i], filenames[i]);
|
// fprintf(stdout, "%d %s\n", counter[i], filenames[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fclose(savefile);
|
fclose(savefile);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user