Exemplo n.º 1
0
/**
 * The "dhcp" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Exit code
 */
static int dhcp_exec ( int argc, char **argv ) {
	static struct option longopts[] = {
		{ "help", 0, NULL, 'h' },
		{ NULL, 0, NULL, 0 },
	};
	const char *netdev_txt;
	struct net_device *netdev;
	int c;
	int rc;

	/* Parse options */
	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
		switch ( c ) {
		case 'h':
			/* Display help text */
		default:
			/* Unrecognised/invalid option */
			dhcp_syntax ( argv );
			return 1;
		}
	}

	/* Need exactly one interface name remaining after the options */
	if ( optind != ( argc - 1 ) ) {
		dhcp_syntax ( argv );
		return 1;
	}
	netdev_txt = argv[optind];

	/* Parse arguments */
	netdev = find_netdev ( netdev_txt );
	if ( ! netdev ) {
		printf ( "No such interface: %s\n", netdev_txt );
		return 1;
	}

	/* Perform DHCP */
	if ( ( rc = dhcp ( netdev ) ) != 0 ) {
		printf ( "Could not configure %s: %s\n", netdev->name,
			 strerror ( rc ) );
		return 1;
	}

	return 0;
}
Exemplo n.º 2
0
/**
 * The "dhcp" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Exit code
 */
static int dhcp_exec ( int argc, char **argv ) {
	static struct option longopts[] = {
		{ "help", 0, NULL, 'h' },
		{ NULL, 0, NULL, 0 },
	};
	const char *netdev_name;
	struct net_device *netdev;
	int c;
	int rc;

	/* Parse options */
	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
		switch ( c ) {
		case 'h':
			/* Display help text */
		default:
			/* Unrecognised/invalid option */
			dhcp_syntax ( argv );
			return 1;
		}
	}

	if ( optind != argc ) {
		/* Treat arguments as a list of interfaces to try */
		while ( optind != argc ) {
			netdev_name = argv[optind++];
			if ( ( rc = dhcp_exec_name ( netdev_name ) ) == 0 )
				return 0;
		}
	} else {
		/* Try all interfaces */
		for_each_netdev ( netdev ) {
			if ( ( rc = dhcp_exec_netdev ( netdev ) ) == 0 )
				return 0;
		}
	}

	return 1;
}