File: gnatcoll_support.c

package info (click to toggle)
libgnatcoll 18-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,068 kB
  • sloc: ada: 40,393; python: 354; ansic: 310; makefile: 245; sh: 31
file content (198 lines) | stat: -rw-r--r-- 5,231 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*----------------------------------------------------------------------------
--                                  G N A T C O L L                         --
--                                                                          --
--                     Copyright (C) 2008-2017, AdaCore                     --
--                                                                          --
-- This is free software;  you can redistribute it  and/or modify it  under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for  more details.  You should have  received  a copy of the GNU --
-- General  Public  License  distributed  with  this  software;   see  file --
-- COPYING3.  If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license.                                                          --
----------------------------------------------------------------------------*/

#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
#endif

#ifdef HAVE_SYSLOG
#include <syslog.h>
void
syslog_wrapper(int priority, const char* msg) {
   syslog(priority, "%s", msg);
}
#endif

// From adaint.h
extern int    __gnat_is_directory            (char *);

#ifdef HAVE_MMAP
#include <sys/mman.h>

int
gnatcoll_has_mmap() {
  return 1;
}

void *
gnatcoll_mmap (void *start, long length, int prot, int flags,
               int fd, long offset)
{
  return mmap (start, (size_t)length, prot, flags, fd, (off_t)offset);
}

int gnatcoll_munmap (void *start, long length)
{
  return munmap (start, (size_t)length);
}

#ifdef HAVE_MADVISE

void gnatcoll_madvise(void* addr, size_t len, int advice) {
   int adv = (advice == 1 ? MADV_NORMAL
             : advice == 2 ? MADV_RANDOM
             : advice == 4 ? MADV_SEQUENTIAL
             : MADV_NORMAL);
   madvise(addr, len, adv);
}

#else  /* not HAVE_MADVISE */

void gnatcoll_madvise(void* addr, size_t len, int advice) {
}

#endif  /* HAVE_MADVISE */

#else
/* No mmap support, so no madvise support either */

int
gnatcoll_has_mmap ()
{
  return 0;
}

void gnatcoll_madvise(void* addr, size_t len, int advice) {
}

void
*gnatcoll_mmap (void *start, long length, int prot, int flags,
		int fd, long offset)
{
  return (void*)0;
}

int gnatcoll_munmap (void *start, long length)
{
  return 0;
}

#endif

int
__gnatcoll_get_logical_drive_strings (char *buffer, int len)
{
#ifdef _WIN32
  return GetLogicalDriveStringsA ((DWORD)len, (LPSTR)buffer);
#else
  return 0;
#endif
}

void
__gnatcoll_set_readable (char *file, int set)
{
#ifdef _WIN32
  /* ??? NOT CURRENTLY SUPPORTED.
     There is no support for setting a file as unreadable using the
     standard chmod routine on Windows. With this routine it is only
     possible to set a file as read-only. To set a file as unreadable it is
     required to use the more complex [Get|Set]FileSecurity Win32 API by
     setting the proper ACL. */
#elif defined (__VMS__)
  /* ??? NOT CURRENTLY SUPPORTED. */
#else
  struct stat statbuf;

  if (!stat (file, &statbuf))
    {
      if (set)
        chmod (file, statbuf.st_mode | S_IREAD);
      else
        chmod (file, statbuf.st_mode & (~S_IREAD));
    }
#endif
}

/**********************************************************
 ** __gnatcoll_get_tmp_dir ()
 ** Return the tmp directory.
 ** Return value must be freed by caller
 **********************************************************/

char*
__gnatcoll_get_tmp_dir (void)
{
  static char *result = NULL;

  /* test static result to see if result has already been found */
  if (result != NULL)
    return strdup (result);

#ifdef _WIN32
  {
    DWORD dwRet;

    result = malloc ((MAX_PATH + 1) * sizeof (char));
    dwRet = GetTempPath (MAX_PATH, result);
    if (dwRet > 0) {
      result[dwRet] = '\0';
      if (__gnat_is_directory (result))
        return strdup (result);
    }
    free (result);
  }
#endif

  result = getenv ("TMPDIR");
  if (result)
    if (__gnat_is_directory (result))
      return strdup (result);

  result = getenv ("TMP");
  if (result)
    if (__gnat_is_directory (result))
      return strdup (result);

  /* On Windows systems, this is the documented way of retrieving the tmp dir.
   * However, the TMP env variable should also be defined */
  result = getenv ("TEMP");
  if (result)
    if (__gnat_is_directory (result))
      return strdup (result);

  /* need to duplicate twice: one is for caching, the second one will be freed
   * by user */
  result = strdup ("/tmp");
  return strdup (result);
}

/************************************************************************
 * Support for atomic operations
 ************************************************************************/

#ifdef ATOMIC_INTRINSICS
int gnatcoll_sync_bool_compare_and_swap_access
  (void** ptr, void* oldval, void* newval)
{
   return __sync_bool_compare_and_swap(ptr, oldval, newval);
}
#endif