File: pgrep.c

package info (click to toggle)
pcre 1.09-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 456 kB
  • ctags: 291
  • sloc: ansic: 3,710; makefile: 149; perl: 103; sh: 5
file content (220 lines) | stat: -rw-r--r-- 4,756 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*************************************************
*               PCRE grep program                *
*************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "pcre.h"


#define FALSE 0
#define TRUE 1

typedef int BOOL;



/*************************************************
*               Global variables                 *
*************************************************/

static pcre *pattern;
static pcre_extra *hints;

static BOOL count_only = FALSE;
static BOOL filenames_only = FALSE;
static BOOL invert = FALSE;
static BOOL number = FALSE;
static BOOL silent = FALSE;
static BOOL whole_lines = FALSE;



#ifdef STRERROR_FROM_ERRLIST
/*************************************************
*     Provide strerror() for non-ANSI libraries  *
*************************************************/

/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror()
in their libraries, but can provide the same facility by this simple
alternative function. */

extern int   sys_nerr;
extern char *sys_errlist[];

char *
strerror(int n)
{
if (n < 0 || n >= sys_nerr) return "unknown error number";
return sys_errlist[n];
}
#endif /* STRERROR_FROM_ERRLIST */



/*************************************************
*              Grep an individual file           *
*************************************************/

static int
pgrep(FILE *in, char *name)
{
int rc = 1;
int linenumber = 0;
int count = 0;
int offsets[2];
char buffer[BUFSIZ];

while (fgets(buffer, sizeof(buffer), in) != NULL)
  {
  BOOL match;
  int length = (int)strlen(buffer);
  if (length > 0 && buffer[length-1] == '\n') buffer[--length] = 0;
  linenumber++;

  match = pcre_exec(pattern, hints, buffer, length, 0, offsets, 2) >= 0;
  if (match && whole_lines && offsets[1] != length) match = FALSE;

  if (match != invert)
    {
    if (count_only) count++;

    else if (filenames_only)
      {
      fprintf(stdout, "%s\n", (name == NULL)? "<stdin>" : name);
      return 0;
      }

    else if (silent) return 0;

    else
      {
      if (name != NULL) fprintf(stdout, "%s:", name);
      if (number) fprintf(stdout, "%d:", linenumber);
      fprintf(stdout, "%s\n", buffer);
      }

    rc = 0;
    }
  }

if (count_only)
  {
  if (name != NULL) fprintf(stdout, "%s:", name);
  fprintf(stdout, "%d\n", count);
  }

return rc;
}




/*************************************************
*                Usage function                  *
*************************************************/

static int
usage(int rc)
{
fprintf(stderr, "Usage: pgrep [-chilnsvx] pattern [file] ...\n");
return rc;
}




/*************************************************
*                Main program                    *
*************************************************/

int
main(int argc, char **argv)
{
int i;
int rc = 1;
int options = 0;
int errptr;
const char *error;
BOOL filenames = TRUE;

/* Process the options */

for (i = 1; i < argc; i++)
  {
  char *s;
  if (argv[i][0] != '-') break;
  s = argv[i] + 1;
  while (*s != 0)
    {
    switch (*s++)
      {
      case 'c': count_only = TRUE; break;
      case 'h': filenames = FALSE; break;
      case 'i': options |= PCRE_CASELESS; break;
      case 'l': filenames_only = TRUE;
      case 'n': number = TRUE; break;
      case 's': silent = TRUE; break;
      case 'v': invert = TRUE; break;
      case 'x': whole_lines = TRUE; options |= PCRE_ANCHORED; break;
      default:
      fprintf(stderr, "pgrep: unknown option %c\n", s[-1]);
      return usage(2);
      }
    }
  }

/* There must be at least a regexp argument */

if (i >= argc) return usage(0);

/* Compile the regular expression. */

pattern = pcre_compile(argv[i++], options, &error, &errptr);
if (pattern == NULL)
  {
  fprintf(stderr, "pgrep: error in regex at offset %d: %s\n", errptr, error);
  return 2;
  }

/* Study the regular expression, as we will be running it may times */

hints = pcre_study(pattern, 0, &error);
if (error != NULL)
  {
  fprintf(stderr, "pgrep: error while studing regex: %s\n", error);
  return 2;
  }

/* If there are no further arguments, do the business on stdin and exit */

if (i >= argc) return pgrep(stdin, NULL);

/* Otherwise, work through the remaining arguments as files. If there is only
one, don't give its name on the output. */

if (i == argc - 1) filenames = FALSE;
if (filenames_only) filenames = TRUE;

for (; i < argc; i++)
  {
  FILE *in = fopen(argv[i], "r");
  if (in == NULL)
    {
    fprintf(stderr, "%s: failed to open: %s\n", argv[i], strerror(errno));
    rc = 2;
    }
  else
    {
    int frc = pgrep(in, filenames? argv[i] : NULL);
    if (frc == 0 && rc == 1) rc = 0;
    fclose(in);
    }
  }

return rc;
}

/* End */