nckernel  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
stdio.c
Go to the documentation of this file.
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <unistd.h>
7 #include <pthread.h>
8 #include <errno.h>
9 
10 #include <object.h>
11 #include <crt0.h>
12 #include <list.h>
13 #include <thread.h>
14 
15 #include <debug.h>
16 
17 int fputc(int c, FILE *stream)
18 {
19  if (!stream->size) {
20  int ret;
21 
22  ret = write(fileno(stream), (char *)&c, 1);
23  if (ret < 0) {
24  return ret;
25  }
26 
27  return c;
28  }
29 
30  stream->buffer[stream->offset] = (char)c;
31  stream->offset++;
32 
33  if ((stream->offset == stream->size) || c == '\n' || c == '\r') {
34  int ret;
35 
36  ret = fflush(stream);
37  if (ret < 0) {
38  return ret;
39  }
40  }
41 
42  return c;
43 }
44 
45 int fputs(const char *s, FILE *stream)
46 {
47  register int i = 0;
48  int ret;
49 
50  while (*s) {
51  ret = fputc(*s, stream);
52  if (ret < 0) {
53  return ret;
54  }
55 
56  s++;
57  i++;
58  }
59 
60  return i;
61 }
62 
64 {
65  int ret;
66 
67  ret = write(fileno(stream), stream->buffer, stream->offset);
68  if (ret < 0) {
69  return ret;
70  }
71 
72  stream->offset -= ret;
73  return 0;
74 }
75 
77 {
78 }
79 
81 {
82  return -ENOSYS;
83 }
84 
86 {
87  return -ENOSYS;
88 }
89 
91 {
92  struct thread *tcb;
93  int fd;
94 
95  tcb = pthread_self();
96  if (!tcb) {
97  return -EFAULT;
98  }
99 
105  fd = (int)((unsigned long)stream - (unsigned long)tcb->file_ctx->table);
106  fd /= sizeof(tcb->file_ctx->table[0]);
107 
108  return fd;
109 }
110 
111 /* End of a file */