mailing list of musl libc
 help / color / mirror / code / Atom feed
1c129579dad77c7bd04685d61d50792a9c602575 blob 3465 bytes (raw)

  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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <mntent.h>
#include <errno.h>
#include <limits.h>
#include "stdio_impl.h"

static char *internal_buf;
static size_t internal_bufsize;

#define SENTINEL (char *)&internal_buf

FILE *setmntent(const char *name, const char *mode)
{
	return fopen(name, mode);
}

int endmntent(FILE *f)
{
	if (f) fclose(f);
	return 1;
}

static char* decode(char* buf) {
	assert(buf != NULL);
	char* src = buf;
	char* dest = buf;
	while (1) {
		char* next_src = __strchrnul(src, '\\');
		int offset = next_src - src;
		memmove(dest, src, offset);
		src = next_src;
		dest += offset;

		if(*src == '\0') {
			*dest = *src;
			return buf;
		}
		assert (*src == '\\');
		src++;

		const char *replacements =
			"\040"	"040"	"\0"  // space
			"\011"	"011"	"\0"  // tab
			"\012"	"012"	"\0"  // newline
			"\134"	"134"	"\0"  // backslash
			"\\"	"\\"	"\0"
			// Fallback for unrecognized escape sequence,
			// copy literally:
			"\\"	"";
		while(1) {
			char c = *replacements++;
			size_t n = strlen(replacements);
			if (strncmp(src, replacements, n) == 0) {
				*dest++ = c;
				src += n;
				break;
			}
			replacements += n+1;
        }
	}
}

struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
{
	int n[8], use_internal = (linebuf == SENTINEL);
	size_t len, i;

	mnt->mnt_freq = 0;
	mnt->mnt_passno = 0;

	do {
		if (use_internal) {
			getline(&internal_buf, &internal_bufsize, f);
			linebuf = internal_buf;
		} else {
			fgets(linebuf, buflen, f);
		}
		if (feof(f) || ferror(f)) return 0;
		if (!strchr(linebuf, '\n')) {
			fscanf(f, "%*[^\n]%*[\n]");
			errno = ERANGE;
			return 0;
		}

		len = strlen(linebuf);
		if (len > INT_MAX) continue;
		for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
		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 (linebuf[n[0]] == '#' || n[1]==len);

	linebuf[n[1]] = 0;
	linebuf[n[3]] = 0;
	linebuf[n[5]] = 0;
	linebuf[n[7]] = 0;

	mnt->mnt_fsname = decode(linebuf+n[0]);
	mnt->mnt_dir = decode(linebuf+n[2]);
	mnt->mnt_type = decode(linebuf+n[4]);
	mnt->mnt_opts = decode(linebuf+n[6]);

	return mnt;
}

struct mntent *getmntent(FILE *f)
{
	static struct mntent mnt;
	return getmntent_r(f, &mnt, SENTINEL, 0);
}

static int escape_and_write_string(FILE *f, const char* str)
{
	char c;
	int error_occured = 0;
	while(str && !error_occured && (c = *str++) != 0) {
		if(c == '\040') // space
			error_occured = fputs("\\040", f) < 0;
		else if (c == '\011') // tab
			error_occured = fputs("\\011", f) < 0;
		else if (c == '\012') // newline
			error_occured = fputs("\\012", f) < 0;
		else if (c == '\\')
			error_occured = fputs("\\\\", f) < 0;
		else
			error_occured = putc_unlocked(c, f) < 0;
	}
	return error_occured;
}

int addmntent(FILE *f, const struct mntent *mnt)
{
	if (fseek(f, 0, SEEK_END)) return 1;
	FLOCK(f);
	int error_occured =
		escape_and_write_string(f, mnt->mnt_fsname)
		|| (0 > putc_unlocked('\t', f))
		|| escape_and_write_string(f, mnt->mnt_dir)
		|| (0 > putc_unlocked('\t', f))
		|| escape_and_write_string(f, mnt->mnt_type)
		|| (0 > putc_unlocked('\t', f))
		|| escape_and_write_string(f, mnt->mnt_opts)
		|| (0 > fprintf(f, "\t%d\t%d\n",
			mnt->mnt_freq, mnt->mnt_passno));
	FUNLOCK(f);
	return error_occured;
}

char *hasmntopt(const struct mntent *mnt, const char *opt)
{
	return strstr(mnt->mnt_opts, opt);
}
debug log:

solving 1c129579 ...
found 1c129579 in https://inbox.vuxu.org/musl/20230330092352.1186349-1-matthias.goergens@gmail.com/
found d404fbe3 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 d404fbe3929b0aa0b68c1a44046bfa5202069d68	src/misc/mntent.c

applying [1/1] https://inbox.vuxu.org/musl/20230330092352.1186349-1-matthias.goergens@gmail.com/
diff --git a/src/misc/mntent.c b/src/misc/mntent.c
index d404fbe3..1c129579 100644

Checking patch src/misc/mntent.c...
Applied patch src/misc/mntent.c cleanly.

index at:
100644 1c129579dad77c7bd04685d61d50792a9c602575	src/misc/mntent.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).