/
fileaccesses.c
96 lines (82 loc) · 2.9 KB
/
fileaccesses.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* bigbro filetracking library
Copyright (C) 2015,2016 David Roundy
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA */
#define _GNU_SOURCE
#define _XOPEN_SOURCE 700
#define __BSD_VISIBLE 1
#include "bigbro.h"
#ifndef _WIN32
#include "realpath.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
int main(int argc, char **argv) {
// the following are to test the corner and error cases of
// flexible_realpath.
#ifndef _WIN32
assert(! flexible_realpath(NULL, NULL,
look_for_file_or_directory, true));
assert(! flexible_realpath("", NULL,
look_for_file_or_directory, true));
// the following should give null because flexible_realpath wants an
// absolute path.
assert(! flexible_realpath("tmp", NULL,
look_for_file_or_directory, true));
#endif
if (argc < 2) {
fprintf(stderr, "Usage: %s cmdline\n", argv[0]);
exit(1);
}
char **written_to_files = NULL;
char **read_from_files = NULL;
char **read_from_directories = NULL;
char **mkdir_directories = NULL;
int cmdlength = 10; // a little leeway
for (int i=1; argv[i]; i++) {
cmdlength += strlen(argv[i]) + 1;
}
char *cmdline = (char *)calloc(1,cmdlength);
strcpy(cmdline, argv[1]);
for (int i=2; argv[i]; i++) {
strcat(cmdline, " ");
strcat(cmdline, argv[i]);
}
pid_t child_pid;
int retval = bigbro_with_mkdir(".", &child_pid, 0, 0, NULL, cmdline,
&read_from_directories, &mkdir_directories,
&read_from_files, &written_to_files);
free(cmdline);
if (read_from_directories)
for (int i=0; read_from_directories[i]; i++) {
fprintf(stderr, "l: %s\n", read_from_directories[i]);
}
if (read_from_files)
for (int i=0; read_from_files[i]; i++) {
fprintf(stderr, "r: %s\n", read_from_files[i]);
}
if (written_to_files)
for (int i=0; written_to_files[i]; i++) {
fprintf(stderr, "w: %s\n", written_to_files[i]);
}
if (mkdir_directories)
for (int i=0; mkdir_directories[i]; i++) {
fprintf(stderr, "m: %s\n", mkdir_directories[i]);
}
free(read_from_directories);
free(read_from_files);
free(written_to_files);
return retval;
}