aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-03-11 19:40:20 +0000
committerRon Yorston <rmy@pobox.com>2014-03-11 19:40:20 +0000
commitdec20ad781163656832c571d52d0303c67ea8c09 (patch)
tree830a27bc8f2383d4ba50680f5d6fac1ec5c96b6a /libbb
parent0a2a7b55a9212d8fc708fd564407e5505b5e8363 (diff)
parent69f9567de28976cfbc7b216c46aa391ce82bd3b7 (diff)
downloadbusybox-w32-dec20ad781163656832c571d52d0303c67ea8c09.tar.gz
busybox-w32-dec20ad781163656832c571d52d0303c67ea8c09.tar.bz2
busybox-w32-dec20ad781163656832c571d52d0303c67ea8c09.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'libbb')
-rw-r--r--libbb/appletlib.c6
-rw-r--r--libbb/inode_hash.c36
-rw-r--r--libbb/loop.c18
-rw-r--r--libbb/pw_encrypt.c9
-rw-r--r--libbb/replace.c45
5 files changed, 96 insertions, 18 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 44161a219..14b21f827 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -608,7 +608,11 @@ static void install_links(const char *busybox, int use_symbolic_links,
608 } 608 }
609} 609}
610# else 610# else
611# define install_links(x,y,z) ((void)0) 611static void install_links(const char *busybox UNUSED_PARAM,
612 int use_symbolic_links UNUSED_PARAM,
613 char *custom_install_dir UNUSED_PARAM)
614{
615}
612# endif 616# endif
613 617
614/* If we were called as "busybox..." */ 618/* If we were called as "busybox..." */
diff --git a/libbb/inode_hash.c b/libbb/inode_hash.c
index 715535ef5..f11c2afb2 100644
--- a/libbb/inode_hash.c
+++ b/libbb/inode_hash.c
@@ -11,14 +11,23 @@
11#include "libbb.h" 11#include "libbb.h"
12 12
13typedef struct ino_dev_hash_bucket_struct { 13typedef struct ino_dev_hash_bucket_struct {
14 struct ino_dev_hash_bucket_struct *next;
15 ino_t ino; 14 ino_t ino;
16 dev_t dev; 15 dev_t dev;
16 /*
17 * Above fields can be 64-bit, while pointer may be 32-bit.
18 * Putting "next" field here may reduce size of this struct:
19 */
20 struct ino_dev_hash_bucket_struct *next;
21 /*
22 * Reportedly, on cramfs a file and a dir can have same ino.
23 * Need to also remember "file/dir" bit:
24 */
25 char isdir; /* bool */
17 char name[1]; 26 char name[1];
18} ino_dev_hashtable_bucket_t; 27} ino_dev_hashtable_bucket_t;
19 28
20#define HASH_SIZE 311 /* Should be prime */ 29#define HASH_SIZE 311u /* Should be prime */
21#define hash_inode(i) ((i) % HASH_SIZE) 30#define hash_inode(i) ((unsigned)(i) % HASH_SIZE)
22 31
23/* array of [HASH_SIZE] elements */ 32/* array of [HASH_SIZE] elements */
24static ino_dev_hashtable_bucket_t **ino_dev_hashtable; 33static ino_dev_hashtable_bucket_t **ino_dev_hashtable;
@@ -38,6 +47,7 @@ char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf)
38 while (bucket != NULL) { 47 while (bucket != NULL) {
39 if ((bucket->ino == statbuf->st_ino) 48 if ((bucket->ino == statbuf->st_ino)
40 && (bucket->dev == statbuf->st_dev) 49 && (bucket->dev == statbuf->st_dev)
50 && (bucket->isdir == !!S_ISDIR(statbuf->st_mode))
41 ) { 51 ) {
42 return bucket->name; 52 return bucket->name;
43 } 53 }
@@ -52,17 +62,18 @@ void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *
52 int i; 62 int i;
53 ino_dev_hashtable_bucket_t *bucket; 63 ino_dev_hashtable_bucket_t *bucket;
54 64
55 i = hash_inode(statbuf->st_ino);
56 if (!name) 65 if (!name)
57 name = ""; 66 name = "";
58 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name)); 67 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name));
59 bucket->ino = statbuf->st_ino; 68 bucket->ino = statbuf->st_ino;
60 bucket->dev = statbuf->st_dev; 69 bucket->dev = statbuf->st_dev;
70 bucket->isdir = !!S_ISDIR(statbuf->st_mode);
61 strcpy(bucket->name, name); 71 strcpy(bucket->name, name);
62 72
63 if (!ino_dev_hashtable) 73 if (!ino_dev_hashtable)
64 ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable)); 74 ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable));
65 75
76 i = hash_inode(statbuf->st_ino);
66 bucket->next = ino_dev_hashtable[i]; 77 bucket->next = ino_dev_hashtable[i];
67 ino_dev_hashtable[i] = bucket; 78 ino_dev_hashtable[i] = bucket;
68} 79}
@@ -72,13 +83,18 @@ void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *
72void FAST_FUNC reset_ino_dev_hashtable(void) 83void FAST_FUNC reset_ino_dev_hashtable(void)
73{ 84{
74 int i; 85 int i;
75 ino_dev_hashtable_bucket_t *bucket; 86 ino_dev_hashtable_bucket_t *bucket, *next;
87
88 if (!ino_dev_hashtable)
89 return;
90
91 for (i = 0; i < HASH_SIZE; i++) {
92 bucket = ino_dev_hashtable[i];
76 93
77 for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) { 94 while (bucket != NULL) {
78 while (ino_dev_hashtable[i] != NULL) { 95 next = bucket->next;
79 bucket = ino_dev_hashtable[i]->next; 96 free(bucket);
80 free(ino_dev_hashtable[i]); 97 bucket = next;
81 ino_dev_hashtable[i] = bucket;
82 } 98 }
83 } 99 }
84 free(ino_dev_hashtable); 100 free(ino_dev_hashtable);
diff --git a/libbb/loop.c b/libbb/loop.c
index 823fba079..c96c5e070 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -94,19 +94,19 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
94 94
95 /* Open the file. Barf if this doesn't work. */ 95 /* Open the file. Barf if this doesn't work. */
96 mode = ro ? O_RDONLY : O_RDWR; 96 mode = ro ? O_RDONLY : O_RDWR;
97 open_ffd:
97 ffd = open(file, mode); 98 ffd = open(file, mode);
98 if (ffd < 0) { 99 if (ffd < 0) {
99 if (mode != O_RDONLY) { 100 if (mode != O_RDONLY) {
100 mode = O_RDONLY; 101 mode = O_RDONLY;
101 ffd = open(file, mode); 102 goto open_ffd;
102 } 103 }
103 if (ffd < 0) 104 return -errno;
104 return -errno;
105 } 105 }
106 106
107 /* Find a loop device. */ 107 /* Find a loop device. */
108 try = *device ? *device : dev; 108 try = *device ? *device : dev;
109 /* 1048575 is a max possible minor number in Linux circa 2010 */ 109 /* 1048575 (0xfffff) is a max possible minor number in Linux circa 2010 */
110 for (i = 0; rc && i < 1048576; i++) { 110 for (i = 0; rc && i < 1048576; i++) {
111 sprintf(dev, LOOP_FORMAT, i); 111 sprintf(dev, LOOP_FORMAT, i);
112 112
@@ -121,7 +121,7 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
121 goto try_to_open; 121 goto try_to_open;
122 } 122 }
123 /* Ran out of block devices, return failure. */ 123 /* Ran out of block devices, return failure. */
124 rc = -ENOENT; 124 rc = -1;
125 break; 125 break;
126 } 126 }
127 try_to_open: 127 try_to_open:
@@ -131,8 +131,14 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
131 mode = O_RDONLY; 131 mode = O_RDONLY;
132 dfd = open(try, mode); 132 dfd = open(try, mode);
133 } 133 }
134 if (dfd < 0) 134 if (dfd < 0) {
135 if (errno == ENXIO) {
136 /* Happens if loop module is not loaded */
137 rc = -1;
138 break;
139 }
135 goto try_again; 140 goto try_again;
141 }
136 142
137 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo); 143 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
138 144
diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c
index 39ffa084f..bfc7030a8 100644
--- a/libbb/pw_encrypt.c
+++ b/libbb/pw_encrypt.c
@@ -142,7 +142,14 @@ char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
142 142
143char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup) 143char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
144{ 144{
145 return xstrdup(crypt(clear, salt)); 145 char *s;
146
147 s = crypt(clear, salt);
148 /*
149 * glibc used to return "" on malformed salts (for example, ""),
150 * but since 2.17 it returns NULL.
151 */
152 return xstrdup(s ? s : "");
146} 153}
147 154
148#endif 155#endif
diff --git a/libbb/replace.c b/libbb/replace.c
new file mode 100644
index 000000000..8711f957d
--- /dev/null
+++ b/libbb/replace.c
@@ -0,0 +1,45 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9
10//kbuild:lib-y += replace.o
11
12#include "libbb.h"
13
14unsigned FAST_FUNC count_strstr(const char *str, const char *sub)
15{
16 size_t sub_len = strlen(sub);
17 unsigned count = 0;
18
19 while ((str = strstr(str, sub)) != NULL) {
20 count++;
21 str += sub_len;
22 }
23 return count;
24}
25
26char* FAST_FUNC xmalloc_substitute_string(const char *src, int count, const char *sub, const char *repl)
27{
28 char *buf, *dst, *end;
29 size_t sub_len = strlen(sub);
30 size_t repl_len = strlen(repl);
31
32 //dbg_msg("subst(s:'%s',count:%d,sub:'%s',repl:'%s'", src, count, sub, repl);
33
34 buf = dst = xmalloc(strlen(src) + count * ((int)repl_len - (int)sub_len) + 1);
35 /* we replace each sub with repl */
36 while ((end = strstr(src, sub)) != NULL) {
37 dst = mempcpy(dst, src, end - src);
38 dst = mempcpy(dst, repl, repl_len);
39 /*src = end + 1; - GNU findutils 4.5.10 doesn't do this... */
40 src = end + sub_len; /* but this. Try "xargs -Iaa echo aaa" */
41 }
42 strcpy(dst, src);
43 //dbg_msg("subst9:'%s'", buf);
44 return buf;
45}