Posted: . At: 9:13 PM. This was 12 years ago. Post ID: 4404
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.

Cool C programming trick, run /bin/sh with the C execve() function.

This programming trick is pretty cool, you can run the /bin/sh shell with a C program. The execve() function is useful for running a command within a C program and passing arguments to it, but I have replaced the arguments with NULL instead, that is what you put when you do not need arguments.

#include <stdlib.h>
#include <unistd.h>
 
int main(void)
{
        execve("/bin/sh", NULL, NULL);
        return 0;
}

This programming example will print the date and time. This would be useful in a program for sure. This is the proper way to do this.

#include <stdio.h>
#include <time.h>	// For time function (random seed).
 
#define format "The time and date is: %A %d %B %Y. The time is: %H:%M:%S, %Z."
 
int print_time(void) {
 
	struct tm *ptr;
	time_t tm;
	char length[60];
	tm = time(NULL);
	ptr = localtime(&tm);
	strftime(length, 100, format, ptr);
 
	printf("%s\n", length);
 
}
 
int main () {
    print_time();
 
    return 0;
}

And finally, an example of a C program that will write to a logfile. Very simple.

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
 
/********************************************************************
* Description:
* Author:  <shoggoth>
* Created at: Wed Jan 19 13:06:21 EST 2011
* Computer: myhost
* System: Linux 2.6.33-ARCH on x86_64
*
* Copyright (c) 2011   All rights reserved.
*
********************************************************************/
 
#include <stdio.h>
#include <time.h>
 
#define format "At this time: %H:%M:%S"
#define text "This is an entry in the log."
 
int lineofstars (void) {
	int x = 0;
	while (x < 64) {
		printf("*");
		x++;
		if (x == 31) {
			printf("<|>");
		} else if (x == 64) {
			printf("\n-\n");
		}
	}
	return 0;
}
 
int main (int argc, char** argv) {
 
	lineofstars();
 
	char *File;
	char String[60];
	struct tm *ptr;
	time_t tm;
	char length[60];
 
	tm = time(NULL);
	ptr = localtime(&tm);
	strftime(length, 100, format, ptr);
 
	File = "log.txt";
	snprintf(String, 100, "%s, %s\n", length, text);
 
	FILE *f;
	f = fopen (File, "a+");
 
	if (!f) {
		printf("Sorry, I cannot open the file %s.\n", File);
		return 0;
	}
 
	fprintf(f, String);
 
	fflush(stdout);
	fclose(f);
 
	return 0;
}

Leave a Comment

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