updated on Thu Jan 12 08:01:00 UTC 2012
[aur-mirror.git] / udev-old-kernel / resolve-modalias.c
blobd2a40ead72d27ae8a8092c38bb81b9068b8c1d55
1 #include <stdio.h>
2 #include <fnmatch.h>
3 #include <string.h>
4 #include <malloc.h>
6 static char *my_getline(FILE *file) {
7 static size_t size = 1024;
8 static char *buf = NULL;
9 static unsigned int i = 0, r = 0;;
11 if(buf == NULL)
12 buf = (char*)malloc(size);
14 if(i) {
15 memmove(buf, buf+i, size-i);
16 r -= i;
17 i = 0;
20 while(1) {
21 if(i == size) {
22 size *= 2;
23 buf = (char*)realloc(buf, size);
26 if(i==r)
27 r += fread(buf+i, 1, size-i, file);
29 if(i==r && i == 0) {
30 free(buf);
31 buf = NULL;
32 r = 0;
33 return NULL;
36 if(i==r || buf[i] == '\n') {
37 buf[i++] = '\0';
38 return buf;
40 i++;
44 int main(int argc, char *argv[]) {
45 char *line, *pattern, *module;
46 char *pos1, *pos2;
48 if(argc != 3) {
49 fprintf(stderr, "usage: resolve-modalias <alias file> <modalias>\n");
50 return 1;
53 FILE *f=fopen(argv[1], "r");
54 if(!f) {
55 perror("error opening alias file");
56 return 1;
59 while((line=my_getline(f))!=NULL) {
60 if(!strncmp(line, "alias", strlen("alias"))) {
61 pos1 = index(line, ' ');
62 pos2 = index(pos1+1, ' ');
63 pattern = pos1+1;
64 *pos2 = '\0';
65 module = pos2+1;
67 if(!fnmatch(pattern, argv[2], 0))
68 printf("%s\n", module);
71 return 0;
73 //vim: set ts=2 sw=2 et: