revert between 56095 -> 55830 in arch
[AROS.git] / arch / all-mingw32 / bsdsocket / bsdsocket_util.c
blobff4aea09eb4c92f6a998257ad2e23b930ad4673f
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <sys/errno.h>
7 #include <string.h>
9 #include "bsdsocket_intern.h"
10 #include "bsdsocket_util.h"
12 void SetError(int error, struct TaskBase *libPtr)
14 switch (libPtr->errnoSize)
16 case 8:
17 *(UQUAD *)libPtr->errnoPtr = (UQUAD)error;
18 break;
20 case 4:
21 *(ULONG *)libPtr->errnoPtr = (ULONG)error;
22 break;
24 case 2:
25 *(UWORD *)libPtr->errnoPtr = (UWORD)error;
26 break;
28 case 1:
29 *(UBYTE *)libPtr->errnoPtr = (UBYTE)error;
30 break;
32 default:
33 D(bug("[SetErrno] Bogus errno size %u for TaskBase 0x%p\n", libPtr->errnoSize, libPtr));
34 break;
38 ULONG SetDTableSize(ULONG size, struct TaskBase *taskBase)
40 struct Socket **old = taskBase->dTable;
41 struct Socket **table;
42 ULONG oldsize = taskBase->dTableSize * sizeof(struct Socket *);
43 ULONG newsize = size * sizeof(struct Socket *);
46 /* FIXME: just a temporary measure */
47 if (size < taskBase->dTableSize)
48 return EMFILE;
50 table = AllocPooled(taskBase->pool, newsize);
52 if (!table)
53 return ENOMEM;
55 memset(table, 0, newsize);
57 if (old)
58 CopyMem(old, table, oldsize);
60 taskBase->dTable = table;
61 taskBase->dTableSize = size;
63 if (old)
64 FreePooled(taskBase->pool, old, oldsize);
66 return 0;
69 int GetFreeFD(struct TaskBase *taskBase)
71 int i;
73 for (i = 0; i < taskBase->dTableSize; i++)
75 if (!taskBase->dTable[i])
76 return i;
79 SetError(EMFILE, taskBase);
80 return -1;
83 struct Socket *GetSocket(int s, struct TaskBase *taskBase)
85 struct Socket *sd;
87 if (s >= taskBase->dTableSize)
89 SetError(ENOTSOCK, taskBase);
90 return NULL;
93 sd = taskBase->dTable[s];
94 if (!sd)
95 SetError(ENOTSOCK, taskBase);
97 return sd;
100 struct WSsockaddr *MakeSockAddr(const struct sockaddr *src, int len, struct TaskBase *taskBase)
102 struct WSsockaddr *sa = AllocPooled(taskBase->pool, len);
104 if (sa)
106 CopyMem(src, sa, len);
107 sa->sa_family = src->sa_family;
109 else
110 SetError(ENOMEM, taskBase);
112 return sa;