Jump to content
Sign in to follow this  
markiemrboo

Oops, Gcc Is Dangerous :)

Recommended Posts

I was just messing around with connecting to a MySQL database in C and using pipes and forks to get text output from other console programs in to my program, linking to mysqlclient and what not. Got it all working and it was cool! But then...

 

gcc -c -lz -lmysqlclient -L/usr/local/lib/mysql/ -I/usr/local/include/mysql/ mysql.c -o mysql.c

 

Whoopsies......... can you see what I just managed to do there? I just tried to compile the source file over itself (outputting the result as mysql.c) and as a result that "deleted" the source file. That was very clever of me now, wasn't it :)

 

Just thought I would share my uber leet retardedness skillz!

Share this post


Link to post
Share on other sites

Heh.

 

In simple terms... it would go something like this:

 

I just wrote a 15 meg word document (of course, my code wasn't 15 meg... it was under 100 lines I expect), saved it .. but before I went to print it to hand in, I proceeded to open another copy of MS Word and saved over my lovely spanky completed 15 meg coursework document with a nice blank document :)

Share this post


Link to post
Share on other sites
Markie, if you're the good coder here... than we're all doomed.

503431[/snapback]

 

:lol: yeppers!!! Nah, the world will be fine. I was just tired. Missed out on a lot of sleep over the past couple of days :D

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

int main(void) {
       pid_t pid;
       int fd[2], bytesRead = 0;
       char *data;

       // not sure if read() takes a \0 in to account, so I will try be safe here and allocate 1 more byte than necessary?
       data = (char *)calloc(1, 101);
       assert(data != NULL);
       memset(data, 0, 101);

       if (pipe(fd) == -1) {
               fprintf(stderr, "pipe error\n");
               exit(1);
       }

       pid = fork();

       // parent
       if (pid) {
               // overwrite stdin
               dup2(fd[0], 0);
               close(fd[1]);

               // read 100 bytes from forked exec output
               // no checks for errors here
               while ((bytesRead = read(fd[0], data, 100)) > 0) {
                       printf("%s", data);
               }
       // child
       } else {
               // overwrite stdout
               dup2(fd[1], 1);
               close(fd[0]);
               // replace forked process with `scriptuptime`
               execl("/usr/local/bin/scriptuptime", "/usr/local/bin/scriptuptime");
       }

       return 0;
}

 

There's the pipe bit rewritten for.. anyone who gives a crap. execl runs scriptuptime, which is my hacked up version of `uptime` which... only prints the uptime stuff and one field per line, like...

 

days uptime

hours uptime

minutes uptime

seconds uptime

users logged in

load average - 5 min

load average - 10 min

load average - 15 min

 

like so

 

(mrboo|bone)/home/mrboo$ scriptuptime
17
4
47
22
2
0.02
0.06
0.02

 

and normally it'd look like

 

(mrboo|bone)/home/mrboo$ uptime
7:30PM  up 17 days,  4:47, 2 users, load averages: 0.02, 0.06, 0.02

 

Makes it a little easier to process in scripts and things that way :rolleyes: I use that for my "dynamic" sig image actually.

 

Can't be arsed to redo the MySQL stuff. Can't think of anything interesting to do with either the pipes or MySQL actually :) Joy!!!!

 

EDIT: Oh and that wont work on Windowsy systems I dont think. Windows doesn't appear to have fork()... CreateProcess() instead or something.

Share this post


Link to post
Share on other sites
:lol: yeppers!!! Nah, the world will be fine. I was just tired. Missed out on a lot of sleep over the past couple of days :D

....

EDIT: Oh and that wont work on Windowsy systems I dont think. Windows doesn't appear to have fork()... CreateProcess() instead or something.

503439[/snapback]

 

 

bah you and me both not getting sleep! sadly, we both know why :(

 

 

 

and darn my stewpid windows for not having the right cutlery! :lol:

Share this post


Link to post
Share on other sites
bah you and me both not getting sleep! sadly, we both know why :(

and darn my stewpid windows for not having the right cutlery! :lol:

503497[/snapback]

 

lol yeah, sucks eh?

 

haha! cutlery :) i'll have to make up knife() and spoon() .... and chopsticks() :) any ideas what each of those might want to do? :blink:

Share this post


Link to post
Share on other sites

well, the knife() can be modelled on my cheating ex....and stab people in the back when they least expect it. it could be a termination command of some sort?

Share this post


Link to post
Share on other sites

knife.c, just for mr fox :)

 

#include <stdio.h>

void knife(void);

int main(void) {
       knife();
}

void knife(void) {
       printf("
       O
      \\|/
 o-(---|--
      / \\ \n");
}

 

output (lets see if occ can screw this one up...)

 

(mrboo|bone)/home/mrboo$ ./knife

       O
      \|/
 o-(---|--
      / \

Share this post


Link to post
Share on other sites
wow looks just like me! :lol:

 

how'd SHE get on here?! :blink:

503523[/snapback]

 

:lol: minus all the important details.. like.. grey hair :)

 

here's spoon.c

 

#include <stdio.h>

void spoon(void);

int main(void) {
       spoon();
}

void spoon(void) {
       printf("There is no spoon.\n");
}

 

output (impossible for occ to screw up)

(mrboo|bone)/home/mrboo$ ./spoon
There is no spoon.

Share this post


Link to post
Share on other sites

oh my word :O

 

its the question that noone could answer!

 

the meaning of life: "there is no spoon"

 

 

I knew it!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...