diff options
author | Mike Frysinger <vapier@gentoo.org> | 2006-02-03 00:25:37 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2006-02-03 00:25:37 +0000 |
commit | a421ba8203df8cd8a09d4bfb158057d9e717648f (patch) | |
tree | c657bc96973f8f67b71917d6cddb31365e58022e /util-linux/mdev.c | |
parent | 248d2220f9985754268f4492278758052494b80a (diff) | |
download | busybox-w32-a421ba8203df8cd8a09d4bfb158057d9e717648f.tar.gz busybox-w32-a421ba8203df8cd8a09d4bfb158057d9e717648f.tar.bz2 busybox-w32-a421ba8203df8cd8a09d4bfb158057d9e717648f.zip |
cleanup style ... just because you use less spaces doesnt mean the resulting code is smaller
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r-- | util-linux/mdev.c | 113 |
1 files changed, 63 insertions, 50 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c index c100e0fa6..aef84ac45 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c | |||
@@ -23,19 +23,20 @@ | |||
23 | #include "xregex.h" | 23 | #include "xregex.h" |
24 | 24 | ||
25 | #define DEV_PATH "/dev" | 25 | #define DEV_PATH "/dev" |
26 | #define MDEV_CONF "/etc/mdev.conf" | ||
26 | 27 | ||
27 | #include <busybox.h> | 28 | #include <busybox.h> |
28 | 29 | ||
29 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ | 30 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ |
30 | static void make_device(char *path) | 31 | static void make_device(char *path) |
31 | { | 32 | { |
32 | char *device_name,*s; | 33 | char *device_name, *s; |
33 | int major,minor,type,len,fd; | 34 | int major, minor, type, len, fd; |
34 | int mode=0660; | 35 | int mode = 0660; |
35 | uid_t uid=0; | 36 | uid_t uid = 0; |
36 | gid_t gid=0; | 37 | gid_t gid = 0; |
37 | 38 | ||
38 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); | 39 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX); |
39 | 40 | ||
40 | /* Try to read major/minor string */ | 41 | /* Try to read major/minor string */ |
41 | 42 | ||
@@ -43,60 +44,64 @@ static void make_device(char *path) | |||
43 | fd = open(temp, O_RDONLY); | 44 | fd = open(temp, O_RDONLY); |
44 | len = read(fd, temp, PATH_MAX-1); | 45 | len = read(fd, temp, PATH_MAX-1); |
45 | close(fd); | 46 | close(fd); |
46 | if (len<1) goto end; | 47 | if (len < 1) goto end; |
47 | 48 | ||
48 | /* Determine device name, type, major and minor */ | 49 | /* Determine device name, type, major and minor */ |
49 | 50 | ||
50 | device_name = strrchr(path, '/') + 1; | 51 | device_name = strrchr(path, '/') + 1; |
51 | type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK; | 52 | type = strncmp(path+5, "block/", 6) ? S_IFCHR : S_IFBLK; |
52 | if(sscanf(temp, "%d:%d", &major, &minor) != 2) goto end; | 53 | if (sscanf(temp, "%d:%d", &major, &minor) != 2) |
54 | goto end; | ||
53 | 55 | ||
54 | /* If we have a config file, look up permissions for this device */ | 56 | /* If we have a config file, look up permissions for this device */ |
55 | 57 | ||
56 | if (ENABLE_FEATURE_MDEV_CONF) { | 58 | if (ENABLE_FEATURE_MDEV_CONF) { |
57 | char *conf,*pos,*end; | 59 | char *conf, *pos, *end; |
58 | 60 | ||
59 | /* mmap the config file */ | 61 | /* mmap the config file */ |
60 | if (-1!=(fd=open("/etc/mdev.conf",O_RDONLY))) { | 62 | if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) { |
61 | len=lseek(fd,0,SEEK_END); | 63 | len = lseek(fd, 0, SEEK_END); |
62 | conf=mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0); | 64 | conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); |
63 | if (conf) { | 65 | if (conf) { |
64 | int line=0; | 66 | int line = 0; |
65 | 67 | ||
66 | /* Loop through lines in mmaped file*/ | 68 | /* Loop through lines in mmaped file*/ |
67 | for (pos=conf;pos-conf<len;) { | 69 | for (pos=conf; pos-conf<len;) { |
68 | int field; | 70 | int field; |
69 | char *end2; | 71 | char *end2; |
70 | 72 | ||
71 | line++; | 73 | line++; |
72 | /* find end of this line */ | 74 | /* find end of this line */ |
73 | for(end=pos;end-conf<len && *end!='\n';end++); | 75 | for(end=pos; end-conf<len && *end!='\n'; end++) |
76 | ; | ||
74 | 77 | ||
75 | /* Three fields: regex, uid:gid, mode */ | 78 | /* Three fields: regex, uid:gid, mode */ |
76 | for (field=3;field;field--) { | 79 | for (field=3; field; field--) { |
77 | /* Skip whitespace */ | 80 | /* Skip whitespace */ |
78 | while (pos<end && isspace(*pos)) pos++; | 81 | while (pos<end && isspace(*pos)) |
79 | if (pos==end || *pos=='#') break; | 82 | pos++; |
80 | for (end2=pos; | 83 | if (pos==end || *pos=='#') |
81 | end2<end && !isspace(*end2) && *end2!='#'; end2++); | 84 | break; |
82 | switch(field) { | 85 | for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++) |
86 | ; | ||
87 | |||
88 | switch (field) { | ||
83 | /* Regex to match this device */ | 89 | /* Regex to match this device */ |
84 | case 3: | 90 | case 3: |
85 | { | 91 | { |
86 | char *regex=strndupa(pos,end2-pos); | 92 | char *regex = strndupa(pos,end2-pos); |
87 | regex_t match; | 93 | regex_t match; |
88 | regmatch_t off; | 94 | regmatch_t off; |
89 | int result; | 95 | int result; |
90 | 96 | ||
91 | /* Is this it? */ | 97 | /* Is this it? */ |
92 | xregcomp(&match,regex,REG_EXTENDED); | 98 | xregcomp(&match,regex,REG_EXTENDED); |
93 | result=regexec(&match,device_name,1,&off,0); | 99 | result = regexec(&match,device_name,1,&off,0); |
94 | regfree(&match); | 100 | regfree(&match); |
95 | 101 | ||
96 | /* If not this device, skip rest of line */ | 102 | /* If not this device, skip rest of line */ |
97 | if(result || off.rm_so | 103 | if (result || off.rm_so || off.rm_eo!=strlen(device_name)) |
98 | || off.rm_eo!=strlen(device_name)) | 104 | goto end_line; |
99 | goto end_line; | ||
100 | 105 | ||
101 | break; | 106 | break; |
102 | } | 107 | } |
@@ -106,48 +111,54 @@ static void make_device(char *path) | |||
106 | char *s2; | 111 | char *s2; |
107 | 112 | ||
108 | /* Find : */ | 113 | /* Find : */ |
109 | for(s=pos;s<end2 && *s!=':';s++); | 114 | for(s=pos; s<end2 && *s!=':'; s++) |
110 | if(s==end2) goto end_line; | 115 | ; |
116 | if (s == end2) | ||
117 | goto end_line; | ||
111 | 118 | ||
112 | /* Parse UID */ | 119 | /* Parse UID */ |
113 | uid=strtoul(pos,&s2,10); | 120 | uid = strtoul(pos,&s2,10); |
114 | if(s!=s2) { | 121 | if (s != s2) { |
115 | struct passwd *pass; | 122 | struct passwd *pass; |
116 | pass=getpwnam(strndupa(pos,s-pos)); | 123 | pass = getpwnam(strndupa(pos,s-pos)); |
117 | if(!pass) goto end_line; | 124 | if (!pass) |
118 | uid=pass->pw_uid; | 125 | goto end_line; |
126 | uid = pass->pw_uid; | ||
119 | } | 127 | } |
120 | s++; | 128 | s++; |
121 | /* parse GID */ | 129 | /* parse GID */ |
122 | gid=strtoul(s,&s2,10); | 130 | gid = strtoul(s,&s2,10); |
123 | if(end2!=s2) { | 131 | if (end2 != s2) { |
124 | struct group *grp; | 132 | struct group *grp; |
125 | grp=getgrnam(strndupa(s,end2-s)); | 133 | grp = getgrnam(strndupa(s,end2-s)); |
126 | if(!grp) goto end_line; | 134 | if (!grp) |
127 | gid=grp->gr_gid; | 135 | goto end_line; |
136 | gid = grp->gr_gid; | ||
128 | } | 137 | } |
129 | break; | 138 | break; |
130 | } | 139 | } |
131 | /* mode */ | 140 | /* mode */ |
132 | case 1: | 141 | case 1: |
133 | { | 142 | { |
134 | mode=strtoul(pos,&pos,8); | 143 | mode = strtoul(pos,&pos,8); |
135 | if(pos!=end2) goto end_line; | 144 | if (pos != end2) |
136 | goto found_device; | 145 | goto end_line; |
146 | else | ||
147 | goto found_device; | ||
137 | } | 148 | } |
138 | } | 149 | } |
139 | pos=end2; | 150 | pos = end2; |
140 | } | 151 | } |
141 | end_line: | 152 | end_line: |
142 | /* Did everything parse happily? */ | 153 | /* Did everything parse happily? */ |
143 | if (field && field!=3) | 154 | if (field && field!=3) |
144 | bb_error_msg_and_die("Bad line %d",line); | 155 | bb_error_msg_and_die("Bad line %d",line); |
145 | 156 | ||
146 | /* Next line */ | 157 | /* Next line */ |
147 | pos=++end; | 158 | pos = ++end; |
148 | } | 159 | } |
149 | found_device: | 160 | found_device: |
150 | munmap(conf,len); | 161 | munmap(conf, len); |
151 | } | 162 | } |
152 | close(fd); | 163 | close(fd); |
153 | } | 164 | } |
@@ -170,7 +181,7 @@ end: | |||
170 | static void find_dev(char *path) | 181 | static void find_dev(char *path) |
171 | { | 182 | { |
172 | DIR *dir; | 183 | DIR *dir; |
173 | size_t len=strlen(path); | 184 | size_t len = strlen(path); |
174 | struct dirent *entry; | 185 | struct dirent *entry; |
175 | 186 | ||
176 | if ((dir = opendir(path)) == NULL) | 187 | if ((dir = opendir(path)) == NULL) |
@@ -180,7 +191,8 @@ static void find_dev(char *path) | |||
180 | 191 | ||
181 | /* Skip "." and ".." (also skips hidden files, which is ok) */ | 192 | /* Skip "." and ".." (also skips hidden files, which is ok) */ |
182 | 193 | ||
183 | if (entry->d_name[0]=='.') continue; | 194 | if (entry->d_name[0] == '.') |
195 | continue; | ||
184 | 196 | ||
185 | if (entry->d_type == DT_DIR) { | 197 | if (entry->d_type == DT_DIR) { |
186 | snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); | 198 | snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); |
@@ -214,7 +226,8 @@ int mdev_main(int argc, char *argv[]) | |||
214 | } else { | 226 | } else { |
215 | action = getenv("ACTION"); | 227 | action = getenv("ACTION"); |
216 | env_path = getenv("DEVPATH"); | 228 | env_path = getenv("DEVPATH"); |
217 | if (!action || !env_path) bb_show_usage(); | 229 | if (!action || !env_path) |
230 | bb_show_usage(); | ||
218 | 231 | ||
219 | if (!strcmp(action, "add")) { | 232 | if (!strcmp(action, "add")) { |
220 | sprintf(temp, "/sys%s", env_path); | 233 | sprintf(temp, "/sys%s", env_path); |
@@ -225,6 +238,6 @@ int mdev_main(int argc, char *argv[]) | |||
225 | } | 238 | } |
226 | } | 239 | } |
227 | 240 | ||
228 | if(ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); | 241 | if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); |
229 | return 0; | 242 | return 0; |
230 | } | 243 | } |