Example #1
0
int poptReadConfigFile(poptContext con, const char * fn) {
    char * file, * chptr, * end;
    char * buf, * dst;
    int fd, rc;
    int fileLength;

    fd = open(fn, O_RDONLY);
    if (fd < 0) {
	if (errno == ENOENT)
	    return 0;
	else 
	    return POPT_ERROR_ERRNO;
    }

    fileLength = lseek(fd, 0, SEEK_END);
    (void) lseek(fd, 0, 0);

    file = alloca(fileLength + 1);
    if (read(fd, file, fileLength) != fileLength) {
	rc = errno;
	close(fd);
	errno = rc;
	return POPT_ERROR_ERRNO;
    }
    close(fd);

    dst = buf = alloca(fileLength + 1);

    chptr = file;
    end = (file + fileLength);
    while (chptr < end) {
	switch (*chptr) {
	  case '\n':
	    *dst = '\0';
	    dst = buf;
	    while (*dst && isspace(*dst)) dst++;
	    if (*dst && *dst != '#') {
		configLine(con, dst);
	    }
	    chptr++;
	    break;
	  case '\\':
	    *dst++ = *chptr++;
	    if (chptr < end) {
		if (*chptr == '\n') 
		    dst--, chptr++;	
		    /* \ at the end of a line does not insert a \n */
		else
		    *dst++ = *chptr++;
	    }
	    break;
	  default:
	    *dst++ = *chptr++;
	    break;
	}
    }

    return 0;
}
Example #2
0
void MaItem::save(bool onlyWhenDirty)
{
	if (!onlyWhenDirty || mDirty)
	{
		bool encrypt = isListEncrypted();
		if (mFilename.isEmpty())
			mFilename = mParentItem->nextChildFilename();
		QFile file(absPath());

		if (file.open(QIODevice::WriteOnly))
		{
			QTextStream out(&file);
			out.setCodec("UTF-8");
			out << configLine(encrypt);
			out << "\n";
			if (encrypt)
			{
				QByteArray s(mCaption.toAscii());
				s.append("\n");
				s.append(mNotes.toAscii());
				_engine->encrypt(s);
				out << s;
			}
			else
			{
				out << mCaption;
				out << "\n";
				out << mNotes;
			}
			file.close();
			mDirty = false;
		}
	}

	// Saving the password:
	if ((!onlyWhenDirty || mPasswordDirty) && !mPassword.isEmpty())
	{
		QFile pwdFile(absPathToFilename(kPathwordFilename));
		if (pwdFile.open(QIODevice::WriteOnly))
		{
			QTextStream out(&pwdFile);
			out.setCodec("UTF-8");
			QByteArray ba(mPassword.toAscii());
			QByteArray salt(QString(_engine->passwordEncryptionSalt()).toAscii());
			_engine->encrypt(ba, _engine->passwordEncryptionKey().toAscii(), salt);
			out << ba;
			pwdFile.close();
			mPasswordDirty = false;
		}
	}
}
Example #3
0
int poptReadConfigFile(poptContext con, const char * fn)
{
    const char * file, * chptr, * end;
    char * buf;
/*@dependent@*/ char * dst;
    int fd, rc;
    off_t fileLength;

    fd = open(fn, O_RDONLY);
    if (fd < 0)
	return (errno == ENOENT ? 0 : POPT_ERROR_ERRNO);

    fileLength = lseek(fd, 0, SEEK_END);
    if (fileLength == -1 || lseek(fd, 0, 0) == -1) {
	rc = errno;
	(void) close(fd);
	/*@-mods@*/
	errno = rc;
	/*@=mods@*/
	return POPT_ERROR_ERRNO;
    }

    file = alloca(fileLength + 1);
    if (read(fd, (char *)file, fileLength) != fileLength) {
	rc = errno;
	(void) close(fd);
	/*@-mods@*/
	errno = rc;
	/*@=mods@*/
	return POPT_ERROR_ERRNO;
    }
    if (close(fd) == -1)
	return POPT_ERROR_ERRNO;

/*@-boundswrite@*/
    dst = buf = alloca(fileLength + 1);

    chptr = file;
    end = (file + fileLength);
    /*@-infloops@*/	/* LCL: can't detect chptr++ */
    while (chptr < end) {
	switch (*chptr) {
	  case '\n':
	    *dst = '\0';
	    dst = buf;
	    while (*dst && isspace(*dst)) dst++;
	    if (*dst && *dst != '#')
		configLine(con, dst);
	    chptr++;
	    /*@switchbreak@*/ break;
	  case '\\':
	    *dst++ = *chptr++;
	    if (chptr < end) {
		if (*chptr == '\n') 
		    dst--, chptr++;	
		    /* \ at the end of a line does not insert a \n */
		else
		    *dst++ = *chptr++;
	    }
	    /*@switchbreak@*/ break;
	  default:
	    *dst++ = *chptr++;
	    /*@switchbreak@*/ break;
	}
    }
    /*@=infloops@*/
/*@=boundswrite@*/

    return 0;
}