1 | /*DDK*************************************************************************/
|
---|
2 | /* */
|
---|
3 | /* COPYRIGHT Copyright (C) 1991, 2003 IBM Corporation */
|
---|
4 | /* */
|
---|
5 | /* The following IBM OS/2 source code is provided to you solely for */
|
---|
6 | /* the purpose of assisting you in your development of OS/2 device */
|
---|
7 | /* drivers. You may use this code in accordance with the IBM License */
|
---|
8 | /* Agreement provided in the IBM Developer Connection Device Driver */
|
---|
9 | /* Source Kit for OS/2. This Copyright statement may not be removed. */
|
---|
10 | /* */
|
---|
11 | /*****************************************************************************/
|
---|
12 | /**************************************************************************
|
---|
13 | *
|
---|
14 | * SOURCE FILE NAME = LISTDEVN.C
|
---|
15 | *
|
---|
16 | * DESCRIPTIVE NAME = Device names linked list
|
---|
17 | *
|
---|
18 | *
|
---|
19 | * VERSION = V1.0
|
---|
20 | *
|
---|
21 | * DATE
|
---|
22 | *
|
---|
23 | * DESCRIPTION Device names linked list
|
---|
24 | *
|
---|
25 | *
|
---|
26 | * FUNCTIONS
|
---|
27 | *
|
---|
28 | *
|
---|
29 | *
|
---|
30 | * NOTES
|
---|
31 | *
|
---|
32 | *
|
---|
33 | * STRUCTURES
|
---|
34 | *
|
---|
35 | * EXTERNAL REFERENCES
|
---|
36 | *
|
---|
37 | * EXTERNAL FUNCTIONS
|
---|
38 | *
|
---|
39 | */
|
---|
40 |
|
---|
41 | // driver device names
|
---|
42 | // dupechecked sorted linked list
|
---|
43 |
|
---|
44 | // used to read/set driver ea's
|
---|
45 | #include <os2.h>
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <string.h>
|
---|
49 |
|
---|
50 | typedef struct _DEVICE_LIST_NODE
|
---|
51 | {
|
---|
52 | char device_name[50]; //fixme: use defined drivername len
|
---|
53 | struct _DEVICE_LIST_NODE *next;
|
---|
54 | } DEVICE_LIST_NODE;
|
---|
55 |
|
---|
56 | static DEVICE_LIST_NODE *device_list_head = NULL;
|
---|
57 |
|
---|
58 | void DeviceList_Destroy(void)
|
---|
59 | {
|
---|
60 | // free the list
|
---|
61 |
|
---|
62 | DEVICE_LIST_NODE *tmp_node1;
|
---|
63 | DEVICE_LIST_NODE *tmp_node2;
|
---|
64 | tmp_node1 = device_list_head;
|
---|
65 | while(tmp_node1)
|
---|
66 | {
|
---|
67 | tmp_node2 = tmp_node1->next;
|
---|
68 | free(tmp_node1);
|
---|
69 | tmp_node1 = tmp_node2;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | // add driver name to our list
|
---|
75 | int DeviceList_AddName(char *name)
|
---|
76 | {
|
---|
77 | if(device_list_head == NULL)
|
---|
78 | {
|
---|
79 | // instantiate new list
|
---|
80 | device_list_head = malloc(sizeof(DEVICE_LIST_NODE));
|
---|
81 | if(device_list_head == NULL)
|
---|
82 | {
|
---|
83 | goto EXIT_FAIL;
|
---|
84 | }
|
---|
85 | memset(device_list_head,0,sizeof(DEVICE_LIST_NODE));
|
---|
86 | strncpy(device_list_head->device_name,name,49);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | // try to find dupe
|
---|
91 | DEVICE_LIST_NODE *tmp_node1 = device_list_head;
|
---|
92 | DEVICE_LIST_NODE *tmp_node2 = tmp_node1->next;
|
---|
93 | BOOL fFoundDupe = FALSE;
|
---|
94 |
|
---|
95 | // if tmp_node2 is NULL, we have found our last
|
---|
96 | // element... (and actually will be attaching new
|
---|
97 | // node to end of the list)
|
---|
98 | while(tmp_node2)
|
---|
99 | {
|
---|
100 | if(!strcmp(tmp_node1->device_name,name))
|
---|
101 | {
|
---|
102 | fFoundDupe = TRUE;
|
---|
103 | break;
|
---|
104 | }
|
---|
105 |
|
---|
106 | // we are seeking for the right place, where previous value
|
---|
107 | // is less than new string and next value would be more than
|
---|
108 | // new string
|
---|
109 |
|
---|
110 | if(strcmp(tmp_node1->device_name,name) < 0 &&
|
---|
111 | strcmp(tmp_node2->device_name,name) > 0 )
|
---|
112 | {
|
---|
113 | // found place where we should put our new device
|
---|
114 | break;
|
---|
115 | }
|
---|
116 |
|
---|
117 | tmp_node1 = tmp_node2;
|
---|
118 | tmp_node2 = tmp_node1->next;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if(!fFoundDupe)
|
---|
122 | {
|
---|
123 | // add a new entry
|
---|
124 | DEVICE_LIST_NODE *new_node = malloc(sizeof(DEVICE_LIST_NODE));
|
---|
125 | if(new_node == NULL)
|
---|
126 | {
|
---|
127 | goto EXIT_FAIL;
|
---|
128 | }
|
---|
129 |
|
---|
130 | memset(new_node,0,sizeof(DEVICE_LIST_NODE));
|
---|
131 | strncpy(new_node->device_name,name,49);
|
---|
132 | tmp_node1->next = new_node;
|
---|
133 | new_node->next = tmp_node2;
|
---|
134 | }
|
---|
135 | //
|
---|
136 | }
|
---|
137 |
|
---|
138 | return TRUE; // all ok
|
---|
139 |
|
---|
140 | EXIT_FAIL:
|
---|
141 | if(device_list_head)
|
---|
142 | {
|
---|
143 | DeviceList_Destroy();
|
---|
144 | }
|
---|
145 | return FALSE;
|
---|
146 | }
|
---|
147 |
|
---|
148 | char *DeviceList_GetName(int start)
|
---|
149 | {
|
---|
150 | static DEVICE_LIST_NODE *curr_node = NULL;
|
---|
151 |
|
---|
152 | if(start)
|
---|
153 | curr_node=device_list_head;
|
---|
154 | else
|
---|
155 | {
|
---|
156 | if(curr_node)
|
---|
157 | curr_node = curr_node->next;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if(curr_node == NULL) return NULL;
|
---|
161 | else
|
---|
162 | return curr_node->device_name;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|