2 ** Copyright 1998-2003 University of Illinois Board of Trustees
3 ** Copyright 1998-2003 Mark D. Roth
4 ** All rights reserved.
6 ** util.c - miscellaneous utility code for libtar
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>
16 #include <libtar/compat.h>
23 #if defined(_WIN32) && !defined(__CYGWIN__)
26 #include <sys/param.h>
29 /* hashing function for pathnames */
31 path_hashfunc(char *key
, int numbuckets
)
33 char buf
[TAR_MAXPATHLEN
];
39 return (((unsigned int)p
[0]) % numbuckets
);
43 /* matching function for dev_t's */
45 dev_match(dev_t
*dev1
, dev_t
*dev2
)
47 return !memcmp(dev1
, dev2
, sizeof(dev_t
));
51 /* matching function for ino_t's */
53 ino_match(ino_t
*ino1
, ino_t
*ino2
)
55 return !memcmp(ino1
, ino2
, sizeof(ino_t
));
59 /* hashing function for dev_t's */
67 /* hashing function for ino_t's */
69 ino_hash(ino_t
*inode
)
76 ** mkdirhier() - create all directories in a given path
79 ** 1 all directories already exist
80 ** -1 (and sets errno) error
85 char src
[TAR_MAXPATHLEN
], dst
[TAR_MAXPATHLEN
] = "";
86 char *dirp
, *nextp
= src
;
89 if (strlcpy(src
, path
, sizeof(src
)) > sizeof(src
))
98 while ((dirp
= strsep(&nextp
, "/")) != NULL
)
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))
115 /* On some Windows machine, trying to mkdir("C:") would fail miserably */
116 if (dst
[strlen(dst
) - 1] == ':')
121 #if defined(_WIN32) && !defined(__CYGWIN__)
124 mkdir(dst
, 0777) == -1
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
133 if ( errno
== EACCES
)
149 /* calculate header checksum */
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
]);
164 /* string-octal to integer conversion */
166 oct_to_int(char *oct
)
170 sscanf(oct
, "%o", &i
);
176 /* integer to string-octal conversion, no NULL */
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] = ' ';