Dash:
[t2-trunk.git] / misc / tools-source / fl_stparse.c
blob95bb8de2de7361543e52085b03a3f07c4f795143
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/tools-source/fl_stparse.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
8 *
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 #define _GNU_SOURCE
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #define MAXPROCS 512
27 struct procs_t {
28 int pid;
29 char * cwd;
30 } procs[MAXPROCS];
32 void newproc(int pid, char * cwd) {
33 int c;
34 for (c=0; c<MAXPROCS; c++)
35 if (procs[c].pid == 0) {
36 procs[c].pid = pid;
37 procs[c].cwd = cwd;
38 return;
41 fprintf(stderr, "fl_stparse: No more free procs in list!\n");
42 exit(1);
45 struct procs_t * getproc(int pid) {
46 int c;
47 for (c=0; c<MAXPROCS; c++)
48 if (procs[c].pid == pid) {
49 return procs + c;
52 fprintf(stderr, "fl_stparse: Can't find proc %d!\n", pid);
53 exit(1);
56 void setproc(int pid, char * cwd) {
57 int c;
58 for (c=0; c<MAXPROCS; c++)
59 if (procs[c].pid == pid) {
60 free(procs[c].cwd);
61 procs[c].cwd = cwd;
62 return;
65 fprintf(stderr, "fl_stparse: Can't set proc %d!\n", pid);
66 exit(1);
69 void freeproc(int pid) {
70 int c;
71 for (c=0; c<MAXPROCS; c++)
72 if (procs[c].pid == pid) {
73 procs[c].pid=0;
74 free(procs[c].cwd);
75 procs[c].cwd=NULL;
76 return;
79 fprintf(stderr, "fl_stparse: Can't free proc %d!\n", pid);
80 exit(1);
83 int main(int argc, char ** argv) {
84 char line[1024];
85 char buf1[1024];
86 char buf2[1024];
87 int pid, newpid;
88 char *sp1, *sp2;
90 FILE *wlog = fopen("/dev/null", "w");
91 FILE *rlog = fopen("/dev/null", "w");
92 FILE *logfile;
93 int opt;
95 while ( (opt = getopt(argc, argv, "w:r:")) != -1 ) {
96 switch (opt) {
97 case 'w':
98 fclose(wlog);
99 wlog = fopen(optarg, "w");
100 break;
102 case 'r':
103 fclose(rlog);
104 rlog = fopen(optarg, "w");
105 break;
107 default:
108 fprintf(stderr, "Usage: %s [-w wlog-file] "
109 "[-r rlog-file]\n", argv[0]);
110 return 1;
114 if (fgets(line, 1024, stdin) &&
115 sscanf(line, "%d", &pid) == 1 ) {
116 newproc(pid, getcwd((char *) NULL, 0) );
117 } else {
118 fprintf(stderr, "fl_stparse: Can't init using first line "
119 "of input!\n");
120 return 1;
123 do {
125 if ( sscanf(line, "%d fork() = %d", &pid, &newpid) == 2 ||
126 sscanf(line, "%d vfork() = %d", &pid, &newpid) == 2 ||
127 sscanf(line, "%d <... fork resumed> ) = %d",
128 &pid, &newpid) == 2 ||
129 sscanf(line, "%d <... vfork resumed> ) = %d",
130 &pid, &newpid) == 2) {
131 sp1 = getproc(pid)->cwd;
132 sp2 = malloc( strlen(sp1) + 1);
133 strcpy(sp2, sp1);
134 newproc(newpid, sp2 );
135 continue;
138 if ( sscanf(line, "%d _exit(%d", &pid, &newpid) == 2 ||
139 sscanf(line, "%d exit_group(%d", &pid, &newpid) == 2 ) {
140 freeproc(pid);
141 continue;
144 if ( sscanf(line, "%d chdir(\"%[^\"]", &pid, buf1) == 2 ) {
145 chdir( getproc(pid)->cwd ); chdir( buf1 );
146 setproc(pid, getcwd((char *) NULL, 0) );
147 continue;
150 if ( sscanf(line, "%d open(\"%[^\"]\", %s",
151 &pid, buf1, buf2) == 3 ) {
153 if (strstr(buf2, "O_RDONLY") == NULL) logfile = wlog;
154 else logfile = rlog;
156 if (strstr(buf2, "O_DIRECTORY") != NULL) continue;
158 if (buf1[0] == '/') {
159 fprintf(logfile, "%d: %s\n", pid, buf1);
160 } else {
161 sp1 = getproc(pid)->cwd;
162 if (!strcmp(sp1, "/")) sp1="";
163 fprintf(logfile, "%d: %s/%s\n",
164 pid, sp1, buf1);
166 continue;
169 if ( sscanf(line, "%d mkdir(\"%[^\"]\", ", &pid, buf1) == 2 ||
170 sscanf(line, "%d utime(\"%[^\"]\", ", &pid, buf1) == 2 ||
171 sscanf(line, "%d link(\"%[^\"]\", \"%[^\"]\"",
172 &pid, buf2, buf1) == 3 ||
173 sscanf(line, "%d symlink(\"%[^\"]\", \"%[^\"]\"",
174 &pid, buf2, buf1) == 3 ||
175 sscanf(line, "%d rename(\"%[^\"]\", \"%[^\"]\"",
176 &pid, buf2, buf1) == 3 ) {
177 if (buf1[0] == '/') {
178 fprintf(wlog, "%d: %s\n", pid, buf1);
179 } else {
180 sp1 = getproc(pid)->cwd;
181 if (!strcmp(sp1, "/")) sp1="";
182 fprintf(wlog, "%d: %s/%s\n",
183 pid, sp1, buf1);
185 continue;
188 } while (fgets(line, 1024, stdin) != NULL);
189 return 0;