Can anyone please analyse this program for me (write a pseudocode for it).

V

Vusi

/* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
/*
* Copyright (c) 2003, 2004 David Young. All rights reserved.
*
* This code was written by David Young.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgement:
* This product includes software developed by David Young.
* 4. David Young's name may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
* YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifndef lint
__RCSID("$Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $");
#endif

void
usage(const char *proggie)
{
errx(EXIT_FAILURE, "Usage: %s <ip address>", proggie);
}

int
main(int argc, char **argv)
{
struct in_addr addr;

if (argc != 2 || !inet_aton(argv[1], &addr)) {
usage(argv[0]);
}

(void)printf("%s\n", inet_ntoa(addr));
return 0;
}
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

void
usage(const char *proggie)
{
errx(EXIT_FAILURE, "Usage: %s <ip address>", proggie);
}

int
main(int argc, char **argv)
{
struct in_addr addr;

if (argc != 2 || !inet_aton(argv[1], &addr)) {
usage(argv[0]);
}

(void)printf("%s\n", inet_ntoa(addr));
return 0;
}

1. Take an internet host address in dotted decimal form, if it is
valid convert it to an internal representation.
1b. If it isn't, or no address was supplied print usage information and exit.
2. Convert the internal representation back to dotted decimal form and print it.
 
T

Tim Chase

/* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */

Well, let's begin here. You've got your python commenting style
all wrong. This alone won't parse as python. I recommend using
the standard "#" comment notation as described in the python docs.
if (argc != 2 || !inet_aton(argv[1], &addr)) {
usage(argv[0]);
}

(void)printf("%s\n", inet_ntoa(addr));

Then, in your python code, you're using braces. While there are
conventions for doing that:

if (conditions): # {
statements
#}

they clearly aren't being followed in your code, and given their
optional nature, I suggest simply omitting them. Second of all,
you're calling non-idempotent functions in your "if" statement.
Generally considered bad style. Additionally, you never
reference the sys.argv module/list of parameters, but still refer
to argc (which should really be "len(sys.argv)"), and

The lines that look like they should be including your modules:
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

appear to be commented out, as per above, using the standard "#"
notation. Additionally, the python syntax would be "import
socket", not "include" followed by all this other rubbish with
less-than and greater-than signs, and filename extensions.

This isn't rocket-surgery. Both inet_aton() and inet_ntoa()
function calls are in the socket module. Call them accordingly:
>>> import socket
>>> print "\n".join([s for s in dir(socket) if
s.startswith("inet_")])
inet_aton
inet_ntoa
Help on built-in function inet_aton in module _socket:

inet_aton(...)
inet_aton(string) -> packed 32-bit IP representation

Convert an IP address in string format (123.45.67.89) to the
32-bit packed
binary format used in low-level network functions.
Help on built-in function inet_ntoa in module _socket:

inet_ntoa(...)
inet_ntoa(packed_ip) -> ip_address_string

Convert an IP address from 32-bit packed binary format to
string format'192.168.3.14'

The rest is basic candy to get the command-line parameter and
pass it in to aton and back again to ntoa.

I'm sure there are plenty of other problems, but this would be a
good place to start...

-tkc
 
S

Steven D'Aprano

/* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
/*
* Copyright (c) 2003, 2004 David Young. All rights reserved.
*
* This code was written by David Young.

[snip code]

Am I the only one who found it hilarious that this piece of code was made
up of 24 lines of actual code (including lines containing only a single
brace) but 29 lines of legalise?
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

/* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
/*
* Copyright (c) 2003, 2004 David Young. All rights reserved.
*
* This code was written by David Young.

[snip code]

Am I the only one who found it hilarious that this piece of code was made
up of 24 lines of actual code (including lines containing only a single
brace) but 29 lines of legalise?

Good thing it was C then, had it been Python the hilariousness of the
legalese/LOC ratio would have been lethal.
 
J

John Machin

/* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
/*
* Copyright (c) 2003, 2004 David Young. All rights reserved.
*
* This code was written by David Young.

[snip code]

Am I the only one who found it hilarious that this piece of code was made
up of 24 lines of actual code (including lines containing only a single
brace) but 29 lines of legalise?

You're not alone. The program itself is as risible as the request for it
to be analysed ... its ratio of overhead to functionality is exceeded
only by the legendary IEFBR14.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top