BUG: remove warning in test of compiler so -Werror does not fail
[cmake.git] / Utilities / cmtar / util.c
blob0e23a051ebf257a02e9bd139a89995562129b498
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 # ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
28 # endif
29 #endif
31 /* hashing function for pathnames */
32 int
33 path_hashfunc(char *key, int numbuckets)
35 char buf[TAR_MAXPATHLEN];
36 char *p;
38 strcpy(buf, key);
39 p = basename(buf);
41 return (((unsigned int)p[0]) % numbuckets);
45 /* matching function for dev_t's */
46 int
47 dev_match(dev_t *dev1, dev_t *dev2)
49 return !memcmp(dev1, dev2, sizeof(dev_t));
53 /* matching function for ino_t's */
54 int
55 ino_match(ino_t *ino1, ino_t *ino2)
57 return !memcmp(ino1, ino2, sizeof(ino_t));
61 /* hashing function for dev_t's */
62 int
63 dev_hash(dev_t *dev)
65 return *dev % 16;
69 /* hashing function for ino_t's */
70 int
71 ino_hash(ino_t *inode)
73 return *inode % 256;
78 ** mkdirhier() - create all directories in a given path
79 ** returns:
80 ** 0 success
81 ** 1 all directories already exist
82 ** -1 (and sets errno) error
84 int
85 mkdirhier(char *path)
87 char src[TAR_MAXPATHLEN], dst[TAR_MAXPATHLEN] = "";
88 char *dirp, *nextp = src;
89 int retval = 1;
91 if (strlcpy(src, path, sizeof(src)) > sizeof(src))
93 errno = ENAMETOOLONG;
94 return -1;
97 if (path[0] == '/')
98 strcpy(dst, "/");
100 while ((dirp = strsep(&nextp, "/")) != NULL)
102 if (*dirp == '\0')
103 continue;
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))
110 continue;
112 if (dst[0] != '\0')
113 strcat(dst, "/");
114 strcat(dst, dirp);
116 #if defined(_WIN32)
117 /* On some Windows machine, trying to mkdir("C:") would fail miserably */
118 if (dst[strlen(dst) - 1] == ':')
119 continue;
120 #endif
122 if (
123 #if defined(_WIN32) && !defined(__CYGWIN__)
124 mkdir(dst) == -1
125 #else
126 mkdir(dst, 0777) == -1
127 #endif
130 #ifdef __BORLANDC__
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
134 then return false */
135 if ( errno == EACCES)
137 errno = EEXIST;
139 #endif
140 if (errno != EEXIST)
141 return -1;
143 else
144 retval = 0;
147 return retval;
151 /* calculate header checksum */
153 th_crc_calc(TAR *t)
155 int i, sum = 0;
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]);
162 return sum;
166 /* string-octal to integer conversion */
168 oct_to_int(char *oct)
170 int i;
172 sscanf(oct, "%o", &i);
174 return i;
178 /* integer to string-octal conversion, no NULL */
179 void
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] = ' ';