@@ -43,25 +43,35 @@ static void parser_cleanup(struct libmnt_parser *pa)
43
43
memset (pa , 0 , sizeof (* pa ));
44
44
}
45
45
46
- static const char * next_number (const char * s , int * num , int * rc )
46
+ static const char * next_s32 (const char * s , int * num , int * rc )
47
47
{
48
48
char * end = NULL ;
49
49
50
50
if (!s || !* s )
51
51
return s ;
52
52
53
- assert (num );
54
- assert (rc );
55
-
56
53
* rc = - EINVAL ;
57
54
* num = strtol (s , & end , 10 );
58
55
if (end == NULL || s == end )
59
56
return s ;
60
-
61
- /* valid end of number is a space or a terminator */
62
57
if (* end == ' ' || * end == '\t' || * end == '\0' )
63
58
* rc = 0 ;
59
+ return end ;
60
+ }
61
+
62
+ static const char * next_u64 (const char * s , uint64_t * num , int * rc )
63
+ {
64
+ char * end = NULL ;
64
65
66
+ if (!s || !* s )
67
+ return s ;
68
+
69
+ * rc = - EINVAL ;
70
+ * num = (uint64_t ) strtoumax (s , & end , 10 );
71
+ if (end == NULL || s == end )
72
+ return s ;
73
+ if (* end == ' ' || * end == '\t' || * end == '\0' )
74
+ * rc = 0 ;
65
75
return end ;
66
76
}
67
77
@@ -130,7 +140,7 @@ static int mnt_parse_table_line(struct libmnt_fs *fs, const char *s)
130
140
goto done ;
131
141
132
142
/* (5) freq (optional) */
133
- s = next_number (s , & fs -> freq , & rc );
143
+ s = next_s32 (s , & fs -> freq , & rc );
134
144
if (s && * s && rc ) {
135
145
DBG (TAB , ul_debug ("tab parse error: [freq]" ));
136
146
goto fail ;
@@ -141,7 +151,7 @@ static int mnt_parse_table_line(struct libmnt_fs *fs, const char *s)
141
151
goto done ;
142
152
143
153
/* (6) freq (optional) */
144
- s = next_number (s , & fs -> passno , & rc );
154
+ s = next_s32 (s , & fs -> passno , & rc );
145
155
if (s && * s && rc ) {
146
156
DBG (TAB , ul_debug ("tab parse error: [passno]" ));
147
157
goto fail ;
@@ -169,7 +179,7 @@ static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, const char *s)
169
179
fs -> flags |= MNT_FS_KERNEL ;
170
180
171
181
/* (1) id */
172
- s = next_number (s , & fs -> id , & rc );
182
+ s = next_s32 (s , & fs -> id , & rc );
173
183
if (!s || !* s || rc ) {
174
184
DBG (TAB , ul_debug ("tab parse error: [id]" ));
175
185
goto fail ;
@@ -178,7 +188,7 @@ static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, const char *s)
178
188
s = skip_separator (s );
179
189
180
190
/* (2) parent */
181
- s = next_number (s , & fs -> parent , & rc );
191
+ s = next_s32 (s , & fs -> parent , & rc );
182
192
if (!s || !* s || rc ) {
183
193
DBG (TAB , ul_debug ("tab parse error: [parent]" ));
184
194
goto fail ;
@@ -355,48 +365,66 @@ static int mnt_parse_utab_line(struct libmnt_fs *fs, const char *s)
355
365
*/
356
366
static int mnt_parse_swaps_line (struct libmnt_fs * fs , const char * s )
357
367
{
358
- uintmax_t fsz , usz ;
368
+ uint64_t num ;
359
369
int rc ;
360
- char * src = NULL ;
361
-
362
- rc = sscanf (s , UL_SCNsA " " /* (1) source */
363
- UL_SCNsA " " /* (2) type */
364
- "%ju" /* (3) size */
365
- "%ju" /* (4) used */
366
- "%d" , /* priority */
367
-
368
- & src ,
369
- & fs -> swaptype ,
370
- & fsz ,
371
- & usz ,
372
- & fs -> priority );
373
-
374
- if (rc == 5 ) {
375
- size_t sz ;
376
-
377
- fs -> size = fsz ;
378
- fs -> usedsize = usz ;
379
-
380
- /* remove "\040(deleted)" suffix */
381
- sz = strlen (src );
382
- if (sz > PATH_DELETED_SUFFIX_SZ ) {
383
- char * p = src + (sz - PATH_DELETED_SUFFIX_SZ );
384
- if (strcmp (p , PATH_DELETED_SUFFIX ) == 0 )
385
- * p = '\0' ;
386
- }
370
+ char * p ;
387
371
388
- unmangle_string (src );
372
+ /* (1) source */
373
+ p = unmangle (s , & s );
374
+ if (p ) {
375
+ char * x = (char * ) endswith (p , PATH_DELETED_SUFFIX );
376
+ if (x && * x )
377
+ * x = '\0' ;
378
+ }
379
+ if (!p || (rc = __mnt_fs_set_source_ptr (fs , p ))) {
380
+ DBG (TAB , ul_debug ("tab parse error: [source]" ));
381
+ goto fail ;
382
+ }
389
383
390
- rc = mnt_fs_set_source (fs , src );
391
- if (!rc )
392
- mnt_fs_set_fstype (fs , "swap" );
393
- } else {
394
- DBG (TAB , ul_debug ("tab parse error: [sscanf rc=%d]: '%s'" , rc , s ));
395
- rc = - EINVAL ;
384
+ s = skip_separator (s );
385
+
386
+ /* (2) type */
387
+ fs -> swaptype = unmangle (s , & s );
388
+ if (!fs -> swaptype ) {
389
+ DBG (TAB , ul_debug ("tab parse error: [swaptype]" ));
390
+ goto fail ;
391
+ }
392
+
393
+ s = skip_separator (s );
394
+
395
+ /* (3) size */
396
+ s = next_u64 (s , & num , & rc );
397
+ if (!s || !* s || rc ) {
398
+ DBG (TAB , ul_debug ("tab parse error: [size]" ));
399
+ goto fail ;
400
+ }
401
+ fs -> size = num ;
402
+
403
+ s = skip_separator (s );
404
+
405
+ /* (4) size */
406
+ s = next_u64 (s , & num , & rc );
407
+ if (!s || !* s || rc ) {
408
+ DBG (TAB , ul_debug ("tab parse error: [used size]" ));
409
+ goto fail ;
396
410
}
411
+ fs -> usedsize = num ;
397
412
398
- free ( src );
413
+ s = skip_separator ( s );
399
414
415
+ /* (5) priority */
416
+ s = next_s32 (s , & fs -> priority , & rc );
417
+ if (rc ) {
418
+ DBG (TAB , ul_debug ("tab parse error: [priority]" ));
419
+ goto fail ;
420
+ }
421
+
422
+ mnt_fs_set_fstype (fs , "swap" );
423
+ return 0 ;
424
+ fail :
425
+ if (rc == 0 )
426
+ rc = - EINVAL ;
427
+ DBG (TAB , ul_debug ("tab parse error on: '%s' [rc=%d]" , s , rc ));
400
428
return rc ;
401
429
}
402
430
0 commit comments