Пример #1
0
int main(int argc, char *argv[])
{
  const char *prog_name = NULL;
  Ustr *s1 = USTR_NULL;
  Ustr *s2 = USTR_NULL;
  
  if (!argc)
    exit (EXIT_FAILURE);
  
  if ((prog_name = strrchr(argv[0], '/')))
    ++prog_name;
  else
    prog_name = argv[0];
  
  if (argc != 2)
    die(prog_name, "missing operand");

  if (!(s1 = ustr_dup_cstr(argv[1])))
    die(prog_name, strerror(errno));

  if (!(s2 = u_dirname(s1)))
    die(prog_name, strerror(errno));
  ustr_free(s1);
  
  if (!ustr_io_putfileline(&s2, stderr))
    die(prog_name, strerror(errno));

  USTR_CNTL_MALLOC_CHECK_END();
  
  exit (EXIT_SUCCESS);
}
Пример #2
0
/* Helper function called via semanage_fcontext_iterate() */
static int fcontext_matches(const semanage_fcontext_t *fcontext, void *varg)
{
	const char *oexpr = semanage_fcontext_get_expr(fcontext);
	fc_match_handle_t *handp = varg;
	struct Ustr *expr;
	regex_t re;
	int type, retval = -1;

	/* Only match ALL or DIR */
	type = semanage_fcontext_get_type(fcontext);
	if (type != SEMANAGE_FCONTEXT_ALL && type != SEMANAGE_FCONTEXT_ALL)
		return 0;

	/* Convert oexpr into a Ustr and anchor it at the beginning */
	expr = ustr_dup_cstr("^");
	if (expr == USTR_NULL)
		goto done;
	if (!ustr_add_cstr(&expr, oexpr))
		goto done;

	/* Strip off trailing ".+" or ".*" */
	if (ustr_cmp_suffix_cstr_eq(expr, ".+") ||
	    ustr_cmp_suffix_cstr_eq(expr, ".*")) {
		if (!ustr_del(&expr, 2))
			goto done;
	}

	/* Strip off trailing "(/.*)?" */
	if (ustr_cmp_suffix_cstr_eq(expr, "(/.*)?")) {
		if (!ustr_del(&expr, 6))
			goto done;
	}

	if (ustr_cmp_suffix_cstr_eq(expr, "/")) {
		if (!ustr_del(&expr, 1))
			goto done;
	}

	/* Append pattern to eat up trailing slashes */
	if (!ustr_add_cstr(&expr, "/*$"))
		goto done;

	/* Check dir against expr */
	if (regcomp(&re, ustr_cstr(expr), REG_EXTENDED) != 0)
		goto done;
	if (regexec(&re, handp->dir, 0, NULL, 0) == 0)
		handp->matched = 1;
	regfree(&re);

	retval = 0;

done:
	ustr_free(expr);

	return retval;
}
Пример #3
0
static Ustr *replace_all(const char *str, const replacement_pair_t * repl)
{
	Ustr *retval = USTR_NULL;
	int i;

	if (!str || !repl)
		goto done;
	if (!(retval = ustr_dup_cstr(str)))
		goto done;

	for (i = 0; repl[i].search_for; i++) {
		ustr_replace_cstr(&retval, repl[i].search_for,
				  repl[i].replace_with, 0);
	}
	if (ustr_enomem(retval))
		ustr_sc_free(&retval);

      done:
	return retval;
}
Пример #4
0
int tst(void)
{
  Ustr *s3 = ustr_dup_cstr("abcdefg xxxx xxxx xxxx xxxx1234yyyy yyyy yyyy zzz");
  Ustr *s4 = NULL;
  Ustr *s5 = NULL;
  
  ASSERT(s3);

  ASSERT(ustr_spn_cstr_fwd(s3, 0, "abcd") == 4);
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "abcdefg ") == 8);
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "abcdefg x") == 27);
  
  ASSERT(ustr_cspn_cstr_fwd(s3, 0, "x") == 8);
  ASSERT(ustr_cspn_cstr_fwd(s3, 0, "21") == 27);
  
  ASSERT(ustr_spn_cstr_rev(s3, 0, "z")  == 3);
  ASSERT(ustr_spn_cstr_rev(s3, 0, "az") == 3);
  ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz") == 3);
  ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz ") == 18);

  ASSERT(ustr_cspn_cstr_rev(s3, 0, "34") == 18);
  ASSERT(ustr_cspn_cstr_rev(s3, 0, " ") == 3);
  ASSERT(ustr_cspn_cstr_rev(s3, 0, "x") == 22);
  
  ASSERT(ustr_spn_cstr_fwd(s3, 0, " xayzafg1234bcde ") == ustr_len(s3));
  ASSERT(ustr_spn_cstr_rev(s3, 0, "1234abcdefg xayz ") == ustr_len(s3));

  ASSERT(ustr_cspn_cstr_fwd(s3, 0, "!")   == ustr_len(s3));
  ASSERT(ustr_cspn_cstr_rev(s3, 0, "!")   == ustr_len(s3));
  ASSERT(ustr_cspn_cstr_fwd(s3, 0, "!@#") == ustr_len(s3));
  ASSERT(ustr_cspn_cstr_rev(s3, 0, "!@#") == ustr_len(s3));

  ASSERT((s4 = ustr_dup_subustr(s3, 1,
                                ustr_spn_cstr_fwd(s3, 0, "abcdefg "))));
  ASSERT(ustr_del_subustr(&s3, 1, ustr_len(s4)));
  ASSERT((s5 = ustr_dup(s4)));
  ASSERT((s5 = ustr_dup(s4)));
  
  ASSERT(ustr_set_subustr(&s5, s3, 1, ustr_spn_cstr_fwd(s3, 0, "x ")));
  ASSERT(ustr_del_subustr(&s3, 1, ustr_len(s5)));
  
  ASSERT(ustr_spn_cstr_fwd(s4, 0, "abcd") == 4);
  ASSERT(ustr_spn_cstr_fwd(s4, 0, "abcdefg ") == 8);
  ASSERT(ustr_spn_cstr_fwd(s4, 1, "abcdefg ") == 7);
  ASSERT(ustr_spn_cstr_fwd(s4, 2, "abcdefg ") == 6);
  ASSERT(ustr_spn_cstr_fwd(s4, 3, "abcdefg ") == 5);
  ASSERT(ustr_spn_cstr_fwd(s4, 4, "abcdefg ") == 4);
  ASSERT(ustr_spn_cstr_fwd(s4, 5, "abcdefg ") == 3);
  ASSERT(ustr_spn_cstr_fwd(s4, 6, "abcdefg ") == 2);
  ASSERT(ustr_spn_cstr_fwd(s4, 7, "abcdefg ") == 1);
  ASSERT(ustr_spn_cstr_fwd(s4, 8, "abcdefg ") == 0);
  
  ASSERT(ustr_spn_cstr_rev(s3, 0, "z")  == 3);
  ASSERT(ustr_spn_cstr_rev(s3, 4, "y")  == 4);
  ASSERT(ustr_spn_cstr_rev(s3, 0, "az") == 3);
  ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz") == 3);
  ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz ") == 18);

  ASSERT(ustr_spn_cstr_rev(s3, 0, "y ") ==  0);
  ASSERT(ustr_spn_cstr_rev(s3, 1, "y ") ==  0);
  ASSERT(ustr_spn_cstr_rev(s3, 2, "y ") ==  0);
  ASSERT(ustr_spn_cstr_rev(s3, 3, "y ") == 15);
  ASSERT(ustr_spn_cstr_rev(s3, 4, "y ") == 14);
  ASSERT(ustr_spn_cstr_rev(s3, 5, "y ") == 13);
  ASSERT(ustr_spn_cstr_rev(s3, 6, "y ") == 12);
  ASSERT(ustr_spn_cstr_rev(s3, 7, "y ") == 11);
  ASSERT(ustr_spn_cstr_rev(s3, 8, "y ") == 10);
  ASSERT(ustr_spn_cstr_rev(s3, 9, "y ") ==  9);
  
  ASSERT(ustr_cspn_cstr_fwd(s4, 0, "e") == 4);
  ASSERT(ustr_cspn_cstr_fwd(s4, 0, "!-") == 8);
  ASSERT(ustr_cspn_cstr_fwd(s4, 1, "!-") == 7);
  ASSERT(ustr_cspn_cstr_fwd(s4, 2, "!-") == 6);
  ASSERT(ustr_cspn_cstr_fwd(s4, 3, "!-") == 5);
  ASSERT(ustr_cspn_cstr_fwd(s4, 4, "!-") == 4);
  ASSERT(ustr_cspn_cstr_fwd(s4, 5, "!-") == 3);
  ASSERT(ustr_cspn_cstr_fwd(s4, 6, "!-") == 2);
  ASSERT(ustr_cspn_cstr_fwd(s4, 7, "!-") == 1);
  ASSERT(ustr_cspn_cstr_fwd(s4, 8, "!-") == 0);
  ASSERT(ustr_cspn_cstr_fwd(s4, 0, "!")  == 8);
  ASSERT(ustr_cspn_cstr_fwd(s4, 1, "!")  == 7);
  ASSERT(ustr_cspn_cstr_fwd(s4, 2, "!")  == 6);
  ASSERT(ustr_cspn_cstr_fwd(s4, 3, "!")  == 5);
  ASSERT(ustr_cspn_cstr_fwd(s4, 4, "!")  == 4);
  ASSERT(ustr_cspn_cstr_fwd(s4, 5, "!")  == 3);
  ASSERT(ustr_cspn_cstr_fwd(s4, 6, "!")  == 2);
  ASSERT(ustr_cspn_cstr_fwd(s4, 7, "!")  == 1);
  ASSERT(ustr_cspn_cstr_fwd(s4, 8, "!")  == 0);
  
  ASSERT(ustr_cspn_cstr_rev(s4, 0, "e") == 3);
  ASSERT(ustr_cspn_cstr_rev(s4, 0, "!-") == 8);
  ASSERT(ustr_cspn_cstr_rev(s4, 1, "!-") == 7);
  ASSERT(ustr_cspn_cstr_rev(s4, 2, "!-") == 6);
  ASSERT(ustr_cspn_cstr_rev(s4, 3, "!-") == 5);
  ASSERT(ustr_cspn_cstr_rev(s4, 4, "!-") == 4);
  ASSERT(ustr_cspn_cstr_rev(s4, 5, "!-") == 3);
  ASSERT(ustr_cspn_cstr_rev(s4, 6, "!-") == 2);
  ASSERT(ustr_cspn_cstr_rev(s4, 7, "!-") == 1);
  ASSERT(ustr_cspn_cstr_rev(s4, 8, "!-") == 0);
  ASSERT(ustr_cspn_cstr_rev(s4, 0, "!")  == 8);
  ASSERT(ustr_cspn_cstr_rev(s4, 1, "!")  == 7);
  ASSERT(ustr_cspn_cstr_rev(s4, 2, "!")  == 6);
  ASSERT(ustr_cspn_cstr_rev(s4, 3, "!")  == 5);
  ASSERT(ustr_cspn_cstr_rev(s4, 4, "!")  == 4);
  ASSERT(ustr_cspn_cstr_rev(s4, 5, "!")  == 3);
  ASSERT(ustr_cspn_cstr_rev(s4, 6, "!")  == 2);
  ASSERT(ustr_cspn_cstr_rev(s4, 7, "!")  == 1);
  ASSERT(ustr_cspn_cstr_rev(s4, 8, "!")  == 0);
  
  ASSERT(ustr_cspn_cstr_rev(s3, 0, "34") == 18);
  ASSERT(ustr_cspn_cstr_rev(s3, 0, " ") == 3);
  ASSERT(ustr_cspn_cstr_rev(s3, 0, "x") == 22);
  
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "1")     == 1);
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "12")    == 2);
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "123")   == 3);
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "1234")  == 4);
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "1234y") == 8);
  
  ASSERT(ustr_del_subustr(&s3, 4, 4));
  ASSERT(ustr_spn_cstr_fwd(s3, 0, "1234y") == 4);  
  
  ustr_free(s3);
  ustr_free(s4); /* leaked, see above */
  ustr_free(s4);
  ustr_free(s5);
  
  return (EXIT_SUCCESS);
}
Пример #5
0
int tst(void)
{
  /* single char sep; do not keep sep; do keep blank strings */
  Ustr *a = ustr_dup_cstr("blue,yellow,red,green,,orange");
  const char *ans[] = {"blue","yellow","red","green","","orange"};
  size_t off = 0;
  unsigned int flags = (USTR_FLAG_SPLIT_RET_NON);
  Ustr *sep = ustr_dup_cstr(",");
  Ustr *tok = NULL;
  Ustr *ret = NULL;
  size_t i = 0;
  const char *ans2[] = {"a==","b,c==","d,c==","==","==",",12==","xzy==","=="};
  const char *ans3[] = {"a","bx","cx","dx","xx"};
  const char *ans4[] = {"a,bx","cx","dx",",xx"};
  const char *ans5[] = {"this","is","a","test","of","the","emergency","broadcast","system"};
  const char *ans6[] = {" ", " ", "this ","is ","a ","test ","of ","the\t","emergency\n"," ","broadcast ","system"};
  const char *ans7[] = {"a ","b ","c ","d "," "," "," "," "," "," "," "," "," ","e|","f|","g|","|","h"};
  const char *ans8[] = {"a","b","c","d","e","f","g","h"};
  const char *ans9[] = {"a","b","c","d","","","","","","","","","","e","f","g","","h"};
  const char *ans10[] = {"a b c d          e|f|g||h"};
  const char *chrs = "\t\n ";

  while ((tok = ustr_split(a,&off,sep, NULL,flags)))
  {
       ASSERT(ustr_cmp_cstr_eq(tok,ans[i]));
       ++i;
       ustr_sc_free(&tok);
  }
  ASSERT(i == 6);
  ustr_sc_free(&a);
  ustr_sc_free(&sep);
  /* mult-char sep; keep sep; do not keep blank strings */
  a = ustr_dup_cstr("a==b,c==d,c======,12==xzy====");
  sep = ustr_dup_cstr("==");
  flags = (USTR_FLAG_SPLIT_RET_SEP);
  i=0;
  off=0;
  while ((tok = ustr_split(a,&off,sep, NULL,flags)))
  {
     ASSERT(ustr_cmp_cstr_eq(tok,ans2[i]));
     ++i;
     ustr_sc_free(&tok);
  }
  ASSERT(i == 8);
  ustr_sc_free(&a);
  ustr_sc_free(&sep);
  /* single-char sep; skip sep, skip blank */
  a = ustr_dup_cstr("a,bx,,,,cx,,dx,,,xx,,,,");
  sep = ustr_dup_cstr(",");
  flags = 0;
  i=0;
  off=0;
  while ((tok = ustr_split(a,&off,sep, NULL,flags)))
  {
     ASSERT(ustr_cmp_cstr_eq(tok,ans3[i]));
     ++i;
     ustr_sc_free(&tok);
  }
  ustr_sc_free(&sep);
  
  /* multi-char sep; skip sep; skip blank */
  sep = ustr_dup_cstr(",,");
  i=0;
  off=0;
  while ((tok = ustr_split(a,&off,sep, NULL,flags)))
  {
     ASSERT(ustr_cmp_cstr_eq(tok,ans4[i]));
     ++i;
     ustr_sc_free(&tok);
  }
  ustr_sc_free(&sep);
  ASSERT(i==4);
  /* blank sep */
  off = 0;
  sep = USTR("");
  ASSERT((ret = ustr_split(a,&off,sep, NULL,flags)) == USTR_FALSE);
  ASSERT(off==0);
  ustr_sc_free(&ret);
  /* blank target */
  sep = ustr_dup_cstr(",");
  ustr_sc_free(&a);
  a = USTR("");
  ASSERT((ret = ustr_split(a,&off,sep, NULL,flags)) == USTR_FALSE);
  ASSERT(off==0);

  ustr_sc_free(&sep);
  ustr_sc_free(&ret);

  a = USTR1(\x15, "a||bx||||cx||dx||||xx");
  i   = 0;
  off = 0;
  while ((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_DEF)))
  {
    ASSERT(!ustr_sized(a));
    ASSERT(!ustr_sized(tok));
    ASSERT(ustr_cmp_cstr_eq(tok, ans3[i++]));
    ustr_sc_free(&tok);
  }

  ASSERT((a = ustr_dupx(1, 0, 0, 0, a)));
  
  off = 1;
  ASSERT((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_RET_NON)));
  ASSERT( ustr_sized(a));
  ASSERT(!ustr_sized(tok));
  ASSERT( ustr_ro(tok));
  ASSERT(!ustr_len(tok));
  ustr_sc_free(&tok);
  off = 1;
  ASSERT((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_RET_SEP)));
  ASSERT( ustr_sized(a));
  ASSERT(!ustr_sized(tok));
  ASSERT(!ustr_ro(tok));
  ASSERT(ustr_cmp_cstr_eq(tok, "||"));
  ustr_sc_free(&tok);
  ASSERT((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_RET_SEP)));
  ASSERT( ustr_sized(a));
  ASSERT(!ustr_sized(tok));
  ASSERT(!ustr_ro(tok));
  ASSERT(ustr_cmp_cstr_eq(tok, "bx||"));
  ustr_sc_free(&tok);
  
  i   = 2;
  while ((tok = ustr_split_cstr(a, &off, "||", tok, USTR_FLAG_SPLIT_KEEP_CONF)))
  {
    ASSERT( ustr_sized(a));
    ASSERT( ustr_sized(tok));
    ASSERT(ustr_cmp_cstr_eq(tok, ans3[i++]));
    ustr_sc_free(&tok);
  }

  off = 0;
  i   = 0;
  flags = USTR_FLAG_SPLIT_KEEP_CONF;
  while ((tok = ustr_split_cstr(a, &off, "||", tok, flags)))
  {
    ASSERT( ustr_sized(a));
    ASSERT( ustr_sized(tok));
    ASSERT(ustr_cmp_cstr_eq(tok, ans3[i++]));
    flags = USTR_FLAG_SPLIT_DEF; /* doesn't matter after first one */
  }

  off = 0;
  ASSERT(!ustr_split(a, &off, a, NULL, 0));
  ASSERT((off == ustr_len(a)));
  off = 0;
  ASSERT((tok = ustr_split(a, &off, a, NULL, (USTR_FLAG_SPLIT_RET_NON |
                                              USTR_FLAG_SPLIT_KEEP_CONF))));
  ASSERT((off == ustr_len(a)));
  ASSERT( ustr_sized(tok));
  ASSERT( ustr_cstr(tok));
  ASSERT_EQ(tok, USTR(""));
  ustr_sc_free(&tok);
  off = 0;
  ASSERT((tok = ustr_split(a, &off, a, NULL, (USTR_FLAG_SPLIT_RET_SEP |
                                              USTR_FLAG_SPLIT_KEEP_CONF))));
  ASSERT((off == ustr_len(a)));
  ASSERT_EQ(tok, a);
  ASSERT( ustr_sized(tok));
  ASSERT( ustr_cstr(tok));

  off = 0;
  ASSERT((tok = ustr_split(a, &off, a, tok,  USTR_FLAG_SPLIT_RET_NON)));
  ASSERT( ustr_sized(tok));
  ASSERT(!ustr_ro(tok));
  ustr_sc_free(&tok);
  off = 0;
  ASSERT((tok = ustr_split(a, &off, a, tok,  USTR_FLAG_SPLIT_RET_NON)));
  ASSERT(!ustr_sized(tok));
  ASSERT( ustr_ro(tok));
  off = 0;
  ASSERT((tok = ustr_dup_cstr("abcd")));
  ASSERT(!ustr_split(a, &off, a, tok, 0)); /* does the free */
  
  ustr_sc_free(&a);

/* ustr_split_spn_cstr */
  a = ustr_dup_cstr("  this is a test of the\temergency\n broadcast system");
  off = 0;
  i=0;
  tok = USTR_NULL;
  while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,0)))
  {
     ASSERT(ustr_cmp_cstr_eq(tok,ans5[i++]));
     ustr_sc_free(&tok);
  }
  ASSERT(i==9);
  i=0;
  off=0;
  while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_SEP)))
  {
     ASSERT(ustr_cmp_cstr_eq(tok,ans6[i++]));
     ustr_sc_free(&tok);
  }
  ASSERT(i==12);
  i=0;
  off=0;
  while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_SEP|USTR_FLAG_SPLIT_RET_NON)))
  {
     ASSERT(ustr_cmp_cstr_eq(tok,ans6[i++]));
     ustr_sc_free(&tok);
  }
  ASSERT(i==12);
  ustr_sc_free(&a);
  chrs = " |";
  a = ustr_dup_cstr("a b c d          e|f|g||h");
  i=0;
  off=0;
  while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_SEP|USTR_FLAG_SPLIT_RET_NON)))
  {
    ASSERT(ustr_cmp_cstr_eq(tok,ans7[i++]));
    ustr_sc_free(&tok);
  }
  ASSERT(i==18);
  i=0;
  off=0;
  while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,0)))
  {
    ASSERT(ustr_cmp_cstr_eq(tok,ans8[i++]));
    ustr_sc_free(&tok);
  }
  ASSERT(i==8); 
  i=0;
  off=0;
  while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_NON)))
  {
    ASSERT(ustr_cmp_cstr_eq(tok,ans9[i++]));
    ustr_sc_free(&tok);
  }
  ASSERT(i==18);
  chrs = "xyz";
  i=0;
  off=0;

  while ((tok = ustr_split_spn_cstr(a,&off,chrs,tok,USTR_FLAG_SPLIT_RET_NON)))
  {
    ASSERT(ustr_cmp_cstr_eq(tok,ans10[i++]));
  }
  off=0;
  chrs = "g";
  ASSERT(i==1);
  ustr_sc_free(&a);
  a = USTR1(\x3, "agg");
  ASSERT(ustr_assert_valid(a));
  ASSERT( ustr_ro(a));
  ASSERT(!ustr_sized(a));
  tok = ustr_split_spn_cstr(a,&off,chrs,tok,USTR_FLAG_SPLIT_RET_NON);
  ASSERT(ustr_cmp_cstr_eq(tok,"a"));
  ASSERT(!ustr_ro(tok));
  ASSERT(!ustr_sized(tok));
  tok = ustr_split_spn_cstr(a,&off,chrs,tok,USTR_FLAG_SPLIT_RET_NON);
  ASSERT(ustr_cmp_cstr_eq(tok,""));
  ASSERT( ustr_ro(tok));
  ASSERT(!ustr_sized(tok));
  ustr_sc_free(&tok);
  ASSERT((a = ustr_dupx(1, 0, 0, 0, a)));
  off=0;
  tok = ustr_split_spn(a,&off,USTR1(\1, "g"),tok,USTR_FLAG_SPLIT_KEEP_CONF);
  ASSERT(ustr_cmp_cstr_eq(tok,"a"));
  ASSERT(ustr_sized(a));
  ASSERT(ustr_sized(tok));
  ustr_sc_free(&a);
  ustr_sc_free(&tok);
 
  return (EXIT_SUCCESS);
}
Пример #6
0
int tst(void)
{
  struct Ustr *s3 = NULL;
  struct Ustr *s4 = NULL;

  assert(!USTR_CONF_USE_DYNAMIC_CONF ||
         ustr_cntl_opt(USTR_CNTL_OPT_SET_REF_BYTES, 1));
  
  /* move to the new "default" conf */
  ustr_sc_free2(&s2, ustr_dup_buf(ustr_cstr(s2), ustr_len(s2)));
  ASSERT(s2);

  s3 = ustr_dup_cstr("s3 abcd s2");
  s4 = ustr_dup_rep_chr('x', 40);
  
  ASSERT(s3);
  ASSERT(s4);
  
  ASSERT(ustr_len(s1)  ==  0);
  ASSERT(ustr_len(s2)  ==  2);
  ASSERT(ustr_len(s3)  == 10);
  ASSERT(ustr_len(s4)  == 40);
  
  ASSERT(ustr_size(s1) ==  0);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==  2); /* ustr__ns(1 + 1 + 1 + 2 + 1) -
                              *            1 + 1 + 1     + 1 */
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s3) == 12); /* ustr__ns(1 + 1 + 1 + 10 + 1) -
                              *            1 + 1 + 1      + 1 */
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) == 44);

  ASSERT(ustr_add_rep_chr(&s4, '-', 8));
  ASSERT(ustr_len(s4)  == 48);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) == 60);
  
  ASSERT(ustr_del(&s4, 4));
  ASSERT(ustr_len(s4)  == 44);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) == 44);
  
  ASSERT(ustr_del(&s4, 20));
  ASSERT(ustr_len(s4)  == 24);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) == 28);
  
  ustr_sc_free(&s3);

  ASSERT((s3 = ustr_dup(s4)));
  ASSERT(ustr_len(s3)  == 24);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s3) == 28);
  ASSERT(ustr_len(s4)  == 24);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) == 28);
  
  ASSERT(ustr_del(&s4, 15));
  ASSERT(ustr_len(s3)  == 24);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s3) == 28);
  ASSERT(ustr_len(s4)  ==  9);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) == 12);

  ASSERT(ustr_cmp_cstr_eq(s3,   "xxxxxxxxxxxxxxxxxxxxxxxx"));
  ASSERT(!strcmp(ustr_cstr(s3), "xxxxxxxxxxxxxxxxxxxxxxxx"));

  ASSERT(ustr_cmp_cstr_eq(s4,   "xxxxxxxxx"));
  ASSERT(!strcmp(ustr_cstr(s4), "xxxxxxxxx"));

  ASSERT(!ustr_del(&s4, 15));
  ASSERT(ustr_del(&s4, 8));
  ASSERT(ustr_len(s4)  ==  1);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) ==  2);
  ASSERT(ustr_cmp_cstr_eq(s4,   "x"));
  ASSERT(!strcmp(ustr_cstr(s4), "x"));
  ASSERT(ustr_add_rep_chr(&s4, '-', 8));
  ASSERT(ustr_len(s4)  ==  9);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) == 12);
  ASSERT(ustr_cmp_cstr_eq(s4,   "x--------"));
  ASSERT(!strcmp(ustr_cstr(s4), "x--------"));

  ASSERT(ustr_set_cstr(&s4,     "x12345678"));
  ustr_sc_free2(&s3, s4);
  ASSERT(ustr_cmp_cstr_eq(s4,   "x12345678"));
  ASSERT( ustr_owner(s4));
  ASSERT(!ustr_shared(s4));
  ASSERT(s3 == s4);
  ASSERT( ustr_setf_share(s4));
  ASSERT(!ustr_owner(s4));
  ASSERT(ustr_del_subustr(&s4, 4, 4));
  ASSERT(ustr_cmp_cstr_eq(s3,   "x12345678"));
  ASSERT(ustr_cmp_cstr_eq(s4,   "x1278"));

  ustr_sc_free_shared(&s3);
  ustr_sc_free(&s4);
  
  return (EXIT_SUCCESS);
}
Пример #7
0
int tst(void)
{
  struct Ustr *s3 = NULL;
  struct Ustr *s4 = NULL;
  int num = -1;
  
  assert(!USTR_CONF_USE_DYNAMIC_CONF ||
         ustr_cntl_opt(USTR_CNTL_OPT_SET_REF_BYTES, 1));
  /* move to the new "default" conf */
  ustr_sc_free2(&s2, ustr_dup_buf(ustr_cstr(s2), ustr_len(s2)));

  s3 = ustr_dup_cstr("s3 abcd s2");
  s4 = ustr_dup_empty(); /* always allocs */
  
  ASSERT(s2);
  ASSERT(s3);
  ASSERT(s4);
  ASSERT(ustr_cmp_eq(s1, s4));
  
  ASSERT(ustr_len(s1)  ==  0);
  ASSERT(ustr_len(s2)  ==  2);
  ASSERT(ustr_len(s3)  == 10);
  ASSERT(ustr_len(s4)  ==  0);
  
  ASSERT(ustr_size(s1) ==  0);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==  2);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s3) == 12);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) ==  0);

  ASSERT(ustr_srch_fwd(s3, 0, s2) == 9);
  ASSERT(ustr_srch_rev(s3, 0, s2) == 9);
  ASSERT(ustr_srch_fwd(s2, 0, s3) == 0);
  ASSERT(ustr_srch_rev(s2, 0, s3) == 0);
  
  ASSERT(ustr_add_cstr(&s2, "x"));
  ASSERT(ustr_len(s2)  ==   3);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==   4);
  ASSERT(ustr_add_cstr(&s2, "y"));
  ASSERT(ustr_len(s2)  ==   4);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==   4);
  ASSERT(ustr_add_cstr(&s2, "z"));
  ASSERT(ustr_len(s2)  ==   5);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==   8);
  ASSERT(ustr_add_rep_chr(&s2, '-', 11));
  ASSERT(ustr_len(s2)  ==  16);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==  20);
  ASSERT(ustr_cmp_cstr_eq(s2,   "s2xyz-----------"));
  ASSERT(!strcmp(ustr_cstr(s2), "s2xyz-----------"));
  
  ASSERT(ustr_srch_fwd(s3, 0, s2) == 0);
  ASSERT(ustr_srch_rev(s3, 0, s2) == 0);
  ASSERT(ustr_srch_fwd(s2, 0, s3) == 0);
  ASSERT(ustr_srch_rev(s2, 0, s3) == 0);

  /* NOTE: Using system *printf, so can't use %zu as Solaris is retarded */
  ASSERT(ustr_add_fmt(&s1, "%s abcd %13.100s %d %c %lu%n",
                      "------abc------", "", 42, 0,
                      (unsigned long)ustr_len(s3), &num) != -1);
  ASSERT((unsigned)num == ustr_len(s1));
  ASSERT(42  == num);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(44  == ustr_size(s1));

  ASSERT(ustr_add_fmt(&s4, "%2$d%1$u", 2, 4));

  ASSERT(ustr_srch_cstr_fwd(s1, 0, "abcd") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, 0, "abcd") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 0, "abc")  ==  7);
  ASSERT(ustr_srch_cstr_rev(s1, 0, "abc")  == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 0, "10")  == 41);
  ASSERT(ustr_srch_cstr_rev(s1, 0, "10")  == 41);
  ASSERT(ustr_srch_chr_fwd(s1, 0, 0)  == 39);
  ASSERT(ustr_srch_chr_rev(s1, 0, 0)  == 39);
  ASSERT(ustr_srch_fwd(s1, 0, s4) == 36);

  ASSERT(ustr_srch_cstr_fwd(s1,  1, "abcd") == 17);
  ASSERT(ustr_srch_cstr_rev(s1,  1, "abcd") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 10, "abcd") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, 10, "abcd") == 17);
  
  ASSERT(ustr_srch_cstr_fwd(s1,  0, " ") == 16);
  ASSERT(ustr_srch_cstr_fwd(s1, 10, " ") == 16);
  ASSERT(ustr_srch_cstr_fwd(s1, 16, " ") == 21);
  ASSERT(ustr_srch_cstr_fwd(s1, 20, " ") == 21);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, " ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 1) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "  ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 2) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "   ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 3) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "    ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 4) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "     ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 5) == 22);
  
  ASSERT(ustr_srch_cstr_fwd(s1,  0, "a") ==  7);
  ASSERT(ustr_srch_cstr_fwd(s1,  6, "a") ==  7);
  ASSERT(ustr_srch_cstr_fwd(s1,  7, "a") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 16, "a") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 17, "a") ==  0);
  
  ASSERT(ustr_srch_cstr_rev(s1, 0, "a") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 17, "a") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 16, "a") ==  7);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) -  7, "a") ==  7);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) -  6, "a") ==  0);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) -  1, "a") ==  0);

  /* srch_case */
  ASSERT(ustr_srch_case_cstr_fwd(s1,  0, " ") == 16);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 10, " ") == 16);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 16, " ") == 21);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 20, " ") == 21);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, " ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 1) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "  ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 2) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "   ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 3) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "    ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 4) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "     ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 5) == 22);
  
  ASSERT(ustr_srch_case_cstr_fwd(s1,  0, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  6, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  7, "a") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 16, "a") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 17, "a") ==  0);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  0, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  6, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  7, "A") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 16, "A") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 17, "A") ==  0);
  
  ASSERT(ustr_srch_case_cstr_rev(s1, 0, "a") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 17, "a") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 16, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  7, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  6, "a") ==  0);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  1, "a") ==  0);
  ASSERT(ustr_srch_case_cstr_rev(s1, 0, "A") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 17, "A") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 16, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  7, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  6, "A") ==  0);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  1, "A") ==  0);

  if (!USTR_DEBUG)
  ASSERT(ustr_srch_cstr_fwd(s1, ustr_len(s1), "a") ==  0);
  if (!USTR_DEBUG)
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1), "a") ==  0);

  /*  puts(ustr_cstr(s4)); */
  
  ustr_sc_free(&s3);

  ASSERT((s3 = ustr_dup(s4)));
  ASSERT(ustr_add_fmt(&s4, "x"));

  ustr_sc_free(&s4);
  ustr_sc_free(&s3);

  /*
  ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 123456")));
  ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 1234\0xxx"))); */
  ASSERT( ustr_assert_valid(USTR1(\x000F, "123456789 12345")));

  /*    ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 12345\0xxx")));  */
  
  s3 = ustr_dupx(0, 2, 0, 1, USTR1(\x000F, "123456789 12345"));

  ASSERT(ustr_cmp_cstr_eq(s3, "123456789 12345"));
  ASSERT(ustr_cmp_eq(s3, USTR1(\x000F, "123456789 12345")));
  ASSERT(!ustr_ro(s3));
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s3) == 19);
  
  ustr_sc_free(&s3);
  
  return (EXIT_SUCCESS);
}