Ejemplo n.º 1
0
void gen(int end)
{
  int ii;
  unsigned int cnt = 0;
  unsigned char isFinish = 0;
  unsigned char *tbl = (unsigned char*)calloc(1, end*sizeof(char));

  if(tbl == NULL)
      goto exit;

  do{
      int d = rand()%end;
      if(check_dup(tbl, cnt, d) == 0)
        tbl[cnt++] = d;
      if(cnt == end)
        isFinish = 1;
  }while(!isFinish);

  for(ii=0; ii<end; ii++)
    printf("%u, ", tbl[ii]);
  printf("\n");

  free(tbl);

exit:
  return;
}
Ejemplo n.º 2
0
int		main(int argc, char **argv)
{
	t_info	info;
	int		n;

	init_info(&info);
	n = check_args(argc, argv);
	if (n)
		info.elem_a = store_stack(&info.a, n, ft_strsplit(argv[1], ' '), !(n));
	else
		info.elem_a = store_stack(&info.a, argc, argv, !(n));
	info.max = info.elem_a;
	check_dup(info.a);
	info.median = median(&info);
	run_sort_algorithms(&info);
	return (0);
}
Ejemplo n.º 3
0
int		redir_left(t_way *pt)
{
  int		fd_files;
  int		fd_copy;
  int		acces;

  fd_files = open(pt->c_file, O_RDWR);
  if ((acces = check_access(pt->c_file)) == -1)
    return (-1);
  if (fd_files != -1)
    {
      fd_copy = check_dup(fd_files);
      if (fd_copy == -1)
	return (-1);
      check_dup2(fd_files, 0);
      check_dup2(fd_copy, 0);
    }
  if (fd_files == -1)
    return (-1);
  return (fd_files);
}
Ejemplo n.º 4
0
int		redir_right(t_way *pt)
{
  int		right;
  int		acces;
  int		fd_files;
  int		fd_copy;

  right = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
  fd_files = open(pt->c_file, O_CREAT | O_WRONLY | O_TRUNC, right);
  if ((acces = check_access(pt->c_file)) == -1)
    return (-1);
  if (fd_files != -1)
    {
      fd_copy = check_dup(fd_files);
      if (fd_copy == -1)
	return (-1);
      check_dup2(fd_files, 1);
      check_dup2(fd_copy, 1);
    }
  if (fd_files == -1)
    return (-1);
  return (fd_files);
}
Ejemplo n.º 5
0
int
main(int argc, char *argv[])
{
	char dir_path[PATH_MAX], file_path[PATH_MAX];
	int dummy, fd;
	size_t size;

	aio_present = 0;
	size = sizeof(dummy);
	if (sysctlbyname("vfs.aio", &dummy, &size, NULL, 0) < 0) {
		if (errno == EISDIR)
			aio_present = 1;
	}

	strlcpy(dir_path, "/tmp/open-dir.XXXXXXXXXXX", sizeof(dir_path));
	if (mkdtemp(dir_path) == NULL)
		err(-1, "mkdtemp");
	if (chmod(dir_path, PERM_DIR) < 0) {
		warn("chmod %s", dir_path);
		(void)rmdir(dir_path);
		exit(-1);
	}
	strlcpy(file_path, "/tmp/open-file.XXXXXXXXXXX", sizeof(file_path));
	fd = mkstemp(file_path);
	if (fd < 0) {
		warn("mkstemp");
		(void)rmdir(dir_path);
		exit(-1);
	}
	close(fd);
	if (chmod(file_path, PERM_FILE) < 0) {
		warn("chmod %s", file_path);
		(void)unlink(file_path);
		(void)rmdir(dir_path);
		exit(-1);
	}
	check_directory_open_modes(dir_path, file_modes, file_modes_count);

	check_dup("check_dup_dir", dir_path, dir_modes, dir_modes_count);
	check_dup("check_dup_file", file_path, file_modes, file_modes_count);

	check_dup2("check_dup2_dir", dir_path, dir_modes, dir_modes_count);
	check_dup2("check_dup2_file", file_path, file_modes,
	    file_modes_count);

	check_fchdir("check_fchdir", dir_path, dir_modes, dir_modes_count);

	check_fchflags("check_fchflags_dir", dir_path, dir_modes,
	    dir_modes_count);
	check_fchflags("check_fchflags_file", file_path, file_modes,
	    file_modes_count);

	check_fchmod("check_fchmod_dir", dir_path, PERM_DIR, dir_modes,
	    dir_modes_count);
	check_fchmod("check_fchmod_file", file_path, PERM_FILE, file_modes,
	    file_modes_count);

	check_fchown("check_fchown_dir", dir_path, dir_modes,
	    dir_modes_count);
	check_fchown("check_fchown_file", file_path, file_modes,
	    file_modes_count);

	check_flock("check_flock_dir", dir_path, dir_modes, dir_modes_count);
	check_flock("check_flock_file", file_path, file_modes,
	    file_modes_count);

	check_fpathconf("check_fpathconf_dir", dir_path, dir_modes,
	    dir_modes_count);
	check_fpathconf("check_fpathconf_file", file_path, file_modes,
	    file_modes_count);

	check_fstat("check_fstat_dir", dir_path, dir_modes, dir_modes_count);
	check_fstat("check_fstat_file", file_path, file_modes,
	    file_modes_count);

	check_fstatfs("check_fstatfs_dir", dir_path, dir_modes,
	    dir_modes_count);
	check_fstatfs("check_fstatfs_file", file_path, file_modes,
	    file_modes_count);

	check_fsync("check_fsync_dir", dir_path, dir_modes, dir_modes_count);
	check_fsync("check_fsync_file", file_path, file_modes,
	    file_modes_count);

	check_ftruncate("check_ftruncate_dir", dir_path, dir_modes,
	    dir_modes_count);
	check_ftruncate("check_ftruncate_file", file_path, file_modes,
	    file_modes_count);

	check_futimes("check_futimes_dir", dir_path, dir_modes,
	    dir_modes_count);
	check_futimes("check_futimes_file", file_path, file_modes,
	    file_modes_count);

	check_lseek("check_lseek_dir", dir_path, dir_modes, dir_modes_count);
	check_lseek("check_lseek_file", file_path, file_modes,
	    file_modes_count);

	check_getdents("check_getdents_dir", dir_path, 1, dir_modes,
	    dir_modes_count);
	check_getdents("check_getdents_file", file_path, 0, file_modes,
	    file_modes_count);

	check_sendfile("check_sendfile_dir", dir_path, 1, dir_modes,
	    dir_modes_count);
	check_sendfile("check_sendfile_file", file_path, 0, file_modes,
	    file_modes_count);

	check_write("check_write_dir", write, dir_path, dir_modes,
	    dir_modes_count);
	check_write("check_write_file", write, file_path, file_modes,
	    file_modes_count);

	check_write("check_writev_dir", writev_wrapper, dir_path, dir_modes,
	    dir_modes_count);
	check_write("check_writev_file", writev_wrapper, file_path,
	    file_modes, file_modes_count);

	check_write("check_pwrite_dir", pwrite_wrapper, dir_path, dir_modes,
	    dir_modes_count);
	check_write("check_pwrite_file", pwrite_wrapper, file_path,
	    file_modes, file_modes_count);

	check_write("check_pwritev_dir", pwritev_wrapper, dir_path,
	    dir_modes, dir_modes_count);
	check_write("check_pwritev_file", pwritev_wrapper, file_path,
	    file_modes, file_modes_count);

	if (aio_present) {
		check_write("check_aio_write_dir", aio_write_wrapper,
		    dir_path, dir_modes, dir_modes_count);
		check_write("check_aio_write_file", aio_write_wrapper,
		    file_path, file_modes, file_modes_count);
	}

	check_read("check_read_dir", read, dir_path, dir_modes,
	    dir_modes_count);
	check_read("check_read_file", read, file_path, file_modes,
	    file_modes_count);

	check_read("check_readv_dir", readv_wrapper, dir_path, dir_modes,
	    dir_modes_count);
	check_read("check_readv_file", readv_wrapper, file_path,
	    file_modes, file_modes_count);

	check_read("check_pread_dir", pread_wrapper, dir_path, dir_modes,
	    dir_modes_count);
	check_read("check_pread_file", pread_wrapper, file_path,
	    file_modes, file_modes_count);

	check_read("check_preadv_dir", preadv_wrapper, dir_path,
	    dir_modes, dir_modes_count);
	check_read("check_preadv_file", preadv_wrapper, file_path,
	    file_modes, file_modes_count);

	if (aio_present) {
		check_read("check_aio_read_dir", aio_read_wrapper, dir_path,
		    dir_modes, dir_modes_count);
		check_read("check_aio_read_file", aio_read_wrapper,
		    file_path, file_modes, file_modes_count);
	}

	check_mmap_read("check_mmap_read_dir", dir_path, 1, dir_modes,
	    dir_modes_count);
	check_mmap_read("check_mmap_read_file", file_path, 0, file_modes,
	    file_modes_count);

	check_mmap_write("check_mmap_write_dir", dir_path, dir_modes,
	    dir_modes_count);
	check_mmap_write("check_mmap_write_file", file_path, file_modes,
	    file_modes_count);

	check_mmap_exec("check_mmap_exec_dir", dir_path, 1, dir_modes,
	    dir_modes_count);
	check_mmap_exec("check_mmap_exec_file", file_path, 0, file_modes,
	    file_modes_count);

	check_mmap_write_private("check_mmap_write_private_dir", dir_path, 1,
	    dir_modes, dir_modes_count);
	check_mmap_write_private("check_mmap_write_private_file", file_path,
	    0, file_modes, file_modes_count);

	(void)unlink(file_path);
	(void)rmdir(dir_path);
	exit(0);
}
Ejemplo n.º 6
0
/*
 * Read a list of gateways from /etc/gateways and add them to our tables.
 *
 * This file contains a list of "remote" gateways.  That is usually
 * a gateway which we cannot immediately determine if it is present or
 * not as we can do for those provided by directly connected hardware.
 *
 * If a gateway is marked "passive" in the file, then we assume it
 * does not understand RIP and assume it is always present.  Those
 * not marked passive are treated as if they were directly connected
 * and assumed to be broken if they do not send us advertisements.
 * All remote interfaces are added to our list, and those not marked
 * passive are sent routing updates.
 *
 * A passive interface can also be local, hardware interface exempt
 * from RIP.
 */
void
gwkludge(void)
{
#define	STR2(x)	#x
#define	STR(x)	STR2(x)

#define	NETHOST_LEN	4
#define	DNAME_LEN	MAXHOSTNAMELEN
#define	GNAME_LEN	MAXHOSTNAMELEN
#define	QUAL_LEN	8

	FILE *fp;
	char *p, *lptr;
	const char *cp;
	char lbuf[PARMS_MAXLINELEN], net_host[NETHOST_LEN + 1];
	char dname[MAXHOSTNAMELEN + 1];
	char gname[MAXHOSTNAMELEN + 1], qual[QUAL_LEN +1];
	struct interface *ifp;
	uint32_t dst, netmask, gate;
	int n;
	uint32_t lnum;
	struct stat sb;
	uint32_t state, metric;
	boolean_t default_dst;


	fp = fopen(PATH_GATEWAYS, "r");
	if (fp == NULL)
		return;

	if (0 > fstat(fileno(fp), &sb)) {
		msglog("fstat() failed: %s for  "PATH_GATEWAYS,
		    rip_strerror(errno));
		(void) fclose(fp);
		return;
	}

	for (lnum = 1; ; lnum++) {
		if (NULL == fgets(lbuf, sizeof (lbuf), fp))
			break;

		/* Eliminate the /n character at the end of the lbuf */
		if (strlen(lbuf) > 0)
			lbuf[strlen(lbuf) - 1] = '\0';

		/* Move lptr to the first non-space character */
		for (lptr = lbuf; isspace(*lptr); lptr++)
			;

		if (*lptr == '#' || *lptr == '\0')
			continue;

		/* Move p to the end of the line */
		p = lptr + strlen(lptr) - 1;

		/* Skip all trailing spaces except escaped space */
		while (p > lptr && (isspace(*p) && *(p-1) != '\\'))
			p--;

		/* truncate the line to remove trailing spaces */
		*++p = '\0';

		/* notice newfangled parameter lines */
		if (strncasecmp("net", lptr, 3) != 0 &&
		    strncasecmp("host", lptr, 4) != 0) {
			cp = parse_parms(lptr, (sb.st_uid == 0 &&
			    !(sb.st_mode&(S_IRWXG|S_IRWXO))));
			if (cp != 0)
				msglog("%s in line %u of "PATH_GATEWAYS,
				    cp, lnum);
			continue;
		}

		/*
		 * Processes lines of the follwoing format:
		 * net|host <name>[/mask] gateway <Gname> metric <value>
		 * passive|active|extern
		 */
		qual[0] = '\0';
		n = sscanf(lptr, "%"STR(NETHOST_LEN)"s %"STR(DNAME_LEN)
		    "[^ \t] gateway %"STR(GNAME_LEN)"[^ / \t] metric %u %"
		    STR(QUAL_LEN)"s\n", net_host, dname, gname, &metric, qual);
		if (n != 4 && n != 5) {
			msglog("bad "PATH_GATEWAYS" entry \"%s\"; %d values",
			    lptr, n);
			continue;
		}
		if (metric >= HOPCNT_INFINITY) {
			msglog("bad metric in "PATH_GATEWAYS" entry \"%s\"",
			    lptr);
			continue;
		}
		default_dst = _B_FALSE;
		if (strcasecmp(net_host, "host") == 0) {
			if (!gethost(dname, &dst)) {
				msglog("bad host \"%s\" in "PATH_GATEWAYS
				    " entry \"%s\"", dname, lptr);
				continue;
			}
			netmask = HOST_MASK;
		} else if (strcasecmp(net_host, "net") == 0) {
			if (!getnet(dname, &dst, &netmask)) {
				msglog("bad net \"%s\" in "PATH_GATEWAYS
				    " entry \"%s\"", dname, lptr);
				continue;
			}
			default_dst = (dst == RIP_DEFAULT);
			dst = htonl(dst); /* make network # into IP address */
		} else {
			msglog("bad \"%s\" in "PATH_GATEWAYS
			    " entry \"%s\"", net_host, lptr);
			continue;
		}

		if (!gethost(gname, &gate)) {
			msglog("bad gateway \"%s\" in "PATH_GATEWAYS
			    " entry \"%s\"", gname, lptr);
			continue;
		}

		if (strcasecmp(qual, "passive") == 0) {
			/*
			 * Passive entries are not placed in our tables,
			 * only the kernel's, so we don't copy all of the
			 * external routing information within a net.
			 * Internal machines should use the default
			 * route to a suitable gateway (like us).
			 */
			state = IS_REMOTE | IS_PASSIVE;
			if (metric == 0)
				metric = 1;

		} else if (strcasecmp(qual, "external") == 0) {
			/*
			 * External entries are handled by other means
			 * such as EGP, and are placed only in the daemon
			 * tables to prevent overriding them with something
			 * else.
			 */
			(void) strlcpy(qual, "external", sizeof (qual));
			state = IS_REMOTE | IS_PASSIVE | IS_EXTERNAL;
			if (metric == 0)
				metric = 1;

		} else if (strcasecmp(qual, "active") == 0 ||
		    qual[0] == '\0') {

			if (default_dst) {
				msglog("bad net \"%s\" in "PATH_GATEWAYS
				    " entry \"%s\"-- cannot be default",
				    dname, lptr);
				continue;
			}

			if (metric != 0) {
				/*
				 * Entries that are neither "passive" nor
				 * "external" are "remote" and must behave
				 * like physical interfaces.  If they are not
				 * heard from regularly, they are deleted.
				 */
				state = IS_REMOTE;
			} else {
				/*
				 * "remote" entries with a metric of 0
				 * are aliases for our own interfaces
				 */
				state = IS_REMOTE | IS_PASSIVE | IS_ALIAS;
			}

		} else {
			msglog("bad "PATH_GATEWAYS" entry \"%s\";"
			    " unknown type %s", lptr, qual);
			continue;
		}

		if (0 != (state & (IS_PASSIVE | IS_REMOTE)))
			state |= IS_NO_RDISC;
		if (state & IS_PASSIVE)
			state |= IS_NO_RIP;


		if (default_dst) {
			addroutefordefault(dst, gate, netmask, metric,
			    ((state & IS_EXTERNAL)? RTS_EXTERNAL : 0));
			continue;
		}

		ifp = check_dup(NULL, gate, dst, netmask, 0, _B_FALSE);
		if (ifp != NULL) {
			msglog("duplicate "PATH_GATEWAYS" entry \"%s\"", lptr);
			continue;
		}

		ifp = rtmalloc(sizeof (*ifp), "gwkludge()");
		(void) memset(ifp, 0, sizeof (*ifp));

		ifp->int_state = state;
		if (netmask == HOST_MASK)
			ifp->int_if_flags = IFF_POINTOPOINT | IFF_UP;
		else
			ifp->int_if_flags = IFF_UP;
		ifp->int_act_time = NEVER;
		ifp->int_addr = gate;
		ifp->int_dstaddr = dst;
		ifp->int_mask = netmask;
		ifp->int_ripv1_mask = netmask;
		ifp->int_std_mask = std_mask(gate);
		ifp->int_net = ntohl(dst);
		ifp->int_std_net = ifp->int_net & ifp->int_std_mask;
		ifp->int_std_addr = htonl(ifp->int_std_net);
		ifp->int_metric = metric;
		if (!(state & IS_EXTERNAL) &&
		    ifp->int_mask != ifp->int_std_mask)
			ifp->int_state |= IS_SUBNET;
		(void) snprintf(ifp->int_name, sizeof (ifp->int_name),
		    "remote(%s)", gname);

		if_link(ifp, 0);
	}

	(void) fclose(fp);

	/*
	 * After all of the parameter lines have been read,
	 * apply them to any remote interfaces.
	 */
	for (ifp = ifnet; NULL != ifp; ifp = ifp->int_next) {
		get_parms(ifp);

		tot_interfaces++;
		if (!IS_RIP_OFF(ifp->int_state))
			rip_interfaces++;
		if (!IS_RIP_OUT_OFF(ifp->int_state))
			ripout_interfaces++;

		trace_if("Add", ifp);
	}

}
Ejemplo n.º 7
0
Archivo: input.c Proyecto: Toianoke/SAT
int main(int argc, char ** argv)
{
	if (argc < 2)
  	{
  		printf("Error!");
		return 0; // argument error (but we have to return 0)
  	}
  
  	size_t max_line_len;
  	size_t line_count = count_lines(argv[1], &max_line_len);
  	//printf("Line count: %lu\nMax line len: %lu\n", line_count, max_line_len);
  
  	char *lines[line_count];
  	short **clauses;
  	for (size_t i=0; i<= line_count; i++) 
  	{
    	lines[i] = (char *) malloc(sizeof(char)*max_line_len);

    	if (lines[i] == NULL) 
    	{
      		return 0; // internal error (but we have to return 0)
    	}
  	}

  	FILE *fp = fopen(argv[1], "r");
  	if (fp == NULL) 
  	{
  		printf("Error!");
    	return 0; // file error (but we have to return 0)
  	}
  	size_t c_i = 0;
  	for (size_t i=0; i < line_count;) 
  	{
    	lines[i][c_i] = fgetc(fp);
    	if (lines[i][c_i] == '\n' || lines[i][c_i] == '\0') 
    	{
      		lines[i][c_i+1] = '\0';
      		//printf("Line %lu: %s", i, lines[i]);
      		c_i = -1;
      		i++;
    	}
    	c_i++;
  	}
  
  	int i, cls_cnt, var_cnt, temp, first = 1;
  	int com_done = 0;
  	int init_set = 0;
  	int num_clss = 0;
  	int row = 0;
  	for(i = 0; i <= line_count; i++)
  	{
  		//printf("The index: %i and size: %lu\n", i, line_count);
  		if(lines[i][0] == 'c')
  		{
  			if(com_done == 1)
  			{
  				printf("ERROR: Comment!\n");
  				return -1;
  			}
  		}
  		else if(lines[i][0] == 'p')
  		{
  			com_done = 1;
  			if(init_set == 1)
  			{
  				printf("ERROR already init!\n");
  				return -1;
  			}
  			init_set = 1;
  			char *split;
  			split = strtok(lines[i]," ");
  			char* error;
  			while(split != NULL)
  			{
  				temp = strtoll(split, &error, 10);
				if (temp != 0)
				{
					if(first == 1)
					{
						var_cnt = temp;
						first = 0;
					}
					else
					{
						cls_cnt = temp;
					}		
				}
  				split = strtok(NULL, " ");
  			}
  			if((var_cnt != 0) && (cls_cnt != 0))
  			{
  				
  				//printf("Creating an array.\n");
  				clauses = (short **)malloc(cls_cnt * sizeof(short*));
  				int i, j;
  				for(i = 0; i < cls_cnt; i++)
  					clauses[i] = (short *)malloc((var_cnt + 1)* sizeof(short));
  				for(i = 0; i < cls_cnt; i++)
  					for(j = 0; j <= var_cnt+1; j++)
  					{
  						clauses[i][j] = 0;
  					
  					
  					}
  				//for(i = 0; i < cls_cnt; i++)
  				//{
  				//	for(j = 0; j <= var_cnt; j++)
  				//	{
  				//		printf(" %i ", clauses[i][j]);
  				//	
  				//	
  				//	}
  				//	printf("\n");
  				//}
  				
  				
  			//
  			}
  		}
  		else if((isdigit(lines[i][0])) || (lines[i][0] == '-'))
  		{
  			if(init_set != 1)
  			{
  				printf("ERROR not init!\n");
  				return -1;
  			}
  			char *split;
  			int num_tok = 0;
  			split = strtok(lines[i]," ");
  			
  			short temp_clause[max_line_len];
  			char* error;
  			while(split != NULL)
  			{	
  				//printf("The string: %s\n",split);
  				temp = strtoll(split, &error, 10);
  				if(temp != 0)
  				{
  					if(abs(temp) > var_cnt)
  					{
  						printf("ERROR!\n");
  						return -1;
  					}
  				
  					temp_clause[num_tok] = temp;
  					//printf("The num: %i index: %i\n", temp_clause[num_tok], num_tok);
  					num_tok++;
  				}
  				
  				split = strtok(NULL, " ");	
  			}
  			
  			
  			
  			if(check_dup(temp_clause, num_tok))
  			{
  				printf("ERROR\n");
  				return -1;
  			
  			}
  			
  			
  			
  			
  			int num = 0;
  			int i;
  			for(i = 0; i < num_tok; i++)
  			{
  				clauses[row][i] = temp_clause[i];
  			}
  			
  			//printf("Size of array: %i\n", num_tok);
  			//printf("The index where the array is being saved: %i\n", num_clss);
  			//int j;
  			//for(j = 0; j < num_tok; j++)
  			//{
  			//	printf(" %i ", clauses[row][j]);
  			//}
  			//printf("\n");
  			
  			
  			
  			
  			
  			row++;
  		}
  		
  	}
  	printf("The final array with %i clauses and at the most %i variables in each clause:\n", cls_cnt, var_cnt);
  	int j;
  	
  	
  	for(i = 0; i < cls_cnt; i++)
  	{
  		for(j = 0; j <= var_cnt; j++)
  		{
  			printf(" %i ", clauses[i][j]);
  		}
  		printf("\n");
  	}
  
  fclose(fp);
  return 0;
}
Ejemplo n.º 8
0
static rc_t process(const char* dbname)
{
    rc_t rc;
    KHashFile* hf = NULL;
    rc = KHashFileMake(&hf, NULL);
    if (rc) {
        fprintf(stderr, "Couldn't create KHashFile\n");
        return rc;
    }

    KDirectory* srcdir = NULL;

    rc = KDirectoryNativeDir(&srcdir);
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }

    VDBManager* mgr = NULL;
    rc = VDBManagerMakeUpdate(&mgr, NULL); // NULL=No working directory
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }

    VDatabase* db = NULL;
    rc = VDBManagerOpenDBUpdate(mgr, &db, NULL, dbname);
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }

    const VTable* tbl = NULL;
    rc = VDatabaseOpenTableRead(db, &tbl, "hdrs");
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }

    const VCursor* curs = NULL;
    rc = VTableCreateCursorRead(tbl, &curs);
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }
    uint32_t group_idx = 0; // HDR, TAG, VALUE
    uint32_t hdr_idx = 0;
    uint32_t tag_idx = 0;
    uint32_t value_idx = 0;
    rc = VCursorAddColumn(curs, &group_idx, "GROUP");
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }
    rc = VCursorAddColumn(curs, &hdr_idx, "HDR");
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }
    rc = VCursorAddColumn(curs, &tag_idx, "TAG");
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }
    rc = VCursorAddColumn(curs, &value_idx, "VALUE");
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }

    rc = VCursorOpen(curs);
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }

    int64_t start = 0;
    uint64_t count = 0;
    rc = VCursorIdRange(curs, 0, &start, &count);
    if (rc) {
        fprintf(stderr, "Failed %d %d", __LINE__, rc);
        return rc;
    }
    printf("start=%ld,count=%lu\n", start, count);

    while (count--) {
        uint64_t group;
        uint32_t row_len = 0;
        rc = VCursorReadDirect(curs, start, group_idx, 64, &group, 1,
                               &row_len);
        if (rc) {
            fprintf(stderr, "Failed %d %d", __LINE__, rc);
            return rc;
        }
        printf("group=%lu, row_len=%d\n", group, row_len);

        char hdr[8];
        rc = VCursorReadDirect(curs, start, hdr_idx, 8, &hdr, sizeof(hdr),
                               &row_len);
        if (rc) {
            fprintf(stderr, "Failed %d %d", __LINE__, rc);
            return rc;
        }
        hdr[row_len] = '\0';
        printf("hdr=%s, row_len=%d\n", hdr, row_len);

        char tag[8];
        rc = VCursorReadDirect(curs, start, tag_idx, 8, &tag, sizeof(tag),
                               &row_len);
        if (rc) {
            fprintf(stderr, "Failed %d %d", __LINE__, rc);
            return rc;
        }
        tag[row_len] = '\0';
        printf("tag=%s, row_len=%d\n", tag, row_len);

        char value[8192];
        rc = VCursorReadDirect(curs, start, value_idx, 8, &value,
                               sizeof(value), &row_len);
        if (rc) {
            fprintf(stderr, "Failed %d %d", __LINE__, rc);
            return rc;
        }
        value[row_len] = '\0';
        printf("value=%s, row_len=%d\n", value, row_len);

        if (!strcmp(hdr, "SQ") && !strcmp(tag, "SN")) {
            if (check_dup(hf, "SQ:SN", value)) {
                fprintf(stderr, "Duplicate SQ:SN value '%s'\n", value);
            }
        }

        if (!strcmp(hdr, "RG") && !strcmp(tag, "ID")) {
            if (check_dup(hf, "RG:ID", value)) {
                fprintf(stderr, "Duplicate RG:ID value '%s'\n", value);
            }
        }

        if (!strcmp(hdr, "PG") && !strcmp(tag, "ID")) {
            if (check_dup(hf, "PG:ID", value)) {
                fprintf(stderr, "Duplicate PG:ID value '%s'\n", value);
            }
        }

        start++;
        printf("\n");
    }
    printf("Set has %lu elements\n", KHashFileCount(hf));
    fprintf(stderr, "Made verifier for %s\n", dbname);

    KHashFileDispose(hf);

    VCursorRelease(curs);
    VTableRelease(tbl);
    VDatabaseRelease(db);
    VDBManagerRelease(mgr);
    KDirectoryRelease(srcdir);

    return 0;
}
Ejemplo n.º 9
0
// Add, update, freshen, or delete zip entries in a zip file. argc; /*
//   Number of tokens in command line. argv; /* Command line tokens.
int ZipProcess(struct Globals *pG)
{
  int a; // attributes of zip file
  ulg c; // start of central directory
  struct flist *f; // steps through "found" linked list
  int i; // arg counter, root directory flag
  int k; // next argument type, marked counter, comment size, entry count
  int marks; // replaces k as marked counter
  ulg n; // total of entry len's

  // int o; /* true if there were any ZEN_OPEN errors
  char *p; // steps through option arguments
  int r; // temporary variable
  ulg t; // file time, length of central directory
  //  int           first_listarg = 0;  // index of first arg of "process these files" list
  struct zlist *v; // temporary variable
  struct zlist **w; // pointer to last link in "zfiles" list
  struct zlist *z; // steps through "zfiles" linked list
  int altered; // RP 173 counter for altered comments

  int DestType; // 1.75  destination drive type
  unsigned long TotFiles = 0;
  unsigned /* long */__int64 TotSize = 0;
  unsigned long KeptCnt = 0; // number of 'kept' files
  unsigned __int64 KeptSize = 0; // size of 'kept' files
  unsigned long ofs; // current file offset
  int fsz; // file size;
  int No_File = 0; // 1.75 try if file does not exist
  long hi;

  // Process arguments
  diag("processing lists", pG);

  if (pG->verbose)
  {
    switch (pG->action)
    {
      case ADD:
        diag("action = ADD", pG);
        break;
      case UPDATE:
        diag("action = UPDATE", pG);
        break;
      case FRESHEN:
        diag("action = FRESHEN", pG);
        break;
      case PURGE:
        diag("action = PURGE", pG);
    }

    // zcount is no. of entries in linked-list
    // zfiles is name of the linked-list of filenames for the archive
    Inform(pG, 0, IDIAG, "zcount=%d (no. of files in ZIP already)", pG->zcount);
  }

  // if first_listarg is 0, then we didn't got any fspecs on cmd line
  if (pG->doall && (pG->action == UPDATE || pG->action == FRESHEN))
  {
    // if -update or -freshen with no args, do all, but, when present, apply
    //   filters
    for (z = pG->zfiles; z != NULL; z = z->nxt)
    {
      z->mark = pG->pcount ? filter(z->zname, pG): 1;
    }
  }

  if ((r = check_dup(pG)) != ZEN_OK)
  // remove duplicates in list
    return (ziperr(r, pG));

  // Check option combinations
  // ?????
  if (pG->action == PURGE && (pG->dispose || pG->recurse || pG->key))
    return (ziperr(ZEN_PARMS12, pG));
  if (pG->linkput && pG->dosify)
  {
    Inform(pG, 0, IWARNING, "can't use -y with -k, -y ignored");
    pG->linkput = 0;
  }

  // AllowGrow is the "allow append" indicator
  if (!pG->zcount && ((pG->action == ADD) || (pG->action == UPDATE)))
    pG->AllowGrow = 55;
  //1;   // RP173 - create new file normally

  // if zcount is 0, then zipfile doesn't exist, or is empty
  if (pG->zcount == 0 && ((pG->action != ADD && pG->action != UPDATE) || !pG
    ->AllowGrow))
  {
    // RCV150199 added UPDATE
    Inform(pG, 0, IWARNING, "%s: not found or empty", pG->zipfile);
    if (pG->zcount)
      FREE(pG->zsort);
    return 0;
  }

  if (pG->zcount)
  {
    FREE(pG->zsort);
    pG->zsort = NULL;
  }

  DestType = DriveType(pG->zipfile);
  if (pG->verbose < 0)
    Inform(pG, 0, IDIAG, "Destination type = %d", DestType);

  // RP - check destination type - if CD set tempath to Windows Temp
  if (pG->tempath == NULL && (DestType != DRIVE_FIXED && DestType !=
    DRIVE_RAMDISK))
  {
    unsigned int plen;
    plen = GetTempPath(2047, pG->ewemsg);
    if (plen && (pG->tempath = (char*)MALLOC(plen + 1)) != NULL)
      lstrcpy(pG->tempath, pG->ewemsg);
    else
      return (ziperr(ZEN_MEM10, pG));
  }

  // If -b not specified, set temporary path to zipfile path
  if (pG->tempath == NULL && ((p = strrchr(pG->zipfile, '\\')) != NULL || (p =
    strrchr(pG->zipfile, ':')) != NULL))
  {
    if (*p == ':')
      p++;
    if ((pG->tempath = (char*)MALLOC((int)(p - pG->zipfile) + 1)) == NULL)
      return (ziperr(ZEN_MEM10, pG));
    r =  *p;
    *p = 0;
    lstrcpy(pG->tempath, pG->zipfile);
    *p = (char)r;
  }

  // NOTE: "k" is being redefined below this point. Now, it going to
  // track the no. of marked files in the "zfiles" linked list.
  // For each marked entry in "zfiles" linked list, if not deleting, check
  //   if a corresponding "external" file exists. If updating or freshening,
  //   compare date of "external" file with entry in orig zipfile. Unmark if it
  //   external file doesn't exist or is too old, else mark it. Entries that
  //   are marked will cause that file to be rezipped.
  diag("checking marked entries", pG);

  marks = 0; // Initialize marked count

  // zfiles is name of the linked-list of filenames for the archive
  #ifdef USE_STRM_INPUT
    if (!pG->UseInStream)
  #endif
  for (z = pG->zfiles; z != NULL; z = z->nxt)
  {
    if (z->mark)
    {
      ulg FileAttr;
      #ifdef USE_EF_UX_TIME
        ztimbuf f_utim, z_utim;
      #endif
      #ifdef USE_EF_UX_TIME
        if (pG->action != PURGE && ((t = filetime(z->name, &FileAttr, (long*)
          NULL, &f_utim, pG)) == 0 || t < pG->before || ((pG->action == UPDATE
          || pG->action == FRESHEN) && (get_ef_ux_ztime(z, &z_utim) ?
          f_utim.modtime <= z_utim.modtime: t <= z->tim)) || (pG->ArchiveFiles
          && pG->action == FRESHEN && !(FileAttr &A_ARCHIVE))))
        {
          z->mark = 0;
          z->trash = t && t >= pG->before; // delete if -um or -fm
          if (pG->verbose)
          {
            Inform(pG, 0, 0, "%s %s", z->name, z->trash ? "up to date" :
              "missing or early");
          }
        }
        else
          marks++;
        // incr. number of marked entries
      #else
        if (pG->action != PURGE)
        {
          t = filetime(z->name, &FileAttr, &fsz /* NULL */, NULL, pG);
          //           && (
          //               (t = filetime( z->name, &FileAttr, &fsz /*NULL*/, NULL, pG )) == 0
          if ((t == 0 || t < pG->before || ((pG->action == UPDATE || pG->action
            == FRESHEN) && t <= z->tim) || (pG->ArchiveFiles && pG->action ==
            FRESHEN && !(FileAttr &A_ARCHIVE))) || ( /* (int) */fsz <  - 2))
          // RP - check size
          {
            z->mark = 0;
            z->trash = (t && t >= pG->before) || ( /* (int) */fsz <  - 2);
              // delete if -um or -fm
            //              z->trash = t && t >= pG->before;  // delete if -um or -fm
            if (pG->verbose)
            {
              Inform(pG, 0, 0, "%s %s", z->name, z->trash ? (int)fsz <  - 2 ?
                "now too big" : "up to date": "missing or early");
              //                    z->trash ? "up to date" : "missing or early" );
            }
          }
          else
          {
            TotSize += fsz;
            TotFiles++;
            marks++;
          }
        }
        else
        // PURGE
          marks++;
        // incr. number of marked entries
      #endif
    }
  }

  #ifdef USE_STRM_INPUT
    if (pG->UseInStream)
      marks = 1;
  #endif

  // RP - verify file specified to 'Purge'
  if (pG->action == PURGE && !marks)
    return (ziperr(ZEN_NONE03, pG));

  // Remove entries from "found" linked list if: Action is PURGE or FRESHEN
  //   or No "external" matching file is found, or if found, but is too old or
  //   The external file is equal to the ziparchive name while ziparchive name
  //   != "-" If filetime() returns any valid time, then at least we know the
  //   file was found.
  diag("checking new entries", pG);

  // fileio.c built the found list
  #ifdef USE_STRM_INPUT
    if (!pG->UseInStream)
  #endif
  for (f = pG->found; f != NULL;)
  {
    int sz = 0;
    if (pG->action == PURGE || pG->action == FRESHEN || (t = filetime(f->name,
      (ulg*)NULL, &sz, (ztimbuf*)NULL, pG)) == 0
    //         || (t = filetime( f->name, ( ulg * )NULL, ( long * )NULL, ( ztimbuf * )NULL, pG )) == 0
     || t < pG->before || (namecmp(GetFullPath(pG, f->name), pG->zipfile) == 0
       && strcmp(pG->zipfile, "-")) || (sz <  - 2))
    {
      if (sz <  - 2)
        Inform(pG, ZEN_SIZE04, IWARNING, "%s is too large", f->name); 
      if (pG->verbose && t < pG->before)
      {
        Inform(pG, 0, IDIAG, "rejecting %s as too early", f->name );
      }
      if (pG->verbose < 0)
        Inform(pG, 0, IDIAG, "expel being called for %s", f->name);
      f = fexpel(f, pG); // delete an entry in the list.
    }
    else
    // file found, and not too old.
      f = f->nxt;
    // save this one, link it up.
  }

  if (pG->found == NULL)
    diag("found list empty - a", pG);
  else
    diag("found list has at least one entry - a", pG);

  if ( /* ( pG->action == UPDATE || pG->action == FRESHEN ) && */(pG->adjust <=
    0))
    pG->adjust = 0;

  // Make sure there's something left to do RP adjust always 1
  if (marks == 0 && pG->found == NULL && !(pG->zfiles != NULL && (pG->latest ||
    pG->adjust || pG->junk_sfx)))
  {
    // FOUND WAS NULL HERE, so just figure out which error message to show
    //   the user
    if (pG->action == UPDATE || pG->action == FRESHEN)
    {
      finish(pG);
      Inform(pG, 0, IWARNING, "no files %s", (pG->action == UPDATE) ? "updated"
        : "freshened");
      return 0;
    }
    else if (pG->zfiles == NULL && (pG->latest || pG->adjust || pG->junk_sfx))
    {
      return (ziperr(ZEN_NAME01, pG));
    }
    else if (pG->recurse && (pG->pcount == 0) && (!pG->doall))
    //first_listarg > 0) )
    {
      // add the list of filenames from cmd line to error msg
      ///      for ( i = first_listarg; i < argc; i++ )
      ///        lstrcat( lstrcat( pG->errbuf, " " ), argv[i] );
      return (ziperr(ZEN_NONE01, pG));
    }
    else
      return (ziperr(ZEN_NONE02, pG));
  }

  // AllowGrow is false if writing temporary file
  pG->AllowGrow = (pG->AllowGrow && (marks == 0)
    // is allowed and no changes to existing
   && (pG->zipbeg || pG->zfiles != NULL) // something to append to
  );

  // continue on to add new files
  a = 0;

  // ignore self-extracting code in front of the zip file (for -J)
  if (pG->junk_sfx)
    pG->zipbeg = 0;

  // Count files and sizes which we have to Keep; RP Added
  w = &pG->zfiles;
  while ((z =  *w) != NULL)
  {
    if (pG->global_abort_sw)
      return (ziperr(ZEN_ABORT, pG));
    if (!z->mark)
    {
      KeptSize += (z->siz + (ulg)(4+LOCHEAD) + (ulg)z->nam + (ulg)z->ext);
      if (z->lflg &8)
        KeptSize += 16;
      KeptCnt++;
    }

    w = &z->nxt;
  }

  //Inform( pG, 0, IDIAG, "Kept = %u %Lu Total = %u %Lu", KeptCnt, KeptSize, TotFiles, TotSize );
  // Count files and sizes which we have to process; RCV Added
  // First the files in the old zip file...
  // RP - already calculated with new sizes
  // And the found list...
  for (f = pG->found; f != NULL; f = f->nxt)
  {
    if (pG->global_abort_sw)
      return (ziperr(ZEN_ABORT, pG));
    TotSize += f->len;
    TotFiles++;
  }
  //    Inform( pG, 0, IDIAG, "Found = %u %u", tc, ts );
  // OPEN ZIP FILE and temporary output file
  //  diag( "opening zip file and creating temporary zip file", pG );
  pG->hInz = INVALID_HANDLE_VALUE; //0;
  pG->hTempzf = INVALID_HANDLE_VALUE;
  pG->tempzn = 0;


  if (!pG->AllowGrow)
  {
    // check file exists
    No_File = access(pG->zipfile, 0) && errno == ENOENT;
    if (No_File && DestType == DRIVE_FIXED || DestType == DRIVE_RAMDISK)
    {
      // create file using given name
      diag("in dllzip - ready to create new file", pG);
      if ((pG->hOutz = CreateFile(pG->zipfile, GENERIC_WRITE, 0, NULL,
        CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL)) !=
        INVALID_HANDLE_VALUE)
      {
        pG->tempzip = pG->zipfile;

        pG->hTempzf = pG->hOutz;
        pG->AllowGrow =  - 1; // new files do grow
      }
    }
  }

  if (pG->AllowGrow > 0)
  {
    // zipfile is not stdout, and we are allowed to append
    // AllowGrow is true if we're just appending (-g)
    diag("in dllzip - ready to open for appending", pG);
    if ((pG->hOutz = CreateFile(pG->zipfile, GENERIC_READ | GENERIC_WRITE, 0,
      NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
      NULL)) == INVALID_HANDLE_VALUE)
      return (ziperr(ZEN_NAME02, pG));
    pG->tempzip = pG->zipfile;

    pG->hTempzf = pG->hOutz;

    /* long */
    hi = 0;
    if (SetFilePointer(pG->hOutz, pG->cenbeg, &hi, FILE_BEGIN) ==
      INVALID_SET_FILE_POINTER && GetLastError())
      return (ziperr(GetLastError() ? ZEN_READ01 : ZEN_EOF01, pG));
    pG->tempzn = pG->cenbeg;
  }

  if (!pG->AllowGrow)
  {
    diag("in dllzip - ready to open for Exclusive Read", pG);
    if ((pG->zfiles != NULL || pG->zipbeg) && (pG->hInz = CreateFile(pG
      ->zipfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL |
      FILE_FLAG_SEQUENTIAL_SCAN, NULL)) == INVALID_HANDLE_VALUE)
      return (ziperr(ZEN_NAME03, pG));
    if ((pG->tempzip = tempname(pG)) == NULL)
      return (ziperr(ZEN_MEM11, pG));

    //		if (pG->Verbose)
    Inform(pG, 0, IDIAG, "Temp Filename = %s", pG->tempzip);
    if ((pG->hTempzf = pG->hOutz = CreateFile(pG->tempzip, GENERIC_WRITE, 0,
      NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS |
      FILE_ATTRIBUTE_TEMPORARY, NULL)) == INVALID_HANDLE_VALUE)
      return (ziperr(ZEN_TEMP01, pG));
  }

  if (!pG->AllowGrow)
  {
    TotFiles += KeptCnt;
    TotSize += KeptSize;
  }
  // Pass total number of files and Total Filesize.    (moved)
  user_callback(zacCount, 0, TotFiles, NULL, pG);
  user_callback(zacSize, (long)(TotSize >> 32), ((unsigned)TotSize), NULL, pG);
  pG->BatchStarted = 1;

  if (!pG->AllowGrow && pG->zipbeg)
  {
    // copy a compressed file from old archive to new archive
    user_callback(zacItem, 0, pG->zipbeg, "SFX", pG);
    if ((r = fcopy(pG->hInz, pG->hOutz, pG->zipbeg, pG)) != ZEN_OK)
      return (ziperr(r, pG));
    pG->tempzn = pG->zipbeg;
  }

  // o = 0; /* no ZEN_OPEN errors yet
  // Process zip file, copying from old archive to new archive. Rezip any
  //   marked files
  if (pG->zfiles != NULL)
    diag("going through old zip file", pG);
  w = &pG->zfiles;

  while ((z =  *w) != NULL)
  {
    if (pG->global_abort_sw)
    {
      return (ziperr(ZEN_ABORT, pG));
    }

    if (z->mark == 1)
    {
      // This file is marked
      // if not deleting, rezip it
      if (pG->action != PURGE)
      {
        Inform(pG, 0, 0, "updating: %s", Oem2Iso(z->zname, pG));

        // zipup is in file zipup.c
        if ((r = zipup(z, pG->hOutz, pG)) != ZEN_OK && (int)(char)(r &0xFF) !=
          ZEN_OPEN && (int)(char)(r &0xFF) != ZEN_MISS)
        {
          return (ziperr(r, pG));
        }

        if ((int)(char)(r &0xFF) == ZEN_OPEN || (int)(char)(r &0xFF) ==
          ZEN_MISS)
        {
          if ((int)(char)(r &0xFF) == ZEN_OPEN)
          {
            Inform(pG, r, IWARNING, "could not open for reading: %s", z->name);
            user_callback(4, r, 0, z->name, pG);
          }
          else
          {
            Inform(pG, 0, IWARNING, "file and directory with the same name: %s",
              z->name);
            user_callback(4, r, 0, z->name, pG);
          }

          Inform(pG, 0, IWARNING, "will just copy entry over: %s", z->zname);

          if ((r = zipcopy(z, pG->hInz, pG->hOutz, pG)) != ZEN_OK)
          {
            sprintf(pG->errbuf, "was copying %s", z->zname);
            return (ziperr(r, pG));
          }

          z->mark = 0;
        }

        w = &z->nxt;
        pG->files_acted_on++;
      }
      else
      {
        // desired action is DELETE, this file marked
        // NOTE: no progress bar supt for DELETE yet
        Inform(pG, 0, 0, "deleting: %s", Oem2Iso(z->zname, pG));

        v = z->nxt; // delete entry from list
        FREE(z->name);
        FREE(z->zname);
        if (z->ext)
          FREE(z->extra);
        if (z->cext && z->cextra != z->extra)
          FREE(z->cextra);
        if (z->com)
          FREE(z->comment);
        FREE(z);
        *w = v;
        pG->zcount--;
        pG->files_acted_on++;
      }
    }
    else
    {
      // this file wasn't marked
      // copy the original entry verbatim
      if (!pG->AllowGrow)
      {
        Inform(pG, 0, 0, "keeping: %s", Oem2Iso(z->zname, pG));
        if ((r = zipcopy(z, pG->hInz, pG->hOutz, pG)) != ZEN_OK)
        {
          sprintf(pG->errbuf, "was copying %s", z->zname);

          // user_callback ( zacEndOfBatch, 0, 0, NULL, pG ); // done with a
          // batch of files
          return (ziperr(r, pG));
        }
      }
      w = &z->nxt;
    }
  } // end while

  // Process the "found" list, adding them to the zip file.
  // This is used to add files that weren't already in the archive.
  if (pG->verbose)
  {
    Inform(pG, 0, IDIAG, "Zipping up %d NEW entries from found list", pG
      ->fcount);
  }

  // For each new file to add (src names in found list), make a new entry
  //   for it in the "zfiles" linked list, zip up the new file, then remove the
  //   entry from the found list.
  // The last item in the for loop control deallocates spc for fname that
  //   was just zipped up
  for (f = pG->found; f != NULL; f = fexpel(f, pG))
  {
    // add a new entry to "zfiles" list, before we zip up the file. That way
    //   we'll be ready to update the ZIP file's directory later.
    if (pG->global_abort_sw)
    {
      // user_callback ( zacEndOfBatch, 0, 0, NULL, pG ); // done with a
      // batch of files
      return (ziperr(ZEN_ABORT, pG));
    }

    if ((z = (struct zlist*)MALLOC(sizeof(struct zlist))) == NULL)
    {
      return (ziperr(ZEN_MEM12, pG));
    }

    // RP 1.73 clear all fields
    memset(z, 0, sizeof(struct zlist));

    // Similar names below are confusing: f->name f->zname z->name z->zname
    // z z->nxt = NULL;
    z->name = f->name;
    f->name = NULL;

    z->zname = f->zname;
    f->zname = NULL;

    // z z->ext = z->cext = z->com = 0;
    // z z->extra = z->cextra = z->comment = NULL;
    // pRP173 z->ext = strlen( f->passw ); //Added RAEL
    // pRP173 z->extra = f->passw; //Added RAEL for per file password
    z->passw = f->passw; // p
    f->passw = NULL; // Added RAEL
    z->mark = 1;
    z->dosflag = f->dosflag;
    z->len = f->len; // RCV added.

    // zip it up
    Inform(pG, 0, 0, "  adding: %s", Oem2Iso(z->zname, pG));

    // This is it - try to zip up new file
    if ((r = zipup(z, pG->hOutz, pG)) != ZEN_OK && (int)(char)(r &0xFF) !=
      ZEN_OPEN && (int)(char)(r &0xFF) != ZEN_MISS)
    {
      return (ziperr(r, pG));
    }

    if ((int)(char)(r &0xFF) == ZEN_OPEN || (int)(char)(r &0xFF) == ZEN_MISS)
    {
      // o = 1;
      if ((int)(char)(r &0xFF) == ZEN_OPEN)
      {
        Inform(pG, r, IWARNING, "could not open for reading: %s", z->name);
      }
      else
      {
        Inform(pG, 0, IWARNING, "file and directory with the same name: %s", z
          ->name);
      }

      FREE(z->name);
      FREE(z->zname);
      FREE(z);
    }
    else
    {
      // "zipup" of this file was good
      *w = z;
      w = &z->nxt;
      pG->zcount++;
      pG->files_acted_on++;
    }
  }

  // Write central directory and end header to temporary zip
  diag("writing central directory", pG);
  hi = 0;
  ofs = SetFilePointer(pG->hOutz, 0, &hi, FILE_CURRENT);
  if (hi || (ofs > 0xFFFFFFF0u) || (ofs == INVALID_SET_FILE_POINTER &&
    GetLastError()))
  {
    return ziperr(ZEN_SIZE01, pG);
  }

  k = 0; // keep count of new fnames for ZIPfile's end header
  c = pG->tempzn; // get start of central directory
  n = t = 0;
  altered = 0; // RP 173 - count changes
  for (z = pG->zfiles; z != NULL; z = z->nxt)
  {
    // Handle callback and result for the filecomments... DLL v1.609,
    // Component v1.60L
    if (pG->action != PURGE)
    {
      memset(pG->ewetmp, 0, 513); // Clear all
      strncpy(pG->ewetmp, z->zname, 255); // Copy external filename
      if (z->com)
      // Copy old comment if present
        strncpy(pG->ewetmp + 256, z->comment, min(z->com, 255));
      else
        z->comment = 0;
      user_callback(12, 0, z->com, pG->ewetmp, pG);
      if (pG->callbackdata.error_code)
      {
        // User changed the comment
        FREE(z->comment);
        z->com = pG->callbackdata.fsize;
        if ((z->comment = (char*)MALLOC(z->com + 1)) == NULL)
          return (ziperr(ZEN_MEM37, pG));
        strncpy(z->comment, pG->callbackdata.filenameormsg, z->com + 1);
        altered++;
      }
    }

    if (pG->files_acted_on < altered)
    // RP 173 - comments changed is acted on
      pG->files_acted_on = altered;

    if ((r = putcentral(z, pG->hOutz, pG)) != ZEN_OK)
    {
      // v1.6014
      return (ziperr(r, pG));
    }

    pG->tempzn += 4+CENHEAD + z->nam + z->cext + z->com;
    n += z->len;
    t += z->siz;
    k++;
  }

  if (k == 0)
    Inform(pG, 0, IWARNING, "zip file empty");

  if ((pG->verbose) && (pG->action == ADD) && (!pG->global_error_code) && (pG
    ->files_acted_on > 0))
  {
    Inform(pG, 0, 0, "Total Bytes=%lu, compr bytes=%lu -> %d%% savings", n, t,
      percent(n, t));
  }

  t = pG->tempzn - c; // compute length of central
  diag("writing end of central directory", pG);

  if (k && ((r = putend(k, t, c, pG->hOutz, pG)) != ZEN_OK))
  {
    return (ziperr(r, pG));
  }

  //       if ( pG->hInz != INVALID_HANDLE_VALUE )
  //       {
  Close_Handle(&pG->hInz);
  //         pG->hInz = INVALID_HANDLE_VALUE;
  //       }

  // pG->tempzf = NULL;
  // if ( fclose ( pG->y ) )
  pG->hTempzf = INVALID_HANDLE_VALUE; //NULL;
  //       if ( pG->hOutz != INVALID_HANDLE_VALUE && !CloseHandle( pG->hOutz ) )
  if (!Close_Handle(&pG->hOutz))
  {
    return (ziperr(pG->AllowGrow ? ZEN_WRITE03 : ZEN_TEMP02, pG));
  }

  //       pG->hOutz = INVALID_HANDLE_VALUE;

  #ifdef DYN_ALLOC
    lm_free(pG);
  #endif

  //  if ( pG->verbose )
  //    Inform( pG, 0, IDIAG, "Files closed in: %X out: %x", pG->hInz, pG->hOutz );
  // Replace old zip file with new zip file, leaving only the new one
  //  if ( !pG->AllowGrow )     // && lstrcmp(pG->zipfile, pG->tempzip))
  if (!pG->AllowGrow && (k || pG->action != PURGE))
  // && lstrcmp(pG->zipfile, pG->tempzip))
  {
    diag("replacing old zip file with new zip file", pG);
    if ((r = replace(pG->zipfile, pG->tempzip, pG)) != ZEN_OK)
    {
      Inform(pG, 0, IWARNING, "new zip file left as: %s", pG->tempzip);
      FREE(pG->tempzip);
      pG->tempzip = NULL;

      return (ziperr(r, pG));
    }

    FREE(pG->tempzip);
    pG->tempzip = NULL;
  }

  // 1.78.1.2
  if (!k && pG->action == PURGE)
  // empty file - remove
  {
    a = pG->latest = 0;
    DeleteFile(pG->zipfile);
    if (!pG->AllowGrow)
      DeleteFile(pG->tempzip);
  }

  pG->tempzip = NULL;
  if (a)
    setfileattr(pG->zipfile, a);

  // Reset the archive bit when needed for all successfull zipped files
  if (pG->ResetArchiveBit && pG->action != PURGE)
  {
    unsigned cnt = 0; // 1.71.0.0 added extra callbacks
    diag("resetting archive bits", pG);
    for (z = pG->zfiles; z != NULL; z = z->nxt)
      if (z->mark)
        cnt++;
    if (cnt)
    {
      // new file op.
      // Pass total number of files. filesize.
      user_callback(zacXItem, 1, cnt, "*resetting archive bits", pG);
    }

    cnt = 0;
    for (z = pG->zfiles; z != NULL; z = z->nxt)
    {
      if (z->mark)
      {
        char *fullname;
        if (++cnt == 30)
        {
          user_callback(zacXProgress, 1, cnt, "", pG);
          cnt = 0;
          if (pG->global_abort_sw)
            break;
        }

        fullname = GetFullPath(pG, z->name); // v.16017
        if (!SetFileAttributes(fullname, GetFileAttributes(fullname)
          &~FILE_ATTRIBUTE_ARCHIVE))
        {
          Inform(pG, 0, IWARNING, "Archive bit could not be set for: %s", z
            ->name);
        }
      }
    }

    if (cnt)
    {
      user_callback(zacXProgress, 1, cnt, "", pG); // last few
    }
  }

  finish(pG);
  return 0;
}