]> git.saurik.com Git - apple/boot.git/blob - i386/boot1/replace.c
ac8e2886a70efa9e4b75060cc9a7f71092d549c8
[apple/boot.git] / i386 / boot1 / replace.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright 1993 NeXT, Inc. All rights reserved. */
26
27 /* Replace characters in a file. */
28
29 #import <stdio.h>
30 #import <mach/mach.h>
31 #import <sys/types.h>
32 #import <sys/stat.h>
33 #import <sys/file.h>
34
35 void usage(void)
36 {
37 fprintf(stderr,"Usage: yuck <infile> <outfile> <oldstring> <newstring>\n");
38 exit(1);
39 }
40
41 char *strnstr(char *s1, char *s2, int len)
42 {
43 register char c1;
44 register const char c2 = *s2;
45
46 while (len--) {
47 c1 = *s1++;
48 if (c1 == c2) {
49 register const char *p1, *p2;
50
51 p1 = s1;
52 p2 = &s2[1];
53 while (*p1++ == (c1 = *p2++) && c1)
54 continue;
55 if (c1 == '\0') return ((char *)s1) - 1;
56 }
57 }
58 return NULL;
59 }
60
61 char *
62 strFromQuotedStr(char *oldstr)
63 {
64 char newstr[1024], *p;
65 char c;
66
67 p = newstr;
68 while (*oldstr) {
69 switch (c = *oldstr++) {
70 case '\\':
71 switch(c = *oldstr++) {
72 case 'r':
73 *p++ = '\r';
74 break;
75 case 'n':
76 *p++ = '\n';
77 break;
78 case 't':
79 *p++ = '\t';
80 break;
81 default:
82 *p++ = c;
83 break;
84 }
85 break;
86 default:
87 *p++ = c;
88 break;
89 }
90 }
91 *p = '\0';
92 p = (char *)malloc(strlen(newstr) + 1);
93 strcpy(p, newstr);
94 return p;
95 }
96
97 main(int argc, char **argv)
98 {
99 int c, fd, ofd, filesize;
100 kern_return_t r;
101 char *infile, *outfile, *memfile, *oldstring, *os, *newstring;
102 struct stat statbuf;
103
104 if (argc != 5)
105 usage();
106
107 infile = argv[1];
108 outfile = argv[2];
109 fd = open(infile, O_RDONLY);
110 if (fd < 0) {
111 perror("open infile");
112 exit(1);
113 }
114 if (fstat(fd, &statbuf) < 0) {
115 perror("stat infile");
116 exit(1);
117 }
118 ofd = open(outfile, O_TRUNC|O_RDWR|O_CREAT, 0644);
119 if (ofd < 0) {
120 perror("open outfile");
121 exit(1);
122 }
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");
128 }
129 r = map_fd(fd, (vm_offset_t)0, (vm_offset_t *)&memfile, TRUE,
130 (vm_size_t)filesize);
131
132 if (r != KERN_SUCCESS) {
133 mach_error("Error calling map_fd()", r);
134 exit(1);
135 } else {
136 os = (char *)strnstr(memfile, oldstring, filesize);
137 if (os == NULL) {
138 fprintf(stderr, "String not found\n");
139 exit(1);
140 }
141 while (*newstring)
142 *os++ = *newstring++;
143 *os++ = *newstring++;
144 lseek(fd, 0, 0);
145 c = write(ofd, memfile, filesize);
146 if (c < filesize) {
147 perror("write outfile");
148 exit(2);
149 }
150 exit(0);
151 }
152 }