aboutsummaryrefslogtreecommitdiff
path: root/util-linux/mdev.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-16 19:39:37 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-16 19:39:37 +0000
commitf46be091060ad48b124a99d4a996405e85c63a33 (patch)
tree5cf50dd087f158960b79730e4412505a303b002c /util-linux/mdev.c
parent4e33e07f6096f9fa8ebc04a29cee5c0b28ea13a6 (diff)
downloadbusybox-w32-f46be091060ad48b124a99d4a996405e85c63a33.tar.gz
busybox-w32-f46be091060ad48b124a99d4a996405e85c63a33.tar.bz2
busybox-w32-f46be091060ad48b124a99d4a996405e85c63a33.zip
mdev: reduce excessive indentation
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r--util-linux/mdev.c217
1 files changed, 112 insertions, 105 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index 1df144fa9..c03dd6130 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -25,7 +25,7 @@ struct mdev_globals
25static void make_device(char *path, int delete) 25static void make_device(char *path, int delete)
26{ 26{
27 char *device_name; 27 char *device_name;
28 int major, minor, type, len, fd; 28 int major, minor, type, len;
29 int mode = 0660; 29 int mode = 0660;
30 uid_t uid = 0; 30 uid_t uid = 0;
31 gid_t gid = 0; 31 gid_t gid = 0;
@@ -53,119 +53,125 @@ static void make_device(char *path, int delete)
53 53
54 if (ENABLE_FEATURE_MDEV_CONF) { 54 if (ENABLE_FEATURE_MDEV_CONF) {
55 char *conf, *pos, *end; 55 char *conf, *pos, *end;
56 int line, fd;
56 57
57 /* mmap the config file */ 58 /* mmap the config file */
58 if (-1 != (fd=open("/etc/mdev.conf",O_RDONLY))) { 59 fd = open("/etc/mdev.conf", O_RDONLY);
59 len = lseek(fd, 0, SEEK_END); 60 if (fd < 0)
60 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); 61 goto end_parse;
61 if (conf) { 62 len = xlseek(fd, 0, SEEK_END);
62 int line = 0; 63 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
63 64 close(fd);
64 /* Loop through lines in mmaped file*/ 65 if (!conf)
65 for (pos=conf; pos-conf<len;) { 66 goto end_parse;
66 int field; 67
67 char *end2; 68 line = 0;
68 69 /* Loop through lines in mmaped file*/
69 line++; 70 for (pos=conf; pos-conf<len;) {
70 /* find end of this line */ 71 int field;
71 for(end=pos; end-conf<len && *end!='\n'; end++) 72 char *end2;
73
74 line++;
75 /* find end of this line */
76 for(end=pos; end-conf<len && *end!='\n'; end++)
77 ;
78
79 /* Three fields: regex, uid:gid, mode */
80 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
81 field++)
82 {
83 /* Skip whitespace */
84 while (pos<end && isspace(*pos)) pos++;
85 if (pos==end || *pos=='#') break;
86 for (end2=pos;
87 end2<end && !isspace(*end2) && *end2!='#'; end2++)
88 ;
89
90 if (field == 0) {
91 /* Regex to match this device */
92
93 char *regex = strndupa(pos, end2-pos);
94 regex_t match;
95 regmatch_t off;
96 int result;
97
98 /* Is this it? */
99 xregcomp(&match,regex, REG_EXTENDED);
100 result = regexec(&match, device_name, 1, &off, 0);
101 regfree(&match);
102
103 /* If not this device, skip rest of line */
104 if (result || off.rm_so
105 || off.rm_eo != strlen(device_name))
106 break;
107 }
108 if (field == 1) {
109 /* uid:gid */
110
111 char *s, *s2;
112
113 /* Find : */
114 for(s=pos; s<end2 && *s!=':'; s++)
72 ; 115 ;
116 if (s == end2) break;
117
118 /* Parse UID */
119 uid = strtoul(pos, &s2, 10);
120 if (s != s2) {
121 struct passwd *pass;
122 pass = getpwnam(strndupa(pos, s-pos));
123 if (!pass) break;
124 uid = pass->pw_uid;
125 }
126 s++;
127 /* parse GID */
128 gid = strtoul(s, &s2, 10);
129 if (end2 != s2) {
130 struct group *grp;
131 grp = getgrnam(strndupa(s, end2-s));
132 if (!grp) break;
133 gid = grp->gr_gid;
134 }
135 }
136 if (field == 2) {
137 /* mode */
73 138
74 /* Three fields: regex, uid:gid, mode */ 139 mode = strtoul(pos, &pos, 8);
75 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); 140 if (pos != end2) break;
76 field++) 141 }
77 { 142 if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
78 /* Skip whitespace */ 143 // Command to run
79 while (pos<end && isspace(*pos)) pos++; 144 char *s = "@$*", *s2;
80 if (pos==end || *pos=='#') break; 145 s2 = strchr(s, *pos++);
81 for (end2=pos; 146 if (!s2) {
82 end2<end && !isspace(*end2) && *end2!='#'; end2++) 147 // Force error
83 ; 148 field = 1;
84 149 break;
85 if (!field) {
86 /* Regex to match this device */
87
88 char *regex = strndupa(pos, end2-pos);
89 regex_t match;
90 regmatch_t off;
91 int result;
92
93 /* Is this it? */
94 xregcomp(&match,regex, REG_EXTENDED);
95 result = regexec(&match, device_name, 1, &off, 0);
96 regfree(&match);
97
98 /* If not this device, skip rest of line */
99 if (result || off.rm_so
100 || off.rm_eo != strlen(device_name))
101 break;
102
103 } else if (field == 1) {
104 /* uid:gid */
105
106 char *s, *s2;
107
108 /* Find : */
109 for(s=pos; s<end2 && *s!=':'; s++)
110 ;
111 if (s == end2) break;
112
113 /* Parse UID */
114 uid = strtoul(pos,&s2,10);
115 if (s != s2) {
116 struct passwd *pass;
117 pass = getpwnam(strndupa(pos, s-pos));
118 if (!pass) break;
119 uid = pass->pw_uid;
120 }
121 s++;
122 /* parse GID */
123 gid = strtoul(s, &s2, 10);
124 if (end2 != s2) {
125 struct group *grp;
126 grp = getgrnam(strndupa(s, end2-s));
127 if (!grp) break;
128 gid = grp->gr_gid;
129 }
130 } else if (field == 2) {
131 /* mode */
132
133 mode = strtoul(pos, &pos, 8);
134 if (pos != end2) break;
135 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
136 // Command to run
137 char *s = "@$*", *s2;
138 if (!(s2 = strchr(s, *pos++))) {
139 // Force error
140 field = 1;
141 break;
142 }
143 if ((s2-s+1) & (1<<delete))
144 command = xstrndup(pos, end-pos);
145 }
146
147 pos = end2;
148 } 150 }
151 if ((s2-s+1) & (1<<delete))
152 command = xstrndup(pos, end-pos);
153 }
154
155 pos = end2;
156 }
149 157
150 /* Did everything parse happily? */ 158 /* Did everything parse happily? */
151 159
152 if (field > 2) break; 160 if (field > 2) break;
153 if (field) bb_error_msg_and_die("Bad line %d",line); 161 if (field) bb_error_msg_and_die("bad line %d",line);
154 162
155 /* Next line */ 163 /* Next line */
156 pos = ++end; 164 pos = ++end;
157 }
158 munmap(conf, len);
159 }
160 close(fd);
161 } 165 }
166 munmap(conf, len);
167 end_parse: /* nothing */ ;
162 } 168 }
163 169
164 umask(0); 170 umask(0);
165 if (!delete) { 171 if (!delete) {
166 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return; 172 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
167 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST) 173 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
168 bb_perror_msg_and_die("mknod %s failed", device_name); 174 bb_perror_msg_and_die("mknod %s", device_name);
169 175
170 if (major == bbg.root_major && minor == bbg.root_minor) 176 if (major == bbg.root_major && minor == bbg.root_minor)
171 symlink(device_name, "root"); 177 symlink(device_name, "root");
@@ -176,10 +182,10 @@ static void make_device(char *path, int delete)
176 int rc; 182 int rc;
177 char *s; 183 char *s;
178 184
179 s=xasprintf("MDEV=%s",device_name); 185 s = xasprintf("MDEV=%s", device_name);
180 putenv(s); 186 putenv(s);
181 rc = system(command); 187 rc = system(command);
182 s[4]=0; 188 s[4] = 0;
183 putenv(s); 189 putenv(s);
184 free(s); 190 free(s);
185 free(command); 191 free(command);
@@ -197,7 +203,8 @@ static void find_dev(char *path)
197 size_t len = strlen(path); 203 size_t len = strlen(path);
198 struct dirent *entry; 204 struct dirent *entry;
199 205
200 if ((dir = opendir(path)) == NULL) 206 dir = opendir(path);
207 if (dir == NULL)
201 return; 208 return;
202 209
203 while ((entry = readdir(dir)) != NULL) { 210 while ((entry = readdir(dir)) != NULL) {
@@ -235,9 +242,9 @@ int mdev_main(int argc, char *argv[])
235 if (argc == 2 && !strcmp(argv[1],"-s")) { 242 if (argc == 2 && !strcmp(argv[1],"-s")) {
236 struct stat st; 243 struct stat st;
237 244
238 stat("/", &st); // If this fails, we have bigger problems. 245 xstat("/", &st);
239 bbg.root_major=major(st.st_dev); 246 bbg.root_major = major(st.st_dev);
240 bbg.root_minor=minor(st.st_dev); 247 bbg.root_minor = minor(st.st_dev);
241 strcpy(temp,"/sys/block"); 248 strcpy(temp,"/sys/block");
242 find_dev(temp); 249 find_dev(temp);
243 strcpy(temp,"/sys/class"); 250 strcpy(temp,"/sys/class");