]>
git.saurik.com Git - apple/boot.git/blob - i386/boot1/replace.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* Copyright 1993 NeXT, Inc. All rights reserved. */
26 /* Replace characters in a file. */
36 fprintf(stderr
,"Usage: yuck <infile> <outfile> <oldstring> <newstring>\n");
40 char *strnstr(char *s1
, char *s2
, int len
)
43 register const char c2
= *s2
;
48 register const char *p1
, *p2
;
52 while (*p1
++ == (c1
= *p2
++) && c1
)
54 if (c1
== '\0') return ((char *)s1
) - 1;
61 strFromQuotedStr(char *oldstr
)
63 char newstr
[1024], *p
;
68 switch (c
= *oldstr
++) {
70 switch(c
= *oldstr
++) {
91 p
= (char *)malloc(strlen(newstr
) + 1);
96 main(int argc
, char **argv
)
98 int c
, fd
, ofd
, filesize
;
100 char *infile
, *outfile
, *memfile
, *oldstring
, *os
, *newstring
;
108 fd
= open(infile
, O_RDONLY
);
110 perror("open infile");
113 if (fstat(fd
, &statbuf
) < 0) {
114 perror("stat infile");
117 ofd
= open(outfile
, O_TRUNC
|O_RDWR
|O_CREAT
, 0644);
119 perror("open outfile");
122 filesize
= statbuf
.st_size
;
123 oldstring
= strFromQuotedStr(argv
[3]);
124 newstring
= strFromQuotedStr(argv
[4]);
125 if (strlen(newstring
) > strlen(oldstring
)) {
126 fprintf(stderr
, "Warning: new string is bigger than old string.\n");
128 r
= map_fd(fd
, (vm_offset_t
)0, (vm_offset_t
*)&memfile
, TRUE
,
129 (vm_size_t
)filesize
);
131 if (r
!= KERN_SUCCESS
) {
132 mach_error("Error calling map_fd()", r
);
135 os
= (char *)strnstr(memfile
, oldstring
, filesize
);
137 fprintf(stderr
, "String not found\n");
141 *os
++ = *newstring
++;
142 *os
++ = *newstring
++;
144 c
= write(ofd
, memfile
, filesize
);
146 perror("write outfile");