forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringreplace.c
34 lines (27 loc) · 941 Bytes
/
stringreplace.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
// This file is a part of Julia. License is MIT: http://julialang.org/license
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char ** argv ) {
if( argc < 5 ) {
printf("Usage:\n");
printf(" %s <hex offset> <string to write> <maxlen> <file>\n", argv[0] );
return -1;
}
unsigned long offset = strtoul(argv[1], NULL, 16);
char * replacement = argv[2];
unsigned long maxlen = strtoul(argv[3], NULL, 10);
FILE * f = fopen( argv[4], "r+" );
if( !f ) {
printf( "ERROR: Could not open %s for writing!\n", argv[4] );
return -1;
}
if( strlen(replacement) > maxlen ) {
printf( "ERROR: Replacement string length (%lu) is greater than maxlen! (%lu)\n", strlen(replacement), maxlen );
return -1;
}
fseek( f, offset, SEEK_SET );
fwrite( replacement, strlen(replacement)+1, 1, f );
fclose( f );
return 0;
}