示例#1
0
文件: id.c 项目: luckboy/toybox
void do_id(char *username)
{
  int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g';
  struct passwd *pw;
  struct group *grp;
  uid_t uid = getuid(), euid = geteuid();
  gid_t gid = getgid(), egid = getegid(), *groups;

  if (cmd_groups)
      toys.optflags |= FLAG_G | FLAG_n;

  flags = toys.optflags;

  // check if a username is given
  if (username) {
    pw = xgetpwnam(username);
    uid = euid = pw->pw_uid;
    gid = egid = pw->pw_gid;
    if (cmd_groups) printf("%s : ", pw->pw_name);
  }

  i = flags & FLAG_r;
  pw = xgetpwuid(i ? uid : euid);
  if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);

  grp = xgetgrgid(i ? gid : egid);
  if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);

  if (!(flags & FLAG_G)) {
    showid("uid=", pw->pw_uid, pw->pw_name);
    showid(" gid=", grp->gr_gid, grp->gr_name);

    if (!i) {
      if (uid != euid) {
        pw = xgetpwuid(euid);
        showid(" euid=", pw->pw_uid, pw->pw_name);
      }
      if (gid != egid) {
        grp = xgetgrgid(egid);
        showid(" egid=", grp->gr_gid, grp->gr_name);
      }
    }

    showid(" groups=", grp->gr_gid, grp->gr_name);
  }

  groups = (gid_t *)toybuf;
  i = sizeof(toybuf)/sizeof(gid_t);
  ngroups = username ? getgrouplist(username, gid, groups, &i)
    : getgroups(i, groups);
  if (ngroups<0) perror_exit(0);

  for (i = 0; i<ngroups; i++) {
    if (i) xputc(' ');
    if (!(grp = getgrgid(groups[i]))) perror_msg(0);
    else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
    else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
  }
  xputc('\n');
}
示例#2
0
文件: lmdbg-m2s.c 项目: cheusov/lmdbg
static void process_line (char *buf)
{
	char *p;

	if (!buf [0])
		return;

	if (buf [0] == ' '){
		for (p = buf; *p; ++p){
			if (*p == '\t')
				*p = '\033';
			else if (*p == ' ')
				*p = '\032';
		}

		xputc ('\034', stdout);

		printf ("%s", buf + 1);
	}else{
		if (line_num > 1)
			xputc ('\n', stdout);

		printf ("%s \031", buf);
	}
}
示例#3
0
static int print_string(FILE* fp, ei_x_buff* x, char* s, int len)
{
    int ch_written = 0;		/* counter of written chars */

    xputc('"', fp, x);
    ++ch_written;
    for (; len > 0; ++s, --len) {
	int c = *s;
	if (c >= ' ') {
	    xputc((char)c, fp, x); ++ch_written; }
	else {
	    switch (c) {
	    case '\n': xputs("\\n", fp, x); ch_written += 2; break;
	    case '\r': xputs("\\r", fp, x); ch_written += 2; break;
	    case '\t': xputs("\\t", fp, x); ch_written += 2; break;
	    case '\v': xputs("\\v", fp, x); ch_written += 2; break;
	    case '\b': xputs("\\b", fp, x); ch_written += 2; break;
	    case '\f': xputs("\\f", fp, x); ch_written += 2; break;
		break;
	    default:
		ch_written += xprintf(fp, x, "\\x%x", c);
		break;
	    }
	}
    }
    xputc('"', fp, x); ch_written++;
    return ch_written;
}
示例#4
0
static void
output_property(bool* first,
                char sep,
                const char* format,
                const char* name,
                const char* value)
{
    if (*first)
        *first = false;
    else
        xputc(sep, xstdout);

    char c;
    bool escaped = false;
    while ((c = *format++)) {
        if (escaped) {
            if (c == '%') {
                xputc('%', xstdout);
            } else if (c == 'n') {
                xputs(name, xstdout);
            } else if (c == 'v' && value) {
                xputs(value, xstdout);
            } else {
                usage_error("incorrect format string \"%s\"", format-2);
            }
            escaped = false;
        } else {
            if (c == '%') {
                escaped = true;
            } else {
                xputc(c, xstdout);
            }
        }
    }
}
示例#5
0
void demo_scankey_main(void)
{
  time_t t[2];
  unsigned width, height, tick;
  char c = 'X', scratch[16];
  int key, x, y;

  t[0] = t[1] = x = tick = 0;
  memset(scratch, 0, 16);
  y = 1;

  sigatexit(tty_sigreset);  // Make ctrl-c restore tty
  tty_esc("?25l");          // hide cursor
  tty_esc("0m");            // reset color to default
  tty_esc("2J");            // Clear screen
  xset_terminal(1, 1, 0, 0); // Raw mode

  for (;;) {
    tty_jump(x, y);
    xputc(c);
    t[1&++tick] = time(0);
    if (t[0] != t[1]) terminal_probesize(&width, &height);
    // Don't block first time through, to force header print
    key = scan_key_getsize(scratch, -1*!!t[0], &width, &height);
    tty_jump(0, 0);
    printf("ESC to exit: ");
    // Print unknown escape sequence
    if (*scratch) {
      printf("key=[ESC");
      // Fetch rest of sequence after deviation, time gap determines end
      while (0<(key = scan_key_getsize(scratch, 0, &width, &height)))
        printf("%c", key);
      printf("] ");
    } else printf("key=%d ", key);
    printf("x=%d y=%d width=%d height=%d\033[K", x, y, width, height);
    fflush(0);

    if (key == -2) continue;
    if (key <= ' ') break;
    if (key>=256) {
      tty_jump(x, y);
      xputc(' ');

      key -= 256;
      if (key==KEY_UP) y--;
      else if (key==KEY_DOWN) y++;
      else if (key==KEY_RIGHT) x++;
      else if (key==KEY_LEFT) x--;
      else if (key==KEY_PGUP) y = 0;
      else if (key==KEY_PGDN) y = 999;
      else if (key==KEY_HOME) x = 0;
      else if (key==KEY_END) x = 999;
      if (y<1) y = 1;
      if (y>=height) y = height-1;
      if (x<0) x = 0;
      if (x>=width) x = width-1;
    } else c = key;
  }
  tty_reset();
}
示例#6
0
void
putchar(int c)
{

	if (c == '\n')
		xputc('\r');
	xputc(c);
}
void xprintf (
	const char*	str,	/* Pointer to the format string */
	...					/* Optional arguments */
)
{
	va_list arp;
	unsigned int r, i, w, f;
	unsigned long val;
	char s[16], c, d;


	va_start(arp, str);

	while ((c = *str++) != 0) {
		if (c != '%') {
			xputc(c); continue;
		}
		c = *str++;
		f = 0;
		if (c == '0') {				/* Flag: '0' padded */
			f = 1; c = *str++;
		}
		w = 0;
		while (c >= '0' && c <= '9') {	/* Minimum width */
			w = w * 10 + c - '0';
			c = *str++;
		}
		if (c == 'l' || c == 'L') {	/* Prefix: Size is long int */
			f |= 2; c = *str++;
		}
		if (!c) break;
		d = c;
		if (d >= 'a') d -= 0x20;
		switch (d) {				/* Type is... */
		case 'S' :					/* String */
			xputs(va_arg(arp, char*)); continue;
		case 'C' :					/* Character */
			xputc((char)va_arg(arp, int)); continue;
		case 'B' :					/* Binary */
			r = 2; break;
		case 'O' :					/* Octal */
			r = 8; break;
		case 'D' :					/* Signed decimal */
		case 'U' :					/* Unsigned decimal */
			r = 10; break;
		case 'X' :					/* Hexdecimal */
			r = 16; break;
		default:					/* Unknown */
			xputc(c); continue;
		}

		/* Get an argument and put it in numeral */
		// mthomas val = (f & 2) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : va_arg(arp, unsigned int));
		if (f & 2) {
			val = va_arg(arp, long);
		} else {
			if (d == 'D') {
示例#8
0
void dbricks(void)
{
	unsigned char z;
	for(z=0; z<sizeof(bricks); z++) {
		if ((z & 0xF) == 0) xputc('\n');
		xputc(0x30+bricks[z]);
	}
	xputc('\n');
}
示例#9
0
void debugPrintf (
	const char*	str,	// Pointer to the format string
	...					// Optional arguments
)
{
	va_list arp;
	unsigned int r, i, w, f;
	unsigned long val;
	char s[16], c, d;


	va_start(arp, str);

	while ((c = *str++) != 0) {
		if (c != '%') {
			xputc(c); continue;
		}
		c = *str++;
		f = 0;
		if (c == '0') {				// Flag: '0' padded
			f = 1; c = *str++;
		}
		w = 0;
		while (c >= '0' && c <= '9') {	// Minimum width
			w = w * 10 + c - '0';
			c = *str++;
		}
		if (c == 'l' || c == 'L') {	// Prefix: Size is long int
			f |= 2; c = *str++;
		}
		if (!c) break;
		d = c;
		if (d >= 'a') d -= 0x20;
		switch (d) {				// Type is...
		case 'S' :					// String
			xputs(va_arg(arp, char*)); continue;
		case 'C' :					// Character
			xputc((char)va_arg(arp, int)); continue;
		case 'B' :					// Binary
			r = 2; break;
		case 'O' :					// Octal
			r = 8; break;
		case 'D' :					// Signed decimal
		case 'U' :					// Unsigned decimal
			r = 10; break;
		case 'X' :					// Hexdecimal
			r = 16; break;
		default:					// Unknown
			xputc(c); continue;
		}

		// Get an argument and put it in numeral
		if (f & 2) {
			val = va_arg(arp, long);
		} else {
			if (d == 'D') {
示例#10
0
static bool rast_line(struct urf_context *ctx)
{
#ifdef NODEFLATE
#if ASCII85 == 1
	return xprint85(ctx, ctx->line_data, ctx->page_line_bytes);
#else
	size_t i = 0;
	for (; i < ctx->page_line_bytes; ++i) {
		if (!xputc(ctx, ctx->line_data[i])) {
			return false;
		}
	}

	return true;
#endif
#else


	z_stream *strm = &IMPL(ctx)->strm;
	strm->avail_in = ctx->page_line_bytes;
	strm->next_in = (unsigned char*)ctx->line_data;

	do {
		strm->avail_out = IMPL(ctx)->zlen;
		strm->next_out = IMPL(ctx)->zbuf;

		int flush = (ctx->line_n < ctx->page_hdr->height) ?
			Z_NO_FLUSH : Z_FINISH;

		if (deflate(strm, flush) != Z_STREAM_ERROR) {
			size_t have = IMPL(ctx)->zlen - strm->avail_out;
			if (have) {
#if ASCII85 == 1
				if (!xprint85(ctx, IMPL(ctx)->zbuf, have)) {
					return false;
				}
#else
				size_t i = 0;
				for (; i < have; ++i) {
					if (!xputc(ctx, IMPL(ctx)->zbuf[i])) {
						return false;
					}
				}
#endif
			}
		} else {
			URF_SET_ERROR(ctx, "deflate", Z_ERRNO);
			return false;
		}
	} while (strm->avail_out == 0);

	return true;
#endif
}
示例#11
0
文件: xprintf.c 项目: glocklueng/BLDM
void xitoa (long val, int radix, int len)
{
    BYTE c, r, sgn = 0, pad = ' ';
    BYTE s[20], i = 0;
    DWORD v;


    if (radix < 0) {
        radix = -radix;
        if (val < 0) {
            val = -val;
            sgn = '-';
        }
    }
    v = val;
    r = radix;
    if (len < 0) {
        len = -len;
        pad = '0';
    }
    if (len > 20) return;
    do {
        c = (BYTE)(v % r);
        if (c >= 10) c += 7;
        c += '0';
        s[i++] = c;
        v /= r;
    } while (v);
    if (sgn) s[i++] = sgn;
    while (i < len)
        s[i++] = pad;
    do
        xputc(s[--i]);
    while (i);
}
示例#12
0
int cli_usage_args(const int argc, const char *const *args, const struct cli_schema *commands, XPRINTF xpf)
{
  unsigned cmd;
  int matched_any = 0;
  for (cmd = 0; commands[cmd].function; ++cmd) {
    unsigned opt;
    const char *word;
    int matched = 1;
    for (opt = 0; matched && opt < argc && (word = commands[cmd].words[opt]); ++opt)
      if (strncmp(word, args[opt], strlen(args[opt])) != 0)
	matched = 0;
    if (matched) {
      matched_any = 1;
      for (opt = 0; (word = commands[cmd].words[opt]); ++opt) {
	if (word[0] == '|')
	  ++word;
	xprintf(xpf, " %s", word);
      }
      xputc('\n', xpf);
      if (commands[cmd].description && commands[cmd].description[0])
	xprintf(xpf, "   %s\n", commands[cmd].description);
    }
  }
  if (!matched_any && argc) {
    strbuf b = strbuf_alloca(160);
    strbuf_append_argv(b, argc, args);
    xprintf(xpf, " No commands matching %s\n", strbuf_str(b));
  }
  return 0;
}
示例#13
0
文件: xfile.c 项目: nyuichi/xfile
static int print_int(xFILE *stream, long x, int base) {
  static const char digits[] = "0123456789abcdef";
  char buf[20];
  int i, c, neg;

  neg = 0;
  if (x < 0) {
    neg = 1;
    x = -x;
  }

  i = 0;
  do {
    buf[i++] = digits[x % base];
  } while ((x /= base) != 0);

  if (neg) {
    buf[i++] = '-';
  }

  c = i;
  while (i-- > 0) {
    xputc(buf[i], stream);
  }
  return c;
}
示例#14
0
void start()
{
	uint32_t temp;
	uint8_t getstr[20];

	GPJ2CON = 0x1111;
	GPJ2DAT = 0x0a;

	uart0_init(115200);
	fimd_init();



	xdev_out(uart0_putc);
	xdev_in(uart0_getc);

	xprintf("FA210 Tester v1\n");

	while(1){

		xputc('>');
		xgets(getstr,15);


	}
}
示例#15
0
void xputs (const char* str)
{
	while (*str)
	{
		xputc(*str++);
	}
}
示例#16
0
文件: lmdbg-m2s.c 项目: cheusov/lmdbg
int main (int argc, char **argv)
{
	int i;
	FILE *fd;

	--argc, ++argv;

	if (!argc){
		process_stream (stdin);
	}else{
		for (i=0; i < argc; ++i){
			fd = fopen (argv [i], "r");
			if (!fd){
				perror ("fopen(3) failed");
				exit (1);
			}

			process_stream (fd);
			fclose (fd);
		}
	}

	if (line_num >= 1)
		xputc ('\n', stdout);

	return 0;
}
示例#17
0
文件: lsattr.c 项目: emaste/toybox
// Get directory information.
static int retell_dir(struct dirtree *root)
{
  char *fpath = NULL;
  
  if (root->again) {
    xputc('\n');
    return 0;
  }
  if (S_ISDIR(root->st.st_mode) && !root->parent) 
    return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);

  fpath = dirtree_path(root, NULL);
  //Special case: with '-a' option and '.'/'..' also included in printing list.
  if ((root->name[0] != '.') || (toys.optflags & FLAG_a)) {
    print_file_attr(fpath);
    if (S_ISDIR(root->st.st_mode) && (toys.optflags & FLAG_R)
        && dirtree_notdotdot(root)) {
      xprintf("\n%s:\n", fpath);
      free(fpath);
      return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
    }
  }
  free(fpath);
  return 0;
}
示例#18
0
/**
 * @brief  USR_KEYBRD_ProcessData
 *         Process Keyboard data
 * @param  data : Keyboard data to be displayed
 * @retval None
 */
void USR_KEYBRD_ProcessData(uint8_t data1,uint8_t data2 )
{
	//USB_DEBUG_MARKER();

	debug(KEYBOARD, "USR_KEYBRD_ProcessData: data1 = %x, data2 = %x\n", data1, data2);

	// Skip control characters like enter, bell, escape..
	//if ((data1 >= ' ' && data2 < 112)||(data1 == 10)||(data1==8)) {
	//if (data1 >= ' ' && data2 < 112) {
	// Could eliminate data2 and use only data1--data1 can be used to identify Function keys.

	// Check the Function Keys F1 thru F10
	if (data1 >=0x80 && data1<=0x8B) {
		//check for modified ASCII codes for F1-F12
		data1 = data1 - 0x80;
		kybd_dispFunctionKey(data1);
		//Note: 0x90 to 0x99 denote shifted Function keys 1 to 10.
	}
	else if (data1 == 0x8C) { Acquire();}
	else {
		//data1 is ASCII data
		kybd_char_switch(data1);
	}
	xputc(data1);
}
示例#19
0
文件: param.c 项目: lhmagic/rainfall
void rs485_handle(char *buf, uint16_t cnt) {
char rsp[] = "\x01\x10\x00\x00\x00\x92\x02\x00";
	
	switch (cnt) {
		case RCV_CFG_LEN:
			flash_write(PARAM_SAVE_ADDR, (uint8_t *)buf, cnt);
			parse_param(buf, cnt);
			clr_pulse_cnt();
			
			for(cnt=0; cnt<8; cnt++) {
				xputc(rsp[cnt]);
			}
			break;
		case READ_CFG_LEN:	
			if(memcmp(buf, "\x03\x03\x00\x00\x00\xA6\xC4\x52", cnt) == 0) {
				construct_rsp(buf, cnt);
				//response code in construct_rsp function.
			}
		case WRITE_UID_LEN:
			//format
			//01 10 	0A 	dev_num 	rtu_num crc16
			update_n_wirte_uid(buf);
			break;
#ifdef	DEBUG
		case 4:
			if(memcmp(buf, "TIME", 4) == 0) {
			char *time;
				time = read_bcd_time();
				for(cnt=0; cnt<6;cnt++) {
					xputc((time[cnt]>>4)+'0');
					xputc((time[cnt]&0x0F)+'0');
				}
				xputs("\r\n");
			}
			break;
		case 6:
			if(memcmp(buf, "UPLOAD", 6) == 0) {
				simulate_ring = 1;
			}
			break;
		case 10:
			set_time(buf);
			break;
#endif		
		default:
			break;
	}
示例#20
0
int unlzh(unsigned char *in, int insz, unsigned char *out, int outsz) {
    int  i, j, k, r, c;
    unsigned long int  count;

    infile   = in;
    infilel  = in + insz;
    outfile  = out;
    outfilel = out + outsz;

    /*textsize = (xgetc(infile));
    textsize |= (xgetc(infile) << 8);
    textsize |= (xgetc(infile) << 16);
    textsize |= (xgetc(infile) << 24);
    if (textsize == 0)
        return(-1);*/
    textsize = outsz;

    StartHuff();
    for (i = 0; i < N - F; i++)
        text_buf[i] = 0x20;
    r = N - F;
    for (count = 0; count < textsize; ) {
        c = DecodeChar();
        if (c < 256) {
            if (xputc(c, outfile) == -1) {
                return(-1);
            }
            text_buf[r++] = (unsigned char)c;
            r &= (N - 1);
            count++;
        } else {
            i = (r - DecodePosition() - 1) & (N - 1);
            j = c - 255 + THRESHOLD;
            for (k = 0; k < j; k++) {
                c = text_buf[(i + k) & (N - 1)];
                if (xputc(c, outfile) == -1) {
                    return(-1);
                }
                text_buf[r++] = (unsigned char)c;
                r &= (N - 1);
                count++;
            }
        }
    }
    return(outfile - out);
}
示例#21
0
void xprintf (const char* str, ...)
{
	va_list arp;
	int d, r, w, s, l;
	double f;


	va_start(arp, str);

	while ((d = *str++) != 0) {
		if (d != '%') {
			xputc(d); continue;
		}
		d = *str++; w = r = s = l = 0;
		if (d == '0') {
			d = *str++; s = 1;
		}
		while ((d >= '0')&&(d <= '9')) {
			w += w * 10 + (d - '0');
			d = *str++;
		}
		if (s) w = -w;

		if (d == 'l') {
			l = 1;
			d = *str++;
		}
		if (!d) break;
		if(d == '%'){
			xputc(d);
			continue;
		}
		if (d == 's') {
			xputs(va_arg(arp, char*));
			continue;
		}
		if(d == 'f'){
			f = (double )va_arg(arp, double);
			xputf(f,w,xputc);
			continue;
		}
		if (d == 'c') {
			xputc((char)va_arg(arp, int));
			continue;
		}
示例#22
0
文件: syscalls.c 项目: glockwork/dfu
int _write(int file, char *ptr, int len)
{
	int todo;
	file = file; /* avoid warning */
	for (todo = 0; todo < len; todo++) {
		xputc(*ptr++);
	}
	return len;
}
示例#23
0
文件: id.c 项目: luckboy/toybox
static void s_or_u(char *s, unsigned u, int done)
{
  if (toys.optflags & FLAG_n) printf("%s", s);
  else printf("%u", u);
  if (done) {
    xputc('\n');
    exit(0);
  }
}
示例#24
0
文件: nesc-xml.c 项目: tinyprod/nesc
void xml_attr_loc(location loc)
{
    if (loc == dummy_location)
    {
        xprintf("  loc=\"NONE\"");
        return;
    }
    xprintf(" loc=\"%d", loc->lineno);
    if (loc->container)
    {
        xputc('(');
        xqputs(loc->container->instance_name);
        xputc(')');
    }
    xputc(':');
    xqputs(loc->filename);
    xputc('"');
}
示例#25
0
文件: xfile.c 项目: nyuichi/xfile
int xfputs(const char *s, xFILE *stream) {
  const char *ptr = s;
  while(*ptr != '\0') {
    if (xputc(*ptr, stream) == EOF)
      return EOF;
    ++ptr;
  }
  return (int)(ptr - s);
}
示例#26
0
文件: ascii.c 项目: emaste/toybox
void ascii_main(void)
{
  char *low="NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4"
            "NAKSYNETBCANEM SUBESCFS GS RS US ";
  int x, y;

  for (x = 0; x<8; x++) printf("Dec Hex%*c", 2+2*(x<2)+(x>4), ' ');
  xputc('\n');
  for (y=0; y<=15; y++) {
    for (x=0; x<8; x++) {
      int i = x*16+y;

      if (i>95 && i<100) putchar(' ');
      printf("% 3d %02X ", i, i);
      if (i<32 || i==127) printf("%.3s ", (i==127) ? "DEL" : low+3*i);
      else printf("%c ", i);
    }
    xputc('\n');
  }
}
示例#27
0
文件: boot.c 项目: Liambeguin/openlab
int _write(int file, char *ptr, int len)
{
    (void) file;
    int i;

    for (i = 0; i < len; i++)
    {
        xputc(*ptr++);
    }

    return len;
}
示例#28
0
文件: main.c 项目: OoOverflow/toybox
// Multiplexer command, first argument is command to run, rest are args to that.
// If first argument starts with - output list of command install paths.
void toybox_main(void)
{
  static char *toy_paths[]={"usr/","bin/","sbin/",0};
  int i, len = 0;

  // fast path: try to exec immediately.
  // (Leave toys.which null to disable suid return logic.)
  if (toys.argv[1]) toy_exec(toys.argv+1);

  // For early error reporting
  toys.which = toy_list;

  if (toys.argv[1]) {
    if (!strcmp("--version", toys.argv[1])) {
      xputs(TOYBOX_VERSION);
      xexit();
    }
    if (toys.argv[1][0] != '-') {
      toys.exitval = 127;
      error_exit("Unknown command %s", toys.argv[1]);
    }
  }

  // Output list of command.
  for (i=1; i<ARRAY_LEN(toy_list); i++) {
    int fl = toy_list[i].flags;
    if (fl & TOYMASK_LOCATION) {
      if (toys.argv[1]) {
        int j;
        for (j=0; toy_paths[j]; j++)
          if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
      }
      len += printf("%s",toy_list[i].name);
      if (++len > 65) len = 0;
      xputc(len ? ' ' : '\n');
    }
  }
  xputc('\n');
}
示例#29
0
文件: xfile.c 项目: nyuichi/xfile
int xvfprintf(xFILE *stream, const char *fmt, va_list ap) {
  const char *p;
  char *sval;
  int ival;
  double dval;
  void *vp;
  int cnt = 0;

  for (p = fmt; *p; p++) {
    if (*p != '%') {
      xputc(*p, stream);
      cnt++;
      continue;
    }
    switch (*++p) {
    case 'd':
    case 'i':
      ival = va_arg(ap, int);
      cnt += print_int(stream, ival, 10);
      break;
    case 'f':
      dval = va_arg(ap, double);
      cnt += print_int(stream, dval, 10);
      xputc('.', stream);
      cnt++;
      if ((ival = fabs((dval - floor(dval)) * 1e4) + 0.5) == 0) {
        cnt += xfputs("0000", stream);
      } else {
        int i;
        for (i = 0; i < 3 - (int)log10(ival); ++i) {
          xputc('0', stream);
          cnt++;
        }
        cnt += print_int(stream, ival, 10);
      }
      break;
    case 's':
      sval = va_arg(ap, char*);
      cnt += xfputs(sval, stream);
      break;
    case 'p':
      vp = va_arg(ap, void*);
      cnt += xfputs("0x", stream);
      cnt += print_int(stream, (long)vp, 16);
      break;
    case '%':
      xputc(*(p-1), stream);
      cnt++;
      break;
    default:
      xputc('%', stream);
      xputc(*(p-1), stream);
      cnt += 2;
      break;
    }
  }
  return cnt;
}
示例#30
0
文件: cat.c 项目: idunham/toybox
static void do_cat(int fd, char *name)
{
  int i, len, size=(toys.optflags & FLAG_u) ? 1 : sizeof(toybuf);

  for(;;) {
    len = read(fd, toybuf, size);
    if (len < 0) {
      toys.exitval = EXIT_FAILURE;
      perror_msg("%s", name);
    }
    if (len < 1) break;
    if ((CFG_CAT_V || CFG_CATV) && (toys.optflags&~FLAG_u)) {
      for (i=0; i<len; i++) {
        char c=toybuf[i];

        if (c > 126 && (toys.optflags & FLAG_v)) {
          if (c > 127) {
            printf("M-");
            c -= 128;
          }
          if (c == 127) {
            printf("^?");
            continue;
          }
        }
        if (c < 32) {
          if (c == 10) {
            if (toys.optflags & FLAG_e) xputc('$');
          } else if (toys.optflags & (c==9 ? FLAG_t : FLAG_v)) {
            printf("^%c", c+'@');
            continue;
          }
        }
        xputc(c);
      }
    } else xwrite(1, toybuf, len);
  }
}