From 1076cca6b5dfee4ff97b4e6bdce8d28ae64a00c5 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sun, 31 Dec 2017 22:21:47 +0000 Subject: [PATCH] mntent: Continue work - Fix bugs in draft code - Introduce getmntent_r With these we have the calls needed to do the right thing. --- Library/include/mntent.h | 6 ++++-- Library/libs/mntent.c | 41 +++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Library/include/mntent.h b/Library/include/mntent.h index 62d27603..7bd0b583 100644 --- a/Library/include/mntent.h +++ b/Library/include/mntent.h @@ -19,7 +19,9 @@ extern int addmntent(FILE *fp, struct mntent *mnt); extern int endmntent(FILE *fp); extern char *hasmntopt(struct mntent *mnt, char *opt); -/* Extended function found in some Unixen */ -extern int delmntent(FILE *fp, struct mntent *mnt); +/* GNUism but useful and we provide it */ +extern struct mntent *getmntent_r(FILE *fp, struct mntent *mnt, char *buf, int len); +/* Extended function found in some Unixen. Not yet supported */ +/*extern int delmntent(FILE *fp, struct mntent *mnt); */ #endif diff --git a/Library/libs/mntent.c b/Library/libs/mntent.c index e1af5d74..208b4d41 100644 --- a/Library/libs/mntent.c +++ b/Library/libs/mntent.c @@ -24,9 +24,9 @@ FILE *setmntent(char *filep, char *type) if (fp == NULL) return NULL; if (strchr(type,'w') || strchr(type,'a')) - lock = LOCK_SH; - else lock = LOCK_EX; + else + lock = LOCK_SH; if (flock(fileno(fp), lock) == -1) { fclose(fp); return NULL; @@ -44,6 +44,7 @@ static int isoct(char c) static int mntparse(char *p, char **t, char *def) { char *d = strtok(p, " \t\n"); + char *ds = d; if (d == NULL) d = def; else { @@ -58,28 +59,33 @@ static int mntparse(char *p, char **t, char *def) } } if (t) - *t = d; - return atoi(d); + *t = ds; + return atoi(ds); } -struct mntent *getmntent(FILE * fp) +struct mntent *getmntent_r(FILE * fp, struct mntent *me, char *buf, int len) { - static struct mntent me; - char *p = mntbuf; + char *p = buf; /* Skip blank lines and comments */ do { - if (fgets(mntbuf, _MAX_MNTLEN, fp) == NULL) + if (fgets(buf, len, fp) == NULL) return NULL; } while(*p == '\n' || *p == '#'); - mntparse(mntbuf, &me.mnt_fsname, NULL); - mntparse(NULL, &me.mnt_dir, NULL); - mntparse(NULL, &me.mnt_type, "fuzix"); - mntparse(NULL, &me.mnt_opts, ""); - me.mnt_freq = mntparse(NULL, NULL, "0"); - me.mnt_passno = mntparse(NULL, NULL, "0"); - if (me.mnt_fsname == NULL || me.mnt_dir == NULL) + mntparse(buf, &me->mnt_fsname, NULL); + mntparse(NULL, &me->mnt_dir, NULL); + mntparse(NULL, &me->mnt_type, "fuzix"); + mntparse(NULL, &me->mnt_opts, ""); + me->mnt_freq = mntparse(NULL, NULL, "0"); + me->mnt_passno = mntparse(NULL, NULL, "0"); + if (me->mnt_fsname == NULL || me->mnt_dir == NULL) return NULL; - return &me; + return me; +} + +struct mntent *getmntent(FILE * fp) +{ + static struct mntent me; + return getmntent_r(fp, &me, mntbuf, _MAX_MNTLEN); } void quote_out(char **p, char *s) @@ -89,6 +95,7 @@ void quote_out(char **p, char *s) return; while (t < mntbuf + _MAX_MNTLEN - 1) { if (*s == 0) { + *t++ = ' '; *p = t; return; } @@ -122,7 +129,7 @@ int addmntent(FILE * fp, struct mntent *mnt) quote_out_int(&p, mnt->mnt_freq); quote_out_int(&p, mnt->mnt_passno); if (p) { - *p = '\n'; + p[-1] = '\n'; if (fwrite(mntbuf, p - mntbuf, 1, fp) == 1) return 0; } -- 2.34.1