]>
git.saurik.com Git - apple/boot.git/blob - i386/boot1/replace.c
ac8e2886a70efa9e4b75060cc9a7f71092d549c8
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright 1993 NeXT, Inc. All rights reserved. */
27 /* Replace characters in a file. */
37 fprintf(stderr
,"Usage: yuck <infile> <outfile> <oldstring> <newstring>\n");
41 char *strnstr(char *s1
, char *s2
, int len
)
44 register const char c2
= *s2
;
49 register const char *p1
, *p2
;
53 while (*p1
++ == (c1
= *p2
++) && c1
)
55 if (c1
== '\0') return ((char *)s1
) - 1;
62 strFromQuotedStr(char *oldstr
)
64 char newstr
[1024], *p
;
69 switch (c
= *oldstr
++) {
71 switch(c
= *oldstr
++) {
92 p
= (char *)malloc(strlen(newstr
) + 1);
97 main(int argc
, char **argv
)
99 int c
, fd
, ofd
, filesize
;
101 char *infile
, *outfile
, *memfile
, *oldstring
, *os
, *newstring
;
109 fd
= open(infile
, O_RDONLY
);
111 perror("open infile");
114 if (fstat(fd
, &statbuf
) < 0) {
115 perror("stat infile");
118 ofd
= open(outfile
, O_TRUNC
|O_RDWR
|O_CREAT
, 0644);
120 perror("open outfile");
123 filesize
= statbuf
.st_size
;
124 oldstring
= strFromQuotedStr(argv
[3]);
125 newstring
= strFromQuotedStr(argv
[4]);
126 if (strlen(newstring
) > strlen(oldstring
)) {
127 fprintf(stderr
, "Warning: new string is bigger than old string.\n");
129 r
= map_fd(fd
, (vm_offset_t
)0, (vm_offset_t
*)&memfile
, TRUE
,
130 (vm_size_t
)filesize
);
132 if (r
!= KERN_SUCCESS
) {
133 mach_error("Error calling map_fd()", r
);
136 os
= (char *)strnstr(memfile
, oldstring
, filesize
);
138 fprintf(stderr
, "String not found\n");
142 *os
++ = *newstring
++;
143 *os
++ = *newstring
++;
145 c
= write(ofd
, memfile
, filesize
);
147 perror("write outfile");