summaryrefslogtreecommitdiff
path: root/storage/connect/osutil.c
blob: 2e9e120b0c82972d3f67e934304b508a640f4376 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "my_global.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "osutil.h"

#ifdef __WIN__
my_bool CloseFileHandle(HANDLE h) 
  {
  return !CloseHandle(h);
  } /* end of CloseFileHandle */

#else  /* UNIX */
/* code to handle Linux and Solaris */
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <pwd.h>

extern FILE *debug;

/***********************************************************************/
/*  Define some functions not existing in the UNIX library.            */
/***********************************************************************/
PSZ strupr(PSZ p)
  {
  register int i;

  for (i = 0; p[i]; i++)
    p[i] = toupper(p[i]);

  return (p);
  } /* end of strupr */

PSZ strlwr(PSZ p)
  {
  register int i;

  for (i = 0; p[i]; i++)
    p[i] = tolower(p[i]);

  return (p);
  } /* end of strlwr */

#if defined(NOT_USED) /*&& !defined(sun) && !defined(LINUX) && !defined(AIX)*/
/***********************************************************************/
/*  Define stricmp function not existing in some UNIX libraries.       */
/***********************************************************************/
int stricmp(char *str1, char *str2)
  {
  register int i;
  int  n;
  char c;
  char *sup1 = malloc(strlen(str1) + 1);
  char *sup2 = malloc(strlen(str2) + 1);

  for (i = 0; c = str1[i]; i++)
    sup1[i] = toupper(c);

  sup1[i] = 0;

  for (i = 0; c = str2[i]; i++)
   sup2[i] = toupper(c);

  sup2[i] = 0;
  n = strcmp(sup1, sup2);
  free(sup1);
  free(sup2);
  return (n);
  } /* end of stricmp */
#endif   /* sun */

/***********************************************************************/
/*  Define the splitpath function not existing in the UNIX library.    */
/***********************************************************************/
void _splitpath(LPCSTR name, LPSTR drive, LPSTR dir, LPSTR fn, LPSTR ft)
  {
  LPCSTR p2, p = name;

#ifdef DEBTRACE
 htrc("SplitPath: name=%s [%s (%d)]\n", 
   XSTR(name), XSTR(__FILE__), __LINE__);
#endif

  if (drive) *drive = '\0';
  if (dir) *dir = '\0';
  if (fn) *fn = '\0';
  if (ft) *ft = '\0';

  if ((p2 = strrchr(p, '/'))) {
    p2++;
    if (dir) strncat(dir, p, p2 - p);
    p = p2;
    } /* endif p2 */

  if ((p2 = strrchr(p, '.'))) {
    if (fn) strncat(fn, p, p2 - p);
    if (ft) strcpy(ft, p2);
  } else
    if (fn) strcpy(fn, p);

#ifdef DEBTRACE
 htrc("SplitPath: name=%s drive=%s dir=%s filename=%s type=%s [%s(%d)]\n",
  XSTR(name), XSTR(drive), XSTR(dir), XSTR(fn), XSTR(ft), __FILE__, __LINE__);
#endif
  } /* end of _splitpath */

/***********************************************************************/
/*  Define the makepath function not existing in the UNIX library.     */
/***********************************************************************/
void _makepath(LPSTR name, LPCSTR drive __attribute__((unused)), LPCSTR dir, LPCSTR fn, LPCSTR ft)
  {
  int n;

  if (!name)
    return;
  else
    *name = '\0';

  if (dir && (n = strlen(dir)) > 0) {
    strcpy(name, dir);

    if (name[n-1] != '/')
      strcat(name, "/");

    }  /* endif dir */

  if (fn)
    strcat(name, fn);

  if (ft && strlen(ft)) {
    if (*ft != '.')
      strcat(name, ".");

    strcat(name, ft);
    } /* endif ft */

  } /* end of _makepath */

my_bool CloseFileHandle(HANDLE h) 
  {
  return (close(h)) ? TRUE : FALSE;
  }  /* end of CloseFileHandle */

#if 0
void Sleep(DWORD time) 
  {
  //FIXME: TODO
  }  /* end of Sleep */
#endif

int GetLastError() 
  {
  return errno;
  }  /* end of GetLastError */

unsigned long _filelength(int fd) 
  {
  struct stat st;

  if (fd == -1)
    return 0;

  if (fstat(fd, &st) != 0)
    return 0;

  return st.st_size;
  }  /* end of _filelength */

char *_fullpath(char *absPath, const char *relPath, size_t maxLength)
  {
  // Fixme
  char *p;

  if ( *relPath == '\\' || *relPath == '/' ) {
    strncpy(absPath, relPath, maxLength);
  } else if (*relPath == '~') {
    // get the path to the home directory
    struct passwd *pw = getpwuid(getuid());
    const char    *homedir = pw->pw_dir;

    if (homedir)
      strcat(strncpy(absPath, homedir, maxLength), relPath + 1);
    else
      strncpy(absPath, relPath, maxLength);
        
  } else {
    char buff[2*_MAX_PATH];

    p= getcwd(buff, _MAX_PATH);
    assert(p);
    strcat(buff,"/");
    strcat(buff, relPath);
    strncpy(absPath, buff, maxLength);
  }  /* endif's relPath */

  p = absPath;

  for(; *p; p++)
    if (*p == '\\')
      *p = '/';

  return absPath;
  }  /* end of _fullpath */

BOOL MessageBeep(uint i __attribute__((unused)))
  {
  // Fixme
  return TRUE;
  } /* end of MessageBeep */

#if 0
/* This function is ridiculous and should be revisited */
DWORD FormatMessage(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId,
                    DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize, ...)
  {
  char buff[32];
  int n;

//if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
//  return 0;                         /* means error */

  n = sprintf(buff, "Error code: %d", (int) dwMessageId);
  strncpy(lpBuffer, buff, nSize);
  return min(n, nSize);
  }  /* end of FormatMessage */
#endif

#endif  // UNIX