File: fmemopen.c

package info (click to toggle)
libsolv 0.7.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,804 kB
  • sloc: ansic: 73,124; python: 871; perl: 742; tcl: 730; ruby: 705; sh: 263; cpp: 204; makefile: 42
file content (41 lines) | stat: -rw-r--r-- 952 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>

FILE *
fmemopen(void *buf, size_t size, const char *mode)
{
  char temppath[MAX_PATH + 1];
  char tempnam[MAX_PATH + 1];
  DWORD l;
  HANDLE fh;
  FILE *fp;

  if (strcmp(mode, "r") != 0 && strcmp(mode, "r+") != 0)
    return 0;
  l = GetTempPath(MAX_PATH, temppath);
  if (!l || l >= MAX_PATH)
    return 0;
  if (!GetTempFileName(temppath, "solvtmp", 0, tempnam))
    return 0;
  fh = CreateFile(tempnam, DELETE | GENERIC_READ | GENERIC_WRITE, 0,
                  NULL, CREATE_ALWAYS,
                  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
                  NULL);
  if (fh == INVALID_HANDLE_VALUE)
    return 0;
  fp = _fdopen(_open_osfhandle((intptr_t)fh, 0), "w+b");
  if (!fp)
    {
      CloseHandle(fh);
      return 0;
    }
  if (buf && size && fwrite(buf, size, 1, fp) != 1)
    {
      fclose(fp);
      return 0;
    }
  rewind(fp);
  return fp;
}