⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcregrep.c

📁 这是国外的resip协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************               pcregrep program                 **************************************************//* This is a grep program that uses the PCRE regular expression library to doits pattern matching. On a Unix or Win32 system it can recurse intodirectories.           Copyright (c) 1997-2004 University of Cambridge-----------------------------------------------------------------------------Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:    * Redistributions of source code must retain the above copyright notice,      this list of conditions and the following disclaimer.    * Redistributions in binary form must reproduce the above copyright      notice, this list of conditions and the following disclaimer in the      documentation and/or other materials provided with the distribution.    * Neither the name of the University of Cambridge nor the names of its      contributors may be used to endorse or promote products derived from      this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSEARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BELIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER INCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.-----------------------------------------------------------------------------*/#include <ctype.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include "config.h"#include "pcre.h"#define FALSE 0#define TRUE 1typedef int BOOL;#define VERSION "3.0 14-Jan-2003"#define MAX_PATTERN_COUNT 100/**************************************************               Global variables                 **************************************************/static char *pattern_filename = NULL;static int  pattern_count = 0;static pcre **pattern_list;static pcre_extra **hints_list;static BOOL count_only = FALSE;static BOOL filenames = TRUE;static BOOL filenames_only = FALSE;static BOOL invert = FALSE;static BOOL number = FALSE;static BOOL recurse = FALSE;static BOOL silent = FALSE;static BOOL whole_lines = FALSE;/* Structure for options and list of them */typedef struct option_item {  int one_char;  const char *long_name;  const char *help_text;} option_item;static option_item optionlist[] = {  { -1,  "help",         "display this help and exit" },  { 'c', "count",        "print only a count of matching lines per FILE" },  { 'h', "no-filename",  "suppress the prefixing filename on output" },  { 'i', "ignore-case",  "ignore case distinctions" },  { 'l', "files-with-matches", "print only FILE names containing matches" },  { 'n', "line-number",  "print line number with output lines" },  { 'r', "recursive",    "recursively scan sub-directories" },  { 's', "no-messages",  "suppress error messages" },  { 'u', "utf-8",        "use UTF-8 mode" },  { 'V', "version",      "print version information and exit" },  { 'v', "invert-match", "select non-matching lines" },  { 'x', "line-regex",   "force PATTERN to match only whole lines" },  { 'x', "line-regexp",  "force PATTERN to match only whole lines" },  { 0,    NULL,           NULL }};/**************************************************       Functions for directory scanning         **************************************************//* These functions are defined so that they can be made system specific,although at present the only ones are for Unix, Win32, and for "no directoryrecursion support". *//************* Directory scanning in Unix ***********/#if IS_UNIX#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>typedef DIR directory_type;static intisdirectory(char *filename){struct stat statbuf;if (stat(filename, &statbuf) < 0)  return 0;        /* In the expectation that opening as a file will fail */return ((statbuf.st_mode & S_IFMT) == S_IFDIR)? '/' : 0;}static directory_type *opendirectory(char *filename){return opendir(filename);}static char *readdirectory(directory_type *dir){for (;;)  {  struct dirent *dent = readdir(dir);  if (dent == NULL) return NULL;  if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0)    return dent->d_name;  }return NULL;   /* Keep compiler happy; never executed */}static voidclosedirectory(directory_type *dir){closedir(dir);}/************* Directory scanning in Win32 ***********//* I (Philip Hazel) have no means of testing this code. It was contributed byLionel Fourquaux. */#elif HAVE_WIN32API#ifndef STRICT# define STRICT#endif#ifndef WIN32_LEAN_AND_MEAN# define WIN32_LEAN_AND_MEAN#endif#include <windows.h>typedef struct directory_type{HANDLE handle;BOOL first;WIN32_FIND_DATA data;} directory_type;intisdirectory(char *filename){DWORD attr = GetFileAttributes(filename);if (attr == INVALID_FILE_ATTRIBUTES)  return 0;return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) ? '/' : 0;}directory_type *opendirectory(char *filename){size_t len;char *pattern;directory_type *dir;DWORD err;len = strlen(filename);pattern = (char *) malloc(len + 3);dir = (directory_type *) malloc(sizeof(*dir));if ((pattern == NULL) || (dir == NULL))  {  fprintf(stderr, "pcregrep: malloc failed\n");  exit(2);  }memcpy(pattern, filename, len);memcpy(&(pattern[len]), "\\*", 3);dir->handle = FindFirstFile(pattern, &(dir->data));if (dir->handle != INVALID_HANDLE_VALUE)  {  free(pattern);  dir->first = TRUE;  return dir;  }err = GetLastError();free(pattern);free(dir);errno = (err == ERROR_ACCESS_DENIED) ? EACCES : ENOENT;return NULL;}char *readdirectory(directory_type *dir){for (;;)  {  if (!dir->first)    {    if (!FindNextFile(dir->handle, &(dir->data)))      return NULL;    }  else    {    dir->first = FALSE;    }  if (strcmp(dir->data.cFileName, ".") != 0 && strcmp(dir->data.cFileName, "..") != 0)    return dir->data.cFileName;  }#ifndef _MSC_VERreturn NULL;   /* Keep compiler happy; never executed */#endif}voidclosedirectory(directory_type *dir){FindClose(dir->handle);free(dir);}/************* Directory scanning when we can't do it ***********//* The type is void, and apart from isdirectory(), the functions do nothing. */#elsetypedef void directory_type;int isdirectory(char *filename) { return FALSE; }directory_type * opendirectory(char *filename) {}char *readdirectory(directory_type *dir) {}void closedirectory(directory_type *dir) {}#endif#if ! HAVE_STRERROR/**************************************************     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 simplealternative 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 /* HAVE_STRERROR *//**************************************************              Grep an individual file           **************************************************/static intpcregrep(FILE *in, char *name){int rc = 1;int linenumber = 0;int count = 0;int offsets[99];char buffer[BUFSIZ];while (fgets(buffer, sizeof(buffer), in) != NULL)  {  BOOL match = FALSE;  int i;  int length = (int)strlen(buffer);  if (length > 0 && buffer[length-1] == '\n') buffer[--length] = 0;  linenumber++;  for (i = 0; !match && i < pattern_count; i++)    {    match = pcre_exec(pattern_list[i], hints_list[i], buffer, length, 0, 0,      offsets, 99) >= 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)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -