C言語に関するファイル(ファイルオープン、リード、ライト)

来源:互联网 发布:黑色沙漠城镇优化 编辑:程序博客网 时间:2024/06/11 20:25
  1. /*
  2.  *  Copyright (C) 2006 Nigel Horne <njh@bandsman.co.uk>
  3.  *
  4.  *  This program is free software; you can redistribute it and/or modify
  5.  *  it under the terms of the GNU General Public License as published by
  6.  *  the Free Software Foundation; either version 2 of the License, or
  7.  *  (at your option) any later version.
  8.  *
  9.  *  This program is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  *
  14.  *  You should have received a copy of the GNU General Public License
  15.  *  along with this program; if not, write to the Free Software
  16.  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17.  *  MA 02110-1301, USA.
  18.  *
  19.  * Unix/Linux compatibility for Windows
  20.  * Inspired by glib and the cygwin source code
  21.  * Tested under Microsoft Visual Studio 2005
  22.  */
  23. #ifndef CLAMAV_COMPAT_H
  24. #define CLAMAV_COMPAT_H
  25. #ifdef  C_WINDOWS
  26. #pragma warning(disable: 4996)  /* turn off warnings about depracated code */
  27. #ifndef _USE_32BIT_TIME_T
  28. #define _USE_32BIT_TIME_T
  29. #endif
  30. /*#include  "snprintf.h"*/
  31. #define inline  /* it's too different in MSVC to bother */
  32. typedef int ssize_t;
  33. typedef int mode_t;
  34. typedef char *  caddr_t;
  35. typedef long    off_t;
  36. #define X_OK    0
  37. #define W_OK    2
  38. #define R_OK    4
  39. #define PROT_READ   1
  40. #define MAP_PRIVATE 1
  41. #define MAP_FAILED  (caddr_t)-1
  42. caddr_t mmap(caddr_t address, size_t length, int protection, int flags, int fd, off_t offset);
  43. int munmap(caddr_t addr, size_t length);
  44. #define strcasecmp(s1, s2)  _stricmp(s1, s2)
  45. #define strncasecmp(s1, s2, n)  _strnicmp(s1, s2, n)
  46. #define chdir(d)    _chdir(d)
  47. #define umask(n)    _umask(n)
  48. #ifndef S_IRWXU
  49. #define S_IRWXU (_S_IREAD|_S_IWRITE|_S_IEXEC)
  50. #endif
  51. #define S_IWUSR S_IWRITE
  52. #define S_IRUSR S_IREAD
  53. #define S_ISLNK(f)  0
  54. #define S_ISDIR(f)  (((f)&S_IFMT) == S_IFDIR)
  55. #define S_ISREG(f)  (((f)&S_IFMT) == S_IFREG)
  56. #define fsync(fd)   _commit(fd)
  57. #define lstat(file, buf)    stat(file, buf)
  58. #define _CRT_SECURE_NO_DEPRECATE    1
  59. #ifndef _WINSOCKAPI_    /* timeval is in winsock.h */
  60. struct timeval {
  61.     long    tv_sec;
  62.     long    tv_usec;
  63. };
  64. #endif  /* _WINSOCKAPI_ */
  65. /* Maximum filenames under various systems - njh */
  66. #ifndef NAME_MAX    /* e.g. Linux */
  67. # ifdef MAXNAMELEN  /* e.g. Solaris */
  68. #   define  NAME_MAX    MAXNAMELEN
  69. # else
  70. #   ifdef   FILENAME_MAX    /* e.g. SCO */
  71. #     define    NAME_MAX    FILENAME_MAX
  72. #   else
  73. #     define    NAME_MAX    256
  74. #   endif
  75. # endif
  76. #endif
  77. struct DIR {
  78.     char    *dir_name;
  79.     int just_opened;
  80.     void    *find_file_handle;
  81.     void    *find_file_data;    /* LPWIN32_FIND_DATA */
  82. };
  83. typedef struct  DIR DIR;
  84. struct  dirent {
  85.     char  d_name[NAME_MAX + 1];
  86. };
  87. DIR *opendir(const char *dirname);
  88. struct  dirent  *readdir(DIR *dir);
  89. int readdir_r(DIR *dir, struct dirent *dirent, struct dirent **output);
  90. void    rewinddir(DIR *dir);
  91. int closedir(DIR *dir);
  92. int gettimeofday(struct timeval* tp, void* tz);
  93. #define alarm(seconds)
  94. #define sleep(seconds)  Sleep(seconds * 1000)
  95. #define pause();
  96. #ifdef  _DEBUG
  97. /* http://msdn2.microsoft.com/en-us/library/e5ewb1h3(VS.80).asp */
  98. #define _CRTDBG_MAP_ALLOC
  99. #include <stdlib.h>
  100. #include <crtdbg.h>
  101. /* breaks mspack/qtmd.c :-( */
  102. /* #define  free(p) { _free_dbg(p, _NORMAL_BLOCK); } */
  103. #endif
  104. #endif  /* C_WINDOWS */
  105. #endif  /* CLAMAV_COMPAT_H */
  compat.h
  1. /*
  2.  *  Copyright (C) 2006 Nigel Horne <njh@bandsman.co.uk>
  3.  *
  4.  *  This program is free software; you can redistribute it and/or modify
  5.  *  it under the terms of the GNU General Public License as published by
  6.  *  the Free Software Foundation; either version 2 of the License, or
  7.  *  (at your option) any later version.
  8.  *
  9.  *  This program is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  *
  14.  *  You should have received a copy of the GNU General Public License
  15.  *  along with this program; if not, write to the Free Software
  16.  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17.  *  MA 02110-1301, USA.
  18.  *
  19.  * Unix/Linux compatibility for Windows
  20.  * Inspired by glib and the cygwin source code
  21.  * Tested under Microsoft Visual Studio 2005
  22.  */
  23. #include <windows.h>
  24. #if HAVE_CONFIG_H
  25. #include "clamav-config.h"
  26. #endif
  27. #include <errno.h>
  28. #include <string.h>
  29. #include "clamav.h"
  30. #include "others.h"
  31. #ifndef CL_DEBUG
  32. #define NDEBUG  /* map CLAMAV debug onto standard */
  33. #endif
  34. #include <stdlib.h>
  35. #include <direct.h>
  36. #include <io.h>
  37. #include <pthread.h>
  38. static const char *basename (const char *file_name);
  39. /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
  40. #define _W32_FT_OFFSET (116444736000000000ULL)
  41. /*
  42.  * Patches for 64 bit support in opendir by
  43.  *  Mark Pizzolato clamav-win32@subscriptions.pizzolato.net
  44.  */
  45. DIR *
  46. opendir(const char *dirname)
  47. {
  48.     DIR *ret = cli_calloc(1, sizeof(DIR));
  49.     char mask[_MAX_PATH + 3];
  50.     size_t i, j;
  51.     if(ret == NULL)
  52.         return NULL;
  53.     /* struct _WIN32_FIND_DATAA is what a LPWIN32_FIND_DATA points to */
  54.     ret->find_file_data = cli_calloc(1, sizeof(struct _WIN32_FIND_DATAA));
  55.     if(ret->find_file_data == NULL) {
  56.         free(ret);
  57.         return NULL;
  58.     }
  59.     ret->dir_name = cli_strdup(dirname);
  60.     if(ret->dir_name == NULL) {
  61.         free(ret->find_file_data);
  62.         free(ret);
  63.         return NULL;
  64.     }
  65.     j = strlen(dirname);
  66.     for(i = 0; i < j; i++)
  67.         if(ret->dir_name[i] == '/')
  68.             ret->dir_name[i] = '//';
  69.     if(j && dirname[j - 1] == '//')
  70.         ret->dir_name[--j] = '/0';
  71.     sprintf(mask, "%s//*", ret->dir_name);
  72.     ret->find_file_handle = FindFirstFile(mask,
  73.                     (LPWIN32_FIND_DATA)ret->find_file_data);
  74.     if(ret->find_file_handle == INVALID_HANDLE_VALUE) {
  75.         free(ret->find_file_data);
  76.         free(ret->dir_name);
  77.         free(ret);
  78.         cli_warnmsg("Can't opendir(%s)/n", dirname);
  79.         return NULL;
  80.     }
  81.     ret->just_opened = TRUE;
  82.     return ret;
  83. }
  84. struct dirent *
  85. readdir(DIR *dir)
  86. {
  87.     /* NOTE: not thread safe */
  88.     static struct dirent result;
  89.     if(dir == NULL)
  90.         return NULL;
  91.     if(dir->just_opened)
  92.         dir->just_opened = FALSE;
  93.     else if(!FindNextFile((HANDLE)dir->find_file_handle, (LPWIN32_FIND_DATA)dir->find_file_data))
  94.         switch(GetLastError ()) {
  95.             case ERROR_NO_MORE_FILES:
  96.                 return NULL;
  97.             default:
  98.                 errno = EIO;
  99.                 return NULL;
  100.         }
  101.     strcpy(result.d_name, basename(((LPWIN32_FIND_DATA)dir->find_file_data)->cFileName));
  102.     return &result;
  103. }
  104. int
  105. readdir_r(DIR *dir, struct dirent *dirent, struct dirent **output)
  106. {
  107.     if(dir == NULL)
  108.         return -1;
  109.     if(dirent == NULL)
  110.         return -1;
  111.     if(output == NULL)
  112.         return -1;
  113.     if(dir->just_opened)
  114.         dir->just_opened = FALSE;
  115.     else if(!FindNextFile((HANDLE)dir->find_file_handle, (LPWIN32_FIND_DATA)dir->find_file_data))
  116.         switch(GetLastError()) {
  117.             case ERROR_NO_MORE_FILES:
  118.                 *output = NULL;
  119.                 return -1;
  120.             default:
  121.                 errno = EIO;
  122.                 *output = NULL;
  123.                 return -1;
  124.         }
  125.     strcpy(dirent->d_name, basename(((LPWIN32_FIND_DATA)dir->find_file_data)->cFileName));
  126.     *output = dirent;
  127.     return 0;
  128. }
  129. void
  130. rewinddir(DIR *dir)
  131. {
  132.     char mask[_MAX_PATH + 3];
  133.     if(dir == NULL)
  134.         return;
  135.     if(!FindClose((HANDLE)dir->find_file_handle))
  136.         cli_warnmsg("rewinddir(): FindClose() failed/n");
  137.     sprintf(mask, "%s//*", dir->dir_name);
  138.     dir->find_file_handle = FindFirstFile (mask,
  139.                     (LPWIN32_FIND_DATA)dir->find_file_data);
  140.     if(dir->find_file_handle == INVALID_HANDLE_VALUE) {
  141.         errno = EIO;
  142.         return;
  143.     }
  144.     dir->just_opened = TRUE;
  145. }
  146. int
  147. closedir(DIR *dir)
  148. {
  149.     if(dir == NULL)
  150.         return -1;
  151.     if(!FindClose((HANDLE)dir->find_file_handle)) {
  152.         errno = EIO;
  153.         return -1;
  154.     }
  155.     free(dir->dir_name);
  156.     free(dir->find_file_data);
  157.     free(dir);
  158.     return 0;
  159. }
  160. static const char *
  161. basename(const char *file_name)
  162. {
  163.     const char *base;
  164.     if(file_name == NULL)
  165.         return NULL;
  166.     base = strrchr(file_name, '//');
  167.     if(base)
  168.         return base + 1;
  169.     if(isalpha(file_name[0] & 0xFF) && (file_name[1] == ':'))
  170.         return (const char *)(file_name + 2);
  171.     return file_name;
  172. }
  173. /* From the cygwin source code */
  174. int
  175. gettimeofday(struct timeval *tp, void *tz)
  176. {
  177.     if(tp) {
  178.         union {
  179.             unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
  180.             FILETIME ft;
  181.         } _now;
  182.         GetSystemTimeAsFileTime(&_now.ft);
  183.         tp->tv_usec = (long)((_now.ns100 / 10ULL) % 1000000ULL );
  184.         tp->tv_sec = (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
  185.     }
  186.     /*
  187.      * Always return 0 as per Open Group Base Specifications Issue 6.
  188.      * Do not set errno on error.
  189.      */
  190.     return 0;
  191. }
  192. /* TODO */
  193. int
  194. geteuid(void)
  195. {
  196.     return 0;
  197. }
  198. int
  199. getuid(void)
  200. {
  201.     return 0;
  202. }
  203. int
  204. getgid(void)
  205. {
  206.     return 0;
  207. }
  208. /*
  209.  * mmap patches for more than one map area by
  210.  *  Mark Pizzolato clamav-win32@subscriptions.pizzolato.net
  211.  */
  212. static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
  213. static struct mmap_context {
  214.     struct mmap_context *link;
  215.     HANDLE h;
  216.     LPVOID view;
  217.     size_t length;
  218. } *mmaps = NULL;
  219. caddr_t
  220. mmap(caddr_t address, size_t length, int protection, int flags, int fd, off_t offset)
  221. {
  222.     LPVOID addr;
  223.     HANDLE h;
  224.     struct mmap_context *ctx;
  225.     if(flags != MAP_PRIVATE) {
  226.         cli_errmsg("mmap: only MAP_SHARED is supported/n");
  227.         return MAP_FAILED;
  228.     }
  229.     if(protection != PROT_READ) {
  230.         cli_errmsg("mmap: only PROT_READ is supported/n");
  231.         return MAP_FAILED;
  232.     }
  233.     if(address != NULL) {
  234.         cli_errmsg("mmap: only NULL map address is supported/n");
  235.         return MAP_FAILED;
  236.     }
  237.     h = CreateFileMapping((HANDLE)_get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL);
  238.     if(h == NULL) {
  239.         cli_errmsg("mmap: CreateFileMapping failed - error %d/n",
  240.             GetLastError());
  241.         return MAP_FAILED;
  242.     }
  243.     if(GetLastError() == ERROR_ALREADY_EXISTS) {
  244.         cli_errmsg("mmap: ERROR_ALREADY_EXISTS/n");
  245.         CloseHandle(h);
  246.         return MAP_FAILED;
  247.     }
  248.     addr = MapViewOfFile(h, FILE_MAP_READ,
  249.         (DWORD)0, ((DWORD)offset & 0xFFFFFFFF),
  250.         length);
  251.     if(addr == NULL) {
  252.         cli_errmsg("mmap failed - error %d/n", GetLastError());
  253.         CloseHandle(h);
  254.         return MAP_FAILED;
  255.     }
  256.     pthread_mutex_lock(&mmap_mutex);
  257.     ctx = cli_malloc(sizeof(*ctx));
  258.     if(NULL == ctx) {
  259.         pthread_mutex_unlock(&mmap_mutex);
  260.         cli_errmsg("mmap: can't create context block/n");
  261.         UnmapViewOfFile(addr);
  262.         CloseHandle(h);
  263.         return MAP_FAILED;
  264.     }
  265.     ctx->h = h;
  266.     ctx->view = addr;
  267.     ctx->length = length;
  268.     ctx->link = mmaps;
  269.     mmaps = ctx;
  270.     pthread_mutex_unlock(&mmap_mutex);
  271.     return (caddr_t)addr;
  272. }
  273. int
  274. munmap(caddr_t addr, size_t length)
  275. {
  276.     struct mmap_context *ctx, *lctx = NULL;
  277.     pthread_mutex_lock(&mmap_mutex);
  278.     for(ctx = mmaps; ctx && (ctx->view != addr); ) {
  279.         lctx = ctx;
  280.         ctx = ctx->link;
  281.     }
  282.     if(ctx == NULL) {
  283.         pthread_mutex_unlock(&mmap_mutex);
  284.         cli_warnmsg("munmap with no corresponding mmap/n");
  285.         return -1;
  286.     }
  287.     if(ctx->length != length) {
  288.         pthread_mutex_unlock(&mmap_mutex);
  289.         cli_warnmsg("munmap with incorrect length specified - partial munmap unsupported/n");
  290.         return -1;
  291.     }
  292.     if(NULL == lctx)
  293.         mmaps = ctx->link;
  294.     else
  295.         lctx->link = ctx->link;
  296.     pthread_mutex_unlock(&mmap_mutex);
  297.     UnmapViewOfFile(ctx->view);
  298.     CloseHandle(ctx->h);
  299.     free(ctx);
  300.     return 0;
  301. }
  302. int
  303. chown(const char *filename, short uid, short gid)
  304. {
  305.     return 0;
  306. }

compat.c


 

原创粉丝点击