Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8285063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:20:59+00:00 2026-06-08T11:20:59+00:00

I am working only in C regarding this issue. I have two function prototypes

  • 0

I am working only in C regarding this issue.

I have two function prototypes :

int pkg_permserver(const char *service, const char *protocol, int backlog, void (*errlog) (char *msg))

int pkg_permserver_ip(const char *ipOrHostname, const char *service, const char *protocol, int backlog, void (*errlog)(char *msg))

and the following segment of code :

int test_permserv(char *port) {
    int return_val;
    int num_port;
    char *chr_port;
    int nr_test_passed=0;
    chr_port = (char *) bu_malloc(8 * sizeof( char), "port string");

    printf("TESTING  PKG_PERMSERVER.....\n PORT PARAMETER TEST: \n");

    printf("TESTING VALID PORT...\n");
    return_val = pkg_permserver(port ,"tcp", 0, 0);
    display(return_val,1,&nr_test_passed);

    printf("TESTING INVALID PORT...\n");
    num_port = -1;
    sprintf(chr_port, "%d", num_port);
    return_val = pkg_permserver(chr_port ,"tcp", 0, 0);
    display(return_val,0,&nr_test_passed);
}

I am writing a test unit. I need to test each case ( valid / invalid ) for each parameter of a function.

I cannot modify the functions stated above. pkg_permserver and pkg_permserver_ip have the exact same parameters, except pkg_permserver_ip has the IpOrHostname additionally.

if I will write another function “test_permserver_ip” I do not want to copy – paste the part from test_permserver ( because the parameters are identical ).

What I have on my mind is something like int test_permserver(char * port, int which_function);

I want to avoid copying the same code for test_permserver_ip ( for the parameters that are identical to test_permserver ones)
The testing function for pkg_permserver_ip hasn’t been written yet.

This is the code for the two above functions :

int pkg_permserver(const char *service, const char *protocol, int backlog, void (*errlog) (char *msg))
{
    struct in_addr iface;
    iface.s_addr = INADDR_ANY;
    return _pkg_permserver_impl(iface, service, protocol, backlog, errlog);
}


int
pkg_permserver_ip(const char *ipOrHostname, const char *service, const char *protocol, int backlog, void (*errlog)(char *msg))
{
    struct hostent* host;
    struct in_addr iface;
    /* if ipOrHostname starts with a number, it's an IP */
    if (ipOrHostname) {
    if (ipOrHostname[0] >= '0' && ipOrHostname[0] <= '9') {
        iface.s_addr = inet_addr(ipOrHostname);
    } else {
        /* XXX gethostbyname is deprecated on Windows */
        host = gethostbyname(ipOrHostname);
        iface = *(struct in_addr*)host->h_addr;
    }
    return _pkg_permserver_impl(iface, service, protocol, backlog, errlog);
    } else {
    _pkg_perror(errlog, "pkg: ipOrHostname cannot be NULL");
    return -1;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:21:03+00:00Added an answer on June 8, 2026 at 11:21 am

    As ATaylor says, just extract the common stuff from both functions and get something like

    int common_stuff_for_permserver(... all common params ...)
    {
      ....
    }
    
    int pkg_permserver(...)
    {
          /// nothing to add here
          return common_stuff_for_permserver( ... all params ... );
    }
    
    int pkg_permserver_ip(...)
    {
          /// check for errors from common stuff
          if(!common_stuff_for_permserver( ... all params ... )) { return 0; }
    
          /// ip-specific stuff
          ...
    }
    

    If you don’t know how to extract the common part, post some more code for both functions and we’ll think it over.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this strange situation going on. Imagejpeg is only working when I define
at my working place (php only) we have a base class for database abstraction.
Now that I have a read-only application working, I am working on the insert
I've started working on some Android applications and have a question regarding how people
I have a performance issue regarding a quite simple query that run for more
I am working on an NHibernate project and have a question regarding updating transient
I got several questions regarding this issue 1) ActiveMQ after installed and started also
I have a simple Restlet service hosted on AppEngine. This performs basic CRUD operations
I am working only with integers and want to create a new instance of
Is piping parameter into line is working only for functions that accept one parameter?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.