Miscellaneous programming tricks with C.

This is a very simple Hello World program in C.

int main() {
	write(1, "Hello World\n", 14);
}

Counting how long a text string is.

#include 
#include 
#define MSG "Hello Doctor, let's get back to the TARDIS!"
int main() {
int g;
g = strlen(MSG);
if (g < 1) {
printf("The string is not very long!\n");
} else {
printf("The length of the string `MSG' is: %i characters.\n", g);
}
return 0;
}

Code sample to check for a certain argument to a C program. using strncmp() to read from the argv[1], which is the first argument to the C program and checking if it contains the value “2″. And the value BUF sets the maximum length of the string expected.

if (argc > 1 and strncmp(argv[1], "2", BUF) == 0) {
	printf("\t\tRam & swap information.\n");
	kernel("/proc/swaps", 2);
	printf("-Uptime: ");
	kernel("/proc/uptime", 2);
	kernel("/proc/meminfo", 2);
}

More code from my sysinfo C program that reads in files and processes them accordingly.

#ifndef SYSINFO_H_
#define SYSINFO_H_

#define BUF 0x05

/*
 * Function prototypes. Sexy... And unlike on the show `24', function
 * prototypes have nothing to to with hard disk sectors!
 */

void kernel(char,int);

/*
 *  @brief  /proc file opener
 *  @param  File  An output stream.
 *  @param  len  A string length.
 *  @return  none.
 *  @pre  @a len must be a non-NULL int.
 * I hope this little function is not offending anyone. it is the only
 * way I could think to have a single function that would be able to 
 * load the different files quickly and without fuss. And it works just
 * fine, and that is what matters in the end.
 */

struct _kern1 {
	char *File;
	int len;
	char Kyo[40];
} *kern1 = (struct _kern1 *) 0x80;

void kernel(const char *File, int len)
{
	FILE *f;
	char Kyo[40];

	if (len > 10 or len < 2)
		return;

	f = fopen(File, "r");
	if(!f) {
		printf ("Sorry, I cannot open: %s.\n", File);
		printf("Please check your permissions with\n"		\
			"your supervisor. The feature may not\n"	\
			"be compiled and\\or enabled in your\n"		\
			"kernel version. Or a scsi device, eg,\n"	\
			"a USB drive may not be attached.\n");
		return;
	} else {
/* Based on sample code from:
 * www.koders.com/c/fid84CFEFBF311605F963CB04E0F84A2F52A8120F33.aspx
 * Specifically the section on parsing the /proc/version.
 */
		while (feof(f) != 1) {
			fgets(Kyo, len, f);
			if (strncmp(Kyo, "((", 1) == 0)
				printf ("\n-");
			if (strncmp(Kyo, "#", 1) == 0) {
				printf ("\nVersion: #");
			} else {
/*
 * This function is fast, owing to this i feel. especially with gcc 
 * 4.3.2 & glibc 2.5+. it is faster than using: printf (Kyo);
 */
				fprintf (stdout, "%s", Kyo);
			}
			fflush(stdout);
		}
	}
	fclose(f);
}

#endif /* sysinfo.h */
About

I am a computer enthusiast that has always been interested in maintaining and upgrading computer hardware. I was born on a farm and lived there for 13 years before moving into town. I was always interested in technology and my father always gave me good encouragement for developing my knowledge and interest in tinkering with computers. He was using a computer on the farm but that was in the very late 80`s and late 90`s. Seeing what a computer could do always fascinated me and then I knew that is what I wanted to do. The first time I used Linux was Red Hat 6.2 installed off a single CDROM and I was amazed that you did not need to install any more drivers on my old Intel Celeron machine to be able to enjoy a nice 24BPP desktop and use the GIMP to edit photos and enjoy playing the old Linux games like Xbill. Then a while later I tried Mandrake Linux 9.2 and I was hooked. It came with so much software and was very easy to use as well. Then Mandrake Linux 10 and on through FreeBSD, Debian 3.0, Debian 3.1 Suse Linux 9.2, Suse 10 and onto the Ubuntu distributions although I am using Fedora Core 15 and the Gnome 3 desktop at the moment.

Tagged with: , , , , ,
Posted in bejiitaswrath

Leave a Reply

close
Facebook Iconfacebook like buttonYouTube IconSubscribe on YouTubeTwitter Icontwitter follow button
Plugin from the creators of Brindes :: More at Plulz Wordpress Plugins