1 /*
2 * Copyright (C) 2013 Red Hat, Inc.
3 *
4 * All rights reserved.
5 *
6 * Author: Jan Friesse <jfriesse@redhat.com>
7 *
8 * libqb is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * libqb is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with libqb. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <err.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31
32 static void usage(void)
33 {
34 printf("Usage: [ -i input_file] [ -o output_file ] [ -n no_bytes]\n");
35 printf("Changes no_bytes (default 1024) in input_file (default = - = stdin) and store\n");
36 printf("result to output_file (default = - = stdout). It's possible to use same file\n");
37 printf("as both input and output\n");
38
39 exit(1);
40 }
41
42 static void init_rand(void) {
43 unsigned int init_v;
44
45 init_v = time(NULL) + getpid();
46
47 srand(init_v);
48 }
49
50 int main(int argc, char *argv[])
51 {
52 FILE *fi, *fo;
53 int i;
54 const char *input_file_name;
55 const char *output_file_name;
56 int no_bytes;
57 char *ep;
58 int ch;
59 unsigned char *data;
60 size_t data_size;
61 size_t data_pos;
62 size_t input_data_size;
63 unsigned char buf[1024];
64
65 input_file_name = "-";
66 output_file_name = "-";
67 no_bytes = 1024;
68
|
(1) Event cond_true: |
Condition "(ch = getopt(argc, argv, "hi:o:n:")) != -1", taking true branch. |
|
(7) Event loop_begin: |
Jumped back to beginning of loop. |
|
(8) Event cond_true: |
Condition "(ch = getopt(argc, argv, "hi:o:n:")) != -1", taking true branch. |
|
(17) Event loop_begin: |
Jumped back to beginning of loop. |
|
(18) Event cond_true: |
Condition "(ch = getopt(argc, argv, "hi:o:n:")) != -1", taking true branch. |
|
(27) Event loop_begin: |
Jumped back to beginning of loop. |
|
(28) Event cond_true: |
Condition "(ch = getopt(argc, argv, "hi:o:n:")) != -1", taking true branch. |
|
(34) Event loop_begin: |
Jumped back to beginning of loop. |
|
(35) Event cond_false: |
Condition "(ch = getopt(argc, argv, "hi:o:n:")) != -1", taking false branch. |
69 while ((ch = getopt(argc, argv, "hi:o:n:")) != -1) {
|
(2) Event switch: |
Switch case value "105". |
|
(9) Event switch: |
Switch case value "110". |
|
(19) Event switch: |
Switch case value "110". |
|
(29) Event switch: |
Switch case value "111". |
70 switch (ch) {
|
(3) Event switch_case: |
Reached case "105". |
71 case 'i':
72 input_file_name = optarg;
|
(4) Event break: |
Breaking from switch. |
73 break;
|
(10) Event switch_case: |
Reached case "110". |
|
(20) Event switch_case: |
Reached case "110". |
74 case 'n':
75 no_bytes = strtol(optarg, &ep, 10);
|
(11) Event cond_false: |
Condition "no_bytes < 0", taking false branch. |
|
(12) Event cond_false: |
Condition "*ep != 0", taking false branch. |
|
(21) Event cond_false: |
Condition "no_bytes < 0", taking false branch. |
|
(22) Event cond_false: |
Condition "*ep != 0", taking false branch. |
76 if (no_bytes < 0 || *ep != '\0') {
77 warnx("illegal number -- %s", argv[2]);
78 usage();
|
(13) Event if_end: |
End of if statement. |
|
(23) Event if_end: |
End of if statement. |
79 }
|
(14) Event break: |
Breaking from switch. |
|
(24) Event break: |
Breaking from switch. |
80 break;
|
(30) Event switch_case: |
Reached case "111". |
81 case 'o':
82 output_file_name = optarg;
|
(31) Event break: |
Breaking from switch. |
83 break;
84 case 'h':
85 case '?':
86 default:
87 usage();
88 /* NOTREACHED */
|
(5) Event switch_end: |
Reached end of switch. |
|
(15) Event switch_end: |
Reached end of switch. |
|
(25) Event switch_end: |
Reached end of switch. |
|
(32) Event switch_end: |
Reached end of switch. |
89 }
|
(6) Event loop: |
Jumping back to the beginning of the loop. |
|
(16) Event loop: |
Jumping back to the beginning of the loop. |
|
(26) Event loop: |
Jumping back to the beginning of the loop. |
|
(33) Event loop: |
Jumping back to the beginning of the loop. |
|
(36) Event loop_end: |
Reached end of loop. |
90 }
91
|
(37) Event cond_true: |
Condition "strcmp(input_file_name, "-") == 0", taking true branch. |
92 if (strcmp(input_file_name, "-") == 0) {
93 fi = stdin;
|
(38) Event if_fallthrough: |
Falling through to end of if statement. |
94 } else {
95 fi = fopen(input_file_name, "rb");
96 if (fi == NULL) {
97 err(1, "%s", input_file_name);
98 }
|
(39) Event if_end: |
End of if statement. |
99 }
100
101 /*
102 * Open and fully read input file
103 */
104 data = NULL;
105 data_size = 0;
|
(40) Event assignment: |
Assigning: "data_pos" = "0UL". |
| Also see events: |
[divide_by_zero] |
106 data_pos = 0;
|
(41) Event cond_false: |
Condition "(input_data_size = fread(buf, 1, 1024UL /* sizeof (buf) */, fi)) != 0", taking false branch. |
107 while ((input_data_size = fread(buf, 1, sizeof(buf), fi)) != 0) {
108 if (data_pos + input_data_size >= data_size) {
109 data_size = (data_size + input_data_size) * 2;
110 assert((data = realloc(data, data_size)) != NULL);
111 }
112 memcpy(data + data_pos, buf, input_data_size);
113 data_pos += input_data_size;
|
(42) Event loop_end: |
Reached end of loop. |
114 }
115 fclose(fi);
116
117 /*
118 * Change bytes
119 */
120 init_rand();
121
|
(43) Event cond_true: |
Condition "i < no_bytes", taking true branch. |
122 for (i = 0; i < no_bytes; i++) {
|
(44) Event divide_by_zero: |
In expression "rand() % data_pos", modulo by expression "data_pos" which may be zero has undefined behavior. |
| Also see events: |
[assignment] |
123 data[rand() % data_pos] = rand();
124 }
125
126 /*
127 * Fully write ouput file
128 */
129 if (strcmp(output_file_name, "-") == 0) {
130 fo = stdout;
131 } else {
132 fo = fopen(output_file_name, "wb");
133 if (fo == NULL) {
134 err(1, "%s", output_file_name);
135 }
136 }
137 assert(fwrite(data, 1, data_pos, fo) == data_pos);
138 fclose(fo);
139
140 free(data);
141
142 return (0);
143 }
144