Programming Freaks  | دورات ومقالات برمجيه

Please login or register.

Login with username, password and session length
Advanced search  

News:

Please Read our FAQ

Author Topic: I was drunk when i commited this crime  (Read 1044 times)

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
I was drunk when i commited this crime
« on: November 22, 2008, 06:16:37 AM »

 :P
take a look at this code
Code: ($2) [Select]
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <unistd.h>
#include <fcntl.h>


int main(int argc , char **argv)
{
        int fd;
        long int number1 , number2 , number3;
        char op;
        char *msg = "fuck";

        if(!argv[1])
        {
                printf("%s pipe_name\n",argv[0]);
                _exit(0);
        }

        fd = open(argv[1] , O_RDONLY);

        if(fd == -1)
        {
                fprintf(stderr , "An error has occured %s\n",strerror(errno));
                _exit(1);
        }

        close(0); //close the stdin fd

        if(dup2(fd , 0) == -1)
        {
                fprintf(stderr , "An error has occured %s\n",strerror(errno));
                _exit(1);
        }

        close(fd);

        //now i can do what ever i want :D

        scanf("%d %c %d",&number1 , &op , &number2);
        printf("%d %d %c\n",number1 , number2 , op);

        switch(op)
        {
                case '+':
                        number3 = number1 + number2;
                        break;
                default:
                        {
                                char *ptr = (char *)&number3;
                                int x;
                                for(x =0 ; x<5 ; x++)
                                {
                                        ptr[x] = msg[x];
                                }
                        }
                        break;
        }


        if(number3 > 0 && number3 < 9)
        {
                printf("Sum is %ld\n",number3);
        }else
        {
                printf("%s\n",(char *)&number3);
        }

        return 0;
}

here is the make file for it

Code: ($2) [Select]
readpipe : read.o
        gcc -o readpipe read.c
.PHONY : install
.PHONY : clean

install:
        mkfifo pipe

clean :
        rm piperead *.o
are we set ?
try it

readpipe pipe

from another terminal window do this
echo "1 + 1" > pipe
and see the result
do it again with "1 - 1" and let me see
Logged