Skip to content

Commit cf5afa8

Browse files
committed
add streq and strneq macros
1 parent 5bd8d35 commit cf5afa8

25 files changed

+103
-117
lines changed

include/macro.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939

4040
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
4141

42+
#define streq(a,b) (strcmp((a),(b)) == 0)
43+
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
44+
4245
#endif /*!XBPS_MACRO_H*/

lib/package_alternatives.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ normpath(char *path)
6666
char *seg, *p;
6767

6868
for (p = path, seg = NULL; *p; p++) {
69-
if (strncmp(p, "/../", 4) == 0 || strncmp(p, "/..", 4) == 0) {
69+
if (strneq(p, "/../", 4) || strneq(p, "/..", 4)) {
7070
memmove(seg ? seg : p, p+3, strlen(p+3) + 1);
7171
return normpath(path);
72-
} else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) {
72+
} else if (strneq(p, "/./", 3) || strneq(p, "/.", 3)) {
7373
memmove(p, p+2, strlen(p+2) + 1);
74-
} else if (strncmp(p, "//", 2) == 0 || strncmp(p, "/", 2) == 0) {
74+
} else if (strneq(p, "//", 2)|| strneq(p, "/", 2)) {
7575
memmove(p, p+1, strlen(p+1) + 1);
7676
}
7777
if (*p == '/')
@@ -179,7 +179,7 @@ create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
179179

180180
/* create target directory, necessary for dangling symlinks */
181181
dir = xbps_xasprintf("%s/%s", xhp->rootdir, dir);
182-
if (strcmp(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
182+
if (!streq(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
183183
rv = errno;
184184
xbps_dbg_printf(
185185
"failed to create target dir '%s' for group '%s': %s\n",
@@ -192,7 +192,7 @@ create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
192192
/* create link directory, necessary for dangling symlinks */
193193
p = strdup(linkpath);
194194
dir = dirname(p);
195-
if (strcmp(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
195+
if (!streq(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
196196
rv = errno;
197197
xbps_dbg_printf(
198198
"failed to create symlink dir '%s' for group '%s': %s\n",
@@ -273,7 +273,7 @@ xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname,
273273
keysym = xbps_array_get(allkeys, i);
274274
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
275275

276-
if (group && strcmp(keyname, group))
276+
if (group && !streq(keyname, group))
277277
continue;
278278

279279
array = xbps_dictionary_get(alternatives, keyname);
@@ -282,7 +282,7 @@ xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname,
282282

283283
/* remove symlinks from previous alternative */
284284
xbps_array_get_cstring_nocopy(array, 0, &prevpkgname);
285-
if (prevpkgname && strcmp(pkgname, prevpkgname) != 0) {
285+
if (prevpkgname && !streq(pkgname, prevpkgname)) {
286286
if ((prevpkgd = xbps_pkgdb_get_pkg(xhp, prevpkgname)) &&
287287
(prevpkg_alts = xbps_dictionary_get(prevpkgd, "alternatives")) &&
288288
xbps_dictionary_count(prevpkg_alts)) {
@@ -366,7 +366,7 @@ xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
366366
continue;
367367

368368
xbps_array_get_cstring_nocopy(array, 0, &first);
369-
if (strcmp(pkgname, first) == 0) {
369+
if (streq(pkgname, first)) {
370370
/* this pkg is the current alternative for this group */
371371
current = true;
372372
rv = remove_symlinks(xhp,
@@ -427,7 +427,7 @@ prune_altgroup(struct xbps_handle *xhp, xbps_dictionary_t repod,
427427

428428
/* if using alt group from another package, we won't switch anything */
429429
xbps_array_get_cstring_nocopy(array, 0, &curpkg);
430-
current = (strcmp(pkgname, curpkg) == 0);
430+
current = streq(pkgname, curpkg);
431431

432432
/* actually prune the alt group for the current package */
433433
xbps_remove_string_from_array(array, pkgname);
@@ -508,7 +508,7 @@ remove_obsoletes(struct xbps_handle *xhp, const char *pkgname, const char *pkgve
508508
array2 = xbps_dictionary_get(pkgdb_alts, keyname);
509509
if (array2) {
510510
xbps_array_get_cstring_nocopy(array2, 0, &first);
511-
if (strcmp(pkgname, first) == 0) {
511+
if (streq(pkgname, first)) {
512512
remove_symlinks(xhp, array_repo, keyname);
513513
}
514514
}
@@ -576,7 +576,7 @@ xbps_alternatives_register(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
576576
} else {
577577
if (xbps_match_string_in_array(array, pkgname)) {
578578
xbps_array_get_cstring_nocopy(array, 0, &first);
579-
if (strcmp(pkgname, first)) {
579+
if (!streq(pkgname, first)) {
580580
/* current alternative does not match */
581581
continue;
582582
}

lib/package_config_files.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ xbps_entry_is_a_conf_file(xbps_dictionary_t filesd,
5050
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
5151
d = xbps_array_get(array, i);
5252
xbps_dictionary_get_cstring_nocopy(d, "file", &cffile);
53-
if (strcmp(cffile, entry_pname) == 0)
53+
if (streq(cffile, entry_pname))
5454
return true;
5555
}
5656
return false;
@@ -116,7 +116,7 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
116116
xbps_dictionary_get_cstring_nocopy(obj2,
117117
"file", &cffile);
118118
snprintf(buf, sizeof(buf), ".%s", cffile);
119-
if (strcmp(entry_pname, buf) == 0) {
119+
if (streq(entry_pname, buf)) {
120120
xbps_dictionary_get_cstring_nocopy(obj2, "sha256", &sha256_orig);
121121
break;
122122
}
@@ -139,7 +139,7 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
139139
while ((obj = xbps_object_iterator_next(iter))) {
140140
xbps_dictionary_get_cstring_nocopy(obj, "file", &cffile);
141141
snprintf(buf, sizeof(buf), ".%s", cffile);
142-
if (strcmp(entry_pname, buf)) {
142+
if (!streq(entry_pname, buf)) {
143143
continue;
144144
}
145145
if (!xbps_file_sha256(sha256_cur, sizeof sha256_cur, buf)) {
@@ -162,9 +162,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
162162
*
163163
* Keep file as is (no changes).
164164
*/
165-
if ((strcmp(sha256_orig, sha256_cur) == 0) &&
166-
(strcmp(sha256_orig, sha256_new) == 0) &&
167-
(strcmp(sha256_cur, sha256_new) == 0)) {
165+
if (streq(sha256_orig, sha256_cur) &&
166+
streq(sha256_orig, sha256_new) &&
167+
streq(sha256_cur, sha256_new)) {
168168
xbps_dbg_printf("%s: conf_file %s orig = X, "
169169
"cur = X, new = X\n", pkgver, entry_pname);
170170
rv = 0;
@@ -175,9 +175,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
175175
* Install new file (installed file hasn't been modified) if
176176
* configuration option keepconfig is NOT set.
177177
*/
178-
} else if ((strcmp(sha256_orig, sha256_cur) == 0) &&
179-
(strcmp(sha256_orig, sha256_new)) &&
180-
(strcmp(sha256_cur, sha256_new)) &&
178+
} else if (streq(sha256_orig, sha256_cur) &&
179+
!streq(sha256_orig, sha256_new) &&
180+
!streq(sha256_cur, sha256_new) &&
181181
(!(xhp->flags & XBPS_FLAG_KEEP_CONFIG))) {
182182
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
183183
0, pkgver,
@@ -192,9 +192,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
192192
* but new package doesn't contain new changes compared
193193
* to the original version.
194194
*/
195-
} else if ((strcmp(sha256_orig, sha256_new) == 0) &&
196-
(strcmp(sha256_cur, sha256_new)) &&
197-
(strcmp(sha256_orig, sha256_cur))) {
195+
} else if (streq(sha256_orig, sha256_new) &&
196+
!streq(sha256_cur, sha256_new) &&
197+
!streq(sha256_orig, sha256_cur)) {
198198
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
199199
0, pkgver,
200200
"Keeping modified configuration file `%s'.",
@@ -207,9 +207,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
207207
* Keep file as is because changes made are compatible
208208
* with new version.
209209
*/
210-
} else if ((strcmp(sha256_cur, sha256_new) == 0) &&
211-
(strcmp(sha256_orig, sha256_new)) &&
212-
(strcmp(sha256_orig, sha256_cur))) {
210+
} else if (streq(sha256_cur, sha256_new) &&
211+
!streq(sha256_orig, sha256_new) &&
212+
!streq(sha256_orig, sha256_cur)) {
213213
xbps_dbg_printf("%s: conf_file %s orig = X, "
214214
"cur = Y, new = Y\n", pkgver, entry_pname);
215215
rv = 0;
@@ -221,9 +221,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
221221
*
222222
* Install new file as <file>.new-<version>
223223
*/
224-
} else if (((strcmp(sha256_orig, sha256_cur)) &&
225-
(strcmp(sha256_cur, sha256_new)) &&
226-
(strcmp(sha256_orig, sha256_new))) ||
224+
} else if ((!streq(sha256_orig, sha256_cur) &&
225+
!streq(sha256_cur, sha256_new) &&
226+
!streq(sha256_orig, sha256_new)) ||
227227
(xhp->flags & XBPS_FLAG_KEEP_CONFIG)) {
228228
version = xbps_pkg_version(pkgver);
229229
assert(version);

lib/package_msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ xbps_cb_message(struct xbps_handle *xhp, xbps_dictionary_t pkgd, const char *key
6161
buf[len] = '\0';
6262

6363
/* notify client to show the post-install message */
64-
if (strcmp(key, "install-msg") == 0)
64+
if (streq(key, "install-msg"))
6565
xbps_set_cb_state(xhp, XBPS_STATE_SHOW_INSTALL_MSG, 0, pkgver, "%s", buf);
6666
else
6767
xbps_set_cb_state(xhp, XBPS_STATE_SHOW_REMOVE_MSG, 0, pkgver, "%s", buf);

lib/package_script.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ xbps_pkg_exec_buffer(struct xbps_handle *xhp,
6262
return 0;
6363
}
6464

65-
if (strcmp(xhp->rootdir, "/") == 0) {
65+
if (streq(xhp->rootdir, "/")) {
6666
tmpdir = getenv("TMPDIR");
6767
if (tmpdir == NULL)
6868
tmpdir = P_tmpdir;

lib/package_state.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ get_state(xbps_dictionary_t dict)
8585
return 0;
8686

8787
for (stp = states; stp->string != NULL; stp++)
88-
if (strcmp(state_str, stp->string) == 0)
88+
if (streq(state_str, stp->string))
8989
break;
9090

9191
return stp->number;

lib/package_unpack.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ unpack_archive(struct xbps_handle *xhp,
160160
entry_pname = archive_entry_pathname(entry);
161161
entry_size = archive_entry_size(entry);
162162

163-
if (strcmp("./INSTALL", entry_pname) == 0 ||
164-
strcmp("./REMOVE", entry_pname) == 0 ||
165-
strcmp("./props.plist", entry_pname) == 0) {
163+
if (streq("./INSTALL", entry_pname) ||
164+
streq("./REMOVE", entry_pname) ||
165+
streq("./props.plist", entry_pname)) {
166166
archive_read_data_skip(ar);
167-
} else if (strcmp("./files.plist", entry_pname) == 0) {
167+
} else if (streq("./files.plist", entry_pname)) {
168168
binpkg_filesd = xbps_archive_get_dictionary(ar, entry);
169169
if (binpkg_filesd == NULL) {
170170
rv = EINVAL;

lib/pkgdb_conversion.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pkgdb038(struct xbps_handle *xhp, const char *opkgdb_plist)
118118
key = xbps_dictionary_keysym_cstring_nocopy(obj2);
119119
curobj = xbps_dictionary_get_keysym(pkgmetad, obj2);
120120
for (uint8_t i = 0; i < ARRAY_SIZE(excluded); i++) {
121-
if (strcmp(excluded[i], key) == 0) {
121+
if (streq(excluded[i], key)) {
122122
skip = true;
123123
break;
124124
}

lib/plist.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ array_foreach_thread(void *arg)
7373
pkgd = xbps_dictionary_get_keysym(thd->dict, obj);
7474
key = xbps_dictionary_keysym_cstring_nocopy(obj);
7575
/* ignore internal objs */
76-
if (strncmp(key, "_XBPS_", 6) == 0)
76+
if (strneq(key, "_XBPS_", 6))
7777
continue;
7878
} else {
7979
pkgd = obj;
@@ -185,7 +185,7 @@ xbps_array_foreach_cb(struct xbps_handle *xhp,
185185
pkgd = xbps_dictionary_get_keysym(dict, obj);
186186
key = xbps_dictionary_keysym_cstring_nocopy(obj);
187187
/* ignore internal objs */
188-
if (strncmp(key, "_XBPS_", 6) == 0)
188+
if (strneq(key, "_XBPS_", 6))
189189
continue;
190190
} else {
191191
pkgd = obj;
@@ -247,7 +247,7 @@ array_replace_dict(xbps_array_t array,
247247
} else {
248248
/* pkgname match */
249249
xbps_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
250-
if (strcmp(pkgname, str) == 0) {
250+
if (streq(pkgname, str)) {
251251
if (!xbps_array_set(array, i, dict)) {
252252
return EINVAL;
253253
}

lib/plist_fetch.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ xbps_archive_fetch_file(const char *url, const char *fname)
167167
if (bfile[0] == '.')
168168
bfile++; /* skip first dot */
169169

170-
if (strcmp(bfile, fname) == 0) {
170+
if (streq(bfile, fname)) {
171171
buf = xbps_archive_get_file(a, entry);
172172
break;
173173
}
@@ -199,12 +199,12 @@ xbps_repo_fetch_remote(struct xbps_repo *repo, const char *url)
199199
if (bfile[0] == '.')
200200
bfile++; /* skip first dot */
201201

202-
if (strcmp(bfile, XBPS_REPOIDX_META) == 0) {
202+
if (streq(bfile, XBPS_REPOIDX_META)) {
203203
buf = xbps_archive_get_file(a, entry);
204204
repo->idxmeta = xbps_dictionary_internalize(buf);
205205
free(buf);
206206
i++;
207-
} else if (strcmp(bfile, XBPS_REPOIDX) == 0) {
207+
} else if (streq(bfile, XBPS_REPOIDX)) {
208208
buf = xbps_archive_get_file(a, entry);
209209
repo->idx = xbps_dictionary_internalize(buf);
210210
free(buf);
@@ -265,7 +265,7 @@ xbps_archive_fetch_file_into_fd(const char *url, const char *fname, int fd)
265265
if (bfile[0] == '.')
266266
bfile++; /* skip first dot */
267267

268-
if (strcmp(bfile, fname) == 0) {
268+
if (streq(bfile, fname)) {
269269
rv = archive_read_data_into_fd(a, fd);
270270
if (rv != ARCHIVE_OK)
271271
rv = archive_errno(a);

0 commit comments

Comments
 (0)