misc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "plt.h"
  2. /* Bit-mask values for 'flags' argument of becomeDaemon() */
  3. #define BD_NO_CHDIR 01 /* Don't chdir("/") */
  4. #define BD_NO_CLOSE_FILES 02 /* Don't close all open files */
  5. #define BD_NO_REOPEN_STD_FDS 04 /* Don't reopen stdin, stdout, and \
  6. stderr to /dev/null */
  7. #define BD_NO_UMASK0 010 /* Don't do a umask(0) */
  8. #define BD_MAX_CLOSE 8192 /* Maximum file descriptors to close if \
  9. sysconf(_SC_OPEN_MAX) is indeterminate */
  10. int misc_createDir(const char *sPathName)
  11. {
  12. char DirName[256];
  13. strcpy(DirName, sPathName);
  14. int i, len = strlen(DirName);
  15. if (DirName[len - 1] != '/')
  16. strcat(DirName, "/");
  17. len = strlen(DirName);
  18. for (i = 1; i < len; i++)
  19. {
  20. if (DirName[i] == '/')
  21. {
  22. DirName[i] = 0;
  23. if (access(DirName, 0755) != 0)
  24. {
  25. if (mkdir(DirName, 0755) == -1)
  26. {
  27. perror("mkdir error");
  28. return -1;
  29. }
  30. }
  31. DirName[i] = '/';
  32. }
  33. }
  34. return 0;
  35. }
  36. double misc_gettimeofday()
  37. {
  38. struct timeval tv;
  39. struct timezone tz;
  40. double starttime;
  41. double endtime;
  42. gettimeofday(&tv, &tz);
  43. return (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
  44. }
  45. int misc_get_datetime(int *y, int *m, int *d, int *h, int *min, int *s)
  46. {
  47. time_t timep;
  48. struct tm *tsp;
  49. int ret = 0;
  50. time(&timep);
  51. // tsp = gmtime(&timep);
  52. tsp = localtime(&timep);
  53. *y = 1900 + tsp->tm_year;
  54. *m = 1 + tsp->tm_mon;
  55. *d = tsp->tm_mday;
  56. *h = tsp->tm_hour;
  57. *min = tsp->tm_min;
  58. *s = tsp->tm_sec;
  59. return ret;
  60. }
  61. int misc_day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end)
  62. {
  63. int y2, m2, d2;
  64. int y1, m1, d1;
  65. m1 = (month_start + 9) % 12;
  66. y1 = year_start - m1 / 10;
  67. d1 = 365 * y1 + y1 / 4 - y1 / 100 + y1 / 400 + (m1 * 306 + 5) / 10 + (day_start - 1);
  68. m2 = (month_end + 9) % 12;
  69. y2 = year_end - m2 / 10;
  70. d2 = 365 * y2 + y2 / 4 - y2 / 100 + y2 / 400 + (m2 * 306 + 5) / 10 + (day_end - 1);
  71. return (d2 - d1);
  72. }
  73. void misc_gen_datetimestr(char *buf, int len)
  74. {
  75. time_t timep;
  76. struct tm *tsp;
  77. char tmpbuf[32];
  78. time(&timep);
  79. // tsp = gmtime(&timep);
  80. tsp = localtime(&timep);
  81. sprintf(tmpbuf, "%04d-%02d-%02d %02d:%02d:%02d", tsp->tm_year + 1900,
  82. tsp->tm_mon + 1,
  83. tsp->tm_mday,
  84. tsp->tm_hour,
  85. tsp->tm_min,
  86. tsp->tm_sec);
  87. strncpy(buf, tmpbuf, len);
  88. }
  89. void misc_gen_timestring(char *buf, int len)
  90. {
  91. time_t timep;
  92. struct tm *tsp;
  93. char tmpbuf[32];
  94. time(&timep);
  95. // tsp = gmtime(&timep);
  96. tsp = localtime(&timep);
  97. sprintf(tmpbuf, "%02d:%02d:%02d", tsp->tm_hour, tsp->tm_min, tsp->tm_sec);
  98. strncpy(buf, tmpbuf, len);
  99. }
  100. void misc_get_datetimestr(char *buf, int len)
  101. {
  102. time_t timep;
  103. struct tm *tsp;
  104. char tmpbuf[32];
  105. time(&timep);
  106. // tsp = gmtime(&timep);
  107. tsp = localtime(&timep);
  108. sprintf(tmpbuf, "%04d-%02d-%02d %02d:%02d:%02d", tsp->tm_year + 1900,
  109. tsp->tm_mon + 1,
  110. tsp->tm_mday,
  111. tsp->tm_hour,
  112. tsp->tm_min,
  113. tsp->tm_sec);
  114. strncpy(buf, tmpbuf, len);
  115. }
  116. unsigned char CalChecksum(unsigned char *psrc, int count)
  117. {
  118. unsigned char checksum = 0;
  119. int i;
  120. for (i = 0; i < count; i++)
  121. {
  122. checksum += psrc[i];
  123. }
  124. return (checksum);
  125. }
  126. unsigned int calChecksum32(unsigned char *psrc, int count)
  127. {
  128. unsigned int checksum = 0;
  129. int i;
  130. for (i = 0; i < count; i++)
  131. {
  132. checksum += psrc[i];
  133. }
  134. return (checksum);
  135. }
  136. unsigned short calChecksum16(unsigned char *psrc, int count)
  137. {
  138. unsigned short checksum = 0;
  139. int i;
  140. for (i = 0; i < count; i++)
  141. {
  142. checksum += psrc[i];
  143. }
  144. return (checksum);
  145. }
  146. #if 0
  147. static void dump(unsigned char* buf, int len)
  148. {
  149. char buf_s[1024];
  150. char buf_t[16];
  151. int i;
  152. memset(buf_s, 0, sizeof(buf_s));
  153. //strcat(buf_s,"\n");
  154. for(i = 0; i<len; i++){
  155. sprintf(buf_t,"%02X ",buf[i]);
  156. strcat(buf_s,buf_t);
  157. }
  158. strcat(buf_s,"\n");
  159. syslog(LOG_INFO,"%s",buf_s);
  160. }
  161. #else
  162. void dump(char *buf_dst, unsigned char *buf_src, int len)
  163. {
  164. char buf_t[16];
  165. int i;
  166. buf_dst[0] = 0;
  167. for (i = 0; i < len; i++)
  168. {
  169. sprintf(buf_t, "%02X ", buf_src[i]);
  170. strcat(buf_dst, buf_t);
  171. }
  172. }
  173. #endif
  174. // 0 : big end
  175. // 1 : little end
  176. int JudgeEnd(void)
  177. {
  178. int num = 1;
  179. // *((char*)&num)���num������ֽڣ�Ϊ0x00,˵���Ǵ�� Ϊ0x01,˵����С��
  180. return *((char *)&num) ? 1 : 0;
  181. }
  182. // little end
  183. // val -> short -> unsigned short
  184. // NOTE:modbus use big end
  185. unsigned short f32_to_u16(float val)
  186. {
  187. short stmp;
  188. unsigned short ret;
  189. stmp = (short)val;
  190. memcpy((unsigned char *)&ret, (unsigned char *)&stmp, 2);
  191. return ret;
  192. }
  193. void f32_to_pi32(unsigned char *pdst, float fval)
  194. {
  195. int itmp = (int)fval;
  196. *pdst++ = itmp & 0xFF;
  197. *pdst++ = (itmp >> 8) & 0xFF;
  198. *pdst++ = (itmp >> 16) & 0xFF;
  199. *pdst++ = (itmp >> 24) & 0xFF;
  200. }
  201. void f32_to_pi16(unsigned char *pdst, float fval)
  202. {
  203. short itmp = (int)fval;
  204. *pdst++ = itmp & 0xFF;
  205. *pdst++ = (itmp >> 8) & 0xFF;
  206. }
  207. void i32_to_pu8(unsigned char *pdst, int ival)
  208. {
  209. unsigned int utmp = (unsigned int)ival;
  210. *pdst++ = utmp & 0xFF;
  211. *pdst++ = (utmp >> 8) & 0xFF;
  212. *pdst++ = (utmp >> 16) & 0xFF;
  213. *pdst++ = (utmp >> 24) & 0xFF;
  214. }
  215. // little end
  216. short pu8_to_i16(unsigned char *pval)
  217. {
  218. return (short)(*pval | (*(pval + 1)) << 8);
  219. }
  220. // little end
  221. int pu8_to_i32(unsigned char *pval)
  222. {
  223. return (int)(*pval | (*(pval + 1)) << 8 | (*(pval + 2)) << 16 | (*(pval + 3)) << 24);
  224. }
  225. int file_size(char *filename)
  226. {
  227. struct stat statbuf;
  228. stat(filename, &statbuf);
  229. int size = statbuf.st_size;
  230. return size;
  231. }
  232. /*
  233. * System function daemon() replacement based on FreeBSD implementation.
  234. * Original source file CVS tag:
  235. * $FreeBSD: src/lib/libc/gen/daemon.c,v 1.3 2000/01/27 23:06:14 jasone Exp $
  236. */
  237. int misc_daemon(int nochdir, int noclose)
  238. {
  239. int fd;
  240. switch (fork())
  241. {
  242. case -1:
  243. return (-1);
  244. case 0:
  245. break;
  246. default:
  247. _exit(0);
  248. }
  249. if (setsid() == -1)
  250. return (-1);
  251. if (!nochdir)
  252. (void)chdir("/");
  253. if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1)
  254. {
  255. (void)dup2(fd, STDIN_FILENO);
  256. (void)dup2(fd, STDOUT_FILENO);
  257. (void)dup2(fd, STDERR_FILENO);
  258. if (fd > 2)
  259. (void)close(fd);
  260. }
  261. return (0);
  262. }
  263. /* Returns 0 on success, -1 on error */
  264. int misc_becomeDaemon(int flags)
  265. {
  266. int maxfd, fd;
  267. switch (fork())
  268. { /* Become background process */
  269. case -1:
  270. return -1;
  271. case 0:
  272. break; /* Child falls through... */
  273. default:
  274. _exit(EXIT_SUCCESS); /* while parent terminates */
  275. }
  276. if (setsid() == -1) /* Become leader of new session */
  277. return -1;
  278. switch (fork())
  279. { /* Ensure we are not session leader */
  280. case -1:
  281. return -1;
  282. case 0:
  283. break;
  284. default:
  285. _exit(EXIT_SUCCESS);
  286. }
  287. if (!(flags & BD_NO_UMASK0))
  288. umask(0); /* Clear file mode creation mask */
  289. if (!(flags & BD_NO_CHDIR))
  290. chdir("/"); /* Change to root directory */
  291. if (!(flags & BD_NO_CLOSE_FILES))
  292. { /* Close all open files */
  293. maxfd = sysconf(_SC_OPEN_MAX);
  294. if (maxfd == -1) /* Limit is indeterminate... */
  295. maxfd = BD_MAX_CLOSE; /* so take a guess */
  296. for (fd = 0; fd < maxfd; fd++)
  297. close(fd);
  298. }
  299. if (!(flags & BD_NO_REOPEN_STD_FDS))
  300. {
  301. close(STDIN_FILENO); /* Reopen standard fd's to /dev/null */
  302. fd = open("/dev/null", O_RDWR);
  303. if (fd != STDIN_FILENO) /* 'fd' should be 0 */
  304. return -1;
  305. if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
  306. return -1;
  307. if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
  308. return -1;
  309. }
  310. return 0;
  311. }