Пример #1
0
/*
 * read the arena header and trailer blocks from disk
 */
static int
loadarena(Arena *arena)
{
	ArenaHead head;
	ZBlock *b;

	b = alloczblock(arena->blocksize, 0, arena->part->blocksize);
	if(b == nil)
		return -1;
	if(readpart(arena->part, arena->base + arena->size, b->data, arena->blocksize) < 0){
		freezblock(b);
		return -1;
	}
	if(unpackarena(arena, b->data) < 0){
		freezblock(b);
		return -1;
	}
	if(arena->version != ArenaVersion4 && arena->version != ArenaVersion5){
		seterr(EAdmin, "unknown arena version %d", arena->version);
		freezblock(b);
		return -1;
	}
	scorecp(arena->score, &b->data[arena->blocksize - VtScoreSize]);

	if(readpart(arena->part, arena->base - arena->blocksize, b->data, arena->blocksize) < 0){
		logerr(EAdmin, "can't read arena header: %r");
		freezblock(b);
		return 0;
	}
	if(unpackarenahead(&head, b->data) < 0)
		logerr(ECorrupt, "corrupted arena header: %r");
	else if(namecmp(arena->name, head.name)!=0
	     || arena->clumpmagic != head.clumpmagic
	     || arena->version != head.version
	     || arena->blocksize != head.blocksize
	     || arena->size + 2 * arena->blocksize != head.size){
		if(namecmp(arena->name, head.name)!=0)
			logerr(ECorrupt, "arena tail name %s head %s", 
				arena->name, head.name);
		else if(arena->clumpmagic != head.clumpmagic)
			logerr(ECorrupt, "arena %d tail clumpmagic 0x%lux head 0x%lux",
				debugarena, (uint32_t)arena->clumpmagic,
				(uint32_t)head.clumpmagic);
		else if(arena->version != head.version)
			logerr(ECorrupt, "arena tail version %d head version %d",
				arena->version, head.version);
		else if(arena->blocksize != head.blocksize)
			logerr(ECorrupt, "arena tail block size %d head %d",
				arena->blocksize, head.blocksize);
		else if(arena->size+2*arena->blocksize != head.size)
			logerr(ECorrupt, "arena tail size %lud head %lud",
				(uint32_t)arena->size+2*arena->blocksize,
			       head.size);
		else
			logerr(ECorrupt, "arena header inconsistent with arena data");
	}
	freezblock(b);

	return 0;
}
Пример #2
0
/*
 * Check if any clock skew policy matches
 */
static void
get_clock_skew(val_context_t *ctx,
               u_char *name_n,
               int *skew,
               u_int32_t *ttl_x)
{
    policy_entry_t *cs_pol, *cs_cur;
    u_char       *p;
    size_t       name_len;

    if (ctx == NULL || name_n == NULL || skew == NULL || ttl_x == NULL) {
        val_log(ctx, LOG_DEBUG, "get_clock_skew(): Cannot check for clock skew policy, bad args"); 
        return; 
    }
    
    RETRIEVE_POLICY(ctx, P_CLOCK_SKEW, cs_pol);
    if (cs_pol) {

        name_len = wire_name_length(name_n);

        for (cs_cur = cs_pol;
             cs_cur && (wire_name_length(cs_cur->zone_n) > name_len);
             cs_cur = cs_cur->next);
        /*
         * for all zones which are shorter or as long, do a strstr 
         */
        /*
         * Because of the ordering, the longest match is found first 
         */
        for (; cs_cur; cs_cur = cs_cur->next) {
            int             root_zone = 0;
            if (!namecmp(cs_cur->zone_n, (const u_char *) ""))
                root_zone = 1;
            else {
                /*
                 * Find the last occurrence of cs_cur->zone_n in name_n 
                 */
                p = name_n;
                while (p && (*p != '\0')) {
                    if (!namecmp(p, cs_cur->zone_n))
                        break;
                    p = p + *p + 1;
                }
            }
            if (root_zone || (!namecmp(p, cs_cur->zone_n))) {
                val_log(ctx, LOG_DEBUG, "get_clock_skew(): Found clock skew policy"); 
                if (cs_cur->pol) {
                    *skew = ((struct clock_skew_policy *)(cs_cur->pol))->clock_skew;
                    if (cs_cur->exp_ttl > 0)
                        *ttl_x = cs_cur->exp_ttl;
                    return;
                }
            }
        }
    }
    val_log(ctx, LOG_DEBUG, "get_clock_skew(): No clock skew policy found"); 
    *skew = 0;
}
Пример #3
0
Файл: proc.c Проект: yonatana/OS
int
kernel_unlink(char* path)
{
  struct inode *ip, *dp;
  struct dirent de;
  char name[DIRSIZ];
  uint off;

//   if(argstr(0, &path) < 0)
//     return -1;
  if((dp = nameiparent(path, name)) == 0)
    return -1;

  begin_trans();

  ilock(dp);

  // Cannot unlink "." or "..".
  if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
    goto bad;

  if((ip = dirlookup(dp, name, &off)) == 0)
    goto bad;
  ilock(ip);

  if(ip->nlink < 1)
    panic("unlink: nlink < 1");
  /*
  if(ip->type == T_DIR && !isdirempty(ip)){
    iunlockput(ip);
    goto bad;
  }
*/
  memset(&de, 0, sizeof(de));
  if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
    panic("unlink: writei");
  if(ip->type == T_DIR){
    dp->nlink--;
    iupdate(dp);
  }
  iunlockput(dp);

  ip->nlink--;
  iupdate(ip);
  iunlockput(ip);

  commit_trans();

  return 0;

bad:
  iunlockput(dp);
  commit_trans();
  return -1;
}
Пример #4
0
static int
veccmp(const void *a, const void *b)
{
    int i;
    struct dvec **d1 = (struct dvec **) a;
    struct dvec **d2 = (struct dvec **) b;

    if ((i = namecmp((*d1)->v_plot->pl_typename,
                     (*d2)->v_plot->pl_typename)) != 0)
        return (i);
    return (namecmp((*d1)->v_name, (*d2)->v_name));
}
Пример #5
0
static Dir*
xdirstat0(char **path, int (*namecmp)(char *, char *), char *err)
{
	char *base, *name;
	Dir *d, *t;
	int n, i;

	if(d = dirstat(*path))
		return d;
	if(!splitpath(*path, &base, &name))
		return nil;
	if((n = xdirread0(&base, namecmp, &t)) < 0)
		goto out;
	for(i=0; i<n; i++){
		if(namecmp(t[i].name, name))
			continue;
		free(*path); *path = conspath(base, t[i].name);
		d = xdirdup(&t[i], 1);
		goto out;
	}
	werrstr("%s", err);
out:
	free(base);
	free(name);
	return d;
}
Пример #6
0
// Look for a directory entry in a directory.
// If found, set *poff to byte offset of entry.
struct inode*
dirlookup(struct inode *dp, char *name, uint *poff)
{
  uint off, inum;
  struct dirent de;

  if(dp->type != T_DIR)
    panic("dirlookup not DIR");

  for(off = 0; off < dp->size; off += sizeof(de)){
    if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
      panic("dirlink read");
    if(de.inum == 0)
      continue;
    if(namecmp(name, de.name) == 0){
      // entry matches path element
      if(poff)
        *poff = off;
      inum = de.inum;
      return iget(dp->dev, inum);
    }
  }

  return 0;
}
Пример #7
0
local int zbcmp(ZCONST zvoid *n, ZCONST zvoid far *z)
//ZCONST zvoid *n;        /* string to search for */
//ZCONST zvoid far *z;    /* pointer to a pointer to a zip entry */
/* Used by search() to compare a target to an entry in the zfile list. */
{
  return namecmp((char *)n, ((struct zlist far *)z)->zname);
}
Пример #8
0
//-----------------------------------------------
// PURPOSE:      run simple module "name"
// RETURN VALUE: passed from module. -1 if something was failed
//-----------------------------------------------
int module_run(char* name)
{
    int i;
    for (i=0; i<MAX_SIMPLE_MODULE; i++)
    {
        // Check if module loaded (otherwise name not valid)
        if (*h_run[i].lib != h_run[i].default_lib)
        {
            if (namecmp(name, h_run[i].name))
            {
                // Already loaded
                return (*h_run[i].lib)->run();
            }
        }
    }
    for (i=0; i<MAX_SIMPLE_MODULE; i++)
    {
        // Look for empty slot
        if (*h_run[i].lib == h_run[i].default_lib)
        {
            // Found space - run module
            strcpy(h_run[i].name, name);
            return (*h_run[i].lib)->run();
        }
    }

    // Error - no space
    module_run_error(LANG_MODULE_NO_SPACE, name);

    return -1;
}
Пример #9
0
/*
 * Ordering for mastercmp:
 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
 * as larger than directories.  Within either group, use the sort function.
 * All other levels use the sort function.  Error entries remain unsorted.
 */
static int
mastercmp(const FTSENT **a, const FTSENT **b)
{
	int a_info, b_info;

	a_info = (*a)->fts_info;
	if (a_info == FTS_ERR)
		return (0);
	b_info = (*b)->fts_info;
	if (b_info == FTS_ERR)
		return (0);

	if (a_info == FTS_NS || b_info == FTS_NS) {
		if (b_info != FTS_NS)
			return (1);
		else if (a_info != FTS_NS)
			return (-1);
		else
			return (namecmp(*a, *b));
	}

	if (a_info != b_info && !f_listdir &&
	    (*a)->fts_level == FTS_ROOTLEVEL) {
		if (a_info == FTS_D)
			return (1);
		else if (b_info == FTS_D)
			return (-1);
	}
	return (sortfcn(*a, *b));
}
Пример #10
0
// Look for a directory entry in a directory.
// If found, set *poff to byte offset of entry.
// Caller must have already locked dp.
struct inode*
dirlookup(struct inode *dp, char *name, uint *poff)
{
  uint off, inum;
  struct buf *bp;
  struct dirent *de;

  if(dp->type != T_DIR)
    panic("dirlookup not DIR");

  for(off = 0; off < dp->size; off += BSIZE){
    bp = bread(dp->dev, bmap(dp, off / BSIZE));
    for(de = (struct dirent*)bp->data;
        de < (struct dirent*)(bp->data + BSIZE);
        de++){
      if(de->inum == 0)
        continue;
      if(namecmp(name, de->name) == 0){
        // entry matches path element
        if(poff)
          *poff = off + (uchar*)de - bp->data;
        inum = de->inum;
        brelse(bp);
        return iget(dp->dev, inum);
      }
    }
    brelse(bp);
  }
  return 0;
}
Пример #11
0
// Look for a directory entry in a directory.
// If found, set *poff to byte offset of entry.
struct inode*
sfs_dirlookup(struct inode *dp, char *name, uint *poff)
{
  uint off, inum;
  struct sfs_dirent de;
  struct sfs_inode *sdp = vop_info(dp, sfs_inode);
  if(sdp->type != T_DIR)
    panic("dirlookup not DIR");

  for(off = 0; off < sdp->size; off += sizeof(de)){
    if(sfs_readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
      panic("dirlink read");

    if(de.inum == 0)
      continue;

    if(namecmp(name, de.name) == 0){
      // entry matches path element
      if(poff)
        *poff = off;
      inum = de.inum;
  //    cprintf("inum = %d\n", inum);
      return sfs_iget(sdp->dev, inum, 0);
    }    
  }
  return 0;
}
Пример #12
0
local int rqcmp(ZCONST zvoid *a, ZCONST zvoid *b)
//ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries */
/* Used by qsort() to compare entries in the zfile list.
 * Compare the internal names z->iname, but in reverse order. */
{
  return namecmp((*(struct zlist far **)b)->iname,
                 (*(struct zlist far **)a)->iname);
}
Пример #13
0
Datum
namege(PG_FUNCTION_ARGS)
{
	Name		arg1 = PG_GETARG_NAME(0);
	Name		arg2 = PG_GETARG_NAME(1);

	PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
}
Пример #14
0
Datum
btnamecmp(PG_FUNCTION_ARGS)
{
	Name		arg1 = PG_GETARG_NAME(0);
	Name		arg2 = PG_GETARG_NAME(1);

	PG_RETURN_INT32(namecmp(arg1, arg2, PG_GET_COLLATION()));
}
Пример #15
0
Файл: cmp.c Проект: derek2001/ls
/*size compare function*/
int sizecmp(const FTSENT** fsent1, const FTSENT** fsent2)
{
	if((*fsent1)->fts_statp->st_size > (*fsent2)->fts_statp->st_size)
		return -1;
	else if((*fsent1)->fts_statp->st_size < (*fsent2)->fts_statp->st_size)
		return 1;
	else 
		return namecmp(fsent1, fsent2);
}
Пример #16
0
int
sizecmp(const FTSENT *a, const FTSENT *b)
{
	if (b->fts_statp->st_size > a->fts_statp->st_size)
		return (1);
	if (b->fts_statp->st_size < a->fts_statp->st_size)
		return (-1);
	else
		return (namecmp(a, b));
}
Пример #17
0
Файл: ls.c Проект: Kaikaiw/apue
/**
 * main comparation function passed to fts_open
 * it takes pointer to pointer to FTSENT
 * using '->' operator is better than using ``(*(*)).'' here.
 */
static int
comp(const FTSENT **a, const FTSENT **b)
{
    u_short ai=(*a)->fts_info;
    u_short bi=(*b)->fts_info;
    
    if (ai==FTS_ERR || ai==FTS_DNR || bi==FTS_ERR || bi==FTS_DNR) return 0;
    if (ai!=FTS_NS && bi==FTS_NS) return -1;
    if (ai==FTS_NS && bi!=FTS_NS) return 1;
    if (ai==FTS_NS && bi==FTS_NS) return namecmp(*a, *b);
    return sortfunc(*a, *b);
}
Пример #18
0
int
sfs_unlink(struct inode *dp, char *name){
  struct inode *ip;
  struct sfs_dirent de;
  uint off;

  vop_ilock(dp);
  // Cannot unlink "." or "..".
  if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
    goto bad;

  if((ip = vop_dirlookup(dp, name, &off)) == 0)
    goto bad;
  vop_ilock(ip);

  if(vop_getnlink(ip) < 1)
    panic("unlink: nlink < 1");
  if(vop_gettype(ip) == T_DIR && !vop_isdirempty(ip)){
    vop_iunlockput(ip);
    goto bad;
  }

  memset(&de, 0, sizeof(de));
  if(vop_write(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
    panic("unlink: writei");
  if(vop_gettype(ip) == T_DIR){
    vop_link_dec(dp);
    vop_iupdate(dp);
  }
  vop_iunlockput(dp);

  vop_link_dec(ip);
  vop_iupdate(ip);
  vop_iunlockput(ip);
  return 0;

  bad:
    vop_iunlockput(dp);
    return -1;
}
Пример #19
0
static int slotcmp(const void *p1, const void *p2)
{
    serverslot_t *s1 = *(serverslot_t **)p1;
    serverslot_t *s2 = *(serverslot_t **)p2;
    int r;

    // sort by validity
    r = statuscmp(s1, s2);
    if (r)
        return r;

    // sort by primary column
    switch (m_servers.list.sortcol) {
    case COL_NAME:
        break;
    case COL_MOD:
        r = namecmp(s1, s2, COL_MOD);
        break;
    case COL_MAP:
        r = namecmp(s1, s2, COL_MAP);
        break;
    case COL_PLAYERS:
        r = playercmp(s1, s2);
        break;
    case COL_RTT:
        r = pingcmp(s1, s2);
        break;
    }
    if (r)
        return r;

    // stabilize sort
    r = namecmp(s1, s2, COL_NAME);
    if (r)
        return r;

    return addresscmp(s1, s2);
}
Пример #20
0
int
acccmp(const FTSENT *a, const FTSENT *b)
{
	if (b->fts_statp->st_atime > a->fts_statp->st_atime)
		return (1);
	else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
		return (-1);
	else if (b->fts_statp->st_atimensec > a->fts_statp->st_atimensec)
		return (1);
	else if (b->fts_statp->st_atimensec < a->fts_statp->st_atimensec)
		return (-1);
	else
		return (namecmp(a, b));
}
Пример #21
0
static FILE *open_index(const char *indexname, const char *name)
{
    char line[1024];
    FILE *f;
    int len = strlen(name);

    f = fopen(indexname, "r");
    if (f != NULL) {
        while (getline(line, sizeof(line), f, 1)) {
            if (*line == '[' && line[1 + len] == ']'
                    &&  !namecmp(line + 1, name, len)) {
                return f;
            }
        }
        fclose(f);
    }
    return NULL;
}
Пример #22
0
static void
lookup_minimal_symbol_mangled (const char *lookup_name,
			       const char *sfile,
			       struct objfile *objfile,
			       struct minimal_symbol **table,
			       unsigned int hash,
			       int (*namecmp) (const char *, const char *),
			       found_minimal_symbols &found)
{
  for (minimal_symbol *msymbol = table[hash];
       msymbol != NULL;
       msymbol = msymbol->hash_next)
    {
      const char *symbol_name = MSYMBOL_LINKAGE_NAME (msymbol);

      if (namecmp (symbol_name, lookup_name) == 0
	  && found.maybe_collect (sfile, objfile, msymbol))
	return;
    }
}
Пример #23
0
void
xdirflush(char *path, int (*namecmp)(char *, char *))
{
	XDir **pp, **xx, *x;
	char *d, *s;
	int n;

	n = strlen(path);
	if(s = strrchr(path, '/'))
		n = s - path;
	d = smprint("%.*s", n, path);
	s = malloc(++n);
	for(pp = &xdirlist; x = *pp; pp = xx){
		xx = &x->next;
		snprint(s, n, "%s", x->path);
		if(namecmp(d, s) == 0){
			*pp = *xx; xx = pp;
			xdircount--;
			freexdir(x);
		}
	}
	free(s);
	free(d);
}
Пример #24
0
/*
 *	Add a client to the tree.
 */
int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
{
	RADCLIENT *old;
	char buffer[INET6_ADDRSTRLEN + 3];

	if (!client) {
		return 0;
	}

	/*
	 *	Hack to fixup wildcard clients
	 */
	if (is_wildcard(&client->ipaddr)) client->ipaddr.prefix = 0;

	fr_ntop(buffer, sizeof(buffer), &client->ipaddr);
	DEBUG3("Adding client %s (%s) to prefix tree %i", buffer, client->longname, client->ipaddr.prefix);

	/*
	 *	If "clients" is NULL, it means add to the global list.
	 */
	if (!clients) {
		/*
		 *	Initialize it, if not done already.
		 */
		if (!root_clients) {
			root_clients = clients_init(NULL);
			if (!root_clients) return 0;
		}
		clients = root_clients;
	}

	/*
	 *	Create a tree for it.
	 */
	if (!clients->trees[client->ipaddr.prefix]) {
		clients->trees[client->ipaddr.prefix] = rbtree_create(client_ipaddr_cmp, NULL, 0);
		if (!clients->trees[client->ipaddr.prefix]) {
			return 0;
		}
	}

#define namecmp(a) ((!old->a && !client->a) || (old->a && client->a && (strcmp(old->a, client->a) == 0)))

	/*
	 *	Cannot insert the same client twice.
	 */
	old = rbtree_finddata(clients->trees[client->ipaddr.prefix], client);
	if (old) {
		/*
		 *	If it's a complete duplicate, then free the new
		 *	one, and return "OK".
		 */
		if ((fr_ipaddr_cmp(&old->ipaddr, &client->ipaddr) == 0) &&
		    (old->ipaddr.prefix == client->ipaddr.prefix) &&
		    namecmp(longname) && namecmp(secret) &&
		    namecmp(shortname) && namecmp(nas_type) &&
		    namecmp(login) && namecmp(password) && namecmp(server) &&
#ifdef WITH_DYNAMIC_CLIENTS
		    (old->lifetime == client->lifetime) &&
		    namecmp(client_server) &&
#endif
#ifdef WITH_COA
		    namecmp(coa_name) &&
		    (old->coa_server == client->coa_server) &&
		    (old->coa_pool == client->coa_pool) &&
#endif
		    (old->message_authenticator == client->message_authenticator)) {
			WARN("Ignoring duplicate client %s", client->longname);
			client_free(client);
			return 1;
		}

		ERROR("Failed to add duplicate client %s",
		       client->shortname);
		return 0;
	}
#undef namecmp

	/*
	 *	Other error adding client: likely is fatal.
	 */
	if (!rbtree_insert(clients->trees[client->ipaddr.prefix], client)) {
		return 0;
	}

#ifdef WITH_STATS
	if (!tree_num) {
		tree_num = rbtree_create(client_num_cmp, NULL, 0);
	}

#ifdef WITH_DYNAMIC_CLIENTS
	/*
	 *	More catching of clients added by rlm_sql.
	 *
	 *	The sql modules sets the dynamic flag BEFORE calling
	 *	us.  The client_from_request() function sets it AFTER
	 *	calling us.
	 */
	if (client->dynamic && (client->lifetime == 0)) {
		RADCLIENT *network;

		/*
		 *	If there IS an enclosing network,
		 *	inherit the lifetime from it.
		 */
		network = client_find(clients, &client->ipaddr, client->proto);
		if (network) {
			client->lifetime = network->lifetime;
		}
	}
#endif

	client->number = tree_num_max;
	tree_num_max++;
	if (tree_num) rbtree_insert(tree_num, client);
#endif

	if (client->ipaddr.prefix < clients->min_prefix) {
		clients->min_prefix = client->ipaddr.prefix;
	}

	(void) talloc_steal(clients, client); /* reparent it */

	return 1;
}
Пример #25
0
int main(void)
{
    FILE* f;
    char str[MS];
    int k;
    struct mylist* head=NULL, *head1 = NULL, *headr=NULL, *headr1=NULL, *kwn, *cir, *cir1;
    struct anime* mas=NULL, *mas1=NULL, *masr = NULL, *masr1 = NULL;
    setlocale(0, "russian");

    printf("Тестирование функции readline из mystruct.c\n");
    f = fopen("testread/test1.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "bbbb") == 0 && k == 4)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    fclose(f);

    f = fopen("testread/test2.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "bbbbb") == 0 && k == 5)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    fclose(f);

    f = fopen("testread/test3.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "aba aba b") == 0 && k == 9)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    fclose(f);

    f = fopen("testread/test4.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "trata trata") == 0 && k == 11)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    fclose(f);

    f = fopen("testread/test5.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "") == 0 && k == 0)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    fclose(f);

    f = fopen("testread/test6.txt","r");
    k = freadline(f, str, 3);
    if (strcmp(str, "AA") == 0 && k == 2)
        printf("\nTest 6 - OK!");
    else
        printf("\nTest 6 - FAILED!");
    fclose(f);



    printf("\n\nТестирование функции readdate из mystruct.c\n");
    mas = malloc(sizeof(struct anime));
    f = fopen("testread/test_d.txt","r");
    readdate(f, mas);
    if (strcmp(mas->name, "One Punch Man") == 0 && strcmp(mas->stud, "Madhouse") == 0 && mas->year==2015)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    readdate(f, mas);
    if (strcmp(mas->name, "Overlord") == 0 && strcmp(mas->stud, "Mad House") == 0 && mas->year==2014)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    fclose(f);
    free(mas);  mas = NULL;


    printf("\n\nТестирование функции readfile из mystruct.c\n");
    mas = NULL;
    f = fopen("testread/test_d.txt","r");
    mas = readfile(f, &k);
    if (strcmp(mas->name, "One Punch Man") == 0 && strcmp(mas->stud, "Madhouse") == 0 && mas->year==2015 && k == 2 &&
        strcmp((mas+1)->name, "Overlord") == 0 && strcmp((mas+1)->stud, "Mad House") == 0 && (mas+1)->year==2014)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    fclose(f);
    mas = NULL;
    f = fopen("testread/test_de.txt","r");
    mas = readfile(f, &k);
    if (mas == NULL && k == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    fclose(f);


    printf("\n\nТестирование функции formlist из mystruct.c\n");
    mas = NULL;
    f = fopen("testread/test_d.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (strcmp(((struct anime*) head->date)->name, "One Punch Man") == 0 && strcmp(((struct anime*) head->date)->stud, "Madhouse") == 0 && ((struct anime*) head->date)->year==2015 && k == 2 &&
        strcmp(((struct anime*) head->next->date)->name, "Overlord") == 0 && strcmp(((struct anime*) head->next->date)->stud, "Mad House") == 0 && ((struct anime*) head->next->date)->year==2014)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    fclose(f);

    mas = NULL;
    head = NULL;
    f = fopen("testread/test_de.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (head == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    fclose(f);

    printf("\n\nТестирование функции namecmp из mystruct.c\n");
    mas = malloc(2*sizeof(struct anime));
    snprintf(mas->name, MS, "AAA");
    snprintf(mas->stud, MS, "A");
    mas->year = 0;
    snprintf((mas+1)->name, MS, "BBB");
    snprintf((mas+1)->stud, MS, "A");
    mas->year = 0;
    head = create_mylist(mas);
    head1 = create_mylist(mas+1);
    if (namecmp(head, head1) < 0)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    snprintf(mas->name, MS, "CCC");
    if (namecmp(head, head1) > 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    snprintf((mas+1)->name, MS, "CCC");
    if (namecmp(head, head1) == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    mas = NULL;
    free(head);
    head = NULL;
    free(head1);
    head1 = NULL;

    printf("\n\nТестирование функции lookup из exfunclo.c\n");
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;

    head1 = create_mylist(mas1);
    f = fopen("testlook/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    free(head1);
    head1 = create_mylist(mas1);
    f = fopen("testlook/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) == NULL)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);

    printf("\n\nТестирование функции insert из exfunclo.c\n");
    f = fopen("testins/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testins/test11.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    insert(&head, head1);
    int s = 0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    free_list(headr); headr = NULL;
    free(masr);  masr = NULL;
    free(mas1);  mas1 = NULL;
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;
    head1 = create_mylist(mas1);
    f = fopen("testins/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testins/test12.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    kwn = head->next->next->next;
    insert(&kwn, head1);
    s = 0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    free_list(headr); headr = NULL;
    free(masr);  masr = NULL;
    free(mas1);  mas1 = NULL;

    f = fopen("testins/test2.txt","r");
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;
    head1 = create_mylist(mas1);
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    insert(&head, head1);
    if (allcmp(head, head1) == 0 && head->next == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas1);  mas1 = NULL;
    free(mas);  mas = NULL;
    free(head); head = NULL;

    printf("\n\nТестирование функции remove_duplicates из exfunclo.c\n");
    f = fopen("testrmd/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test1r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    if (head == headr && head == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test2r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test3r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test4r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    printf("\n\nТестирование функции front_back_split из exfunclo.c\n");
    headr = front_back_split(head);
    if (head == NULL && headr == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");

    f = fopen("testfbs/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test1r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test1r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testfbs/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test2r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test2r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testfbs/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test3r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test3r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;


    printf("\n\nТестирование функции sorted_merge из exfunclo.c");
    f = fopen("testmerge/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test1a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test1r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 1 - OK!");
    else
        printf("\nTest 1 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test2a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test2r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    //free_list(headr);
    headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test3a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test3r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test4a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test4r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test5a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test5r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test6.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test6a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test6r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 6 - OK!");
    else
        printf("\nTest 6 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test7.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test7a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test7r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 7 - OK!");
    else
        printf("\nTest 7 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test8.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test8a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test8r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 8 - OK!");
    else
        printf("\nTest 8 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    printf("\n\nТестирование функции merge_sort из exfunclo.c\n");
    f = fopen("testbacisort/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test1r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test2r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test3r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test4r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head = merge_sort(head, namecmp);
    s=0;
    if (head == headr && head == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test5r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    return 0;
}
Пример #26
0
static int
xdirread0(char **path, int (*namecmp)(char *, char *), Dir **d)
{
	XDir *x, *p;
	int fd, n;
	Dir *t;

	t = nil;
	for(p = nil, x = xdirlist; x; p=x, x=x->next){
		if(namecmp(x->path, *path))
			continue;
		if(x == xdirlist)
			xdirlist = x->next;
		else
			p->next = x->next;
		while(t = dirstat(x->path)){
			if(qidcmp(&t->qid, &x->qid))
				break;
			free(t);
			x->next = xdirlist;
			xdirlist = x;
			if(strcmp(x->path, *path)){
				free(*path);
				*path = strdup(x->path);
			}
			if(d) *d = x->dir;
			return x->ndir;
		}
		xdircount--;
		freexdir(x);
		break;
	}
	if((fd = open(*path, OREAD)) < 0){
		free(t);
		if(t = xdirstat0(path, namecmp, "directory entry not found"))
			fd = open(*path, OREAD);
	} else if(t == nil)
		t = dirfstat(fd);

	n = -1;
	if(fd < 0 || t == nil)
		goto out;
	if((t->qid.type & QTDIR) == 0){
		werrstr("not a directory");
		goto out;
	}
	if((n = dirreadall(fd, d)) < 0)
		goto out;

	if(xdircount >= 8){
		xdircount--;
		for(p = xdirlist, x = xdirlist->next; x->next; p = x, x = x->next)
			;
		p->next = nil;
		freexdir(x);
	}

	x = mallocz(sizeof(*x), 1);
	x->qid = t->qid;
	x->path = strdup(*path);
	x->ndir = n;
	x->dir = *d;

	x->next = xdirlist;
	xdirlist = x;
	xdircount++;

out:
	if(fd >= 0)
		close(fd);
	free(t);
	return n;
}
Пример #27
0
/** Add a client to a RADCLIENT_LIST
 *
 * @param clients list to add client to, may be NULL if global client list is being used.
 * @param client to add.
 * @return true on success, false on failure.
 */
bool client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
{
	RADCLIENT *old;
	char buffer[INET6_ADDRSTRLEN + 3];

	if (!client) return false;

	/*
	 *	Hack to fixup wildcard clients
	 *
	 *	If the IP is all zeros, with a 32 or 128 bit netmask
	 *	assume the user meant to configure 0.0.0.0/0 instead
	 *	of 0.0.0.0/32 - which would require the src IP of
	 *	the client to be all zeros.
	 */
	if (fr_inaddr_any(&client->ipaddr) == 1) switch (client->ipaddr.af) {
	case AF_INET:
		if (client->ipaddr.prefix == 32) client->ipaddr.prefix = 0;
		break;

	case AF_INET6:
		if (client->ipaddr.prefix == 128) client->ipaddr.prefix = 0;
		break;

	default:
		rad_assert(0);
	}

	fr_ntop(buffer, sizeof(buffer), &client->ipaddr);
	DEBUG3("Adding client %s (%s) to prefix tree %i", buffer, client->longname, client->ipaddr.prefix);

	/*
	 *	If the client also defines a server, do that now.
	 */
	if (client->defines_coa_server) if (!realm_home_server_add(client->coa_server)) return false;

	/*
	 *	If "clients" is NULL, it means add to the global list,
	 *	unless we're trying to add it to a virtual server...
	 */
	if (!clients) {
		if (client->server != NULL) {
			CONF_SECTION *cs;

			cs = cf_section_sub_find_name2(main_config.config, "server", client->server);
			if (!cs) {
				ERROR("Failed to find virtual server %s", client->server);
				return false;
			}

			/*
			 *	If the client list already exists, use that.
			 *	Otherwise, create a new client list.
			 */
			clients = cf_data_find(cs, "clients");
			if (!clients) {
				clients = client_list_init(cs);
				if (!clients) {
					ERROR("Out of memory");
					return false;
				}

				if (cf_data_add(cs, "clients", clients, (void (*)(void *)) client_list_free) < 0) {
					ERROR("Failed to associate clients with virtual server %s", client->server);
					client_list_free(clients);
					return false;
				}
			}

		} else {
			/*
			 *	Initialize the global list, if not done already.
			 */
			if (!root_clients) {
				root_clients = client_list_init(NULL);
				if (!root_clients) return false;
			}
			clients = root_clients;
		}
	}

	/*
	 *	Create a tree for it.
	 */
	if (!clients->trees[client->ipaddr.prefix]) {
		clients->trees[client->ipaddr.prefix] = rbtree_create(clients, client_ipaddr_cmp, NULL, 0);
		if (!clients->trees[client->ipaddr.prefix]) {
			return false;
		}
	}

#define namecmp(a) ((!old->a && !client->a) || (old->a && client->a && (strcmp(old->a, client->a) == 0)))

	/*
	 *	Cannot insert the same client twice.
	 */
	old = rbtree_finddata(clients->trees[client->ipaddr.prefix], client);
	if (old) {
		/*
		 *	If it's a complete duplicate, then free the new
		 *	one, and return "OK".
		 */
		if ((fr_ipaddr_cmp(&old->ipaddr, &client->ipaddr) == 0) &&
		    (old->ipaddr.prefix == client->ipaddr.prefix) &&
		    namecmp(longname) && namecmp(secret) &&
		    namecmp(shortname) && namecmp(nas_type) &&
		    namecmp(login) && namecmp(password) && namecmp(server) &&
#ifdef WITH_DYNAMIC_CLIENTS
		    (old->lifetime == client->lifetime) &&
		    namecmp(client_server) &&
#endif
#ifdef WITH_COA
		    namecmp(coa_name) &&
		    (old->coa_server == client->coa_server) &&
		    (old->coa_pool == client->coa_pool) &&
#endif
		    (old->message_authenticator == client->message_authenticator)) {
			WARN("Ignoring duplicate client %s", client->longname);
			client_free(client);
			return true;
		}

		ERROR("Failed to add duplicate client %s", client->shortname);
		return false;
	}
#undef namecmp

	/*
	 *	Other error adding client: likely is fatal.
	 */
	if (!rbtree_insert(clients->trees[client->ipaddr.prefix], client)) {
		return false;
	}

#ifdef WITH_STATS
	if (!tree_num) {
		tree_num = rbtree_create(clients, client_num_cmp, NULL, 0);
	}

#ifdef WITH_DYNAMIC_CLIENTS
	/*
	 *	More catching of clients added by rlm_sql.
	 *
	 *	The sql modules sets the dynamic flag BEFORE calling
	 *	us.  The client_afrom_request() function sets it AFTER
	 *	calling us.
	 */
	if (client->dynamic && (client->lifetime == 0)) {
		RADCLIENT *network;

		/*
		 *	If there IS an enclosing network,
		 *	inherit the lifetime from it.
		 */
		network = client_find(clients, &client->ipaddr, client->proto);
		if (network) {
			client->lifetime = network->lifetime;
		}
	}
#endif

	client->number = tree_num_max;
	tree_num_max++;
	if (tree_num) rbtree_insert(tree_num, client);
#endif

	if (client->ipaddr.prefix < clients->min_prefix) {
		clients->min_prefix = client->ipaddr.prefix;
	}

	(void) talloc_steal(clients, client); /* reparent it */

	return true;
}
Пример #28
0
Inode *Ext2Inode::SearchDirAndAct(const char *lookup_name, enum Inode::action action, ino_t inum)
{
    //anvil_syslog(0, "Ext2Inode::SearchDir %s dirsize=%d\n", lookup_name, m_size);

    Ext2Inode *inode = nullptr;

    // Now search the pages that hold the directory
    off_t offset_of_page = 0;
    off_t offset_of_space = 0;
    off_t offs = 0;
    uint32_t remains = m_size;

    // If we are creating work out how much space we need
    off_t needed_space;

    if (action == Inode::action::create)
    {
        needed_space = (8 + strlen(lookup_name) + 3) & ~0x3;
        anvil_syslog(0, "Ext2Inode::SearchDir space needed = %d\n", needed_space);
    }

    while (1)
    {
        Page *p = GetPage(offs);
        char *addr = p->GetAddr();
        struct ext2_direntry *item = (struct ext2_direntry *)addr;

        // Figure out how much of this page is used
        struct ext2_direntry *end = (struct ext2_direntry *)(addr + (remains > __PAGESIZE ? __PAGESIZE : remains));
        while (item < end)
        {
            char found_name[1024];
            memcpy(found_name, item+1, item->d_namelen);
            found_name[item->d_namelen] = 0;
            //anvil_syslog(0, "found_name: %.20s - ", found_name);
            int space = item->d_reclen - item->d_namelen - 8;
            if (action == Inode::action::create && offset_of_page == 0 && needed_space <= space)
            {
                offset_of_page = offs;
                offset_of_space = (char *)item - addr;
            }
            //anvil_syslog(0, "inode=%d reclen=%d namelen=%d type=%d SPACE=%d\n", item->d_inode, item->d_reclen, item->d_namelen, item->d_type, space);
            if (!namecmp(found_name, lookup_name))
            {
                // It's a match
                inode = new Ext2Inode((Ext2filesystem *)m_fs, item->d_inode);
                inode->Read();
                //anvil_syslog(0, "Returning inode %016lx\n", inode);
                return inode;
            }
            item = (ext2_direntry *)((char *)item + item->d_reclen);
            //anvil_syslog(0, "item=%016lx end=%016lx\n", item, end);
        }
        offs += __PAGESIZE;
        if (offs > m_size)
            break;
    }

    if (action == Inode::action::create)
    {
        if (offset_of_page == 0)
        {
            // Todo: create a new page



        }
        Page *p = GetPage(offset_of_page);
        char *addr = p->GetAddr();
        struct ext2_direntry *item = (struct ext2_direntry *)addr;
        item = (ext2_direntry *)((char *)item + offset_of_space);
        off_t space = item->d_reclen - item->d_namelen - 8;
        off_t actual_space_needed = (8 + item->d_namelen + 3) & ~0x3;
        anvil_syslog(0, "CREATING: space_needed=%d\n", needed_space);
        anvil_syslog(0, "CREATING: inode=%d reclen=%d namelen=%d type=%d SPACE=%d\n",
                item->d_inode, item->d_reclen, item->d_namelen, item->d_type, space);

        anvil_syslog(0, "inode=%d reclen=%d namelen=%d type=%d\n", item->d_inode, item->d_reclen, item->d_namelen, item->d_type);

        struct ext2_direntry *new_item = (struct ext2_direntry *)((char *)item + actual_space_needed);
        new_item->d_inode = inum;
        new_item->d_namelen = strlen(lookup_name);
        memcpy(new_item+1, lookup_name, new_item->d_namelen);
        new_item->d_reclen = item->d_reclen - actual_space_needed;
        new_item->d_type = 0;

        // change it
        item->d_reclen = actual_space_needed;

        anvil_syslog(0, "inode=%d reclen=%d namelen=%d type=%d\n", item->d_inode, item->d_reclen, item->d_namelen, item->d_type);
        anvil_syslog(0, "inode=%d reclen=%d namelen=%d type=%d\n", new_item->d_inode, new_item->d_reclen, new_item->d_namelen, new_item->d_type);

        p->set_dirty(0xff);
        p->flush();

        inode = new Ext2Inode((Ext2filesystem *)m_fs, new_item->d_inode);
        inode->Read();
        anvil_syslog(0, "Returning inode %016lx\n", inode);
        return inode;
    }

    //anvil_syslog(0, "Returning null\n");
    return NULL;
}
Пример #29
0
/*
 * initialize an entirely new index
 */
Index *
newindex(char *name, ISect **sects, int n)
{
	Index *ix;
	AMap *smap;
	u64int nb;
	u32int div, ub, xb, start, stop, blocksize, tabsize;
	int i, j;

	if(n < 1){
		seterr(EOk, "creating index with no index sections");
		return nil;
	}

	/*
	 * compute the total buckets available in the index,
	 * and the total buckets which are used.
	 */
	nb = 0;
	blocksize = sects[0]->blocksize;
	tabsize = sects[0]->tabsize;
	for(i = 0; i < n; i++){
		/*
		 * allow index, start, and stop to be set if index is correct
		 * and start and stop are what we would have picked.
		 * this allows calling fmtindex to reformat the index after
		 * replacing a bad index section with a freshly formatted one.
		 * start and stop are checked below.
		 */
		if(sects[i]->index[0] != '\0' && strcmp(sects[i]->index, name) != 0){
			seterr(EOk, "creating new index using non-empty section %s", sects[i]->name);
			return nil;
		}
		if(blocksize != sects[i]->blocksize){
			seterr(EOk, "mismatched block sizes in index sections");
			return nil;
		}
		if(tabsize != sects[i]->tabsize){
			seterr(EOk, "mismatched config table sizes in index sections");
			return nil;
		}
		nb += sects[i]->blocks;
	}

	/*
	 * check for duplicate names
	 */
	for(i = 0; i < n; i++){
		for(j = i + 1; j < n; j++){
			if(namecmp(sects[i]->name, sects[j]->name) == 0){
				seterr(EOk, "duplicate section name %s for index %s", sects[i]->name, name);
				return nil;
			}
		}
	}

	if(nb >= ((u64int)1 << 32)){
		fprint(2, "%s: index is 2^32 blocks or more; ignoring some of it\n",
			argv0);
		nb = ((u64int)1 << 32) - 1;
	}

	div = (((u64int)1 << 32) + nb - 1) / nb;
	if(div < 100){
		fprint(2, "%s: index divisor %d too coarse; "
			"index larger than needed, ignoring some of it\n",
			argv0, div);
		div = 100;
		nb = (((u64int)1 << 32) - 1) / (100 - 1);
	}
	ub = (((u64int)1 << 32) - 1) / div + 1;
	if(ub > nb){
		seterr(EBug, "index initialization math wrong");
		return nil;
	}
	xb = nb - ub;

	/*
	 * initialize each of the index sections
	 * and the section map table
	 */
	smap = MKNZ(AMap, n);
	if(smap == nil){
		seterr(EOk, "can't create new index: out of memory");
		return nil;
	}
	start = 0;
	for(i = 0; i < n; i++){
		stop = start + sects[i]->blocks - xb / n;
		if(i == n - 1)
			stop = ub;

		if(sects[i]->start != 0 || sects[i]->stop != 0)
		if(sects[i]->start != start || sects[i]->stop != stop){
			seterr(EOk, "creating new index using non-empty section %s", sects[i]->name);
			return nil;
		}

		sects[i]->start = start;
		sects[i]->stop = stop;
		namecp(sects[i]->index, name);

		smap[i].start = start;
		smap[i].stop = stop;
		namecp(smap[i].name, sects[i]->name);
		start = stop;
	}

	/*
	 * initialize the index itself
	 */
	ix = MKZ(Index);
	if(ix == nil){
		seterr(EOk, "can't create new index: out of memory");
		free(smap);
		return nil;
	}
	ix->version = IndexVersion;
	namecp(ix->name, name);
	ix->sects = sects;
	ix->smap = smap;
	ix->nsects = n;
	ix->blocksize = blocksize;
	ix->buckets = ub;
	ix->tabsize = tabsize;
	ix->div = div;

	if(initindex1(ix) < 0){
		free(smap);
		return nil;
	}

	return ix;
}
Пример #30
0
Index*
initindex(char *name, ISect **sects, int n)
{
	IFile f;
	Index *ix;
	ISect *is;
	u32int last, blocksize, tabsize;
	int i;

	if(n <= 0){
fprint(2, "bad n\n");
		seterr(EOk, "no index sections to initialize index");
		return nil;
	}
	ix = MKZ(Index);
	if(ix == nil){
fprint(2, "no mem\n");
		seterr(EOk, "can't initialize index: out of memory");
		freeindex(ix);
		return nil;
	}

	tabsize = sects[0]->tabsize;
	if(partifile(&f, sects[0]->part, sects[0]->tabbase, tabsize) < 0)
		return nil;
	if(parseindex(&f, ix) < 0){
		freeifile(&f);
		freeindex(ix);
		return nil;
	}
	freeifile(&f);
	if(namecmp(ix->name, name) != 0){
		seterr(ECorrupt, "mismatched index name: found %s expected %s", ix->name, name);
		return nil;
	}
	if(ix->nsects != n){
		seterr(ECorrupt, "mismatched number index sections: found %d expected %d", n, ix->nsects);
		freeindex(ix);
		return nil;
	}
	ix->sects = sects;
	last = 0;
	blocksize = ix->blocksize;
	for(i = 0; i < ix->nsects; i++){
		is = sects[i];
		if(namecmp(ix->name, is->index) != 0
		|| is->blocksize != blocksize
		|| is->tabsize != tabsize
		|| namecmp(is->name, ix->smap[i].name) != 0
		|| is->start != ix->smap[i].start
		|| is->stop != ix->smap[i].stop
		|| last != is->start
		|| is->start > is->stop){
			seterr(ECorrupt, "inconsistent index sections in %s", ix->name);
			freeindex(ix);
			return nil;
		}
		last = is->stop;
	}
	ix->tabsize = tabsize;
	ix->buckets = last;

	if(initindex1(ix) < 0){
		freeindex(ix);
		return nil;
	}

	ix->arenas = MKNZ(Arena*, ix->narenas);
	if(maparenas(ix->amap, ix->arenas, ix->narenas, ix->name) < 0){
		freeindex(ix);
		return nil;
	}

	return ix;
}