Indentation fix, cleanup.
[AROS.git] / arch / all-unix / devs / unixio / deviceutil.c
blob772f59ec453f4acf839de5ee010b004a1f433d9c
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
7 #define DOPEN(x)
9 #include <errno.h>
11 #include <aros/debug.h>
12 #include <aros/symbolsets.h>
13 #include <exec/errors.h>
14 #include <proto/exec.h>
16 #include "unixio_device.h"
18 static int unixdevice_Open(struct UnixDevice *unixioDev, struct IOStdReq *ioreq, STRPTR unitname, ULONG flags)
20 struct UnitData *unit;
22 D(bug("unixio.device: open UnitData %s\n", unitname));
24 ObtainSemaphore(&unixioDev->sigsem);
26 unit = (struct UnitData *)FindName(&unixioDev->units, unitname);
28 if (UnitData)
30 /* TODO: Check sharing permission here */
31 UnitData->usecount++;
32 ReleaseSemaphore(&unixioDev->sigsem);
34 ioreq->io_Unit = (struct Unit *)unit;
35 ioreq->io_Error = 0;
36 ioreq->io_Message.mn_Node.ln_Type = NT_REPLYMSG;
38 DOPEN(bug("unixio.device: in libopen func. Unit is already open\n"));
39 return TRUE;
42 DOPEN(bug("unixio.device: in libopen func. Creating new UnitData ...\n"));
44 unit = (struct UnitData *)AllocVec(sizeof(struct UnitData) + strlen(unitname), MEMF_PUBLIC);
45 if (unit)
47 unit->fd = Hidd_UnixIO_Open(unixioDev->unixio, unitname, O_RWDR|O_NONBLOCK, 0755, NULL);
48 if (unit->fd != -1)
50 unit->unitNode.ln_Name = unit->unitName;
51 unit->unixio = unixioDev->unixio;
52 unit->usecount = 1;
53 unit->writeLength = 0;
54 unit->stopped = FALSE;
55 unit->eofmode = FALSE;
56 NEWLIST(&unit->readQueue);
57 NEWLIST(&unit->writeQueue);
58 strcpy(unit->unitName, unitname);
60 AddTail((struct List *)&unixioDev->units, &UnitData->n);
61 ReleaseSemaphore(&unixioDev->sigsem);
63 ioreq->io_Unit = (struct Unit *)unit;
64 ioreq->io_Error = 0;
65 ioreq->io_Message.mn_Node.ln_Type = NT_REPLYMSG;
67 return TRUE;
71 ReleaseSemaphore(&unixioDev->sigsem);
72 ioreq->io_Error = IOERR_OPENFAIL;
74 return FALSE;
77 ADD2OPENDEV(unixdevice_Open, 0);
79 /****************************************************************************************/
81 static int unixdevice_Close((struct UnixDevice *unixioDev, struct IOStdReq *ioreq)
83 struct UnitData *unit = (struct UnitData *)ioreq->io_Unit;
84 ULONG usecnt;
86 D(bug("unixio.device: close unit %s\n", unit->n.ln_Name));
88 ObtainSemaphore(&unixioDev->sigsem);
90 usecnt = --unit->usecount;
91 if (!usecnt)
93 D(bug("unixio.device: Unit is no more in use, disposing...\n"));
95 Remove(&unit->unitNode);
98 ReleaseSemaphore(&unixioDev->sigsem);
100 if (!usecnt)
102 Hidd_UnixIO_CloseFile(unixioDev->unixio, unit->fd, NULL);
103 FreeVec(unit);
106 return TRUE;
109 ADD2CLOSEDEV(unixdevice_Close, 0);
111 /****************************************************************************************/