Exemplo n.º 1
0
int main(int argc, char **argv)
{
	int		fd;
	FILE		*fp;
	header_struct	*header;
	gd_addr		*addr;
	gd_region	*region;
	gd_segment	*segment;
	int4		*long_ptr;
	uint4	size;
	int		i, alloc, extend, block, global, key, lock, record;

	argv++;
	alloc = 100;
	extend = 100;
	block = 1024;
	global = 128;
	lock = 20;
	record = 256;
	key = 64;
	for (i = argc; i > 1; i--)
	{
		if (argv[0][0] != '-' || argv[0][1] == 'h')
		{
			PRINTF("syntax is dtgbldir [-abegklr] with each qualifier followed by a number\n");
			PRINTF("and separated by spaces. i.e. dtgbldir -a100 -b2048 -l30\n");
			PRINTF("-a is alloc [100]     -b is block size [1024] -e is ext [100]\n");
			PRINTF("-g is glo buff [128]  -k is keysize [64]      -l is lock [20]\n");
			PRINTF("-r is rec size [256]  -h prints this message\n");
			PRINTF("The global directory is created as ./mumps.gld\n");
			return 0;
		}
		switch (argv[0][1])
		{
			case 'a':	if (-1 == (alloc = asc2i((uchar_ptr_t)&argv[0][2],strlen(argv[0]) - 2)))
					{
						PRINTF("Invalid value for -a: %s\n", &argv[0][2]);
						return 0;
					}
					break;
			case 'b':	if (-1 == (block = asc2i((uchar_ptr_t)&argv[0][2],strlen(argv[0]) - 2)))
					{
						PRINTF("Invalid value for -b: %s\n", &argv[0][2]);
						return 0;
					}
					break;
			case 'e':	if (-1 == (extend = asc2i((uchar_ptr_t)&argv[0][2],strlen(argv[0]) - 2)))
					{
						PRINTF("Invalid value for -e: %s\n", &argv[0][2]);
						return 0;
					}
					break;
			case 'g':	if (-1 == (global = asc2i((uchar_ptr_t)&argv[0][2], strlen(argv[0]) - 2)))
					{
						PRINTF("Invalid value for -g: %s\n", &argv[0][2]);
						return 0;
					}
					break;
			case 'k':	if (-1 == (key = asc2i((uchar_ptr_t)&argv[0][2],strlen(argv[0]) - 2)))
					{
						PRINTF("Invalid value for -k: %s\n", &argv[0][2]);
						return 0;
					}
					break;
			case 'l':	if (-1 == (lock = asc2i((uchar_ptr_t)&argv[0][2],strlen(argv[0]) - 2)))
					{
						PRINTF("Invalid value for -l: %s\n", &argv[0][2]);
						return 0;
					}
					break;
			case 'r':	if (-1 == (record = asc2i((uchar_ptr_t)&argv[0][2],strlen(argv[0]) - 2)))
					{
						PRINTF("Invalid value for -r: %s\n", &argv[0][2]);
						return 0;
					}
					break;
			default:	PRINTF("unrecognized qualifier %s\n",argv[0]);
					return 0;
		}
		argv++;
	}
	if (argc == 1)
	{
		PRINTF("syntax is dtgbldir [-abegklr] with each qualifier followed by a number\n");
		PRINTF("and separated by spaces. i.e. dtgbldir -a100 -b2048 -l30\n");
		PRINTF("-a is alloc [100]     -b is block size [1024] -e is ext [100]\n");
		PRINTF("-g is glo buff [128]  -k is keysize [64]      -l is lock [20]\n");
		PRINTF("-r is rec size [256]  -h prints this message\n");
		PRINTF("The global directory is created as ./mumps.gld\n");
		PRINTF("\n\nCreating default global directory\n");
	}
	if (record > (block / 2) - 4)
	{
		PRINTF("Record size %d is too large, block size %d will only support record size of %d\n",
			record, block, (block / 2) - 4);
		return 0;
	}
	if (key >= record)
	{
		PRINTF("Key size %d is too large, record size %d will only support key size of %d\n",
			key, record, record - 1);
		return 0;
	}

	if (lock > 100 || lock < 4)
	{
		PRINTF("Lock size %d is invalid, must be between 4 and 100\n", lock);
		return 0;
	}

	if (block % DISK_BLOCK_SIZE)
	{
		PRINTF("Block size %d is invalid, must be a multiple of %d\n", block, DISK_BLOCK_SIZE);
		return 0;
	}

	if (global < 64 || global > 4096)
	{
		PRINTF("Global value %d is invalid, must be between 64 and 4096\n", global);
		return 0;
	}
	if (-1 == (fd = OPEN4("./mumps.gld", O_CREAT | O_EXCL | O_RDWR, 0600, 0 )))
	{
		PERROR("Error opening file");
		return 0;
	}
	size = sizeof(header_struct) + sizeof(gd_addr) + 3 * sizeof(gd_binding) + 1 * sizeof(gd_region) + 1 * sizeof(gd_segment);
	header = (header_struct *)malloc(ROUND_UP(size, DISK_BLOCK_SIZE));
	memset(header, 0, ROUND_UP(size, DISK_BLOCK_SIZE));
	header->filesize = size;
	size = ROUND_UP(size, DISK_BLOCK_SIZE);
	memcpy(header->label, GDE_LABEL_LITERAL, sizeof(GDE_LABEL_LITERAL));
	addr = (gd_addr *)((char *)header + sizeof(header_struct));
	addr->max_rec_size = 256;
	addr->maps = (gd_binding*)(sizeof(gd_addr));
	addr->n_maps = 3;
	addr->regions = (gd_region*)((int4)(addr->maps) + 3 * sizeof(gd_binding));
	addr->n_regions = 1;
	addr->segments = (gd_segment*)((int4)(addr->regions) + sizeof(gd_region));
	addr->n_segments = 1;
	addr->link = 0;
	addr->tab_ptr = 0;
	addr->id = 0;
	addr->local_locks = 0;
	addr->end = (int4)(addr->segments + 1 * sizeof(gd_segment));
	long_ptr = (int4*)((char*)addr + (int4)(addr->maps));
	*long_ptr++ = 0x232FFFFF;
	*long_ptr++ = 0xFFFFFFFF;
	*long_ptr++ = (int4)addr->regions;
	*long_ptr++ = 0x24FFFFFF;
	*long_ptr++ = 0xFFFFFFFF;
	*long_ptr++ = (int4)addr->regions;
	*long_ptr++ = 0xFFFFFFFF;
	*long_ptr++ = 0xFFFFFFFF;
	*long_ptr++ = (int4)addr->regions;
	region = (gd_region*)((char*)addr + (int4)(addr->regions));
	segment = (gd_segment*)((char*)addr + (int4)(addr->segments));
	region->rname_len = 7;
	memcpy(region->rname,"DEFAULT",7);
	region->dyn.offset = (int4)addr->segments;
	region->max_rec_size = record;
	region->max_key_size = key;
	region->open = region->lock_write = region->null_subs = region->jnl_state = 0;
	region->jnl_alq = region->jnl_deq = region->jnl_buffer_size = region->jnl_before_image = 0;
	region->jnl_file_len = region->node = region->cmx_regnum = 0;
	region->sec_size = 0;
	segment->sname_len = 7;
	memcpy(segment->sname,"DEFAULT",7);
	memcpy(segment->fname,"mumps.dat",9);
	segment->fname_len = 9;
	segment->blk_size = block;
	segment->allocation = alloc;
	segment->ext_blk_count = extend;
	segment->cm_blk = 0;
	segment->lock_space = lock;
	memcpy(segment->defext,".dat",4);
	segment->global_buffers = global;
	segment->buckets = 0;
	segment->windows = 0;
	segment->acc_meth = dba_bg;
	segment->defer_time = 1;
	segment->file_cntl = 0;

	if (NULL == (fp = FDOPEN(fd,"r+")))
	{
		PERROR("Error doing fdopen on file");
		return 0;
	}
	if (fseek(fp, (long)0, SEEK_SET))
	{
		PERROR("Error seeking on file");
		return 0;
	}
	if (fwrite(header,1,size,fp) != size)
	{
		PERROR("Error writing to file");
		return 0;
	}
	return 1;
}
Exemplo n.º 2
0
static bool mu_open_try(io_log_name *naml, io_log_name *tl, mval *pp, mval *mspace)
{
	int4		status;
	int4		size;
	mstr		tn;		/* translated name */
	mstr		chset_mstr;
	int		oflag;
	unsigned char	ch;
	int		file_des;
	struct stat	outbuf;
	char		*buf, namebuf[LOGNAME_LEN + 1];
	d_mt_struct	*mt_ptr;
	int		umask_orig, umask_creat;
	int		char_or_block_special;
	int		fstat_res;
	int		save_errno;
	int		p_offset;
	boolean_t	ichset_specified, ochset_specified;

	error_def(ERR_SYSCALL);

	mt_ptr = NULL;
	char_or_block_special = FALSE;
	file_des = -2;
	oflag = 0;
	tn.len = tl->len;
	if (tn.len > LOGNAME_LEN)
		tn.len = LOGNAME_LEN;
	tn.addr = tl->dollar_io;
	memcpy(namebuf, tn.addr, tn.len);
	namebuf[tn.len] = '\0';
	buf = namebuf;
	if (0 == naml->iod)
	{
		if (0 == tl->iod)
		{
			tl->iod =  (io_desc *)malloc(sizeof(io_desc));
			memset((char*)tl->iod, 0, sizeof(io_desc));
			tl->iod->pair.in  = tl->iod;
			tl->iod->pair.out = tl->iod;
			tl->iod->trans_name = tl;
			tl->iod->type = n_io_dev_types;
			p_offset = 0;
			while(iop_eol != *(pp->str.addr + p_offset))
			{
				ch = *(pp->str.addr + p_offset++);
				if (iop_sequential == ch)
					tl->iod->type = rm;

				if (IOP_VAR_SIZE == io_params_size[ch])
					p_offset += *(pp->str.addr + p_offset) + 1;
				else
					p_offset += io_params_size[ch];
			}
		}
		if ((n_io_dev_types == tl->iod->type) && mspace && mspace->str.len)
			tl->iod->type = us;
		if (n_io_dev_types == tl->iod->type)
		{
			if (0 == memvcmp(tn.addr, tn.len, sys_input.addr, sys_input.len))
			{
				file_des = 0;
				FSTAT_FILE(file_des, &outbuf, fstat_res);
				if (-1 == fstat_res)
				{
					save_errno = errno;
					rts_error(VARLSTCNT(8) ERR_SYSCALL, 5,
						  RTS_ERROR_LITERAL("fstat()"),
						  CALLFROM, save_errno);
				}
			} else
			{
				if (0 == memvcmp(tn.addr, tn.len, sys_output.addr, sys_output.len))
				{
					file_des = 1;
					FSTAT_FILE(file_des, &outbuf, fstat_res);
					if (-1 == fstat_res)
					{
						save_errno = errno;
						rts_error(VARLSTCNT(8) ERR_SYSCALL, 5,
							  RTS_ERROR_LITERAL("fstat()"),
							  CALLFROM, save_errno);
					}
				} else  if (0 == memvcmp(tn.addr, tn.len, "/dev/null", 9))
					tl->iod->type = nl;
				else  if ((-1 == Stat(buf, &outbuf)) && (n_io_dev_types == tl->iod->type))
				{

					if (ENOENT == errno)
						tl->iod->type = rm;
					else
					{
						save_errno = errno;
						rts_error(VARLSTCNT(8) ERR_SYSCALL, 5,
							  RTS_ERROR_LITERAL("fstat()"),
							  CALLFROM, save_errno);
					}
				}
			}
		}
		if (n_io_dev_types == tl->iod->type)
		{
			switch(outbuf.st_mode & S_IFMT)
			{
				case S_IFCHR:
				case S_IFBLK:
					char_or_block_special = TRUE;
					break;
				case S_IFIFO:
					tl->iod->type = ff;
					break;
				case S_IFREG:
				case S_IFDIR:
					tl->iod->type = rm;
					break;
				case S_IFSOCK:
				case 0:
					tl->iod->type = ff;
					break;
				default:
					break;
			}
		}
		naml->iod = tl->iod;
	}
	active_device = naml->iod;

	if ((-2 == file_des) && (dev_open != naml->iod->state) && (us != naml->iod->type) && (tcp != naml->iod->type))
	{
		oflag |= (O_RDWR | O_CREAT | O_NOCTTY);
		size = 0;
		p_offset = 0;
		ichset_specified = ochset_specified = FALSE;
		while(iop_eol != *(pp->str.addr + p_offset))
		{
			assert((params) *(pp->str.addr + p_offset) < (params)n_iops);
			switch ((ch = *(pp->str.addr + p_offset++)))
			{
				case iop_allocation:
					if (rm == naml->iod->type)
					{
						GET_LONG(size, pp->str.addr + p_offset);
						size *= 512;
					}
					break;
				case iop_append:
					if (rm == naml->iod->type)
						oflag |= O_APPEND;
					break;
				case iop_contiguous:
					break;
				case iop_newversion:
					if ((dev_open != naml->iod->state) && (rm == naml->iod->type))
						oflag |= O_TRUNC;
					break;
				case iop_readonly:
					oflag  &=  ~(O_RDWR | O_CREAT | O_WRONLY);
					oflag  |=  O_RDONLY;
					break;
				case iop_writeonly:
					oflag  &= ~(O_RDWR | O_RDONLY);
					oflag  |= O_WRONLY | O_CREAT;
					break;
				case iop_ipchset:
#ifdef KEEP_zOS_EBCDIC
					if ( (iconv_t)0 != naml->iod->input_conv_cd )
					{
						ICONV_CLOSE_CD(naml->iod->input_conv_cd);
					}
					SET_CODE_SET(naml->iod->in_code_set,
						     (char *)(pp->str.addr + p_offset + 1));
					if (DEFAULT_CODE_SET != naml->iod->in_code_set)
						ICONV_OPEN_CD(naml->iod->input_conv_cd,
							      (char *)(pp->str.addr + p_offset + 1), INSIDE_CH_SET);
					break;
#endif
					if (gtm_utf8_mode)
					{
						chset_mstr.addr = (char *)(pp->str.addr + p_offset + 1);
                                                chset_mstr.len = *(pp->str.addr + p_offset);
						SET_ENCODING(naml->iod->ichset, &chset_mstr);
						ichset_specified = TRUE;
					}
					break;

				case iop_opchset:
#ifdef KEEP_zOS_EBCDIC
					if ( (iconv_t)0 != naml->iod->output_conv_cd)
					{
						ICONV_CLOSE_CD(naml->iod->output_conv_cd);
					}
					SET_CODE_SET(naml->iod->out_code_set,
						     (char *)(pp->str.addr + p_offset + 1));
					if (DEFAULT_CODE_SET != naml->iod->out_code_set)
						ICONV_OPEN_CD(naml->iod->output_conv_cd, INSIDE_CH_SET,
							      (char *)(pp->str.addr + p_offset + 1));
					break;
#endif
					if (gtm_utf8_mode)
					{
                                                chset_mstr.addr = (char *)(pp->str.addr + p_offset + 1);
                                                chset_mstr.len = *(pp->str.addr + p_offset);
                                                SET_ENCODING(naml->iod->ochset, &chset_mstr);
                                                ochset_specified = TRUE;
					}
					break;
				case iop_m:
				case iop_utf8:
				case iop_utf16:
				case iop_utf16be:
				case iop_utf16le:
					if (gtm_utf8_mode)
					{
						naml->iod->ichset = naml->iod->ochset =
							(iop_m       == ch) ? CHSET_M :
							(iop_utf8    == ch) ? CHSET_UTF8 :
							(iop_utf16   == ch) ? CHSET_UTF16 :
							(iop_utf16be == ch) ? CHSET_UTF16BE : CHSET_UTF16LE;
						ichset_specified = ochset_specified = TRUE;
					}
					break;
				default:
					break;
			}
			if (IOP_VAR_SIZE == io_params_size[ch])
				p_offset += *(pp->str.addr + p_offset) + 1;
			else
				p_offset += io_params_size[ch];
		}
		if (!ichset_specified)
			naml->iod->ichset = (gtm_utf8_mode) ? CHSET_UTF8 : CHSET_M;
		if (!ochset_specified)
			naml->iod->ochset = (gtm_utf8_mode) ? CHSET_UTF8 : CHSET_M;
		if (CHSET_M != naml->iod->ichset && CHSET_UTF16 != naml->iod->ichset)
			get_chset_desc(&chset_names[naml->iod->ichset]);
		if (CHSET_M != naml->iod->ochset && CHSET_UTF16 != naml->iod->ochset)
			get_chset_desc(&chset_names[naml->iod->ochset]);
		/* RW permissions for owner and others as determined by umask. */
		umask_orig = umask(000);	/* determine umask (destructive) */
		(void)umask(umask_orig);	/* reset umask */
		umask_creat = 0666 & ~umask_orig;
		/*
		 * the check for EINTR below is valid and should not be converte d to an EINTR
		 * wrapper macro, due to the other errno values being checked.
		 */
		while ((-1 == (file_des = OPEN4(buf, oflag, umask_creat, size))))
		{
			if (   EINTR == errno
			       || ETXTBSY == errno
			       || ENFILE == errno
			       || EBUSY == errno
			       || ((mb == naml->iod->type) && (ENXIO == errno)))
				continue;
			else
				break;
		}

		if (-1 == file_des)
			return FALSE;
	}

	assert (tcp != naml->iod->type);
#ifdef KEEP_zOS_EBCDIC
	SET_CODE_SET(naml->iod->in_code_set, OUTSIDE_CH_SET);
	if (DEFAULT_CODE_SET != naml->iod->in_code_set)
		ICONV_OPEN_CD(naml->iod->input_conv_cd, OUTSIDE_CH_SET, INSIDE_CH_SET);
	SET_CODE_SET(naml->iod->out_code_set, OUTSIDE_CH_SET);
	if (DEFAULT_CODE_SET != naml->iod->out_code_set)
		ICONV_OPEN_CD(naml->iod->output_conv_cd, INSIDE_CH_SET, OUTSIDE_CH_SET);
#endif

	/* smw 99/12/18 not possible to be -1 here */
	if (-1 == file_des)
	{
		rts_error(VARLSTCNT(8) ERR_SYSCALL, 5,
			  RTS_ERROR_LITERAL("open()"),
			  CALLFROM, save_errno);
	}

	if (n_io_dev_types == naml->iod->type)
	{
	        if (isatty(file_des))
			naml->iod->type = tt;
		else if (char_or_block_special && file_des > 2)
			/* assume mag tape */
			naml->iod->type = mt;
		else
			naml->iod->type = rm;
	}
	assert(naml->iod->type < n_io_dev_types);
	naml->iod->disp_ptr = &io_dev_dispatch_mupip[naml->iod->type];
	active_device = naml->iod;
	if (dev_never_opened == naml->iod->state)
	{
		naml->iod->wrap = DEFAULT_IOD_WRAP;
		naml->iod->width = DEFAULT_IOD_WIDTH;
		naml->iod->length = DEFAULT_IOD_LENGTH;
		naml->iod->write_filter = 0; /* MUPIP should not use FILTER */
	}
	if (dev_open != naml->iod->state)
	{
		naml->iod->dollar.x = 0;
		naml->iod->dollar.y = 0;
		naml->iod->dollar.za = 0;
		naml->iod->dollar.zb[0] = 0;
	}
	status = (naml->iod->disp_ptr->open)(naml, pp, file_des, mspace, NO_M_TIMEOUT);
	if (TRUE == status)
		naml->iod->state = dev_open;
	else  if (dev_open == naml->iod->state)
		naml->iod->state = dev_closed;
	if (1 == file_des)
		naml->iod->dollar.zeof = TRUE;
	active_device = 0;

	if (run_time)
		return (status);
	return TRUE;
}