static void getPortLocation()

in src/main/c/Posix/PosixHelperFunctions.c [358:415]


static void getPortLocation(const char* portDirectory, char* portLocation, int physicalPortNumber)
{
	// Set location of busnum and devpath files
	char* busnumFile = (char*)malloc(strlen(portDirectory) + 16);
	strcpy(busnumFile, portDirectory);
	strcat(busnumFile, "busnum");
	char* devpathFile = (char*)malloc(strlen(portDirectory) + 16);
	strcpy(devpathFile, portDirectory);
	strcat(devpathFile, "devpath");
	int portLocationLength = 0;
	portLocation[0] = '\0';

	// Read the bus number
	FILE *input = fopen(busnumFile, "rb");
	if (input)
	{
		int ch = getc(input);
		while (((char)ch != '\n') && (ch != EOF))
		{
			portLocation[portLocationLength++] = (char)ch;
			ch = getc(input);
		}
		portLocation[portLocationLength++] = '-';
		fclose(input);
	}
	else
	{
		portLocation[portLocationLength++] = '0';
		portLocation[portLocationLength++] = '-';
	}

	// Read the device path
	input = fopen(devpathFile, "rb");
	if (input)
	{
		int ch = getc(input);
		while (((char)ch != '\n') && (ch != EOF))
		{
			portLocation[portLocationLength++] = (char)ch;
			ch = getc(input);
		}
		portLocation[portLocationLength] = '\0';
		fclose(input);
	}
	else
	{
		portLocation[portLocationLength++] = '0';
		portLocation[portLocationLength] = '\0';
	}

	// Append the physical port number if applicable
	if (physicalPortNumber >= 0)
		sprintf(portLocation + portLocationLength, ".%d", physicalPortNumber);

	// Clean up the dynamic memory
	free(devpathFile);
	free(busnumFile);
}