SOME PROGRAMMING TIPS ===================== * Signals and friends The lib function kill works just as the user command kill: it takes the process ID and the signal number as parameter #include #include int kill( pid_t pid, int sig); * raise() is the special case of kill, where the PID is the id of the own process, use it to send a signal to myself int raise( int sig); * another special case sends a predefined signal SIGALARM to itself, but not immediately instead after some delay: #include unsigned int alarm( unsigned int seconds); * "Sleeping" on signals int pause(); The pause() call suspends the calling process until a signal, that is not ignored is delivered to the process. If a signal is caught by the process, the pause() returns immediately after the signal handler function is done. int got_signal_a = 0; while ( got_signal_a == 0 ) pause(); pause() will return/unblock for many types of signals, but only for those signal handler, that toggles the state of 'got_signal_a' will also make the while loop stop. * Check if stdout is redirected The system call `isatty(int fdes)' returns TRUE (1) if the file descriptor `fdes' points to a terminal. If it returns 0 a program can assume that its output is redirected to a file. * Building shared libraries -fPIC is for "position independent code" and needed for re-location gcc -fPIC -g -c -Wall a.c gcc -fPIC -g -c -Wall b.c gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc * Working with named pipes mkfifo( filename, mask) - if open() is not called with O_NONBLOCK flag, both read() and write() on the named pipe will block (exception is the case O_RDWR) Writing blocks until something reads on the other side, reading blocks until something is written in the pipe (a blocking read is normally what is desired) - O_NONBLOCK can only be used for read access. write() with O_NONBLOCK will fail - (f)close() on the fifo will cause an EOF to be read for those processes that read on the pipe - Only maximum PIPE_BUF bytes are written atomically at once. But a write() with less than PIPE_BUF bytes is guaranteed to be an atomic operation. * Perl File Handles change files on the fly, maybe useful in the "wanted" handler of the File::Find module sub wanted { my $fh = new FileHandle($_,"r+"); # read and write # read all ... my @data = fh->getlines(); work_on_data(\@data); seek($fh, 0, SEEK_SET); # set the pointer in the file to start truncate($fh, 0); # truncate the file to zero $fh->print(join @data); # write it completely new $fh->close(); } * Remove a complete directory tree use File::Path[()]; File::Path::rmtree('directory_name'); . use IO::Handle qw( SEEK_SET SEEK_CUR SEEK_END ); use IO::Handle; use FileHandle; # or use FileHandle(); . $fh = new FileHandle($file, 'r') does the same as ... $fh = new FileHandle("<$file") or open(FH, "<$file) . # go to Byte 10 of the opened file $fh->setpos(10, SEEK_SET); # get the position $offset = $fh->getpos() or as old-style function: $offset = tell($fh) . Move 2 Bytes from the current position (back) to the beginning of the file seek($fh, -2, SEEK_CUR); . $fh->setpos(0,SEEK_END) does the same as: seek($fh, 0, SEEK_END) . $fh->truncate(0); # Set Size of the file to zero . # Read from the file handle my $line = $fh->getline(); my @lines = $fh->getlines(); * DHTML with the DOM table model Normally you won't change the table header part , but only the body part 1) Use getElementById(...) for a reference to the object ... and work on this object ----> m |+---------+-----------+--------+---+--- || | | | | |+---------+-----------+--------+---+--- V| | | | | +---------+-----------+--------+---+--- n| | | | | +---------+-----------+--------+---+--- | | | | | 2) tr = tbody.insertRow(n); n is the position of the new row (should not be higher than the total number of rows in the table) 3) td = tr.insertCell(m); m is is the column position in row tr, at which the cell will be inserted (if this is only done in one row, the row in the table with have different number of columns. This does normally not look good when rendered by common browsers) 4) td.setAttribute("attribute", "attributeValue"); ... And put some text content in the new table cell: aNode = document.createTextNode("bla..bla..bla"); td.appendChild( aNode ); Instead of the sequence "createTextNode()" and "td.appendChild()" the not so clean alternative td.innerHtml("..html code snippet") can be used.