PERFORCE change 167453 for review
David Forsythe
dforsyth at FreeBSD.org
Mon Aug 17 18:35:50 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=167453
Change 167453 by dforsyth at squirrel on 2009/08/17 18:35:47
Macro list realloc
Affected files ...
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_db_hierdb_read.c#7 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest.c#7 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest_plist.c#8 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_util.h#16 edit
Differences ...
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_db_hierdb_read.c#7 (text+ko) ====
@@ -228,11 +228,17 @@
if (!pkg_db_hierdb_file_exists(db, p, REQUIRED_BY_FILE))
return (PKG_OK); /* +REQUIRED_BY doesn't have to exist. */
-
+
+ list = NULL;
reqdby_stream = pkg_db_hierdb_open_file_stream_read(db, p, REQUIRED_BY_FILE);
for (count = 0, reqdby = NULL; fgets(line, FILENAME_MAX, reqdby_stream) != NULL;) {
pkg_util_trim_newline(line);
- STRING_LIST_APPEND(reqdby, line, list, count, 10);
+ LIST_FACTOR_REALLOC(reqdby, list, count, 10);
+ if (reqdby == NULL) {
+ for (reqdby = list; count; count--)
+ free(reqdby[count - 1]);
+ return (PKG_MEMORY_ERR | PKG_NOT_OK);
+ }
}
fclose(reqdby_stream);
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest.c#7 (text+ko) ====
@@ -340,82 +340,79 @@
/* Add command strings to a package. Do not sort. */
-/* Add an exec command to a manifest, which will be run at install time. Returns the
- * status of the operation. */
+/* Add an exec command to a manifest, which will be run at install time. Returns
+ * index of new cmd if successful, -1 otherwise. */
int
pkg_manifest_add_exec_cmd(struct pkg_manifest *pm, const char *cmd)
{
- int status;
char **list;
pkg_error_null_argument("command", cmd, __func__);
+
+ list = NULL;
+ LIST_FACTOR_REALLOC(pm->exec_list, list, pm->exec_count, 10);
+ if (pm->exec_list == NULL) {
+ pm->exec_list = list;
+ return (-1);
+ }
+ if (pkg_util_strdup(cmd, &pm->exec_list[pm->exec_count - 1]) != PKG_OK)
+ return (-1);
- if (pm->exec_count % 10 == 0) {
- list = pm->exec_list;
- pm->exec_list = realloc(list, sizeof(*list) * (pm->exec_count + 11));
- if (pm->exec_list == NULL) {
- pm->exec_list = list;
- return (PKG_MEMORY_ERR | PKG_NOT_OK);
- }
- }
-
- status = pkg_util_strdup(cmd, &pm->exec_list[pm->exec_count++]);
pm->exec_list[pm->exec_count] = NULL;
- return (status);
+ return (pm->exec_count - 1);
}
/* Add an unexec command to a manifest, which will be executed at deinstall time.
- * Returns the status of the operation. */
+ * Return index of new cmd if successful, -1 otherwise. */
int
pkg_manifest_add_unexec_cmd(struct pkg_manifest *pm, const char *cmd)
{
- int status;
char **list;
pkg_error_null_argument("command", cmd, __func__);
- if (pm->unexec_count % 10 == 0) {
- list = pm->unexec_list;
- pm->unexec_list = realloc(list, sizeof(*list) * (pm->unexec_count + 11));
- if (pm->unexec_list == NULL) {
- pm->unexec_list = list;
- return (PKG_MEMORY_ERR | PKG_NOT_OK);
- }
+ list = NULL;
+ LIST_FACTOR_REALLOC(pm->unexec_list, list, pm->unexec_count, 10);
+ if (pm->unexec_list == NULL) {
+ pm->unexec_list = list;
+ return (-1);
}
- status = pkg_util_strdup(cmd, &pm->unexec_list[pm->unexec_count++]);
+ if (pkg_util_strdup(cmd, &pm->unexec_list[pm->unexec_count - 1]) != PKG_OK)
+ return (-1);
+
pm->unexec_list[pm->unexec_count] = NULL;
- return (status);
+ return (pm->unexec_count - 1);
}
/* Add a dirrm command to a manifest that will remove the directory dir at deinstall
- * time. Returns the status of the operation. */
+ * time. Return index of new cmd if successful, -1 otherwise. */
int
pkg_manifest_add_dirrm_cmd(struct pkg_manifest *pm, const char *dir)
{
- int status;
char **list;
pkg_error_null_argument("directory", dir, __func__);
+
+ list = NULL;
+ LIST_FACTOR_REALLOC(pm->dirrm_list, list, pm->dirrm_count, 10);
+ if (pm->dirrm_list == NULL) {
+ pm->dirrm_list = list;
+ return (-1);
+ }
- if (pm->dirrm_count % 10 == 0) {
- list = pm->dirrm_list;
- pm->dirrm_list = realloc(list, sizeof(*list) * (pm->dirrm_count + 11));
- if (pm->dirrm_list == NULL) {
- pm->dirrm_list = list;
- return (PKG_MEMORY_ERR | PKG_NOT_OK);
- }
- }
+
+ if (pkg_util_strdup(dir, &pm->dirrm_list[pm->dirrm_count - 1]) != PKG_OK)
+ return (-1);
- status = pkg_util_strdup(dir, &pm->dirrm_list[pm->dirrm_count++]);
pm->dirrm_list[pm->dirrm_count] = NULL;
- return (status);
+ return (pm->dirrm_count - 1);
}
void
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_manifest_plist.c#8 (text+ko) ====
@@ -253,7 +253,7 @@
status = PKG_NOT_OK;
break;
}
- status = pkg_manifest_add_exec_cmd(pm, argument);
+ pkg_manifest_add_exec_cmd(pm, argument);
break;
case (PM_UNEXEC):
if (strlen(argument) == 0) {
@@ -261,7 +261,7 @@
status = PKG_NOT_OK;
break;
}
- status = pkg_manifest_add_unexec_cmd(pm, argument);
+ pkg_manifest_add_unexec_cmd(pm, argument);
break;
case (PM_MODE):
status = (strlen(isolate) == 0) ?
@@ -317,7 +317,7 @@
status = PKG_NOT_OK;
break;
}
- status = pkg_manifest_add_dirrm_cmd(pm, isolate);
+ pkg_manifest_add_dirrm_cmd(pm, isolate);
break;
case (PM_MTREE):
if (strlen(isolate) == 0) {
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg_util.h#16 (text+ko) ====
@@ -19,15 +19,13 @@
void rage_quit(void);
-#define STRING_LIST_APPEND(list, append, tmp_list, list_sz, factor) \
+#define LIST_FACTOR_REALLOC(list, tmp_list, list_sz, factor) \
if (list_sz % factor == 0) { \
tmp_list = list; \
list = realloc(tmp_list, sizeof(*tmp_list) * (list_sz + factor + 1)); \
} \
- if (list != NULL) { \
- list[list_sz++] = strdup(append); \
- list[list_sz] = NULL; \
- }
+ if (list != NULL) \
+ list_sz++
#define ARRAY_FACTOR_REALLOC(array, tmp_array, array_sz, factor) \
if (array_sz % factor == 0) { \
More information about the p4-projects
mailing list