Use mkstemp rather than mktemp to prevent a small race.

Obtained from: OpenBSD
This commit is contained in:
Warner Losh 1997-03-24 05:44:28 +00:00
parent 7d951713e8
commit 722ceb3f84
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=24180
2 changed files with 22 additions and 22 deletions

View File

@ -800,36 +800,36 @@ edit(lp, f)
struct disklabel *lp; struct disklabel *lp;
int f; int f;
{ {
register int c; register int c, fd;
struct disklabel label; struct disklabel label;
FILE *fd; FILE *fp;
(void) mktemp(tmpfil); if ((fd = mkstemp(tmpfil)) == -1 ||
fd = fopen(tmpfil, "w"); (fp = fdopen(fd, "w")) == NULL) {
if (fd == NULL) {
fprintf(stderr, "%s: Can't create\n", tmpfil); fprintf(stderr, "%s: Can't create\n", tmpfil);
return (1); return (1);
} }
(void)fchmod(fileno(fd), 0600); display(fp, lp);
display(fd, lp); fclose(fp);
fclose(fd);
for (;;) { for (;;) {
if (!editit()) if (!editit())
break; break;
fd = fopen(tmpfil, "r"); fp = fopen(tmpfil, "r");
if (fd == NULL) { if (fp == NULL) {
fprintf(stderr, "%s: Can't reopen for reading\n", fprintf(stderr, "%s: Can't reopen for reading\n",
tmpfil); tmpfil);
break; break;
} }
bzero((char *)&label, sizeof(label)); bzero((char *)&label, sizeof(label));
if (getasciilabel(fd, &label)) { if (getasciilabel(fp, &label)) {
*lp = label; *lp = label;
if (writelabel(f, bootarea, lp) == 0) { if (writelabel(f, bootarea, lp) == 0) {
fclose(fp);
(void) unlink(tmpfil); (void) unlink(tmpfil);
return (0); return (0);
} }
} }
fclose(fp);
printf("re-edit the label? [y]: "); fflush(stdout); printf("re-edit the label? [y]: "); fflush(stdout);
c = getchar(); c = getchar();
if (c != EOF && c != (int)'\n') if (c != EOF && c != (int)'\n')

View File

@ -800,36 +800,36 @@ edit(lp, f)
struct disklabel *lp; struct disklabel *lp;
int f; int f;
{ {
register int c; register int c, fd;
struct disklabel label; struct disklabel label;
FILE *fd; FILE *fp;
(void) mktemp(tmpfil); if ((fd = mkstemp(tmpfil)) == -1 ||
fd = fopen(tmpfil, "w"); (fp = fdopen(fd, "w")) == NULL) {
if (fd == NULL) {
fprintf(stderr, "%s: Can't create\n", tmpfil); fprintf(stderr, "%s: Can't create\n", tmpfil);
return (1); return (1);
} }
(void)fchmod(fileno(fd), 0600); display(fp, lp);
display(fd, lp); fclose(fp);
fclose(fd);
for (;;) { for (;;) {
if (!editit()) if (!editit())
break; break;
fd = fopen(tmpfil, "r"); fp = fopen(tmpfil, "r");
if (fd == NULL) { if (fp == NULL) {
fprintf(stderr, "%s: Can't reopen for reading\n", fprintf(stderr, "%s: Can't reopen for reading\n",
tmpfil); tmpfil);
break; break;
} }
bzero((char *)&label, sizeof(label)); bzero((char *)&label, sizeof(label));
if (getasciilabel(fd, &label)) { if (getasciilabel(fp, &label)) {
*lp = label; *lp = label;
if (writelabel(f, bootarea, lp) == 0) { if (writelabel(f, bootarea, lp) == 0) {
fclose(fp);
(void) unlink(tmpfil); (void) unlink(tmpfil);
return (0); return (0);
} }
} }
fclose(fp);
printf("re-edit the label? [y]: "); fflush(stdout); printf("re-edit the label? [y]: "); fflush(stdout);
c = getchar(); c = getchar();
if (c != EOF && c != (int)'\n') if (c != EOF && c != (int)'\n')