Skip to content

Commit

Permalink
add streq and strneq macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncaen committed Sep 1, 2023
1 parent 5bd8d35 commit cf5afa8
Show file tree
Hide file tree
Showing 25 changed files with 103 additions and 117 deletions.
3 changes: 3 additions & 0 deletions include/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@

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

#define streq(a,b) (strcmp((a),(b)) == 0)
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)

#endif /*!XBPS_MACRO_H*/
22 changes: 11 additions & 11 deletions lib/package_alternatives.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ normpath(char *path)
char *seg, *p;

for (p = path, seg = NULL; *p; p++) {
if (strncmp(p, "/../", 4) == 0 || strncmp(p, "/..", 4) == 0) {
if (strneq(p, "/../", 4) || strneq(p, "/..", 4)) {
memmove(seg ? seg : p, p+3, strlen(p+3) + 1);
return normpath(path);
} else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) {
} else if (strneq(p, "/./", 3) || strneq(p, "/.", 3)) {
memmove(p, p+2, strlen(p+2) + 1);
} else if (strncmp(p, "//", 2) == 0 || strncmp(p, "/", 2) == 0) {
} else if (strneq(p, "//", 2)|| strneq(p, "/", 2)) {
memmove(p, p+1, strlen(p+1) + 1);
}
if (*p == '/')
Expand Down Expand Up @@ -179,7 +179,7 @@ create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)

/* create target directory, necessary for dangling symlinks */
dir = xbps_xasprintf("%s/%s", xhp->rootdir, dir);
if (strcmp(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
if (!streq(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
rv = errno;
xbps_dbg_printf(
"failed to create target dir '%s' for group '%s': %s\n",
Expand All @@ -192,7 +192,7 @@ create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
/* create link directory, necessary for dangling symlinks */
p = strdup(linkpath);
dir = dirname(p);
if (strcmp(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
if (!streq(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
rv = errno;
xbps_dbg_printf(
"failed to create symlink dir '%s' for group '%s': %s\n",
Expand Down Expand Up @@ -273,7 +273,7 @@ xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname,
keysym = xbps_array_get(allkeys, i);
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);

if (group && strcmp(keyname, group))
if (group && !streq(keyname, group))
continue;

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

/* remove symlinks from previous alternative */
xbps_array_get_cstring_nocopy(array, 0, &prevpkgname);
if (prevpkgname && strcmp(pkgname, prevpkgname) != 0) {
if (prevpkgname && !streq(pkgname, prevpkgname)) {
if ((prevpkgd = xbps_pkgdb_get_pkg(xhp, prevpkgname)) &&
(prevpkg_alts = xbps_dictionary_get(prevpkgd, "alternatives")) &&
xbps_dictionary_count(prevpkg_alts)) {
Expand Down Expand Up @@ -366,7 +366,7 @@ xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
continue;

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

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

/* actually prune the alt group for the current package */
xbps_remove_string_from_array(array, pkgname);
Expand Down Expand Up @@ -508,7 +508,7 @@ remove_obsoletes(struct xbps_handle *xhp, const char *pkgname, const char *pkgve
array2 = xbps_dictionary_get(pkgdb_alts, keyname);
if (array2) {
xbps_array_get_cstring_nocopy(array2, 0, &first);
if (strcmp(pkgname, first) == 0) {
if (streq(pkgname, first)) {
remove_symlinks(xhp, array_repo, keyname);
}
}
Expand Down Expand Up @@ -576,7 +576,7 @@ xbps_alternatives_register(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
} else {
if (xbps_match_string_in_array(array, pkgname)) {
xbps_array_get_cstring_nocopy(array, 0, &first);
if (strcmp(pkgname, first)) {
if (!streq(pkgname, first)) {
/* current alternative does not match */
continue;
}
Expand Down
36 changes: 18 additions & 18 deletions lib/package_config_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ xbps_entry_is_a_conf_file(xbps_dictionary_t filesd,
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
d = xbps_array_get(array, i);
xbps_dictionary_get_cstring_nocopy(d, "file", &cffile);
if (strcmp(cffile, entry_pname) == 0)
if (streq(cffile, entry_pname))
return true;
}
return false;
Expand Down Expand Up @@ -116,7 +116,7 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
xbps_dictionary_get_cstring_nocopy(obj2,
"file", &cffile);
snprintf(buf, sizeof(buf), ".%s", cffile);
if (strcmp(entry_pname, buf) == 0) {
if (streq(entry_pname, buf)) {
xbps_dictionary_get_cstring_nocopy(obj2, "sha256", &sha256_orig);
break;
}
Expand All @@ -139,7 +139,7 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
while ((obj = xbps_object_iterator_next(iter))) {
xbps_dictionary_get_cstring_nocopy(obj, "file", &cffile);
snprintf(buf, sizeof(buf), ".%s", cffile);
if (strcmp(entry_pname, buf)) {
if (!streq(entry_pname, buf)) {
continue;
}
if (!xbps_file_sha256(sha256_cur, sizeof sha256_cur, buf)) {
Expand All @@ -162,9 +162,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
*
* Keep file as is (no changes).
*/
if ((strcmp(sha256_orig, sha256_cur) == 0) &&
(strcmp(sha256_orig, sha256_new) == 0) &&
(strcmp(sha256_cur, sha256_new) == 0)) {
if (streq(sha256_orig, sha256_cur) &&
streq(sha256_orig, sha256_new) &&
streq(sha256_cur, sha256_new)) {
xbps_dbg_printf("%s: conf_file %s orig = X, "
"cur = X, new = X\n", pkgver, entry_pname);
rv = 0;
Expand All @@ -175,9 +175,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
* Install new file (installed file hasn't been modified) if
* configuration option keepconfig is NOT set.
*/
} else if ((strcmp(sha256_orig, sha256_cur) == 0) &&
(strcmp(sha256_orig, sha256_new)) &&
(strcmp(sha256_cur, sha256_new)) &&
} else if (streq(sha256_orig, sha256_cur) &&
!streq(sha256_orig, sha256_new) &&
!streq(sha256_cur, sha256_new) &&
(!(xhp->flags & XBPS_FLAG_KEEP_CONFIG))) {
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
0, pkgver,
Expand All @@ -192,9 +192,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
* but new package doesn't contain new changes compared
* to the original version.
*/
} else if ((strcmp(sha256_orig, sha256_new) == 0) &&
(strcmp(sha256_cur, sha256_new)) &&
(strcmp(sha256_orig, sha256_cur))) {
} else if (streq(sha256_orig, sha256_new) &&
!streq(sha256_cur, sha256_new) &&
!streq(sha256_orig, sha256_cur)) {
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
0, pkgver,
"Keeping modified configuration file `%s'.",
Expand All @@ -207,9 +207,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
* Keep file as is because changes made are compatible
* with new version.
*/
} else if ((strcmp(sha256_cur, sha256_new) == 0) &&
(strcmp(sha256_orig, sha256_new)) &&
(strcmp(sha256_orig, sha256_cur))) {
} else if (streq(sha256_cur, sha256_new) &&
!streq(sha256_orig, sha256_new) &&
!streq(sha256_orig, sha256_cur)) {
xbps_dbg_printf("%s: conf_file %s orig = X, "
"cur = Y, new = Y\n", pkgver, entry_pname);
rv = 0;
Expand All @@ -221,9 +221,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
*
* Install new file as <file>.new-<version>
*/
} else if (((strcmp(sha256_orig, sha256_cur)) &&
(strcmp(sha256_cur, sha256_new)) &&
(strcmp(sha256_orig, sha256_new))) ||
} else if ((!streq(sha256_orig, sha256_cur) &&
!streq(sha256_cur, sha256_new) &&
!streq(sha256_orig, sha256_new)) ||
(xhp->flags & XBPS_FLAG_KEEP_CONFIG)) {
version = xbps_pkg_version(pkgver);
assert(version);
Expand Down
2 changes: 1 addition & 1 deletion lib/package_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ xbps_cb_message(struct xbps_handle *xhp, xbps_dictionary_t pkgd, const char *key
buf[len] = '\0';

/* notify client to show the post-install message */
if (strcmp(key, "install-msg") == 0)
if (streq(key, "install-msg"))
xbps_set_cb_state(xhp, XBPS_STATE_SHOW_INSTALL_MSG, 0, pkgver, "%s", buf);
else
xbps_set_cb_state(xhp, XBPS_STATE_SHOW_REMOVE_MSG, 0, pkgver, "%s", buf);
Expand Down
2 changes: 1 addition & 1 deletion lib/package_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ xbps_pkg_exec_buffer(struct xbps_handle *xhp,
return 0;
}

if (strcmp(xhp->rootdir, "/") == 0) {
if (streq(xhp->rootdir, "/")) {
tmpdir = getenv("TMPDIR");
if (tmpdir == NULL)
tmpdir = P_tmpdir;
Expand Down
2 changes: 1 addition & 1 deletion lib/package_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ get_state(xbps_dictionary_t dict)
return 0;

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

return stp->number;
Expand Down
8 changes: 4 additions & 4 deletions lib/package_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ unpack_archive(struct xbps_handle *xhp,
entry_pname = archive_entry_pathname(entry);
entry_size = archive_entry_size(entry);

if (strcmp("./INSTALL", entry_pname) == 0 ||
strcmp("./REMOVE", entry_pname) == 0 ||
strcmp("./props.plist", entry_pname) == 0) {
if (streq("./INSTALL", entry_pname) ||
streq("./REMOVE", entry_pname) ||
streq("./props.plist", entry_pname)) {
archive_read_data_skip(ar);
} else if (strcmp("./files.plist", entry_pname) == 0) {
} else if (streq("./files.plist", entry_pname)) {
binpkg_filesd = xbps_archive_get_dictionary(ar, entry);
if (binpkg_filesd == NULL) {
rv = EINVAL;
Expand Down
2 changes: 1 addition & 1 deletion lib/pkgdb_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pkgdb038(struct xbps_handle *xhp, const char *opkgdb_plist)
key = xbps_dictionary_keysym_cstring_nocopy(obj2);
curobj = xbps_dictionary_get_keysym(pkgmetad, obj2);
for (uint8_t i = 0; i < ARRAY_SIZE(excluded); i++) {
if (strcmp(excluded[i], key) == 0) {
if (streq(excluded[i], key)) {
skip = true;
break;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/plist.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ array_foreach_thread(void *arg)
pkgd = xbps_dictionary_get_keysym(thd->dict, obj);
key = xbps_dictionary_keysym_cstring_nocopy(obj);
/* ignore internal objs */
if (strncmp(key, "_XBPS_", 6) == 0)
if (strneq(key, "_XBPS_", 6))
continue;
} else {
pkgd = obj;
Expand Down Expand Up @@ -185,7 +185,7 @@ xbps_array_foreach_cb(struct xbps_handle *xhp,
pkgd = xbps_dictionary_get_keysym(dict, obj);
key = xbps_dictionary_keysym_cstring_nocopy(obj);
/* ignore internal objs */
if (strncmp(key, "_XBPS_", 6) == 0)
if (strneq(key, "_XBPS_", 6))
continue;
} else {
pkgd = obj;
Expand Down Expand Up @@ -247,7 +247,7 @@ array_replace_dict(xbps_array_t array,
} else {
/* pkgname match */
xbps_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
if (strcmp(pkgname, str) == 0) {
if (streq(pkgname, str)) {
if (!xbps_array_set(array, i, dict)) {
return EINVAL;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/plist_fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ xbps_archive_fetch_file(const char *url, const char *fname)
if (bfile[0] == '.')
bfile++; /* skip first dot */

if (strcmp(bfile, fname) == 0) {
if (streq(bfile, fname)) {
buf = xbps_archive_get_file(a, entry);
break;
}
Expand Down Expand Up @@ -199,12 +199,12 @@ xbps_repo_fetch_remote(struct xbps_repo *repo, const char *url)
if (bfile[0] == '.')
bfile++; /* skip first dot */

if (strcmp(bfile, XBPS_REPOIDX_META) == 0) {
if (streq(bfile, XBPS_REPOIDX_META)) {
buf = xbps_archive_get_file(a, entry);
repo->idxmeta = xbps_dictionary_internalize(buf);
free(buf);
i++;
} else if (strcmp(bfile, XBPS_REPOIDX) == 0) {
} else if (streq(bfile, XBPS_REPOIDX)) {
buf = xbps_archive_get_file(a, entry);
repo->idx = xbps_dictionary_internalize(buf);
free(buf);
Expand Down Expand Up @@ -265,7 +265,7 @@ xbps_archive_fetch_file_into_fd(const char *url, const char *fname, int fd)
if (bfile[0] == '.')
bfile++; /* skip first dot */

if (strcmp(bfile, fname) == 0) {
if (streq(bfile, fname)) {
rv = archive_read_data_into_fd(a, fd);
if (rv != ARCHIVE_OK)
rv = archive_errno(a);
Expand Down
10 changes: 5 additions & 5 deletions lib/plist_find.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ get_pkg_in_array(xbps_array_t array, const char *str, xbps_trans_type_t tt, bool
}
} else if (xbps_pkg_version(str)) {
/* match by exact pkgver */
if (strcmp(str, pkgver) == 0) {
if (streq(str, pkgver)) {
found = true;
break;
}
Expand All @@ -78,7 +78,7 @@ get_pkg_in_array(xbps_array_t array, const char *str, xbps_trans_type_t tt, bool
abort();
}
/* match by pkgname */
if (strcmp(pkgname, str) == 0) {
if (streq(pkgname, str)) {
found = true;
break;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ match_pkg_by_pkgver(xbps_dictionary_t repod, const char *p)
d = xbps_dictionary_get(repod, pkgname);
if (d) {
xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
if (strcmp(pkgver, p)) {
if (!streq(pkgver, p)) {
d = NULL;
errno = ENOENT;
}
Expand Down Expand Up @@ -256,12 +256,12 @@ vpkg_user_conf(struct xbps_handle *xhp, const char *vpkg, bool only_conf)
if (!xbps_pkg_name(buf, sizeof(buf), vpkg)) {
abort();
}
if (strcmp(buf, vpkgname)) {
if (!streq(buf, vpkgname)) {
free(vpkgname);
continue;
}
} else {
if (strcmp(vpkg, vpkgname)) {
if (!streq(vpkg, vpkgname)) {
free(vpkgname);
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plist_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ match_string_in_array(xbps_array_t array, const char *str, int mode)
pkgdep = xbps_string_cstring_nocopy(obj);
if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgdep))
break;
if (strcmp(pkgname, str) == 0) {
if (streq(pkgname, str)) {
found = true;
break;
}
Expand All @@ -134,7 +134,7 @@ match_string_in_array(xbps_array_t array, const char *str, int mode)
pkgdep = xbps_string_cstring_nocopy(obj);
if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, str))
break;
if (strcmp(pkgname, pkgdep) == 0) {
if (streq(pkgname, pkgdep)) {
found = true;
break;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/plist_remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ remove_obj_from_array(xbps_array_t array, const char *str, int mode)
if (!xbps_pkg_name(curpkgname, sizeof(curpkgname), pkgdep))
break;

if (strcmp(curpkgname, str) == 0) {
if (streq(curpkgname, str)) {
found = true;
break;
}
} else if (mode == 2) {
/* match by pkgname, obj is a dictionary */
xbps_dictionary_get_cstring_nocopy(obj,
"pkgname", &curname);
if (strcmp(curname, str) == 0) {
if (streq(curname, str)) {
found = true;
break;
}
} else if (mode == 3) {
/* match by pkgver, obj is a dictionary */
xbps_dictionary_get_cstring_nocopy(obj,
"pkgver", &curname);
if (strcmp(curname, str) == 0) {
if (streq(curname, str)) {
found = true;
break;
}
Expand Down
Loading

0 comments on commit cf5afa8

Please sign in to comment.