blob: 31e831507ec4d26c77791ebaeb715f58575f1153 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
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
11*/
12
13#include <internal.h>
14
15#include <stdio.h>
16#include <sys/param.h>
17#include <errno.h>
18
19#ifdef STDC_HEADERS
20# include <string.h>
21#endif
22
23
24/* hashing function for pathnames */
25int
26path_hashfunc(char *key, int numbuckets)
27{
28 char buf[MAXPATHLEN];
29 char *p;
30
31 strcpy(buf, key);
32 p = basename(buf);
33
34 return (((unsigned int)p[0]) % numbuckets);
35}
36
37
38/* matching function for dev_t's */
39int
40dev_match(dev_t *dev1, dev_t *dev2)
41{
42 return !memcmp(dev1, dev2, sizeof(dev_t));
43}
44
45
46/* matching function for ino_t's */
47int
48ino_match(ino_t *ino1, ino_t *ino2)
49{
50 return !memcmp(ino1, ino2, sizeof(ino_t));
51}
52
53
54/* hashing function for dev_t's */
55int
56dev_hash(dev_t *dev)
57{
58 return *dev % 16;
59}
60
61
62/* hashing function for ino_t's */
63int
64ino_hash(ino_t *inode)
65{
66 return *inode % 256;
67}
68
69
70/*
71** mkdirhier() - create all directories in a given path
72** returns:
73** 0 success
74** 1 all directories already exist
75** -1 (and sets errno) error
76*/
77int
78mkdirhier(char *path)
79{
80 char src[MAXPATHLEN], dst[MAXPATHLEN] = "";
81 char *dirp, *nextp = src;
82 int retval = 1;
83
84 if (strlcpy(src, path, sizeof(src)) > sizeof(src))
85 {
86 errno = ENAMETOOLONG;
87 return -1;
88 }
89
90 if (path[0] == '/')
91 strcpy(dst, "/");
92
93 while ((dirp = strsep(&nextp, "/")) != NULL)
94 {
95 if (*dirp == '\0')
96 continue;
97
98 if (dst[0] != '\0')
99 strcat(dst, "/");
100 strcat(dst, dirp);
101
102 if (mkdir(dst, 0777) == -1)
103 {
104 if (errno != EEXIST)
105 return -1;
106 }
107 else
108 retval = 0;
109 }
110
111 return retval;
112}
113
114
115/* calculate header checksum */
116int
117th_crc_calc(TAR *t)
118{
119 int i, sum = 0;
120
121 for (i = 0; i < T_BLOCKSIZE; i++)
122 sum += ((unsigned char *)(&(t->th_buf)))[i];
123 for (i = 0; i < 8; i++)
124 sum += (' ' - (unsigned char)t->th_buf.chksum[i]);
125
126 return sum;
127}
128
129
130/* calculate a signed header checksum */
131int
132th_signed_crc_calc(TAR *t)
133{
134 int i, sum = 0;
135
136 for (i = 0; i < T_BLOCKSIZE; i++)
137 sum += ((signed char *)(&(t->th_buf)))[i];
138 for (i = 0; i < 8; i++)
139 sum += (' ' - (signed char)t->th_buf.chksum[i]);
140
141 return sum;
142}
143
144
145/* string-octal to integer conversion */
146int
147oct_to_int(char *oct)
148{
149 int i;
150
151 sscanf(oct, "%o", &i);
152
153 return i;
154}
155
156
157/* integer to string-octal conversion, no NULL */
158void
159int_to_oct_nonull(int num, char *oct, size_t octlen)
160{
161 snprintf(oct, octlen, "%*lo", octlen - 1, (unsigned long)num);
162 oct[octlen - 1] = ' ';
163}
164
165