rf.c

Well I’ve done some correcting to my little back scratcher, including two bug fixes and changes to the man page.

/*-
* Copyright (C) 2007
* Terry *. P*****. All rights reserved.
*
* permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


// vim: set noexpandtab ts=8 sw=4 ai :
// vi: set ai nu ts=8 sw=4 :

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>

struct lnpos {
long nl;
struct lnpos *next;
};

static void readall( FILE *, int );
static void readtop( FILE *, int );
static void readbottom( FILE *, int );
static void usage( void );

/*
* rf - read file to standard out v1.1
*/

int
main( int argc, char *argv[] ) {

char *erptr;
int t_flag = 0, b_flag = 0;
int ch, lncnt;
FILE *fp;

extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;

while ( (ch = getopt(argc, argv, "b:t:")) != -1 ) {
switch (ch) {
case 'b':
b_flag++;
lncnt = strtol(optarg, &erptr, 10);
if ( *erptr || lncnt <= 0 )
errx( 1, "Improper line count -- %s", optarg );
break;
case 't':
t_flag++;
lncnt = strtol(optarg, &erptr, 10);
if ( *erptr || lncnt <= 0 )
errx( 1, "Improper line count -- %s", optarg );
break;
case '?':
default:
usage();
return 1;
}
}

if ( (t_flag < 1) && (b_flag < 1) ) {
fp = fopen( argv[1], "r" );
if ( fp == NULL )
errx( 1, "File can not be opened or"
" does not exist -- %sn", argv[1] );
readall( fp, lncnt );
} else if ( t_flag > 0 ) {
fp = fopen( argv[3], "r" );
if ( fp == NULL )
errx( 1, "File can not be opened or"
" does not exist -- %sn", argv[3] );
readtop( fp, lncnt );
} else if ( b_flag > 0 ) {
fp = fopen( argv[3], "r" );
if ( fp == NULL )
errx( 1, "File can not be opened or"
" does not exist -- %sn", argv[3] );
readbottom( fp, lncnt );
} else {
usage();
/* NOTREACHED */
}

fclose( fp );
return 0;
}

/*
* print out an open file to standard output
*/

static void
readall( FILE *fp, int lncnt ) {

while ( (lncnt = fgetc( fp )) != EOF ) {
printf( "%c", lncnt );
}
}

/* Read n lines from the top of the file.
* note that it was very inspired by the head(1) implementation of BSD
*/
static void
readtop( FILE *fp, int lncnt ) {

char *cp;
size_t error, rlen;
while ( lncnt && (cp = fgetln( fp, &rlen )) != NULL ) {
error = fwrite( cp, sizeof(char), rlen, stdout );
if ( error != rlen )
err( 1, "stdout" );
lncnt--;
}
}


/* Read n lines from the bottom of the file
*/
static void
readbottom( FILE *fp, int lncnt ) {

int hmany = lncnt;
long nlnum = 0;
long where;

struct lnpos *root;
struct lnpos *cur;

root = malloc( sizeof(struct lnpos) );
if ( root == NULL )
err( 1, "can't init the list" );
root->next = 0;
cur = root;

cur->next = malloc( sizeof(struct lnpos) );
if ( cur->next == NULL )
err( 1, "can't add nodes" );
cur = cur->next;

/* read the file, count every 'n' and store them in a new member of
* our linked list.
*/
while ( (lncnt = fgetc( fp )) != EOF ) {
if ( lncnt == 'n' ) {
nlnum++;
cur->nl = ftell( fp );
if ( cur->next != NULL )
cur = cur->next;
cur->next = malloc( sizeof(struct lnpos) );
if ( cur->next == NULL )
err( 1, "can't add nodes" );
cur = cur->next;
}
}

/* rewind our linked-list and seek to b_flag + 1 segments short of the
* end of the list _before_ calling readall. So readall _starts_ from
* the correct fseek offset to print till EOF.
*/
cur = root->next;
hmany++;
while ( hmany < nlnum ) {
cur = cur->next;
nlnum--;

}

where = fseek( fp, cur->nl, SEEK_SET );
if ( where != 0 )
err( 1, "could not seek through the filen" );

readall( fp, lncnt );
}

static void
usage( void ) {
fprintf( stderr, "usage:n rf filen "
"rf [-t number lines from the top] filen "
"rf [ -b number of lines from the bottom ] [ file ]n" );
}

And the manual which writing got me interested in roff, I’ll post when I have time to learn more about it. Basically the problem I had was caused by working on it. Originally under the conditions when 2 + 2 = 5 which is not good, because 2 + 2 is really == 4 !!!! I should never do stuff when I’m like that, but if I don’t try nothing gets done.

Go, what?

Well, to day I was working on some file. And for the first time I asked my self if I should use a ‘Goto’. The goto statement is probably one of the most controversial things I’ve ever read of in computing history.

The problem was, I had the option of repeating several lines of code at several conditionals in a if-elif-else construct. I thought about using a goto to walkk on over to that. So as to only right it once. When I asked my self, Do I even remember the syntax for a Goto ! LMAO !!! I can’t even ever remember using a Go to in any language. The only time I recall learning about using was breifly in an historical C tutorial by by Brian W. Kernighan. Then I thought, since I didn’t want to clutter things by getting repetitive – Why not get off my lazy arse and wrap it in a function?

Really the only purpose of using goto often I could see would be to avoid using functions at all. The best usage of it I can see, which could still be wrapped in a function if wanted. Although I’m sure thats not always best. I don’t see any thing wrong with goto, but I don’t see it as an excuse not to use functions, if-elif-else, switch/case, for/while/do-while e.t.c. what ever constructs the language provides.

I think, in my opinion this would be a good clause to use the ‘infernal goto’

for condintion
do
code
if true
goto handle
else
skip
A number of things like this and eventually

handle:
code to handle false
done

As a method of keeping some exception handling at the end of a loop rather then packing it some where with a nice interface or some thing. I dunno, personally I like doing things in a manor that I know is crystal clear.

For example, column stacking on a door for a bang & clear. It should be clear as can be who is banging, who is openning, and which one enters first, e.t.c.

TDFTN

  • Finish fixing rf.c
  • Work on implementing more realistic CS Gas in S4:TSS
  • Finish that letter
  • Do my home work *snore

Post it for the future.

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

Rest

My idea of R&R ??

Find me a pair of palm trees. String a hammock between’em and relax in the shade with a nice cold drink.

Mm, maybe some day hehehehe.

Some times I dislike being the only one in this burg I know. With a passion for computers and coding in my age group…

Sheesh man, I was in diapers with a Tandy 1000 and TRS-80 Colour Computer for toys. WebTV introduced me to the World Wide Web. A Computer donated from our Church showed me Windows 98 and got me doing like 8+ hour a day learning stuff via Earthlink Dialup.

Look at me now? I’ll be 19 this June. 3 Computers, desktop, desktop turned server, and laptop. Plus Ma’s desktop. Stuck living with Windows often. Yet I breath naturally on my FreeBSD / PC-BSD systems. Feels good to long into FreeBSD even if its through PuTTY at times.

No clue wtfrig I started learning programming, I don’t even care much for C++ in retrospect. Yet I love C !!! GO figure. When I get to a working vacation in July. I plan on boning up on Pythons standard library. I’m only very familer at that level with C and some of the BSD / POSIX extensions on my system. Most of which, I rather like…

Learning to do stuff at CLI levels, learning, understanding, and mastering complexity == heaven. Flib it, man I don’t even _use_ an _IDE_ as most call it. I started with Dev-C++ now? My little bag of tricks. Vi, Vim, Ex, I can use Emacsen but prefer Visen ;-). GCC, the only compiler I’ve ever used but I like it so far. Although I wish it’d be better at reminding me that I transposed a , /or . although thats a font and many lines later issue. I’ve found using GCC from the command line much more flexible. GDB for debugging, skip the GUI ! Lint for simplistic code checking, ctags for larger stuff. A bloody fine manual for system calls, the standard library, and ncurses. A very good Bourne Shell implementation for scripting and many choices. BSD & GNU make, and a number of other things I’ve yet to learn like groff and automake.

Most people my age? Ask’em what C Programming is and they’d probably say forging report cards ! Most probably wouldn’t even know what the HTML in index.html stands for.. A lot of my friends are not even the most computer literate.

I’ve been told in my youth that my mind was like some one older then my years… But I’ve always valued my uniqueness. How many lusers do you know. Who’d manufacture their own fscking mother board & write device drivers as necessary for if they only knew how !!!!!!!!!!!!! Ok , so I’m a crazy lune… can’t help that. But, GOD, I Love to learn about stuff that gets my interest. He’s blessed me so much with being able to learn as much as I have. Odds are, if it wasn’t for that one computer Robert gave me. When he heard ma wouldn’t let me repartion & dual boot ma’s computer. I probably never would have gotten into *nix systems as much as I have. If my sister didn’t get me my laptop. I’d probably never been able to learn this much for programming. Its been a great blessing, one that I’m very thankful for. To me, its a joy to learn some thing new and interesting every day. I’d be happy some day, to know as much about computers as Mel Kaye did lol.

Yet still… who do I have to share this passion with? Mm, no one…….

Thats what pains me lately.

Spider Stance

You know, its probably the first time in my life. At least that I’ve noticed it. That I’ve ever sat, in that ‘distinctive’ pose of the amazing Spider man.

straighter then that, back erect and arms straight. I was perched on a couch-ledge, so had to fit feet and hands in the same spot. After a minute, I realized who made it famous ^_^

You know some thing, it felt darn good for my back !! I wonder if a generation of Comic Book artists ever thought about that =/

Pictures not exactly what I wanted but its as close to it as I can find without putting one of my comics in the scanner.

Well I’ve solved one problem, now to tackle another problem.

Solved:

KMail would crash when ever moving or deleting a message in my SMB mounted mailboxes.

Solution? I backed every thing up by moving them to /tmp. Replaced my .kde/share/config/kmail* and .kde/share/apps/kmail files with roots which was never configured. Then I set up kmail and imported my messages from backup.

That fixed it all but now I have a new problem:

How to access mail from any where on my network.

THe Soultion I’d like, kolab- > KDE Group Ware xD

Man, I don’t look forward to source installing that monster on a 500Mhz Pentium III !!! Maybe I can run it in a FreeBSD Jail for security as well.