summaryrefslogtreecommitdiff
path: root/misc/getmntent.c
blob: d2455a2661687e3d850f12076c0fdf3f8000f682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "miscfn.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __aix__
#define COMMENTCHAR '*'
#else
#define COMMENTCHAR '#'
#endif

#if HAVE_STRUCT_MNTTAB 
our_mntent * getmntent(FILE *filep) {
    static struct mnttab entry;
    static our_mntent item = { entry.mt_filsys };

    if (!fread(&entry, sizeof(entry), 1, filep)) return NULL;

    return &item;
}
#else 
our_mntent *getmntent(FILE *filep) {
    static our_mntent item = { NULL };
    char buf[1024], * start;
    char * chptr;

    if (item.our_mntdir) {
	free(item.our_mntdir);
    }
    
    while (fgets(buf, sizeof(buf) - 1, filep)) {
	/* chop off \n */
	buf[strlen(buf) - 1] = '\0';

	chptr = buf;
	while (isspace(*chptr)) chptr++;

	if (*chptr == COMMENTCHAR) continue;

	#if __aix__
	    /* aix uses a screwed up file format */
	    if (*chptr == '/') {
		start = chptr;
		while (*chptr != ':') chptr++;
		*chptr = '\0';
		item.mnt_dir = strdup(start);
		return &item;
	    }
	#else 
	    while (!isspace(*chptr) && (*chptr)) chptr++;
	    if (!*chptr) return NULL;

	    while (isspace(*chptr) && (*chptr)) chptr++;
	    if (!*chptr) return NULL;
	    start = chptr;
	
	    while (!isspace(*chptr) && (*chptr)) chptr++;
	    *chptr = '\0';

	    item.our_mntdir = strdup(start);
	    return &item;
	#endif
    }

    return NULL;
}
#endif