7 static char *putstr(char *d
, char *s
)
14 static int digits(unsigned long n
, int base
)
22 static char *digs
= "0123456789abcdef";
24 static char *putint(char *s
, unsigned long n
, int base
, int sign
)
28 if (sign
&& n
& 0x80000000) {
33 for (i
= 0; i
< d
; i
++) {
34 s
[d
- i
- 1] = digs
[n
% base
];
40 int vsprintf(char *dst
, char *fmt
, va_list ap
)
54 d
= putint(d
, va_arg(ap
, long), 10, 1);
57 d
= putint(d
, va_arg(ap
, long), 10, 0);
61 d
= putint(d
, va_arg(ap
, long), 16, 0);
64 *d
++ = va_arg(ap
, int);
67 d
= putstr(d
, va_arg(ap
, char *));
80 static char buf
[1 << 12];
82 static FILE _stdout
= {1};
83 static FILE _stderr
= {2};
84 FILE *stdout
= &_stdout
;
85 FILE *stderr
= &_stderr
;
87 int printf(char *fmt
, ...)
92 ret
= vsprintf(buf
, fmt
, ap
);
94 return write(1, buf
, ret
);
97 int fprintf(FILE *filp
, char *fmt
, ...)
102 ret
= vsprintf(buf
, fmt
, ap
);
104 return write(filp
->fd
, buf
, ret
);
107 int sprintf(char *dst
, char *fmt
, ...)
112 ret
= vsprintf(dst
, fmt
, ap
);
122 fprintf(stderr
, "%s: %s\n", s
, sys_errlist
[idx
]);
125 FILE *fopen(char *path
, char *mode
)
130 if (strchr(mode
, '+'))
133 flags
= *mode
== 'r' ? O_RDONLY
: O_WRONLY
;
141 fp
= malloc(sizeof(*fp
));
142 fp
->fd
= open(path
, flags
, 0600);
153 void setbuf(FILE *fp
, char *buf
)