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 */
30 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <sys/types.h>
45 #define P_locale "/usr/lib/locale/"
46 #define L_locale (sizeof (P_locale))
47 #define MESSAGES "/LC_MESSAGES/"
50 /* External functions */
54 extern char *strecpy();
55 extern char *strrchr();
56 extern char *strchr();
59 /* External variables */
64 /* Internal functions */
67 static void prnt_str();
69 static void find_msgs();
70 static char *syserr();
72 /* Internal variables */
74 static char *cmdname
; /* points to the name of the command */
75 static int lflg
; /* set if locale is specified on command line */
76 static int mflg
; /* set if message file is specified on */
78 static char locale
[15]; /* save name of current locale */
79 static char *msgfile
; /* points to the argument immediately */
80 /* following the m option */
81 static char *text
; /* pointer to search pattern */
82 static int textflg
; /* set if text pattern is specified on */
84 static int sflg
; /* set if the s option is specified */
85 static char *fname
; /* points to message file name */
86 static int msgnum
; /* message number */
89 main(int argc
, char **argv
)
104 /* find last level of path in command name */
105 if (cmdname
= strrchr(*argv
, '/'))
110 /* parse command line */
111 while ((ch
= getopt(argc
, argv
, "sl:m:")) != -1)
115 (void) strcpy(locale
, optarg
);
127 if (mflg
&& optind
< argc
) {
128 text
= argv
[optind
++];
134 /* create full path name to message files */
136 (void) strcpy(locale
, setlocale(LC_MESSAGES
, ""));
137 (void) strcpy(pathname
, P_locale
);
138 (void) strcpy(&pathname
[L_locale
- 1], locale
);
139 (void) strcat(pathname
, MESSAGES
);
140 len
= strlen(pathname
);
143 /* compile regular expression */
144 if (compile(text
, &ebuf
[0], &ebuf
[ESIZE
]) == NULL
) {
145 (void) fprintf(stderr
,
146 "%s: ERROR: regular expression compile failed\n",
152 /* access message files */
154 end
= msgfile
+ strlen(msgfile
) + 1;
155 if (*msgfile
== ',' || *(end
- 2) == ',')
157 while ((fname
= strtok(msgfile
, ",\0")) != NULL
) {
158 if (strchr(fname
, '/') != NULL
) {
165 msgfile
= msgfile
+ strlen(fname
) + 1;
166 if ((addr
= attach(cp
, len1
, &fd
, &size
)) == -1) {
167 (void) fprintf(stderr
,
168 "%s: ERROR: failed to access message file '%s'\n", cmdname
, cp
);
174 find_msgs(addr
, ebuf
);
175 (void) munmap((caddr_t
)addr
, size
);
180 } else { /* end if (mflg) */
181 if ((dirp
= opendir(pathname
)) == NULL
) {
182 (void) fprintf(stderr
, "%s: ERROR: %s %s\n",
183 cmdname
, pathname
, syserr());
186 while ((dp
= readdir(dirp
)) != NULL
) {
187 if (dp
->d_name
[0] == '.')
190 if ((addr
= attach(pathname
, len
, &fd
, &size
)) == -1) {
191 (void) fprintf(stderr
,
192 "%s: ERROR: failed to access message file '%s'\n", cmdname
, pathname
);
195 find_msgs(addr
, ebuf
);
196 (void) munmap((caddr_t
)addr
, size
);
199 (void) closedir(dirp
);
205 /* print usage message */
209 (void) fprintf(stderr
,
210 "usage: srchtxt [-s]\n srchtxt [-s] -l locale\n"
211 " srchtxt [-s] [-l locale] [-m msgfile,...] [text]\n");
216 * print string - non-graphic characters are printed as alphabetic
223 char outstring
[1024];
225 (void) strecpy(outstring
, instring
, NULL
);
227 (void) fprintf(stdout
, "%s\n", outstring
);
229 (void) fprintf(stdout
, "<%s:%d>%s\n", fname
, msgnum
, outstring
);
232 /* mmap a message file to the address space */
234 attach(path
, len
, fdescr
, size
)
244 (void) strcpy(&path
[len
], fname
);
245 if ((fd
= open(path
, O_RDONLY
)) != -1 &&
246 fstat(fd
, &sb
) != -1 &&
247 (addr
= mmap(NULL
, sb
.st_size
,
248 PROT_READ
, MAP_SHARED
,
249 fd
, 0)) != (caddr_t
)-1) {
261 /* find messages in message files */
263 find_msgs(addr
, regexpr
)
270 num_msgs
= *(int *)addr
;
271 for (msgnum
= 1; msgnum
<= num_msgs
; msgnum
++) {
272 msg
= (char *)(*(int *)(addr
+ sizeof (int) * msgnum
) + addr
);
274 if (step(msg
, regexpr
)) {
284 /* print description of error */
288 return (strerror(errno
));