Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmtar / util.c
blob31f7fc87884e3b80d39b5b3f5cad53e324d2d818
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 <roth@uiuc.edu>
9 ** Campus Information Technologies and Educational Services
10 ** University of Illinois at Urbana-Champaign
13 #include <libtarint/internal.h>
15 #include <stdio.h>
16 #include <libtar/compat.h>
17 #include <errno.h>
19 #ifdef STDC_HEADERS
20 # include <string.h>
21 #endif
23 #if defined(_WIN32) && !defined(__CYGWIN__)
24 #include <direct.h>
25 #else
26 #include <sys/param.h>
27 #endif
29 /* hashing function for pathnames */
30 int
31 path_hashfunc(char *key, int numbuckets)
33 char buf[TAR_MAXPATHLEN];
34 char *p;
36 strcpy(buf, key);
37 p = basename(buf);
39 return (((unsigned int)p[0]) % numbuckets);
43 /* matching function for dev_t's */
44 int
45 dev_match(dev_t *dev1, dev_t *dev2)
47 return !memcmp(dev1, dev2, sizeof(dev_t));
51 /* matching function for ino_t's */
52 int
53 ino_match(ino_t *ino1, ino_t *ino2)
55 return !memcmp(ino1, ino2, sizeof(ino_t));
59 /* hashing function for dev_t's */
60 int
61 dev_hash(dev_t *dev)
63 return *dev % 16;
67 /* hashing function for ino_t's */
68 int
69 ino_hash(ino_t *inode)
71 return *inode % 256;
76 ** mkdirhier() - create all directories in a given path
77 ** returns:
78 ** 0 success
79 ** 1 all directories already exist
80 ** -1 (and sets errno) error
82 int
83 mkdirhier(char *path)
85 char src[TAR_MAXPATHLEN], dst[TAR_MAXPATHLEN] = "";
86 char *dirp, *nextp = src;
87 int retval = 1;
89 if (strlcpy(src, path, sizeof(src)) > sizeof(src))
91 errno = ENAMETOOLONG;
92 return -1;
95 if (path[0] == '/')
96 strcpy(dst, "/");
98 while ((dirp = strsep(&nextp, "/")) != NULL)
100 if (*dirp == '\0')
101 continue;
104 * Don't try to build current or parent dir. It doesn't make sense anyhow,
105 * but it also returns EINVAL instead of EEXIST on BeOS!
107 if ((strcmp(dirp, ".") == 0) || (strcmp(dirp, "..") == 0))
108 continue;
110 if (dst[0] != '\0')
111 strcat(dst, "/");
112 strcat(dst, dirp);
114 #if defined(_WIN32)
115 /* On some Windows machine, trying to mkdir("C:") would fail miserably */
116 if (dst[strlen(dst) - 1] == ':')
117 continue;
118 #endif
120 if (
121 #if defined(_WIN32) && !defined(__CYGWIN__)
122 mkdir(dst) == -1
123 #else
124 mkdir(dst, 0777) == -1
125 #endif
128 #ifdef __BORLANDC__
129 /* There is a bug in the Borland Run time library which makes MKDIR
130 return EACCES when it should return EEXIST
131 if it is some other error besides directory exists
132 then return false */
133 if ( errno == EACCES)
135 errno = EEXIST;
137 #endif
138 if (errno != EEXIST)
139 return -1;
141 else
142 retval = 0;
145 return retval;
149 /* calculate header checksum */
151 th_crc_calc(TAR *t)
153 int i, sum = 0;
155 for (i = 0; i < T_BLOCKSIZE; i++)
156 sum += ((unsigned char *)(&(t->th_buf)))[i];
157 for (i = 0; i < 8; i++)
158 sum += (' ' - (unsigned char)t->th_buf.chksum[i]);
160 return sum;
164 /* string-octal to integer conversion */
166 oct_to_int(char *oct)
168 int i;
170 sscanf(oct, "%o", &i);
172 return i;
176 /* integer to string-octal conversion, no NULL */
177 void
178 int_to_oct_nonull(int num, char *oct, size_t octlen)
180 snprintf(oct, octlen, "%*lo", (int)(octlen-1), (unsigned long)num);
181 oct[octlen - 1] = ' ';