-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodules.c
More file actions
54 lines (42 loc) · 941 Bytes
/
modules.c
File metadata and controls
54 lines (42 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
static int moduleLoaded(char *modName) {
int len = strlen(modName);
int found = false;
FILE *fd = fopen("/proc/modules", "r");
char line [80];
if(fd == NULL) {
fprintf(stderr, "Unable to check modules: %s\n", strerror(errno));
exit(1);
}
while(fgets(line, 80, fd) != NULL) {
if(strncmp(line, modName, len) != 0)
continue;
found = true;
break;
}
fclose(fd);
return found;
}
static void loadModule(char * module) {
char cmd[80];
if (moduleLoaded(module)) {
return;
}
printf("Loading module: %s", module);
sprintf(cmd, "modprobe %s", module);
system(cmd);
sleep(1);
if (!moduleLoaded(module)) {
fprintf(stderr, "Unable to load %s\n", module);
exit(1);
}
}
void loadSpiModules() {
loadModule("spidev");
loadModule("spi_bcm2708");
}