dev.c (1034B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <unistd.h> 5 #include <limits.h> 6 #include <stdlib.h> 7 #include <stdio.h> 8 #include <string.h> 9 #include "../util.h" 10 11 /* read uevent file and set environment variables */ 12 int 13 readuevent(const char *file) 14 { 15 FILE *fp; 16 int status = 0; 17 char buf[BUFSIZ]; 18 char *p, *name, *value; 19 20 if(!(fp = fopen(file, "r"))) 21 return -1; 22 while(!feof(fp)) { 23 fgets(buf, sizeof(buf) - 1, fp); 24 if(ferror(fp)) { 25 status = -2; 26 break; 27 } 28 if((p = strchr(buf, '\n'))) 29 *p = '\0'; 30 if(!(p = strchr(buf, '='))) 31 continue; 32 *p = '\0'; 33 p++; 34 name = buf; 35 value = p; 36 setenv(name, value, 1); 37 } 38 fclose(fp); 39 return status; 40 } 41 42 /* `majmin' format is maj:min */ 43 int 44 devtype(const char *majmin) 45 { 46 char path[PATH_MAX]; 47 48 snprintf(path, sizeof(path), "/sys/dev/block/%s", majmin); 49 if (!access(path, F_OK)) 50 return S_IFBLK; 51 snprintf(path, sizeof(path), "/sys/dev/char/%s", majmin); 52 if (!access(path, F_OK)) 53 return S_IFCHR; 54 return -1; 55 }