6 |
#include <string.h> |
#include <string.h> |
7 |
#include <stdlib.h> |
#include <stdlib.h> |
8 |
#include <errno.h> |
#include <errno.h> |
9 |
|
#include "config.h" |
10 |
#include "pcre.h" |
#include "pcre.h" |
11 |
|
|
|
|
|
12 |
#define FALSE 0 |
#define FALSE 0 |
13 |
#define TRUE 1 |
#define TRUE 1 |
14 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
#ifdef STRERROR_FROM_ERRLIST |
#ifndef HAVE_STRERROR |
36 |
/************************************************* |
/************************************************* |
37 |
* Provide strerror() for non-ANSI libraries * |
* Provide strerror() for non-ANSI libraries * |
38 |
*************************************************/ |
*************************************************/ |
50 |
if (n < 0 || n >= sys_nerr) return "unknown error number"; |
if (n < 0 || n >= sys_nerr) return "unknown error number"; |
51 |
return sys_errlist[n]; |
return sys_errlist[n]; |
52 |
} |
} |
53 |
#endif /* STRERROR_FROM_ERRLIST */ |
#endif /* HAVE_STRERROR */ |
54 |
|
|
55 |
|
|
56 |
|
|
64 |
int rc = 1; |
int rc = 1; |
65 |
int linenumber = 0; |
int linenumber = 0; |
66 |
int count = 0; |
int count = 0; |
67 |
int offsets[2]; |
int offsets[99]; |
68 |
char buffer[BUFSIZ]; |
char buffer[BUFSIZ]; |
69 |
|
|
70 |
while (fgets(buffer, sizeof(buffer), in) != NULL) |
while (fgets(buffer, sizeof(buffer), in) != NULL) |
74 |
if (length > 0 && buffer[length-1] == '\n') buffer[--length] = 0; |
if (length > 0 && buffer[length-1] == '\n') buffer[--length] = 0; |
75 |
linenumber++; |
linenumber++; |
76 |
|
|
77 |
match = pcre_exec(pattern, hints, buffer, length, 0, offsets, 2) >= 0; |
match = pcre_exec(pattern, hints, buffer, length, 0, 0, offsets, 99) >= 0; |
78 |
if (match && whole_lines && offsets[1] != length) match = FALSE; |
if (match && whole_lines && offsets[1] != length) match = FALSE; |
79 |
|
|
80 |
if (match != invert) |
if (match != invert) |
119 |
static int |
static int |
120 |
usage(int rc) |
usage(int rc) |
121 |
{ |
{ |
122 |
fprintf(stderr, "Usage: pgrep [-chilnsvx] pattern [file] ...\n"); |
fprintf(stderr, "Usage: pgrep [-Vchilnsvx] pattern [file] ...\n"); |
123 |
return rc; |
return rc; |
124 |
} |
} |
125 |
|
|
159 |
case 's': silent = TRUE; break; |
case 's': silent = TRUE; break; |
160 |
case 'v': invert = TRUE; break; |
case 'v': invert = TRUE; break; |
161 |
case 'x': whole_lines = TRUE; options |= PCRE_ANCHORED; break; |
case 'x': whole_lines = TRUE; options |= PCRE_ANCHORED; break; |
162 |
|
|
163 |
|
case 'V': |
164 |
|
fprintf(stderr, "PCRE version %s\n", pcre_version()); |
165 |
|
break; |
166 |
|
|
167 |
default: |
default: |
168 |
fprintf(stderr, "pgrep: unknown option %c\n", s[-1]); |
fprintf(stderr, "pgrep: unknown option %c\n", s[-1]); |
169 |
return usage(2); |
return usage(2); |
177 |
|
|
178 |
/* Compile the regular expression. */ |
/* Compile the regular expression. */ |
179 |
|
|
180 |
pattern = pcre_compile(argv[i++], options, &error, &errptr); |
pattern = pcre_compile(argv[i++], options, &error, &errptr, NULL); |
181 |
if (pattern == NULL) |
if (pattern == NULL) |
182 |
{ |
{ |
183 |
fprintf(stderr, "pgrep: error in regex at offset %d: %s\n", errptr, error); |
fprintf(stderr, "pgrep: error in regex at offset %d: %s\n", errptr, error); |