1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
19 #include <sys/socket.h>
21 extern int vsprintf(char *, const char *, va_list);
22 extern void _exit(int status);
24 void exit(int status);
26 int open(const char* name, int flags)
30 /* search free file descriptor */
31 for (fd=0; fd<FILEIO_MAX; ++fd) {
32 if(fd_array[fd].type == FILEIO_TYPE_EMPTY) {
36 if (fd == FILEIO_MAX) {
37 printf("Can not open \"%s\" because file descriptor list is full\n", name);
38 /* there is no free file descriptor available */
42 fd_array[fd].ih = of_open(name);
43 if (fd_array[fd].ih == 0)
46 fd_array[fd].type = FILEIO_TYPE_FILE;
51 int pre_open_ih(int fd, ihandle_t ih)
53 if (fd_array[fd].type != FILEIO_TYPE_EMPTY)
56 fd_array[fd].type = FILEIO_TYPE_FILE;
61 int socket(int domain, int type, int proto, char *mac_addr)
67 /* search free file descriptor */
68 for (fd=0; fd<FILEIO_MAX; ++fd) {
69 if(fd_array[fd].type == FILEIO_TYPE_EMPTY) {
73 if (fd == FILEIO_MAX) {
74 printf("Can not open socket, file descriptor list is full\n");
75 /* there is no free file descriptor available */
79 fd_array[fd].ih = of_interpret_1("my-parent", tmpbuf);
80 if (fd_array[fd].ih == 0) {
81 printf("Can not open socket, no parent instance\n");
84 ph = of_instance_to_package(fd_array[fd].ih);
86 printf("Can not open socket, no parent package\n");
89 if (of_get_mac(ph, mac_addr) < 0) {
90 printf("Can not open socket, no MAC address\n");
93 fd_array[fd].type = FILEIO_TYPE_SOCKET;
100 if (fd < 0 || fd >= FILEIO_MAX ||
101 fd_array[fd].type == FILEIO_TYPE_EMPTY)
103 if (fd_array[fd].type == FILEIO_TYPE_FILE)
104 of_close(fd_array[fd].ih);
105 fd_array[fd].type = FILEIO_TYPE_EMPTY;
109 ssize_t read(int fd, void *buf, size_t len)
111 if (fd < 0 || fd >= FILEIO_MAX ||
112 fd_array[fd].type == FILEIO_TYPE_EMPTY)
115 return of_read(fd_array[fd].ih, buf, len);
118 ssize_t write (int fd, const void *buf, size_t len)
122 const char *dbuf = buf;
125 if (fd == 1 || fd == 2) {
126 dest_buf_ptr = &dest_buf[0];
127 for (i = 0; i < len && i < 256; i++)
129 *dest_buf_ptr++ = *dbuf++;
130 if (dbuf[-1] == '\n')
131 *dest_buf_ptr++ = '\r';
133 len = dest_buf_ptr - &dest_buf[0];
137 if(fd < 0 || fd >= FILEIO_MAX ||
138 fd_array[fd].type == FILEIO_TYPE_EMPTY)
141 return of_write(fd_array[fd].ih, (void *)buf, len);
144 ssize_t lseek (int fd, long offset, int whence)
146 return 0; // this syscall is unused !!!
151 of_seek (fd_array[fd], (unsigned int) (offset>>32), (unsigned int) (offset & 0xffffffffULL));
157 int recv(int fd, void *packet, int packet_len, int flags)
159 return read(fd, packet, packet_len);
162 int send(int fd, const void *packet, int packet_len, int flags)
164 return write(fd, packet, packet_len);
167 int sendto(int fd, const void *packet, int packet_len, int flags,
168 const void *sock_addr, int sock_addr_len)
170 return send(fd, packet, packet_len, flags);
173 void exit(int status)