4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
37 #include <sys/types.h>
43 #define P_locale "/usr/lib/locale/"
44 #define L_locale (sizeof (P_locale))
45 #define MESSAGES "/LC_MESSAGES/"
48 /* External functions */
52 extern char *strecpy();
53 extern char *strrchr();
54 extern char *strchr();
57 /* External variables */
62 /* Internal functions */
65 static void prnt_str();
67 static void find_msgs();
68 static char *syserr();
70 /* Internal variables */
72 static char *cmdname
; /* points to the name of the command */
73 static int lflg
; /* set if locale is specified on command line */
74 static int mflg
; /* set if message file is specified on */
76 static char locale
[15]; /* save name of current locale */
77 static char *msgfile
; /* points to the argument immediately */
78 /* following the m option */
79 static char *text
; /* pointer to search pattern */
80 static int textflg
; /* set if text pattern is specified on */
82 static int sflg
; /* set if the s option is specified */
83 static char *fname
; /* points to message file name */
84 static int msgnum
; /* message number */
87 main(int argc
, char **argv
)
102 /* find last level of path in command name */
103 if (cmdname
= strrchr(*argv
, '/'))
108 /* parse command line */
109 while ((ch
= getopt(argc
, argv
, "sl:m:")) != -1)
113 (void) strcpy(locale
, optarg
);
125 if (mflg
&& optind
< argc
) {
126 text
= argv
[optind
++];
132 /* create full path name to message files */
134 (void) strcpy(locale
, setlocale(LC_MESSAGES
, ""));
135 (void) strcpy(pathname
, P_locale
);
136 (void) strcpy(&pathname
[L_locale
- 1], locale
);
137 (void) strcat(pathname
, MESSAGES
);
138 len
= strlen(pathname
);
141 /* compile regular expression */
142 if (compile(text
, &ebuf
[0], &ebuf
[ESIZE
]) == (char *)NULL
) {
143 (void) fprintf(stderr
,
144 "%s: ERROR: regular expression compile failed\n",
150 /* access message files */
152 end
= msgfile
+ strlen(msgfile
) + 1;
153 if (*msgfile
== ',' || *(end
- 2) == ',')
155 while ((fname
= strtok(msgfile
, ",\0")) != NULL
) {
156 if (strchr(fname
, '/') != (char *)NULL
) {
163 msgfile
= msgfile
+ strlen(fname
) + 1;
164 if ((addr
= attach(cp
, len1
, &fd
, &size
)) == -1) {
165 (void) fprintf(stderr
,
166 "%s: ERROR: failed to access message file '%s'\n", cmdname
, cp
);
172 find_msgs(addr
, ebuf
);
173 (void) munmap((caddr_t
)addr
, size
);
178 } else { /* end if (mflg) */
179 if ((dirp
= opendir(pathname
)) == NULL
) {
180 (void) fprintf(stderr
, "%s: ERROR: %s %s\n",
181 cmdname
, pathname
, syserr());
184 while ((dp
= readdir(dirp
)) != NULL
) {
185 if (dp
->d_name
[0] == '.')
188 if ((addr
= attach(pathname
, len
, &fd
, &size
)) == -1) {
189 (void) fprintf(stderr
,
190 "%s: ERROR: failed to access message file '%s'\n", cmdname
, pathname
);
193 find_msgs(addr
, ebuf
);
194 (void) munmap((caddr_t
)addr
, size
);
197 (void) closedir(dirp
);
203 /* print usage message */
207 (void) fprintf(stderr
,
208 "usage: srchtxt [-s]\n srchtxt [-s] -l locale\n"
209 " srchtxt [-s] [-l locale] [-m msgfile,...] [text]\n");
214 * print string - non-graphic characters are printed as alphabetic
221 char outstring
[1024];
223 (void) strecpy(outstring
, instring
, NULL
);
225 (void) fprintf(stdout
, "%s\n", outstring
);
227 (void) fprintf(stdout
, "<%s:%d>%s\n", fname
, msgnum
, outstring
);
230 /* mmap a message file to the address space */
232 attach(path
, len
, fdescr
, size
)
242 (void) strcpy(&path
[len
], fname
);
243 if ((fd
= open(path
, O_RDONLY
)) != -1 &&
244 fstat(fd
, &sb
) != -1 &&
245 (addr
= mmap(0, sb
.st_size
,
246 PROT_READ
, MAP_SHARED
,
247 fd
, 0)) != (caddr_t
)-1) {
259 /* find messages in message files */
261 find_msgs(addr
, regexpr
)
268 num_msgs
= *(int *)addr
;
269 for (msgnum
= 1; msgnum
<= num_msgs
; msgnum
++) {
270 msg
= (char *)(*(int *)(addr
+ sizeof (int) * msgnum
) + addr
);
272 if (step(msg
, regexpr
))
280 /* print description of error */
284 return (strerror(errno
));