File: test-common.c

package info (click to toggle)
openslide 3.4.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,732 kB
  • sloc: ansic: 13,612; sh: 11,267; python: 586; makefile: 158; javascript: 80; xml: 35
file content (80 lines) | stat: -rw-r--r-- 1,925 bytes parent folder | download | duplicates (5)
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
/*
 *  OpenSlide, a library for reading whole slide image files
 *
 *  Copyright (c) 2007-2014 Carnegie Mellon University
 *  All rights reserved.
 *
 *  OpenSlide is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation, version 2.1.
 *
 *  OpenSlide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with OpenSlide. If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#ifdef WIN32
#define _WIN32_WINNT 0x0600
#include <windows.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <glib.h>

#ifdef F_GETPATH
// Mac OS X: MAXPATHLEN
#include <sys/param.h>
#endif

#include "test-common.h"

char *get_fd_path(int fd) {
  struct stat st;
  char *path = NULL;

  if (fstat(fd, &st)) {
    return NULL;
  }

#if defined WIN32
  // Windows
  HANDLE hdl = (HANDLE) _get_osfhandle(fd);
  if (hdl != INVALID_HANDLE_VALUE) {
    DWORD size = GetFinalPathNameByHandle(hdl, NULL, 0, 0);
    if (size) {
      path = g_malloc(size);
      DWORD ret = GetFinalPathNameByHandle(hdl, path, size - 1, 0);
      if (!ret || ret > size) {
        g_free(path);
        path = NULL;
      }
    }
  }
#elif defined F_GETPATH
  // Mac OS X
  path = g_malloc(MAXPATHLEN);
  if (fcntl(fd, F_GETPATH, path)) {
    g_free(path);
    path = NULL;
  }
#else
  // Fallback; works only on Linux
  char *link_path = g_strdup_printf("/proc/%d/fd/%d", getpid(), fd);
  path = g_file_read_link(link_path, NULL);
  g_free(link_path);
#endif

  if (!path) {
    path = g_strdup("<unknown>");
  }
  return path;
}