File: tempfile.c

package info (click to toggle)
libite 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,188 kB
  • sloc: sh: 4,665; ansic: 4,165; makefile: 141
file content (39 lines) | stat: -rw-r--r-- 889 bytes parent folder | download | duplicates (2)
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
#include <fcntl.h>		/* O_TMPFILE requires -D_GNU_SOURCE */
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include "check.h"

#ifndef  O_TMPFILE
#warning O_TMPFILE is missing, either too old GLIBC, Linux, or non-Linux system
#endif

#define READBACK "Reality is frequently inaccurate.\n"


int main(void)
{
	char buf[80] = "";
	FILE *fp;

	printf("Before tempfile():\n");
	system("ls -lrt " _PATH_TMP " | tail -3");
	fflush(stdout);

	fp = tempfile();
	if (fp == NULL)
		fprintf(stderr, "Failed tempfile(), errno %d: %s", errno, strerror(errno));
	fail_unless(fp != NULL);

	fputs(READBACK, fp);
	rewind(fp);
	if (fgets(buf, sizeof(buf), fp))
		fprintf(stderr, "\nRead-back from tempfile: %s", buf);
	fail_unless(!strcmp(buf, READBACK));

	printf("\nAfter tempfile(), should be same list:\n");
	system("ls -lrt " _PATH_TMP " | tail -3");
	fflush(stdout);

	return fclose(fp);
}