Example #1
0
static int help(int argc, char ** argv)
{
	struct command_list * list;
	struct list_head * pos;
	struct command_t ** cmd_array;
	struct command_t * cmd;
	s32_t i = 0, j, k = 0, cmd_num, swaps;

	if(argc == 1)
	{
		i = 0;
		cmd_num = command_number();
		cmd_array = malloc(sizeof(struct command_t *) * cmd_num);

		if(!cmd_array)
		{
			printf("malloc command array fail for sort command list\r\n");
			return -1;
		}

		for(pos = (&command_list->entry)->next; pos != (&command_list->entry); pos = pos->next)
		{
			list = list_entry(pos, struct command_list, entry);
			cmd_array[i++] = list->cmd;
			j = strlen(list->cmd->name);
			if(j > k)	k = j;
		}

		for(i = cmd_num - 1; i > 0; --i)
		{
			swaps = 0;
			for(j=0; j<i; ++j)
			{
				if (strcmp(cmd_array[j]->name, cmd_array[j + 1]->name) > 0)
				{
					cmd = cmd_array[j];
					cmd_array[j] = cmd_array[j + 1];
					cmd_array[j + 1] = cmd;
					++swaps;
				}
			}
			if(!swaps)
				break;
		}

		for(i = 0; i < cmd_num; i++)
		{
			printf(" %s%*s - %s",cmd_array[i]->name, k-strlen(cmd_array[i]->name), "", cmd_array[i]->desc);
		}
		free(cmd_array);
	}
int stamp_rate(char *string_command){
	int byte = command_number(string_command);
	return byte;

}