Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Playing God in C.

Options
  • 27-11-2001 9:32pm
    #1
    Closed Accounts Posts: 5,564 ✭✭✭


    Is this program alive?
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(int argc,char**argv)
    {
     int file1;
     int file2;
     int a=1,b;
     char m_buf[5239];  //you may have to modify this number all the way down to get a proper run.
     char myname[4096],mynewname[4096];
     char sysexec[256];
     char num[256];
     char*exz[3];
     
     snprintf(myname,sizeof(myname),"norm");
     snprintf(mynewname,sizeof(mynewname),"norm1");
     snprintf(num,sizeof(num),"%d",a);
     if(argv[1]!=NULL)
     { 
    	 b=atoi(argv[1]);
     	a=b+1;
     	snprintf(myname,sizeof(myname),"norm%d",b);
     	snprintf(mynewname,sizeof(mynewname),"norm%d",a);
    	 snprintf(num,sizeof(num),"%d",a);
     	if(a==13)
     	 exit(0);
     };
    
     file1=open(myname,O_RDONLY);
     read(file1,m_buf,5239);
     close(file1);
     
     file2=open(mynewname,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IXUSR);
     write(file2,m_buf,5239);
     close(file2);
     
     snprintf(sysexec,sizeof(sysexec),"%s",mynewname);
     exz[0]=sysexec;
     exz[1]=num;
     exz[2]=NULL;
     execv(exz[0],exz);
    return 0;
    };
    

    Well?


Comments

  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Typedef ... do you happen to have a copy of the dirent.h file handy??

    Also .. define "alive"??


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    hmm check your email, but seeing as how I am using an absolute value for the size of the file norm above may compile fine if you just remove dirent.h anyhow.

    Alive
    Ability to consume, replicate - Fundamental
    Fire and crystal structures are they alive? Maybe by some loose definitions, but if norm has unlimitted disk capacity and we allow him/her/it to iterate past 13 is it not in a sense alive?

    hmm....


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Originally posted by Typedef
    hmm check your email, but seeing as how I am using an absolute value for the size of the file norm above may compile fine if you just remove dirent.h anyhow.

    Boards.ie doesn't seem to like my email address so you're gonna have to send it here

    Alive
    Ability to consume, replicate - Fundamental
    Fire and crystal structures are they alive? Maybe by some loose definitions, but if norm has unlimitted disk capacity and we allow him/her/it to iterate past 13 is it not in a sense alive?

    hmm....

    An interesting theory except for one thing .....

    fire is not alive. it's simply a chemical reaction. Of course you could argue that we're cehmical reactions too .. but not really if you're following my thinking??


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    True but,
    Fire feeds,grows,reproduces and changes itself to suit it's environment, you could make a strong case for fire being a life form.


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Originally posted by Typedef
    Fire feeds,grows,reproduces

    I don't entirely agree with that ...

    It feeds most certainly. It grows - only as by as much fuel is available to feed it. We can grow beyond feeding (granted .. not as good).
    It doesn't reproduce. It consumes.
    changes itself to suit it's environment,

    The only reason it is able to "adapt" (if that's even the right word) to its environment is because it is stateless. Water follows the same property. Does that mean water is alive??

    All in all though, certainly a question that makes you stop and think. :)


  • Advertisement
  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Characteristics of Life:

    Movement
    Reproduction
    Sensitivity
    Growth
    Respiration
    Excretion
    Nutrition

    Now, back on topic, what's the problem with this code?

    Compiles fine here after fixing:

    // C++ comment (don't be lazy)

    "O_RDWR|O_CREAT|O_TRUNC,S_IRUS
    R|S_IWUSR|S_IXUSR);"

    and semi-colon after main(){};

    And you really should be #defining numbers like 5239

    kids...

    Al.


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Originally posted by Trojan
    kids...

    yes father :p

    heheheheh :D


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Originally posted by Trojan
    Characteristics of Life:

    // C++ comment (don't be lazy)

    "O_RDWR|O_CREAT|O_TRUNC,S_IRUS
    R|S_IWUSR|S_IXUSR);"

    and semi-colon after main(){};

    And you really should be #defining numbers like 5239

    kids...

    Al.

    Hmm I should probably be finding the size of the program dynamically instead of making the user append the number of bytes to be read ie if the compiled binary norm is 6012 bytes then we should read and write 6012 bytes and this should probably be done by asking the operating system how many bytes the file takes up on the disc but.... I guess I would like the program to be as small as possible.

    Now all I have to do is extend the princpals of this program to include a compiler so it can dynamically rewrite and compile itself and take over the... I mean flourish into a true form of AI... hehe.


  • Subscribers Posts: 1,911 ✭✭✭Draco


    unless it mutated along the way it wouldn't ever develop into an AI... ;)
    Wasn't there a game people used to play on mainframes where they wrote programs to try and take over as much memory as possible while preventing other programs from doing so?


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Originally posted by Typedef


    Hmm I should probably be finding the size of the program dynamically instead of making the user append the number of bytes to be read ie if the compiled binary norm is 6012 bytes then we should read and write 6012 bytes

    Jeez, use a variable so, you know what I meant, not the same bloody magic number everywhere.

    Originally posted by Typedef

    and this should probably be done by asking the operating system how many bytes the file takes up on the disc but.... I guess I would like the program to be as small as possible.

    Haven't done much file stuff, but can't you use fseek() and ftell() to do that? Hm...
    Originally posted by Typedef

    Now all I have to do is extend the princpals of this program to include a compiler so it can dynamically rewrite and compile itself and take over the... I mean flourish into a true form of AI... hehe.

    Parsed originally as "true form of Al". :)

    Al.


  • Advertisement
  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Originally posted by Draco
    unless it mutated along the way it wouldn't ever develop into an AI... ;)
    Wasn't there a game people used to play on mainframes where they wrote programs to try and take over as much memory as possible while preventing other programs from doing so?

    Yeah I saw it before in either FOLDOC[0] or The Jargon File[1]...

    Al.

    1. http://www.foldoc.org/
    2. http://www.tuxedo.org/~esr/jargon/


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Yes commonly called Core Wars is it not?
    http://www.koth.org/


  • Registered Users Posts: 455 ✭✭Lyconix


    Errrrrrrrm.... umm.....

    AI is something that I've always wanted to program (with the secret goal of accidentally creating an AI that expands across the Net and takes ove... I mean, making a really smart, HARMLESS AI! yeah!)

    But only one problemo - I don't know much C++ yet. Typedef, how did you learn C++ and what compiler do you use?


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    %gcc -v
    Using builtin specs.
    gcc version 2.95.3 20010315 (release) [FreeBSD]
    Does this answer your question?

    I supposed if I were in windows and the relevant libraries had not been ported to (mingw) gcc/g++ I might use the micR0$ith cl compiler but... I mean why is it the microsith cl compiler needs the exe to be 28k just to print hello world when a gcc compiled version will come in at what? 5/6k?

    hmm, how did I learn C/C++?
    I don't know if I really know C++ but, you might want to check out
    www.cprogramming.com
    www.bloodshed.net (if using windows)
    nehe.gamedev.net

    ftp.esat.net - Get Linux or FreeBSD. if you are serious about C/C++ or just about any kind of programming bar VB (though you may be able to run some basic using gwbasic on Unix-like systems), then I don't know if I could think of a better environment to learn these things in? Look at the Chinese, isn't Red-Hat Linux what is being used in the Chinese universities now?
    Keifer Sutherland
    How could a billion Chinese people be wrong?

    If you need Linux CD's or BSD CD's or help installing/configuring then I will help with install and config and supply of CD if need be, within the gReater Dublin Area.


  • Registered Users Posts: 455 ✭✭Lyconix


    But what about Visual C++... isn't that the compiler to go for Typedef?


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Depends I suppose.
    Do I feel a burning desire to pay £500.00 for a bloatware compiler?
    If there were a compelling reason I had to use the VC++ ide and cl compiler then I guess I would, but still (not that I condone warez :rolleyes: that would be wrong :rolleyes: ) but if I could get hold of a low-cost :rolleyes: version of VC++ and didn't mind devoting 300-400 mb of disk space to complier & libs and headers and ide and I couldn't get something working with gcc and it would work with the m$_cl then I wouldn't have a choice really would I?

    So I suppose if you really really have to use VC++ then try to get a ;) low-cost ;) version from your friends or something, I don't know, a :rolleyes: ;) second-hand version maybe ? ;) :rolleyes:


Advertisement