1 | /* Copyright (c) 2016 Phoenix Systems |
---|
2 | All rights reserved. |
---|
3 | |
---|
4 | Redistribution and use in source and binary forms, with or without |
---|
5 | modification, are permitted provided that the following conditions |
---|
6 | are met: |
---|
7 | 1. Redistributions of source code must retain the above copyright |
---|
8 | notice, this list of conditions and the following disclaimer. |
---|
9 | 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | notice, this list of conditions and the following disclaimer in the |
---|
11 | documentation and/or other materials provided with the distribution. |
---|
12 | |
---|
13 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
16 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
17 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
18 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
19 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
20 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
21 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
22 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
23 | SUCH DAMAGE.*/ |
---|
24 | |
---|
25 | #include <errno.h> |
---|
26 | #include <mntent.h> |
---|
27 | #include <malloc.h> |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #define COLUMN_FS_NAME 0 |
---|
33 | #define COLUMN_MOUNTPOINT 1 |
---|
34 | #define COLUMN_FS_TYPE 2 |
---|
35 | #define COLUMN_OPTS 3 |
---|
36 | #define COLUMN_DUMP_FREQ 4 |
---|
37 | #define COLUMN_PASSNO 5 |
---|
38 | |
---|
39 | #define BUFFER_SIZE 4096 |
---|
40 | |
---|
41 | struct mntent *getmntent_r(FILE *fp, struct mntent *result, char *buffer, int bufsize) |
---|
42 | { |
---|
43 | char *token, *buff_ptr; |
---|
44 | char *line = NULL; |
---|
45 | size_t size = 0; |
---|
46 | int i; |
---|
47 | |
---|
48 | buff_ptr = buffer; |
---|
49 | |
---|
50 | if (getline(&line, &size, fp) == -1 || feof(fp)) { |
---|
51 | free(line); |
---|
52 | return NULL; |
---|
53 | } |
---|
54 | |
---|
55 | for (token = strtok(line, " "), i = 0; token != NULL; token = strtok(NULL, " "), ++i) { |
---|
56 | if (((buff_ptr + strlen(token) + 1) - buffer) > bufsize) { |
---|
57 | errno = ENOBUFS; |
---|
58 | free(line); |
---|
59 | return NULL; |
---|
60 | } |
---|
61 | |
---|
62 | switch (i) { |
---|
63 | case COLUMN_FS_NAME: |
---|
64 | result->mnt_fsname = strcpy(buff_ptr, token); |
---|
65 | break; |
---|
66 | case COLUMN_MOUNTPOINT: |
---|
67 | result->mnt_dir = strcpy(buff_ptr, token); |
---|
68 | break; |
---|
69 | case COLUMN_FS_TYPE: |
---|
70 | result->mnt_type = strcpy(buff_ptr, token); |
---|
71 | break; |
---|
72 | case COLUMN_OPTS: |
---|
73 | result->mnt_opts = strcpy(buff_ptr, token); |
---|
74 | break; |
---|
75 | case COLUMN_DUMP_FREQ: |
---|
76 | result->mnt_freq = atoi(token); |
---|
77 | break; |
---|
78 | case COLUMN_PASSNO: |
---|
79 | result->mnt_passno = atoi(token); |
---|
80 | break; |
---|
81 | default: |
---|
82 | free(line); |
---|
83 | return NULL; |
---|
84 | } |
---|
85 | |
---|
86 | buff_ptr += strlen(token) + 1; |
---|
87 | } |
---|
88 | |
---|
89 | free(line); |
---|
90 | return result; |
---|
91 | } |
---|
92 | |
---|
93 | struct mntent *getmntent(FILE *fp) |
---|
94 | { |
---|
95 | static struct mntent result; |
---|
96 | static char buffer[BUFFER_SIZE]; |
---|
97 | memset(&result, 0, sizeof(struct mntent)); |
---|
98 | memset(buffer, 0, sizeof(buffer)); |
---|
99 | |
---|
100 | return getmntent_r(fp, &result, buffer, sizeof(buffer)); |
---|
101 | } |
---|