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 # ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
31 /* hashing function for pathnames */
33 path_hashfunc(char *key
, int numbuckets
)
35 char buf
[TAR_MAXPATHLEN
];
41 return (((unsigned int)p
[0]) % numbuckets
);
45 /* matching function for dev_t's */
47 dev_match(dev_t
*dev1
, dev_t
*dev2
)
49 return !memcmp(dev1
, dev2
, sizeof(dev_t
));
53 /* matching function for ino_t's */
55 ino_match(ino_t
*ino1
, ino_t
*ino2
)
57 return !memcmp(ino1
, ino2
, sizeof(ino_t
));
61 /* hashing function for dev_t's */
69 /* hashing function for ino_t's */
71 ino_hash(ino_t
*inode
)
78 ** mkdirhier() - create all directories in a given path
81 ** 1 all directories already exist
82 ** -1 (and sets errno) error
87 char src
[TAR_MAXPATHLEN
], dst
[TAR_MAXPATHLEN
] = "";
88 char *dirp
, *nextp
= src
;
91 if (strlcpy(src
, path
, sizeof(src
)) > sizeof(src
))
100 while ((dirp
= strsep(&nextp
, "/")) != NULL
)
106 * Don't try to build current or parent dir. It doesn't make sense anyhow,
107 * but it also returns EINVAL instead of EEXIST on BeOS!
109 if ((strcmp(dirp
, ".") == 0) || (strcmp(dirp
, "..") == 0))
117 /* On some Windows machine, trying to mkdir("C:") would fail miserably */
118 if (dst
[strlen(dst
) - 1] == ':')
123 #if defined(_WIN32) && !defined(__CYGWIN__)
126 mkdir(dst
, 0777) == -1
131 /* There is a bug in the Borland Run time library which makes MKDIR
132 return EACCES when it should return EEXIST
133 if it is some other error besides directory exists
135 if ( errno
== EACCES
)
151 /* calculate header checksum */
157 for (i
= 0; i
< T_BLOCKSIZE
; i
++)
158 sum
+= ((unsigned char *)(&(t
->th_buf
)))[i
];
159 for (i
= 0; i
< 8; i
++)
160 sum
+= (' ' - (unsigned char)t
->th_buf
.chksum
[i
]);
166 /* string-octal to integer conversion */
168 oct_to_int(char *oct
)
172 sscanf(oct
, "%o", &i
);
178 /* integer to string-octal conversion, no NULL */
180 int_to_oct_nonull(int num
, char *oct
, size_t octlen
)
182 snprintf(oct
, octlen
, "%*lo", (int)(octlen
-1), (unsigned long)num
);
183 oct
[octlen
- 1] = ' ';