1
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
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.
13 *
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]
19 *
20 * CDDL HEADER END
21 */
22 /* Copyright (c) 1988 AT&T */
23 /* All Rights Reserved */
24 /*
25 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28 /*
29 * from what.c 1.11 06/12/12
30 */
31
32 /* from OpenSolaris "what.c" */
33
34 /*
35 * Portions Copyright (c) 2006 Gunnar Ritter, Freiburg i. Br., Germany
36 *
37 * Sccsid @(#)what.c 1.12 (gritter) 4/14/07
38 */
39 /* from OpenSolaris "sccs:cmd/what.c" */
40
41 #if __GNUC__ >= 3 && __GNUC_MINOR__ >= 4 || __GNUC__ >= 4
42 #define USED __attribute__ ((used))
43 #elif defined __GNUC__
44 #define USED __attribute__ ((unused))
45 #else
46 #define USED
47 #endif
48 static const char sccsid[] USED = "@(#)what.sl 1.12 (gritter) 4/14/07";
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #define MINUS '-'
56 #define MINUS_S "-s"
57 #undef TRUE
58 #define TRUE 1
59 #undef FALSE
60 #define FALSE 0
61
62
63 static int found = FALSE;
64 static int silent = FALSE;
65
66 static char pattern[] = "@(#)";
67
68 static void dowhat(FILE *);
69 static int trypat(FILE *,char *);
70
71
72 int
main(int argc,register char ** argv)73 main(int argc, register char **argv)
74 {
75 register int i;
76 register FILE *iop;
77 int current_optind, c;
78
79 if (argc < 2)
80 dowhat(stdin);
81 else {
82
83 current_optind = 1;
84 optind = 1;
85 opterr = 0;
86 i = 1;
87 /*CONSTCOND*/
88 while (1) {
89 if(current_optind < optind) {
90 current_optind = optind;
91 argv[i] = 0;
92 if (optind > i+1 ) {
93 argv[i+1] = NULL;
94 }
95 }
96 i = current_optind;
97 c = getopt(argc, argv, "-s");
98
99 /* this takes care of options given after
100 ** file names.
101 */
102 if((c == EOF)) {
103 if (optind < argc) {
104 /* if it's due to -- then break; */
105 if(argv[i][0] == '-' &&
106 argv[i][1] == '-') {
107 argv[i] = 0;
108 break;
109 }
110 optind++;
111 current_optind = optind;
112 continue;
113 } else {
114 break;
115 }
116 }
117 switch (c) {
118 case 's' :
119 silent = TRUE;
120 break;
121 default :
122 fprintf(stderr,
123 "Usage: what [ -s ] filename...\n");
124 exit(2);
125 }
126 }
127 for(i=1;(i<argc );i++) {
128 if(!argv[i]) {
129 continue;
130 }
131 if ((iop = fopen(argv[i],"r")) == NULL)
132 fprintf(stderr,"can't open %s (26)\n",
133 argv[i]);
134 else {
135 printf("%s:\n",argv[i]);
136 dowhat(iop);
137 }
138 }
139 }
140 return (!found); /* shell return code */
141 }
142
143
144 static void
dowhat(register FILE * iop)145 dowhat(register FILE *iop)
146 {
147 register int c;
148
149 while ((c = getc(iop)) != EOF) {
150 if (c == pattern[0])
151 if(trypat(iop, &pattern[1]) && silent) break;
152 }
153 fclose(iop);
154 }
155
156
157 static int
trypat(register FILE * iop,register char * pat)158 trypat(register FILE *iop,register char *pat)
159 {
160 register int c = 0;
161
162 for (; *pat; pat++)
163 if ((c = getc(iop)) != *pat)
164 break;
165 if (!*pat) {
166 found = TRUE;
167 putchar('\t');
168 while ((c = getc(iop)) != EOF && c &&
169 !strchr("\"\\>\n", c&0377))
170 putchar(c);
171 putchar('\n');
172 if(silent)
173 return(TRUE);
174 }
175 else if (c != EOF)
176 ungetc(c, iop);
177 return(FALSE);
178 }
179