2005-09-09 05:07:43 +08:00
|
|
|
/* Copyright (C) 2002, 2005 by Red Hat, Incorporated. All rights reserved.
|
2002-06-15 04:51:09 +08:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software
|
|
|
|
* is freely granted, provided that this notice is preserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <argz.h>
|
2007-04-05 04:49:07 +08:00
|
|
|
#define __need_ptrdiff_t
|
|
|
|
#include <stddef.h>
|
2002-06-15 04:51:09 +08:00
|
|
|
|
|
|
|
error_t
|
2017-12-04 11:43:30 +08:00
|
|
|
argz_insert (char **argz,
|
2017-12-04 09:31:41 +08:00
|
|
|
size_t *argz_len,
|
|
|
|
char *before,
|
2003-06-07 03:57:51 +08:00
|
|
|
const char *entry)
|
2002-06-15 04:51:09 +08:00
|
|
|
{
|
|
|
|
int len = 0;
|
2007-06-06 05:40:48 +08:00
|
|
|
ptrdiff_t delta;
|
2002-06-15 04:51:09 +08:00
|
|
|
|
|
|
|
if (before == NULL)
|
2005-09-09 05:07:43 +08:00
|
|
|
return argz_add(argz, argz_len, entry);
|
2002-06-15 04:51:09 +08:00
|
|
|
|
|
|
|
if (before < *argz || before >= *argz + *argz_len)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
while (before != *argz && before[-1])
|
|
|
|
before--;
|
|
|
|
|
2007-03-17 05:16:09 +08:00
|
|
|
/* delta will always be non-negative, and < *argz_len */
|
2007-06-06 05:40:48 +08:00
|
|
|
delta = before - *argz;
|
2007-03-17 05:16:09 +08:00
|
|
|
|
2002-06-15 04:51:09 +08:00
|
|
|
len = strlen(entry) + 1;
|
|
|
|
|
|
|
|
if(!(*argz = (char *)realloc(*argz, *argz_len + len)))
|
|
|
|
return ENOMEM;
|
|
|
|
|
2007-03-17 05:16:09 +08:00
|
|
|
memmove(*argz + delta + len, *argz + delta, *argz_len - delta);
|
|
|
|
memcpy(*argz + delta, entry, len);
|
2002-06-15 04:51:09 +08:00
|
|
|
|
|
|
|
*argz_len += len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|