-
Notifications
You must be signed in to change notification settings - Fork 4
/
fileaccesses.c
120 lines (105 loc) · 3.69 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* 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"
#include <fcntl.h>
#else
#include <io.h>
#include <windows.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));
assert(! flexible_realpath("", NULL, look_for_file_or_directory));
// the following should give null because flexible_realpath wants an
// absolute path.
assert(! flexible_realpath("tmp", NULL, look_for_file_or_directory));
#endif
if (argc < 2) {
fprintf(stderr, "Usage: %s cmdline\n", argv[0]);
exit(1);
}
bigbro_fd_t stdoutfd = invalid_bigbro_fd;
if (strcmp(argv[1],"-o") == 0) {
assert(argc > 2);
printf("outputting to file %s\n", argv[2]);
#ifdef _WIN32
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
stdoutfd = CreateFileA(argv[2], // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
&sa, // default security
CREATE_ALWAYS, // just be sure there is a file here
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
#else
stdoutfd = creat(argv[2], 0666);
#endif
argc -= 2;
argv += 2;
}
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(".", &child_pid, stdoutfd, 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;
}