fixed grep
This commit is contained in:
parent
c4f4bf3ca3
commit
ec265a4c23
9 changed files with 148 additions and 146 deletions
30
libmu/replace.c
Normal file
30
libmu/replace.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
char *mu_replace(const char *prog_name, const char *src, const char *str, const char *fmt) {
|
||||
size_t src_len = strlen(src);
|
||||
size_t str_len = strlen(str);
|
||||
size_t fmt_len = strlen(fmt);
|
||||
|
||||
char *buf = malloc(src_len - fmt_len + str_len + 1);
|
||||
if (buf == NULL) {
|
||||
if (prog_name)
|
||||
fprintf(stderr, "%s: malloc failed: %s\n", prog_name, strerror(errno));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(buf, src);
|
||||
|
||||
/* REPLACE */
|
||||
char *ptr = strstr(buf, fmt);
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
|
||||
memmove(ptr + str_len, ptr + fmt_len, strlen(ptr + fmt_len) + 1);
|
||||
strncpy(ptr, str, str_len);
|
||||
return buf;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue