]>
Commit | Line | Data |
---|---|---|
14c7c974 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
4f6e3300 A |
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 | |
12 | * this file. | |
14c7c974 A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
4f6e3300 | 15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
14c7c974 A |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
4f6e3300 A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
14c7c974 A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* Copyright 1993 NeXT, Inc. All rights reserved. */ | |
25 | ||
26 | /* Replace characters in a file. */ | |
27 | ||
28 | #import <stdio.h> | |
29 | #import <mach/mach.h> | |
30 | #import <sys/types.h> | |
31 | #import <sys/stat.h> | |
32 | #import <sys/file.h> | |
33 | ||
34 | void usage(void) | |
35 | { | |
36 | fprintf(stderr,"Usage: yuck <infile> <outfile> <oldstring> <newstring>\n"); | |
37 | exit(1); | |
38 | } | |
39 | ||
40 | char *strnstr(char *s1, char *s2, int len) | |
41 | { | |
42 | register char c1; | |
43 | register const char c2 = *s2; | |
44 | ||
45 | while (len--) { | |
46 | c1 = *s1++; | |
47 | if (c1 == c2) { | |
48 | register const char *p1, *p2; | |
49 | ||
50 | p1 = s1; | |
51 | p2 = &s2[1]; | |
52 | while (*p1++ == (c1 = *p2++) && c1) | |
53 | continue; | |
54 | if (c1 == '\0') return ((char *)s1) - 1; | |
55 | } | |
56 | } | |
57 | return NULL; | |
58 | } | |
59 | ||
60 | char * | |
61 | strFromQuotedStr(char *oldstr) | |
62 | { | |
63 | char newstr[1024], *p; | |
64 | char c; | |
65 | ||
66 | p = newstr; | |
67 | while (*oldstr) { | |
68 | switch (c = *oldstr++) { | |
69 | case '\\': | |
70 | switch(c = *oldstr++) { | |
71 | case 'r': | |
72 | *p++ = '\r'; | |
73 | break; | |
74 | case 'n': | |
75 | *p++ = '\n'; | |
76 | break; | |
77 | case 't': | |
78 | *p++ = '\t'; | |
79 | break; | |
80 | default: | |
81 | *p++ = c; | |
82 | break; | |
83 | } | |
84 | break; | |
85 | default: | |
86 | *p++ = c; | |
87 | break; | |
88 | } | |
89 | } | |
90 | *p = '\0'; | |
91 | p = (char *)malloc(strlen(newstr) + 1); | |
92 | strcpy(p, newstr); | |
93 | return p; | |
94 | } | |
95 | ||
96 | main(int argc, char **argv) | |
97 | { | |
98 | int c, fd, ofd, filesize; | |
99 | kern_return_t r; | |
100 | char *infile, *outfile, *memfile, *oldstring, *os, *newstring; | |
101 | struct stat statbuf; | |
102 | ||
103 | if (argc != 5) | |
104 | usage(); | |
105 | ||
106 | infile = argv[1]; | |
107 | outfile = argv[2]; | |
108 | fd = open(infile, O_RDONLY); | |
109 | if (fd < 0) { | |
110 | perror("open infile"); | |
111 | exit(1); | |
112 | } | |
113 | if (fstat(fd, &statbuf) < 0) { | |
114 | perror("stat infile"); | |
115 | exit(1); | |
116 | } | |
117 | ofd = open(outfile, O_TRUNC|O_RDWR|O_CREAT, 0644); | |
118 | if (ofd < 0) { | |
119 | perror("open outfile"); | |
120 | exit(1); | |
121 | } | |
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"); | |
127 | } | |
128 | r = map_fd(fd, (vm_offset_t)0, (vm_offset_t *)&memfile, TRUE, | |
129 | (vm_size_t)filesize); | |
130 | ||
131 | if (r != KERN_SUCCESS) { | |
132 | mach_error("Error calling map_fd()", r); | |
133 | exit(1); | |
134 | } else { | |
135 | os = (char *)strnstr(memfile, oldstring, filesize); | |
136 | if (os == NULL) { | |
137 | fprintf(stderr, "String not found\n"); | |
138 | exit(1); | |
139 | } | |
140 | while (*newstring) | |
141 | *os++ = *newstring++; | |
142 | *os++ = *newstring++; | |
143 | lseek(fd, 0, 0); | |
144 | c = write(ofd, memfile, filesize); | |
145 | if (c < filesize) { | |
146 | perror("write outfile"); | |
147 | exit(2); | |
148 | } | |
149 | exit(0); | |
150 | } | |
151 | } |