File: tst-rndseek.c

package info (click to toggle)
glibc 2.24-11+deb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 225,852 kB
  • sloc: ansic: 996,505; asm: 261,827; sh: 10,484; makefile: 9,856; cpp: 4,169; python: 3,971; perl: 2,254; awk: 1,753; pascal: 1,521; yacc: 291; sed: 80
file content (144 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download | duplicates (24)
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


static char fname[] = "/tmp/rndseek.XXXXXX";
static char tempdata[65 * 1024];


static int do_test (void);
#define TEST_FUNCTION do_test ()
#define TIMEOUT 10

#include "../test-skeleton.c"


static int
fp_test (const char *name, FILE *fp)
{
  int result = 0;
  int rounds = 10000;

  do
    {
      int idx = random () % (sizeof (tempdata) - 2);
      char ch1;
      char ch2;

      if (fseek (fp, idx, SEEK_SET) != 0)
	{
	  printf ("%s: %d: fseek failed: %m\n", name, rounds);
	  result = 1;
	  break;
	}

      ch1 = fgetc_unlocked (fp);
      ch2 = tempdata[idx];
      if (ch1 != ch2)
	{
	  printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
		  name, rounds, idx, ch1, ch2);
	  result = 1;
	  break;
	}

      ch1 = fgetc (fp);
      ch2 = tempdata[idx + 1];
      if (ch1 != ch2)
	{
	  printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
		  name, rounds, idx + 1, ch1, ch2);
	  result = 1;
	  break;
	}
    }
  while (--rounds > 0);

  fclose (fp);

  return result;
}


static int
do_test (void)
{
  int fd;
  FILE *fp;
  size_t i;
  int result;

  fd = mkstemp (fname);
  if (fd == -1)
    {
      printf ("cannot open temporary file: %m\n");
      return 1;
    }
  /* Make sure the file gets removed.  */
  add_temp_file (fname);

  /* Repeatability demands this.  */
  srandom (42);

  /* First create some temporary data.  */
  for (i = 0; i < sizeof (tempdata); ++i)
    tempdata[i] = 'a' + random () % 26;

  /* Write this data to a file.  */
  if (TEMP_FAILURE_RETRY (write (fd, tempdata, sizeof (tempdata)))
      != sizeof (tempdata))
    {
      printf ("cannot wrote data to temporary file: %m\n");
      return 1;
    }

  /* Now try reading the data.  */
  fp = fdopen (dup (fd), "r");
  if (fp == NULL)
    {
      printf ("cannot duplicate temporary file descriptor: %m\n");
      return 1;
    }

  rewind (fp);
  for (i = 0; i < sizeof (tempdata); ++i)
    {
      int ch0 = fgetc (fp);
      char ch1 = ch0;
      char ch2 = tempdata[i];

      if (ch0 == EOF)
	{
	  puts ("premature end of file while reading data");
	  return 1;
	}

      if (ch1 != ch2)
	{
	  printf ("%zd: '%c' vs '%c'\n", i, ch1, ch2);
	  return 1;
	}
    }

  result = fp_test ("fdopen(\"r\")", fp);

  fp = fopen (fname, "r");
  result |= fp_test ("fopen(\"r\")", fp);

  fp = fopen64 (fname, "r");
  result |= fp_test ("fopen64(\"r\")", fp);

  /* The "rw" mode will prevent the mmap-using code from being used.  */
  fp = fdopen (fd, "rw");
  result = fp_test ("fdopen(\"rw\")", fp);

  fp = fopen (fname, "rw");
  result |= fp_test ("fopen(\"rw\")", fp);

  fp = fopen64 (fname, "rw");
  result |= fp_test ("fopen64(\"rw\")", fp);

  return result;
}