Example #1
0
File: piper.c Project: kgadek/kpfp
int main() {
	char *msg;
	char *mypid;
	int n;

	msg = malloc(MAXMSGLEN);
	mypid = malloc(MAXPIDLEN);
	sprintf(mypid," #%d",getpid());

	write(STDOUT_FILENO, mypid, strlen(mypid));
	write(STDOUT_FILENO, "\n", 1);
	write(STDERR_FILENO, "wypisalem\n",sizeof("wypisalem\n"));

	while(1) {
		n = read(STDIN_FILENO, msg, MAXMSGLEN);
		if(n == -1)
			myErr(3,"Błąd odczytu z stdin");
		if(!n)
			exit(0);

		if(n>0)
			msg[n-1] = 0; /*usuwamy \n z końca wiersza*/
		if(!strstr(msg,mypid)) { /*nie ma na liście*/
			strcat(msg,mypid);
			write(STDOUT_FILENO, msg, strlen(msg));
			write(STDOUT_FILENO, "\n", 1);
		} else { /*jest na liście*/
			write(STDERR_FILENO, msg, strlen(msg));
			write(STDERR_FILENO, "\n", 3);
		}
	}

	return 0;
}
Example #2
0
bool SysfsGPIO::configureGPIO(GPIO_Pin gpionr, QString direction)
{
    //First check that the export file is there,
    //if it is missing then the sysfs gpio "module" is not loaded.
    QFile gpioExport("/sys/class/gpio/export");
    if(!gpioExport.exists())
    {
        myErr() << "missing file: /sys/class/gpio/export";
        return false;
    }

    //If value does not exist then export
    QFile gpioDirection(QString("/sys/class/gpio/gpio%1/direction").arg(gpionr));
    if(!gpioDirection.exists())
    {
        //myOut() << gpioDirection.fileName();

        if (!gpioExport.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            myErr() << gpioExport.fileName();
            return false;
        }

        QTextStream out(&gpioExport);
        out << gpionr << "\n";
        gpioExport.close();
        sleep(1);
    }

    {
        if (!gpioDirection.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            myErr() << gpioDirection.fileName();
            return false;
        }

        QTextStream out(&gpioDirection);
        out << direction << "\n";
        gpioDirection.close();
    }

    return true;
}
Example #3
0
bool SysfsGPIO::writeGPIO(GPIO_Pin gpionr, GPIO_State value)
{
    if(GPIO_UNDEF == value)
    {
        //What did you plan to do?
        return false;
    }

    //Open and write 1/0 to value file
    QFile gpioFile(QString("/sys/class/gpio/gpio%1/value").arg(gpionr));

    if (!gpioFile.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        myErr() << "FAILURE can't open gpio file gpioPath:" << gpioFile.fileName();
        return false;
    }

    QTextStream out(&gpioFile);
    out << value << "\n";
    gpioFile.close();

    return true;
}