cronie/anacron/matchrx.c

93 lines
2.6 KiB
C
Raw Normal View History

2017-05-08 15:30:03 +02:00
/*
Anacron - run commands periodically
Copyright (C) 1998 Itai Tzur <itzur@actcom.co.il>
Copyright (C) 1999 Sean 'Shaleh' Perry <shaleh@debian.org>
2019-08-06 18:08:05 +02:00
2017-05-08 15:30:03 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
2019-08-06 18:08:05 +02:00
2017-05-08 15:30:03 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2019-08-06 18:08:05 +02:00
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2017-05-08 15:30:03 +02:00
The GNU General Public License can also be found in the file
`COPYING' that comes with the Anacron source distribution.
*/
#include <stdio.h>
#include <regex.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "matchrx.h"
int
2019-08-06 18:08:05 +02:00
match_rx(const char *rx, char *string, unsigned int n_sub, /* char **substrings */...)
2017-05-08 15:30:03 +02:00
/* Return 1 if the regular expression "*rx" matches the string "*string",
* 0 if not, -1 on error.
* "Extended" regular expressions are used.
* Additionally, there should be "n_sub" "substrings" arguments. These,
* if not NULL, and if the match succeeds are set to point to the
* corresponding substrings of the regexp.
* The original string is changed, and the substrings must not overlap,
* or even be directly adjacent.
* This is not the most efficient, or elegant way of doing this.
*/
{
2021-08-09 15:08:54 +02:00
int r;
unsigned int n;
2017-05-08 15:30:03 +02:00
regex_t crx;
va_list va;
char **substring;
regmatch_t *sub_offsets;
2021-08-09 15:08:54 +02:00
2017-05-08 15:30:03 +02:00
sub_offsets = malloc(sizeof(regmatch_t) * (n_sub + 1));
if (sub_offsets == NULL)
return -1;
memset(sub_offsets, 0, sizeof(regmatch_t) * (n_sub + 1));
if (regcomp(&crx, rx, REG_EXTENDED)) {
free(sub_offsets);
return -1;
}
r = regexec(&crx, string, n_sub + 1, sub_offsets, 0);
if (r != 0 && r != REG_NOMATCH) {
free(sub_offsets);
return -1;
}
regfree(&crx);
if (r == REG_NOMATCH) {
free(sub_offsets);
return 0;
}
va_start(va, n_sub);
n = 1;
while (n <= n_sub)
{
substring = va_arg(va, char**);
if (substring != NULL)
{
if (sub_offsets[n].rm_so == -1) {
va_end(va);
free(sub_offsets);
return - 1;
}
*substring = string + sub_offsets[n].rm_so;
*(string + sub_offsets[n].rm_eo) = 0;
}
n++;
}
va_end(va);
free(sub_offsets);
return 1;
}