示例#1
0
/** Allocate and print to string.
 *
 * @param strp Address of the pointer where to store the address of
 *             the newly allocated string.
 * @fmt        Format string.
 *
 * @return Number of characters printed or a negative error code.
 *
 */
int asprintf(char **strp, const char *fmt, ...)
{
	printf_spec_t ps = {
		asprintf_str_write,
		asprintf_wstr_write,
		NULL
	};
	
	va_list args;
	va_start(args, fmt);
	
	int ret = printf_core(fmt, &ps, args);
	va_end(args);
	
	if (ret > 0) {
		*strp = malloc(STR_BOUNDS(ret) + 1);
		if (*strp == NULL)
			return -1;
		
		va_start(args, fmt);
		vsnprintf(*strp, STR_BOUNDS(ret) + 1, fmt, args);
		va_end(args);
	}
	
	return ret;
}
示例#2
0
static void send_char(wchar_t c)
{
	char cbuf[STR_BOUNDS(1)];
	size_t offs;
	int rc;

	offs = 0;
	chr_encode(c, cbuf, &offs, STR_BOUNDS(1));

	rc = conn_send(cbuf, offs);
	if (rc != EOK) {
		printf("[Failed sending data]\n");
	}
}
示例#3
0
文件: scores.c 项目: jvesely/helenos
/** Copy from hiscore table score with index src to dest
 *
 */
static void copyhiscore(int dest, int src)
{
	str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
	    scores[src].hs_name);
	scores[dest].hs_score = scores[src].hs_score;
	scores[dest].hs_level = scores[src].hs_level;
}
示例#4
0
NO_TRACE static cmd_info_t *parse_cmd(const wchar_t *cmdline)
{
	size_t start = 0;
	size_t end;
	char *tmp;
	
	while (isspace(cmdline[start]))
		start++;
	
	end = start + 1;
	
	while (!isspace(cmdline[end]))
		end++;
	
	tmp = malloc(STR_BOUNDS(end - start + 1), 0);
	
	wstr_to_str(tmp, end - start + 1, &cmdline[start]);
	
	spinlock_lock(&cmd_lock);
	
	list_foreach(cmd_list, link, cmd_info_t, hlp) {
		spinlock_lock(&hlp->lock);
		
		if (str_cmp(hlp->name, tmp) == 0) {
			spinlock_unlock(&hlp->lock);
			spinlock_unlock(&cmd_lock);
			free(tmp);
			return hlp;
		}
		
		spinlock_unlock(&hlp->lock);
	}
示例#5
0
int fputc(wchar_t c, FILE *stream)
{
	char buf[STR_BOUNDS(1)];
	size_t sz = 0;
	
	if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
		size_t wr = fwrite(buf, 1, sz, stream);
		
		if (wr < sz)
			return EOF;
		
		return (int) c;
	}
	
	return EOF;
}
示例#6
0
文件: scores.c 项目: jvesely/helenos
void initscores(void)
{
	int i;
	for (i = 0; i < NUMSPOTS; i++) {
		str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
		scores[i].hs_score = (NUMSPOTS - i) * 200;
		scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1);
	}
}
示例#7
0
/** Insert a character at caret position. */
static void insert_char(wchar_t c)
{
	spt_t pt;
	char cbuf[STR_BOUNDS(1) + 1];
	size_t offs;

	tag_get_pt(&pane.caret_pos, &pt);

	offs = 0;
	chr_encode(c, cbuf, &offs, STR_BOUNDS(1) + 1);
	cbuf[offs] = '\0';

	(void) sheet_insert(&doc.sh, &pt, dir_before, cbuf);

	pane.rflags |= REDRAW_ROW;
	if (c == '\n')
		pane.rflags |= REDRAW_TEXT;
}
示例#8
0
static int klog_vprintf_wstr_write(const wchar_t *str, size_t size, void *data)
{
	size_t offset = 0;
	size_t chars = 0;
	
	while (offset < size) {
		char buf[STR_BOUNDS(1)];
		size_t sz = 0;
		
		if (chr_encode(str[chars], buf, &sz, STR_BOUNDS(1)) == EOK)
			klog_write(buf, sz);
		
		chars++;
		offset += sizeof(wchar_t);
	}
	
	return chars;
}
示例#9
0
/** Insert file at caret position.
 *
 * Reads in the contents of a file and inserts them at the current position
 * of the caret.
 */
static int file_insert(char *fname)
{
	FILE *f;
	wchar_t c;
	char buf[BUF_SIZE];
	int bcnt;
	int n_read;
	size_t off;

	f = fopen(fname, "rt");
	if (f == NULL)
		return EINVAL;

	bcnt = 0;

	while (true) {
		if (bcnt < STR_BOUNDS(1)) {
			n_read = fread(buf + bcnt, 1, BUF_SIZE - bcnt, f);
			bcnt += n_read;
		}

		off = 0;
		c = str_decode(buf, &off, bcnt);
		if (c == '\0')
			break;

		bcnt -= off;
		memcpy(buf, buf + off, bcnt);

		insert_char(c);
	}

	fclose(f);

	return EOK;
}
示例#10
0
文件: scores.c 项目: jvesely/helenos
void insertscore(int score, int level)
{
	int i;
	int j;
	size_t off;
	cons_event_t ev;
	kbd_event_t *kev;
	
	clear_screen();
	moveto(10, 10);
	puts("Insert your name: ");
	str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
	    "Player");
	i = 6;
	off = 6;
	
	moveto(10 , 28);
	printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i,
	    "........................................");
	
	while (1) {
		console_flush(console);
		if (!console_get_event(console, &ev))
			exit(1);
		
		if (ev.type != CEV_KEY || ev.ev.key.type == KEY_RELEASE)
			continue;
		
		kev = &ev.ev.key;
		
		if (kev->key == KC_ENTER || kev->key == KC_NENTER)
			break;
		
		if (kev->key == KC_BACKSPACE) {
			if (i > 0) {
				wchar_t uc;
				
				--i;
				while (off > 0) {
					--off;
					size_t otmp = off;
					uc = str_decode(scores[NUMSPOTS - 1].hs_name,
					    &otmp, STR_BOUNDS(MAXLOGNAME) + 1);
					if (uc != U_SPECIAL)
						break;
				}
				
				scores[NUMSPOTS - 1].hs_name[off] = '\0';
			}
		} else if (kev->c != '\0') {
			if (i < (MAXLOGNAME - 1)) {
				if (chr_encode(kev->c, scores[NUMSPOTS - 1].hs_name,
				    &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
					++i;
				}
				scores[NUMSPOTS - 1].hs_name[off] = '\0';
			}
		}
		
		moveto(10, 28);
		printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
		    "........................................");
	}
	
	scores[NUMSPOTS - 1].hs_score = score;
	scores[NUMSPOTS - 1].hs_level = level;
	
	i = NUMSPOTS - 1;
	while ((i > 0) && (scores[i - 1].hs_score < score))
		i--;
	
	for (j = NUMSPOTS - 2; j > i; j--)
		copyhiscore(j, j-1);
	
	copyhiscore(i, NUMSPOTS - 1);
}
示例#11
0
文件: cat.c 项目: jvesely/helenos
static unsigned int cat_file(const char *fname, size_t blen, bool hex,
    off64_t head, off64_t tail, bool tail_first)
{
	int fd, bytes = 0, count = 0, reads = 0;
	char *buff = NULL;
	int i;
	size_t offset = 0, copied_bytes = 0;
	off64_t file_size = 0, length = 0;

	bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0);
	
	if (reading_stdin) {
		fd = fileno(stdin);
		/* Allow storing the whole UTF-8 character. */
		blen = STR_BOUNDS(1);
	} else
		fd = open(fname, O_RDONLY);
	
	if (fd < 0) {
		printf("Unable to open %s\n", fname);
		return 1;
	}

	if (NULL == (buff = (char *) malloc(blen + 1))) {
		close(fd);
		printf("Unable to allocate enough memory to read %s\n",
			fname);
		return 1;
	}

	if (tail != CAT_FULL_FILE) {
		file_size = lseek(fd, 0, SEEK_END);
		if (head == CAT_FULL_FILE) {
			head = file_size;
			length = tail;
		} else if (tail_first) {
			length = head;
		} else {
			if (tail > head)
				tail = head;
			length = tail;
		}

		if (tail_first) {
			lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);
		} else {
			lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);
		}
	} else
		length = head;

	do {
		size_t bytes_to_read;
		if (reading_stdin) {
			bytes_to_read = 1;
		} else {
			if ((length != CAT_FULL_FILE) &&
			    (length - (off64_t)count <= (off64_t)(blen - copied_bytes))) {
				bytes_to_read = (size_t) (length - count);
			} else {
				bytes_to_read = blen - copied_bytes;
			}
		}
		
		bytes = read(fd, buff + copied_bytes, bytes_to_read);
		copied_bytes = 0;

		if (bytes > 0) {
			buff[bytes] = '\0';
			offset = 0;
			for (i = 0; i < bytes && !should_quit; i++) {
				if (hex) {
					paged_char(hexchars[((uint8_t)buff[i])/16]);
					paged_char(hexchars[((uint8_t)buff[i])%16]);
					paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');
				}
				else {
					wchar_t c = str_decode(buff, &offset, bytes);
					if (c == 0) {
						/* Reached end of string */
						break;
					} else if (c == U_SPECIAL && offset + 2 >= (size_t)bytes) {
						/* If an extended character is cut off due to the size of the buffer,
						   we will copy it over to the next buffer so it can be read correctly. */
						copied_bytes = bytes - offset + 1;
						memcpy(buff, buff + offset - 1, copied_bytes);
						break;
					}
					paged_char(c);
				}
				
			}
			count += bytes;
			reads++;
		}
		
		if (reading_stdin)
			fflush(stdout);
	} while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));

	close(fd);
	if (bytes == -1) {
		printf("Error reading %s\n", fname);
		free(buff);
		return 1;
	}

	free(buff);

	return 0;
}