By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

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

P: n/a
/* $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;
}

Jul 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

--
mvh Björn
Jul 3 '06 #2

P: n/a
/* $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(socket.inet_aton)
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(socket.inet_ntoa)
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
>>a = s.inet_aton("192.168.3.14")
a
'\xc0\xa9\x03\x0e'
>>s.inet_ntoa(a)
'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


Jul 3 '06 #3

P: n/a
On Mon, 03 Jul 2006 06:20:36 -0700, Vusi wrote:
/* $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?
--
Steven.

Jul 4 '06 #4

P: n/a
/* $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.

--
mvh Björn
Jul 4 '06 #5

P: n/a
On 5/07/2006 1:09 AM, Steven D'Aprano wrote:
On Mon, 03 Jul 2006 06:20:36 -0700, Vusi wrote:
>/* $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.
Jul 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.