1
0

util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** util.c - miscellaneous utility code for libtar
  7. **
  8. ** Mark D. Roth <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #include <libtarint/internal.h>
  13. #include <stdio.h>
  14. #include <libtar/compat.h>
  15. #include <errno.h>
  16. #ifdef STDC_HEADERS
  17. # include <string.h>
  18. #endif
  19. #if defined(_WIN32) && !defined(__CYGWIN__)
  20. #include <direct.h>
  21. #else
  22. #include <sys/param.h>
  23. #endif
  24. /* hashing function for pathnames */
  25. int
  26. path_hashfunc(char *key, int numbuckets)
  27. {
  28. char buf[TAR_MAXPATHLEN];
  29. char *p;
  30. strcpy(buf, key);
  31. p = basename(buf);
  32. return (((unsigned int)p[0]) % numbuckets);
  33. }
  34. /* matching function for dev_t's */
  35. int
  36. dev_match(dev_t *dev1, dev_t *dev2)
  37. {
  38. return !memcmp(dev1, dev2, sizeof(dev_t));
  39. }
  40. /* matching function for ino_t's */
  41. int
  42. ino_match(ino_t *ino1, ino_t *ino2)
  43. {
  44. return !memcmp(ino1, ino2, sizeof(ino_t));
  45. }
  46. /* hashing function for dev_t's */
  47. int
  48. dev_hash(dev_t *dev)
  49. {
  50. return *dev % 16;
  51. }
  52. /* hashing function for ino_t's */
  53. int
  54. ino_hash(ino_t *inode)
  55. {
  56. return *inode % 256;
  57. }
  58. /*
  59. ** mkdirhier() - create all directories in a given path
  60. ** returns:
  61. ** 0 success
  62. ** 1 all directories already exist
  63. ** -1 (and sets errno) error
  64. */
  65. int
  66. mkdirhier(char *path)
  67. {
  68. char src[TAR_MAXPATHLEN], dst[TAR_MAXPATHLEN] = "";
  69. char *dirp, *nextp = src;
  70. int retval = 1;
  71. if (strlcpy(src, path, sizeof(src)) > sizeof(src))
  72. {
  73. errno = ENAMETOOLONG;
  74. return -1;
  75. }
  76. if (path[0] == '/')
  77. strcpy(dst, "/");
  78. while ((dirp = strsep(&nextp, "/")) != NULL)
  79. {
  80. if (*dirp == '\0')
  81. continue;
  82. if (dst[0] != '\0')
  83. strcat(dst, "/");
  84. strcat(dst, dirp);
  85. if (
  86. #if defined(_WIN32) && !defined(__CYGWIN__)
  87. mkdir(dst) == -1
  88. #else
  89. mkdir(dst, 0777) == -1
  90. #endif
  91. )
  92. {
  93. #ifdef __BORLANDC__
  94. /* There is a bug in the Borland Run time library which makes MKDIR
  95. return EACCES when it should return EEXIST
  96. if it is some other error besides directory exists
  97. then return false */
  98. if ( errno == EACCES)
  99. {
  100. errno = EEXIST;
  101. }
  102. #endif
  103. if (errno != EEXIST)
  104. return -1;
  105. }
  106. else
  107. retval = 0;
  108. }
  109. return retval;
  110. }
  111. /* calculate header checksum */
  112. int
  113. th_crc_calc(TAR *t)
  114. {
  115. int i, sum = 0;
  116. for (i = 0; i < T_BLOCKSIZE; i++)
  117. sum += ((unsigned char *)(&(t->th_buf)))[i];
  118. for (i = 0; i < 8; i++)
  119. sum += (' ' - (unsigned char)t->th_buf.chksum[i]);
  120. return sum;
  121. }
  122. /* string-octal to integer conversion */
  123. int
  124. oct_to_int(char *oct)
  125. {
  126. int i;
  127. sscanf(oct, "%o", &i);
  128. return i;
  129. }
  130. /* integer to string-octal conversion, no NULL */
  131. void
  132. int_to_oct_nonull(int num, char *oct, size_t octlen)
  133. {
  134. snprintf(oct, octlen, "%*lo", (int)(octlen-1), (unsigned long)num);
  135. oct[octlen - 1] = ' ';
  136. }