Posted: . At: 9:24 AM. This was 4 years ago. Post ID: 13919
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Various tricks with C on Linux.


There are various tricks for programming on Linux. One useful trick is to open a file for reading with C.

This is how to do this. The “r” option for fopen() will read the file into memory.

#include <stdlib.h>
#include <stdio.h>
 
int main(void) {
	FILE *f;
	char buf[256];
 
	f = fopen("/etc/hosts", "r");
	if(f) {
		while(fgets(buf, sizeof(buf), f)) {
			fputs(buf, stdout);
		}
	} else {
		printf("File not found.\n");
	}
	fclose(f);
	return 0;
}

This example is for creating a new file if it does not exist, and then writing text into it.

#include <stdlib.h>
#include <stdio.h>
 
int main(void) {
	FILE *f;
	char buf[256];
 
	f = fopen("hosts", "w+");
	if(f) {
		fprintf(f, "This is appended to the file.\n");
	}
	fclose(f);
	return 0;
}

This is very easy to do and shows that C programming is not very difficult.

A more advanced solution for opening a file for reading. This is from my Sysinfo program. This is a void function with defined struct.

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);
/*
 * 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);
}

A more complicated section of code. This is to get system information.

	if (argc > 1 and strncmp(argv[1], "2", BUF) == 0) {
		printf("\t\tSystem information.\n");
 
		/* This code from:
		 * http://stackoverflow.com/questions/14345937/sysinfo-returns-incorrect-value-for-freeram-even-with-mem-unit
		 */
		/* Conversion constants. */
		const long minute = 60;
		const long hour = minute * 60;
		const long day = hour * 24;
		const double megabyte = 1024 * 1024;
 
		/* Obtain system statistics. */
		struct sysinfo si;
		sysinfo (&si);
 
		/* Summarize interesting values. */
		printf ("System uptime : %ld days, %ld:%02ld:%02ld\n",
		    si.uptime / day, (si.uptime % day) / hour,
		    (si.uptime % hour) / minute, si.uptime % minute);
		printf ("Total RAM   : %5.1f MB\n", si.totalram / megabyte);
		printf ("Free RAM   : %5.1f MB\n", si.freeram / megabyte);
		printf ("Number of running processes : %d\n", si.procs);
 
                printf("This system has %d processors configured and %d processors available.\n", get_nprocs_conf(), get_nprocs());
	}

Getting the amount of RAM and other properties is not the hardest thing ever.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.