Commit | Line | Data |
---|---|---|
d49d145d KB |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Cimarron D. Taylor of the University of California, Berkeley. | |
7 | * | |
8 | * %sccs.include.redist.c% | |
9 | */ | |
10 | ||
11 | #ifndef lint | |
91f10ace | 12 | static char sccsid[] = "@(#)misc.c 5.6 (Berkeley) %G%"; |
d49d145d KB |
13 | #endif /* not lint */ |
14 | ||
15 | #include <sys/types.h> | |
16 | #include <sys/stat.h> | |
148184f1 | 17 | #include <sys/errno.h> |
d49d145d | 18 | #include <stdio.h> |
e0482c58 KB |
19 | #include <stdlib.h> |
20 | #include <string.h> | |
91f10ace | 21 | #include "find.h" |
d49d145d | 22 | |
d49d145d | 23 | /* |
e0482c58 | 24 | * brace_subst -- |
d49d145d | 25 | * Replace occurrences of {} in s1 with s2 and return the result string. |
d49d145d | 26 | */ |
e0482c58 KB |
27 | void |
28 | brace_subst(orig, store, path, len) | |
d49d145d KB |
29 | char *orig, **store, *path; |
30 | int len; | |
31 | { | |
32 | register int plen; | |
33 | register char ch, *p; | |
d49d145d KB |
34 | |
35 | plen = strlen(path); | |
36 | for (p = *store; ch = *orig; ++orig) | |
37 | if (ch == '{' && orig[1] == '}') { | |
38 | while ((p - *store) + plen > len) | |
39 | if (!(*store = realloc(*store, len *= 2))) { | |
40 | (void)fprintf(stderr, | |
41 | "find: %s.\n", strerror(errno)); | |
42 | exit(1); | |
43 | } | |
44 | bcopy(path, p, plen); | |
45 | p += plen; | |
46 | ++orig; | |
47 | } else | |
48 | *p++ = ch; | |
49 | *p = '\0'; | |
50 | } | |
51 | ||
52 | /* | |
e0482c58 | 53 | * queryuser -- |
d49d145d KB |
54 | * print a message to standard error and then read input from standard |
55 | * input. If the input is 'y' then 1 is returned. | |
56 | */ | |
e0482c58 | 57 | queryuser(argv) |
d49d145d KB |
58 | register char **argv; |
59 | { | |
e0482c58 | 60 | int ch, first, nl; |
d49d145d KB |
61 | |
62 | (void)fprintf(stderr, "\"%s", *argv); | |
63 | while (*++argv) | |
64 | (void)fprintf(stderr, " %s", *argv); | |
65 | (void)fprintf(stderr, "\"? "); | |
66 | (void)fflush(stderr); | |
e0482c58 KB |
67 | |
68 | first = ch = getchar(); | |
69 | for (nl = 0;;) { | |
70 | if (ch == '\n') { | |
71 | nl = 1; | |
72 | break; | |
73 | } | |
74 | if (ch == EOF) | |
75 | break; | |
76 | ch = getchar(); | |
77 | } | |
78 | ||
79 | if (!nl) { | |
80 | (void)fprintf(stderr, "\n"); | |
81 | (void)fflush(stderr); | |
82 | } | |
83 | return(first == 'y'); | |
d49d145d KB |
84 | } |
85 | ||
86 | /* | |
87 | * bad_arg -- | |
88 | * print out a bad argument message. | |
89 | */ | |
90 | void | |
91 | bad_arg(option, error) | |
92 | char *option, *error; | |
93 | { | |
94 | (void)fprintf(stderr, "find: %s: %s.\n", option, error); | |
95 | exit(1); | |
96 | } | |
97 | ||
98 | /* | |
99 | * emalloc -- | |
100 | * malloc with error checking. | |
101 | */ | |
e0482c58 | 102 | void * |
d49d145d KB |
103 | emalloc(len) |
104 | u_int len; | |
105 | { | |
e0482c58 | 106 | void *p; |
d49d145d KB |
107 | |
108 | if (!(p = malloc(len))) { | |
109 | (void)fprintf(stderr, "find: %s.\n", strerror(errno)); | |
110 | exit(1); | |
111 | } | |
112 | return(p); | |
113 | } | |
114 | ||
115 | usage() | |
116 | { | |
148184f1 | 117 | if (isdeprecated) |
202dd4ce KB |
118 | (void)fprintf(stderr, "usage: find path-list expression\n"); |
119 | else | |
120 | (void)fprintf(stderr, | |
585ba00a | 121 | "usage: find [-drsx] -f path ... expression\n"); |
d49d145d KB |
122 | exit(1); |
123 | } |