PS2SDK
PS2 Homebrew Libraries
platform.c
1 /*
2  platform.c - Contains platform-specific functions.
3 */
4 
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <ctype.h>
8 #include "dprintf.h"
9 #if defined(_WIN32) || defined(WIN32)
10 #include <windows.h>
11 #else
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <time.h>
15 #endif
16 
17 #include "platform.h"
18 
19 #if defined(_WIN32) || defined(WIN32)
20 int GetUsername(char *buffer, unsigned int BufferSize)
21 {
22  int ret;
23  DWORD lpnSize;
24  lpnSize = BufferSize;
25  ret = (GetUserNameA(buffer, &lpnSize) == 0 ? EIO : 0);
26  DPRINTF("(%d): %s\n", ret, buffer);
27  return ret;
28 }
29 #endif
30 
31 int GetLocalhostName(char *buffer, unsigned int BufferSize)
32 {
33  int ret;
34 #if defined(_WIN32) || defined(WIN32)
35  DWORD lpnSize;
36  lpnSize = BufferSize;
37  ret = (GetComputerNameA(buffer, &lpnSize) == 0 ? EIO : 0);
38 #else
39  ret = gethostname(buffer, BufferSize);
40 #endif
41  DPRINTF("(%d): %s\n", ret, buffer);
42  return ret;
43 }
44 
45 // macros for converting between an integer and a BCD number
46 #ifndef btoi
47 #define btoi(b) ((b) / 16 * 10 + (b) % 16) // BCD to int
48 #endif
49 #ifndef itob
50 #define itob(i) ((i) / 10 * 16 + (i) % 10) // int to BCD
51 #endif
52 
53 /* Converts the specified value to a value that looks the same in base 16. E.g. It converts 2012 in decimal to 0x2012 in hexadecimal. */
54 static unsigned short int ConvertToBase16(unsigned short int value)
55 {
56  unsigned short int result;
57 
58  result = value + value / 10 * 0x06;
59  result += value / 100 * 0x60;
60  return (result + value / 1000 * 0x600);
61 }
62 
63 
64 unsigned int GetSystemDate(void)
65 {
66 #if defined(_WIN32) || defined(WIN32)
67  SYSTEMTIME SystemTime;
68  GetSystemTime(&SystemTime);
69 
70  return (((unsigned int)ConvertToBase16(SystemTime.wYear)) << 16 | ConvertToBase16(SystemTime.wMonth) << 8 | ConvertToBase16(SystemTime.wDay));
71 #else
72 
73  time_t time_raw_format;
74  struct tm *ptr_time;
75 
76  time(&time_raw_format);
77  ptr_time = localtime(&time_raw_format);
78  return (((unsigned int)ConvertToBase16(ptr_time->tm_year + 1900)) << 16 | ConvertToBase16(ptr_time->tm_mon + 1) << 8 | ConvertToBase16(ptr_time->tm_mday));
79 #endif
80 }
81 
82 unsigned int GetFileCreationDate(const char *path)
83 {
84 #if defined(_WIN32) || defined(WIN32)
85  HANDLE hFile;
86  FILETIME CreationTime;
87  SYSTEMTIME CreationSystemTime;
88 
89  if ((hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
90  GetFileTime(hFile, &CreationTime, NULL, NULL);
91  CloseHandle(hFile);
92 
93  FileTimeToSystemTime(&CreationTime, &CreationSystemTime);
94  } else
95  GetSystemTime(&CreationSystemTime);
96 
97  return (((unsigned int)ConvertToBase16(CreationSystemTime.wYear)) << 16 | ConvertToBase16(CreationSystemTime.wMonth) << 8 | ConvertToBase16(CreationSystemTime.wDay));
98 #else
99  struct tm *clock; // create a time structure
100  struct stat attrib; // create a file attribute structure
101  stat(path, &attrib); // get the attributes of afile.txt
102  clock = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure
103  return (((unsigned int)ConvertToBase16(clock->tm_year + 1900)) << 16 | ConvertToBase16(clock->tm_mon) << 8 | ConvertToBase16(clock->tm_mday));
104 #endif
105 }
106 
107 int GetCurrentWorkingDirectory(char *buffer, unsigned int BufferSize)
108 {
109 #if defined(_WIN32) || defined(WIN32)
110  return (GetCurrentDirectoryA(BufferSize, buffer) == 0 ? EIO : 0);
111 #else
112  if (getcwd(buffer, BufferSize) != NULL)
113  return 0;
114  else
115  return EIO;
116 #endif
117 }
118 
119 void upperbuff(char *temp)
120 {
121  char *s = temp;
122  while (*s) {
123  *s = toupper((unsigned char) *s);
124  s++;
125  }
126 }
EIO
#define EIO
Definition: errno.h:29
ctype.h
time.h
stdlib.h
errno.h