Do a simple function call in C. This code snippet shows how to do this
This simple C code snippet shows how to do a function call in C. This calls the hi() function and runs it in main(). Very simple but useful code. And this does not need to call any include files. As I am using the built-in write() function.
int hi() { int i; i = 0; write(1, "Hi.\n", 5); while (i < 8) { system("echo 'This is a loop.'"); i++; } } int main(int argc, char** argv) { write(1, "Hello World\n", 15); hi(); return 0; } |
A nice function that reads a file and performs various operations on it, depending on the contents.
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); } |
Feeding the version number and other text into a text block to create a very nice banner for my program to print out.
enum { VERSION = 150 }; #define SYSINFO "Sysinfo version v%i.%i" const char *p = "System Information Program"; char ver[128]; void print_menu() { printf("\tSysinfo. %s\n\n", p); printf("1 - Kernel Information.\n2 - Memory & processes.\n"); printf("3 - CDROM Information.\n4 - View list of sound cards.\n"); printf("5 - View Real Time Clock Information.\n"); printf("6 - View motherboard and BIOS information.\n"); /* printf("7 - View information about your hard drive.\n"); */ sprintf (ver, SYSINFO, VERSION / 100, VERSION % 100); printf("\n%s, by John Cartwright 2007.\nsysinfo comes with ", ver); printf("ABSOLUTELY NO WARRANTY. This\n is open source software,"); printf("and you are welcome to \nredistribute it under certain "); printf("conditions as \noutlined in the GNU Public License.\n\n"); exit(0); } |