fileutil.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //#define _GNU_SOURCE // for canonicalize_file_name
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include "fileutil.h"
  9. /////////////////////////////////////////////////////////////////////////////
  10. size_t GetFileSize(FILE *fd)
  11. {
  12. long nRet = 0;
  13. long nCur = ftell(fd);
  14. if(!fseek(fd, 0, SEEK_END))
  15. {
  16. if((nRet = ftell(fd)) < 0)
  17. nRet = 0;
  18. fseek(fd, nCur, SEEK_SET);
  19. }
  20. return (size_t)nRet;
  21. }
  22. /////////////////////////////////////////////////////////////////////////////
  23. int FileExist(const char *file)
  24. {
  25. if(!file || !*file)
  26. return 0;
  27. struct stat s;
  28. memset(&s, 0, sizeof(s));
  29. return !stat(file, &s) && S_ISREG(s.st_mode);
  30. }
  31. /////////////////////////////////////////////////////////////////////////////
  32. int DirectoryExist(const char *dir)
  33. {
  34. if(!dir || !*dir)
  35. return 0;
  36. struct stat s;
  37. memset(&s, 0, sizeof(s));
  38. return !stat(dir, &s) && S_ISDIR(s.st_mode);
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // creates a directory with read/write/search permissions for owner and group,
  42. // and with read/search permissions for others
  43. int CreateDirectory(const char *pszDir)
  44. {
  45. if(mkdir(pszDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
  46. {
  47. if(errno != EEXIST)
  48. return 0;
  49. }
  50. return 1;
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. const char* GetAppPath(char *pszPath, size_t nCChPath)
  54. {
  55. char szTmp[32];
  56. if(!pszPath || !nCChPath)
  57. {
  58. errno = EINVAL;
  59. return NULL;
  60. }
  61. sprintf(szTmp, "/proc/%d/exe", getpid());
  62. ssize_t nLen = readlink(szTmp, pszPath, nCChPath - 1);
  63. if(nLen < 0)
  64. {
  65. *pszPath = '\0';
  66. return NULL;
  67. }
  68. pszPath[nLen] = '\0';
  69. return pszPath;
  70. }
  71. /////////////////////////////////////////////////////////////////////////////
  72. const char* GetAppDirectory(char *pszDir, size_t nCChDir)
  73. {
  74. char *pszSl;
  75. if(!GetAppPath(pszDir, nCChDir))
  76. return NULL;
  77. if((pszSl = strrchr(pszDir, '/')))
  78. {
  79. if(pszSl != pszDir)
  80. *pszSl = '\0';
  81. else
  82. *(pszSl + 1) = '\0';
  83. }
  84. return pszDir;
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. const char* BuildCanonicalFilePath(const char *pszDir, const char *pszFilespec, char *pszPath, size_t nCChPath)
  88. {
  89. size_t nLen;
  90. char szDir[PATH_MAX];
  91. char *pszCanPath, szFilePath[PATH_MAX];
  92. if(!pszFilespec || !*pszFilespec || !pszPath || !nCChPath)
  93. {
  94. errno = EINVAL;
  95. return NULL;
  96. }
  97. if(!pszDir)
  98. {
  99. memset(szDir, 0, sizeof(szDir));
  100. if(!GetAppDirectory(szDir, sizeof(szDir)))
  101. return NULL;
  102. pszDir = szDir;
  103. }
  104. snprintf(szFilePath, sizeof(szFilePath), "%s/%s", pszDir, pszFilespec);
  105. if(!(pszCanPath = canonicalize_file_name(szFilePath)))
  106. return NULL;
  107. nLen = strlen(pszCanPath);
  108. if(nLen < nCChPath)
  109. strcpy(pszPath, pszCanPath);
  110. else
  111. {
  112. errno = ENOMEM;
  113. pszPath = NULL;
  114. }
  115. free(pszCanPath);
  116. return pszPath;
  117. }