File: file2.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (67 lines) | stat: -rw-r--r-- 1,719 bytes parent folder | download
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
#include <stdio.h>
#define __USE_GNU
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

int main()
{
    long int count = 0;
    int fd;
    int rc;
    FILE *fp;
    char filename[100];
    char dir1[100];
    char dir2[100];

    char *dir = getenv("DMTCP_TMPDIR");
    if (!dir) dir = getenv("TMPDIR");
    if (!dir) dir = "/tmp";
    if (sizeof(filename) < strlen(dir) + 3*sizeof("/dmtcp_file_XXXXXX")) {
      printf("Directory string too large.\n");
      return 1;
    }
    strcpy(filename, dir);
    strcat(filename, "/dmtcp_dir1_XXXXXX");
    strcpy(dir1, filename);
    strcat(filename, "/dmtcp_dir2_XXXXXX");
    strcpy(dir2, filename);
    strcat(filename, "/dmtcp_file_XXXXXX");

    // Create dir1, dir2, and filename
    if (mkdtemp(dir1) == NULL) abort();
    memcpy(dir2, dir1, strlen(dir1));      // Update new prefix
    if (mkdtemp(dir2) == NULL) abort();
    memcpy(filename, dir2, strlen(dir2));  // Update prefix; dest: char[]
    fd = mkstemp(filename);
    if (fd == -1)
      abort();

    // Problematic only when in "w" mode or "a". All + modes and "r" are fine.
    fp = fdopen(fd, "w");

    fprintf(stdout, "Opened %s\n", filename);
    fprintf(stdout, "Deleting %s\n", filename);
    rc = unlink(filename);
    if (rc == -1) abort();
    fprintf(stdout, "Deleting %s\n", dir2);
    rc = rmdir(dir2);
    if (rc == -1) abort();
    fprintf(stdout, "Deleting %s\n", dir1);
    rc = rmdir(dir1);
    if (rc == -1) abort();

    while (1) {
      if (count % (int)1e6 == 0) {
        printf("%ld ", count);
        fflush(stdout);
      }
      fprintf(fp, "%ld", count++);
    }

    return 0;
}