X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Flinux%2Fmntent.c;h=f151625285c36e6e92dcd2667c18beb3430a6478;hb=9cee9307e3b8ed31e772d564d1dc53e6d888acba;hp=e3735666b2abca67db131fe4c198bcb85cf77d0f;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01;p=musl diff --git a/src/linux/mntent.c b/src/linux/mntent.c index e3735666..f1516252 100644 --- a/src/linux/mntent.c +++ b/src/linux/mntent.c @@ -1,6 +1,7 @@ #include #include #include +#include FILE *setmntent(const char *name, const char *mode) { @@ -13,39 +14,50 @@ int endmntent(FILE *f) return 1; } -struct mntent *getmntent(FILE *f) +struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen) { - static char linebuf[256]; - static struct mntent mnt; int cnt, n[8]; - mnt.mnt_freq = 0; - mnt.mnt_passno = 0; + mnt->mnt_freq = 0; + mnt->mnt_passno = 0; do { - fgets(linebuf, sizeof linebuf, f); - if (feof(f)) return NULL; + fgets(linebuf, buflen, f); + if (feof(f) || ferror(f)) return 0; + if (!strchr(linebuf, '\n')) { + if (fseeko(f, -(off_t)strlen(linebuf), SEEK_CUR)) + fscanf(f, "%*[^\n]%*[\n]"); + errno = ERANGE; + return 0; + } cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d", n, n+1, n+2, n+3, n+4, n+5, n+6, n+7, - &mnt.mnt_freq, &mnt.mnt_passno); - } while (cnt >= 8 && linebuf[n[0]] != '#'); + &mnt->mnt_freq, &mnt->mnt_passno); + } while (cnt < 2 || linebuf[n[0]] == '#'); linebuf[n[1]] = 0; linebuf[n[3]] = 0; linebuf[n[5]] = 0; linebuf[n[7]] = 0; - mnt.mnt_fsname = linebuf+n[0]; - mnt.mnt_dir = linebuf+n[2]; - mnt.mnt_type = linebuf+n[4]; - mnt.mnt_opts = linebuf+n[6]; + mnt->mnt_fsname = linebuf+n[0]; + mnt->mnt_dir = linebuf+n[2]; + mnt->mnt_type = linebuf+n[4]; + mnt->mnt_opts = linebuf+n[6]; + + return mnt; +} - return &mnt; +struct mntent *getmntent(FILE *f) +{ + static char linebuf[256]; + static struct mntent mnt; + return getmntent_r(f, &mnt, linebuf, sizeof linebuf); } int addmntent(FILE *f, const struct mntent *mnt) { - fseek(f, 0, SEEK_END); + if (fseek(f, 0, SEEK_END)) return 1; return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n", mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 0;