1

I have to print the day for a given date. For example if output is 18 5 2014 , output should be Sunday . I wrote my program like this:

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

int main()
{
    int d,m,y ;
    char command[20];

    printf("Enter d: ");
    scanf("%d",&d);
    printf("Enter m: ");
    scanf("%d",&m);
    printf("Enter y: ");
    scanf("%d",&y);

    sprintf(command , "date -d %d/%d/%d | cut -b 1-3 ",m,d,y );

    system(command) ;
    return 0 ;
}

But output is a little bit scary:

Sat
*** stack smashing detected ***: ./a.out terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7fd1e8d89f47]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x0)[0x7fd1e8d89f10]
./a.out[0x400743]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fd1e8ca076d]
./a.out[0x4005a9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:05 21758155                           /home/shahjahan/a.out
00600000-00601000 r--p 00000000 08:05 21758155                           /home/shahjahan/a.out
00601000-00602000 rw-p 00001000 08:05 21758155                           /home/shahjahan/a.out
0240d000-0242e000 rw-p 00000000 00:00 0                                  [heap]
7fd1e8a69000-7fd1e8a7e000 r-xp 00000000 08:05 790189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd1e8a7e000-7fd1e8c7d000 ---p 00015000 08:05 790189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd1e8c7d000-7fd1e8c7e000 r--p 00014000 08:05 790189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd1e8c7e000-7fd1e8c7f000 rw-p 00015000 08:05 790189                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd1e8c7f000-7fd1e8e34000 r-xp 00000000 08:05 786443                     /lib/x86_64-linux-gnu/libc-2.15.so
7fd1e8e34000-7fd1e9034000 ---p 001b5000 08:05 786443                     /lib/x86_64-linux-gnu/libc-2.15.so
7fd1e9034000-7fd1e9038000 r--p 001b5000 08:05 786443                     /lib/x86_64-linux-gnu/libc-2.15.so
7fd1e9038000-7fd1e903a000 rw-p 001b9000 08:05 786443                     /lib/x86_64-linux-gnu/libc-2.15.so
7fd1e903a000-7fd1e903f000 rw-p 00000000 00:00 0 
7fd1e903f000-7fd1e9061000 r-xp 00000000 08:05 786555                     /lib/x86_64-linux-gnu/ld-2.15.so
7fd1e9249000-7fd1e924c000 rw-p 00000000 00:00 0 
7fd1e925c000-7fd1e9261000 rw-p 00000000 00:00 0 
7fd1e9261000-7fd1e9262000 r--p 00022000 08:05 786555                     /lib/x86_64-linux-gnu/ld-2.15.so
7fd1e9262000-7fd1e9264000 rw-p 00023000 08:05 786555                     /lib/x86_64-linux-gnu/ld-2.15.so
7fffe219c000-7fffe21bd000 rw-p 00000000 00:00 0                          [stack]
7fffe21fe000-7fffe2200000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

The output should be only the first line but why I am getting like this? Please help me.

1
  • 2
    you try to put more than 20 bytes into the command and that might smash your stack Commented May 18, 2014 at 14:47

2 Answers 2

1

You should have used proper size for your command string. The below program should work.

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

int main()
{
    int d,m,y ;
    char command[1000];

    printf("Enter d: ");
    scanf("%d",&d);
    printf("Enter m: ");
    scanf("%d",&m);
    printf("Enter y: ");
    scanf("%d",&y);

    sprintf(command , "date -d %d/%d/%d | cut -b 1-3",m,d,y );

    system(command) ;
    return 0 ;


}
Sign up to request clarification or add additional context in comments.

Comments

0

The size of the array command is 20. But "date -d %d/%d/%d | cut -b 1-3",m,d,y is more then 20 character long. So you need to increase the size of the command array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.