00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004
00005 #include "hdev.h"
00006 #include "hstructures.h"
00007
00008 static struct hdev* firstd = NULL;
00009 static struct hdev* lastd = NULL;
00010 static int dev_cnt = 0;
00011
00012
00013 struct hdev {
00014 int major;
00015 char devicePath[HDEV_PATH_SIZE];
00016 struct hdev* next;
00017 struct hdev* prev;
00018 };
00019
00020
00021 static int set_major_(struct hdev* device, int major);
00022 static int set_path_(struct hdev* device, char* path);
00023 static int set_next_(struct hdev* device, struct hdev* next);
00024 static int set_prev_(struct hdev* device, struct hdev* prev);
00025 static int set_last_(struct hdev* last);
00026 static int set_first_(struct hdev* first);
00027 static int decrement_size_();
00028 static int increment_size_();
00029
00030
00031 static int increment_size_()
00032 {
00033 dev_cnt += 1;
00034
00035 return dev_cnt;
00036 }
00037
00038
00039 static int decrement_size_()
00040 {
00041 dev_cnt -= 1;
00042
00043 if (dev_cnt < 0)
00044 dev_cnt = 0;
00045
00046 return dev_cnt;
00047 }
00048
00049
00050 static int set_last_(struct hdev* last)
00051 {
00052 lastd = last;
00053
00054 return 0;
00055 }
00056
00057
00058 static int set_first_(struct hdev* first)
00059 {
00060 firstd = first;
00061
00062 return 0;
00063 }
00064
00065
00066 static int set_next_(struct hdev* device, struct hdev* next)
00067 {
00068 if (device == NULL)
00069 return -1;
00070
00071 device->next = next;
00072
00073 return 0;
00074 }
00075
00076
00077 static int set_prev_(struct hdev* device, struct hdev* prev)
00078 {
00079 if (device == NULL)
00080 return -1;
00081
00082 device->prev = prev;
00083
00084 return 0;
00085 }
00086
00087
00088 static int set_major_(struct hdev* device, int major)
00089 {
00090 if (device == NULL)
00091 return -1;
00092
00093 device->major = major;
00094
00095 return 0;
00096 }
00097
00098
00099 static int set_path_(struct hdev* device, char* path)
00100 {
00101 if (device == NULL)
00102 return -1;
00103
00104 if (strncpy(device->devicePath, path, HDEV_PATH_SIZE) != device->devicePath)
00105 return -1;
00106
00107 return 0;
00108 }
00109
00110
00111 int hdev_size()
00112 {
00113 return dev_cnt;
00114 }
00115
00116
00117 int hdev_get_major(struct hdev* device)
00118 {
00119 if (device == NULL)
00120 return -1;
00121
00122 return device->major;
00123 }
00124
00125
00126 struct hdev* hdev_get_by_major(int major)
00127 {
00128 if (hdev_size() == 0)
00129 return NULL;
00130
00131 struct hdev* device = hdev_get_first();
00132
00133 if (hdev_get_major(device) == major)
00134 return device;
00135
00136
00137 while (hdev_get_next(device) != NULL) {
00138 device = hdev_get_next(device);
00139 if (hdev_get_major(device) == major)
00140 return device;
00141 }
00142
00143
00144 return NULL;
00145 }
00146
00147
00148 struct hdev* hdev_add(int major, char* device_path)
00149 {
00150 if (hdev_get_by_major(major) != NULL)
00151 return NULL;
00152
00153 struct hdev* device = calloc(1, sizeof(struct hdev));
00154
00155 set_major_(device, major);
00156 set_path_(device, device_path);
00157
00158 if (hdev_size() == 0)
00159 set_first_(device);
00160
00161 set_prev_(device, hdev_get_last());
00162 set_next_(device, NULL);
00163 set_next_(hdev_get_last(), device);
00164
00165 set_last_(device);
00166
00167 increment_size_();
00168
00169 return device;
00170 }
00171
00172
00173 int hdev_remove(struct hdev* device)
00174 {
00175 if (device == NULL)
00176 return -1;
00177
00178 set_prev_(hdev_get_next(device), hdev_get_prev(device));
00179 set_next_(hdev_get_prev(device), hdev_get_next(device));
00180
00181 if (hdev_get_last() == device)
00182 set_last_(hdev_get_prev(device));
00183
00184 if (hdev_get_first() == device)
00185 set_first_(hdev_get_next(device));
00186
00187 free(device);
00188 decrement_size_();
00189
00190 return 0;
00191 }
00192
00193
00194 int hdev_remove_by_major(int major)
00195 {
00196 struct hdev* device = hdev_get_by_major(major);
00197 return hdev_remove(device);
00198 }
00199
00200
00201 int hdev_remove_all()
00202 {
00203 struct hdev* device = hdev_get_first();
00204
00205 int number_removed = 0;
00206 while (hdev_get_next(device) != NULL) {
00207 device = hdev_get_next(device);
00208 if (hdev_remove(hdev_get_prev(device)) == 0)
00209 number_removed += 1;
00210 }
00211
00212 if (hdev_remove(device) == 0)
00213 number_removed += 1;
00214
00215 return number_removed;
00216 }
00217
00218
00219 struct hdev* hdev_get_first()
00220 {
00221 return firstd;
00222 }
00223
00224
00225 struct hdev* hdev_get_last()
00226 {
00227 return lastd;
00228 }
00229
00230
00231 struct hdev* hdev_get_next(struct hdev* device)
00232 {
00233 if (device == NULL)
00234 return NULL;
00235
00236 return device->next;
00237 }
00238
00239
00240 struct hdev* hdev_get_prev(struct hdev* device)
00241 {
00242 if (device == NULL)
00243 return NULL;
00244
00245 return device->prev;
00246 }
00247
00248