-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffwrite.c
91 lines (70 loc) · 1.59 KB
/
offwrite.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
/*
* This program writes size number of bytes
* at specified offset on specified file
*
* Author: Shylesh kumar <[email protected]>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int opt;
int offset;
int size;
int ret;
int fd;
char *filename;
char pumpstring[10000];
char sample[] = "This is the string to be pumped";
if (argc < 3) {
fprintf (stderr, " Usage: %s filename offset size\n", argv[0]);
exit (1);
}
/* while ((opt = getopt(argc, argv, "fOs")) != -1) {
switch (opt) {
case 'f':
filename = optarg;
break;
case 'O':
offset = atoi(optarg);
break;
case 's':
size = atoi(optarg);
break;
default:
fprintf (stderr, "Usage : %s [-f] file [-O] offset [-s size]",
argv[0]);
exit (EXIT_FAILURE);
}
} */
fd = open (argv[1], O_RDWR);
if (fd == -1 ) {
printf ( "open failed: %s\n", strerror(errno));
exit (EXIT_FAILURE);
}
/* If the size is specified then we have to pump the data */
// pumpstring = (char *) malloc (sizeof(atoi(argv[3])));
offset = atoi(argv[2]);
size = atoi(argv[3]);
while (strlen(pumpstring) < size) {
strcat (pumpstring, sample);
}
printf ("Final pump string: %s", pumpstring);
ret = lseek (fd, atoi(argv[2]), SEEK_SET);
if (ret == -1 ) {
printf ( "lseek failed:%s\n", strerror(errno));
exit (EXIT_FAILURE);
}
ret = write (fd, pumpstring, size);
if (ret == -1) {
printf("stderr", "write failed");
exit (EXIT_FAILURE);
}
}