chiark / gitweb /
[PATCH] first stupid try for a rule compose gui
[elogind.git] / udev_lib.c
1 /*
2  * udev_lib - generic stuff used by udev
3  *
4  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28
29 #include "libsysfs/sysfs/libsysfs.h"
30 #include "udev.h"
31 #include "udev_lib.h"
32
33
34 char *get_action(void)
35 {
36         char *action;
37
38         action = getenv("ACTION");
39         if (action != NULL && strlen(action) > ACTION_SIZE)
40                 action[ACTION_SIZE-1] = '\0';
41
42         return action;
43 }
44
45 char *get_devpath(void)
46 {
47         char *devpath;
48
49         devpath = getenv("DEVPATH");
50         if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
51                 devpath[DEVPATH_SIZE-1] = '\0';
52
53         return devpath;
54 }
55
56 char *get_seqnum(void)
57 {
58         char *seqnum;
59
60         seqnum = getenv("SEQNUM");
61
62         return seqnum;
63 }
64
65 char *get_subsystem(char *subsystem)
66 {
67         if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
68                 subsystem[SUBSYSTEM_SIZE-1] = '\0';
69
70         return subsystem;
71 }
72
73 int file_map(const char *filename, char **buf, size_t *bufsize)
74 {
75         struct stat stats;
76         int fd;
77
78         fd = open(filename, O_RDONLY);
79         if (fd < 0) {
80                 return -1;
81         }
82
83         if (fstat(fd, &stats) < 0) {
84                 return -1;
85         }
86
87         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
88         if (*buf == MAP_FAILED) {
89                 return -1;
90         }
91         *bufsize = stats.st_size;
92
93         close(fd);
94
95         return 0;
96 }
97
98 void file_unmap(char *buf, size_t bufsize)
99 {
100         munmap(buf, bufsize);
101 }
102
103 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
104 {
105         size_t count = 0;
106
107         for (count = cur; count < buflen && buf[count] != '\n'; count++);
108
109         return count - cur;
110 }
111