From b439ec143c4d2e2daf4b0a9c332123f24500cd9a Mon Sep 17 00:00:00 2001 From: Joel Reicher Date: Fri, 14 Apr 2006 14:10:15 +0000 Subject: [PATCH] Created function open_form() to open form file or fallback to default components file, with error handling. Replaced duplicated code with call to this function. --- h/utils.h | 1 + sbr/utils.c | 17 +++++++++++++++++ uip/comp.c | 16 +++------------- uip/dist.c | 10 ++-------- uip/forw.c | 12 ++---------- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/h/utils.h b/h/utils.h index 3eda9d3..92fdaba 100644 --- a/h/utils.h +++ b/h/utils.h @@ -18,3 +18,4 @@ struct msgs_array { }; void app_msgarg(struct msgs_array *, char *); +int open_form(char **, char *); diff --git a/sbr/utils.c b/sbr/utils.c index 8aba5b6..64df443 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -12,6 +12,7 @@ #include #include #include +#include #include /* @@ -184,3 +185,19 @@ app_msgarg(struct msgs_array *msgs, char *cp) msgs->msgs = mh_xrealloc(msgs->msgs, (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs)); msgs->msgs[msgs->size++] = cp; } + +/* Open a form or components file */ +int +open_form(char **form, char *def) +{ + int in; + if (*form) { + if ((in = open (etcpath (*form), O_RDONLY)) == NOTOK) + adios (*form, "unable to open form file"); + } else { + if ((in = open (etcpath (def), O_RDONLY)) == NOTOK) + adios (def, "unable to open default components file"); + *form = def; + } + return in; +} diff --git a/uip/comp.c b/uip/comp.c index 1aca23a..8fced48 100644 --- a/uip/comp.c +++ b/uip/comp.c @@ -10,6 +10,7 @@ */ #include +#include #include static struct swit switches[] = { @@ -227,19 +228,8 @@ main (int argc, char **argv) if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK) adios (form, "unable to open message"); - } else { - /* - * Open a component or forms file - */ - if (form) { - if ((in = open (etcpath (form), O_RDONLY)) == NOTOK) - adios (form, "unable to open form file"); - } else { - if ((in = open (etcpath (components), O_RDONLY)) == NOTOK) - adios (components, "unable to open default components file"); - form = components; - } - } + } else + in = open_form(&form, components); try_it_again: strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft)); diff --git a/uip/dist.c b/uip/dist.c index 4b6ce69..70e515e 100644 --- a/uip/dist.c +++ b/uip/dist.c @@ -10,6 +10,7 @@ */ #include +#include #include static struct swit switches[] = { @@ -195,14 +196,7 @@ main (int argc, char **argv) if (file && (msg || folder)) adios (NULL, "can't mix files and folders/msgs"); - if (form) { - if ((in = open (etcpath (form), O_RDONLY)) == NOTOK) - adios (form, "unable to open form file"); - } else { - if ((in = open (etcpath (distcomps), O_RDONLY)) == NOTOK) - adios (distcomps, "unable to open default components file"); - form = distcomps; - } + in = open_form(&form, distcomps); try_it_again: strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft)); diff --git a/uip/forw.c b/uip/forw.c index 319cc39..93870b1 100644 --- a/uip/forw.c +++ b/uip/forw.c @@ -393,16 +393,8 @@ try_it_again: if (!form) form = digestcomps; in = build_form (form, digest, volume, issue); - } else { - if (form) { - if ((in = open (etcpath (form), O_RDONLY)) == NOTOK) - adios (form, "unable to open form file"); - } else { - if ((in = open (etcpath (forwcomps), O_RDONLY)) == NOTOK) - adios (forwcomps, "unable to open default components file"); - form = forwcomps; - } - } + } else + in = open_form(&form, forwcomps); if ((out = creat (drft, m_gmprot ())) == NOTOK) adios (drft, "unable to create"); -- 1.7.10.4