exercise1-17: Write a program to print all input lines that are longer than 80 characters.
#include<stdio.h>
#define MAXLINE 1000
#define LIMIT 80
int getlines(char line[],int maxline);
void copy(char to[], char from[]);
/*print the lines more than 80*/
main()
{
int len ; /*current line length*/
char line[LIMIT]; /*current input line*/
char longer[LIMIT]; /*longer line saved here*/
while((len = getlines(line,MAXLINE))>0)
if(len > LIMIT){
copy(longer , line);
}
printf("%s", longer);
return 0;
}
/*getline:read a line into s,return length*/
int getlines(char s[],int lim)
{
int c,i;
for(i=0;i<lim-1 && (c=getchar()) != EOF && c != '\n';++i)
s[i] = c;
if(c == '\n')
{
s[i] = c;
++ i;
}
s[i] = '\0';
return i;
}
/*copy:copy 'from' into 'to'*/
void copy(char to[],char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++ i;
}
"longer.c" 50 lines, 856 characters
非常神奇的结果啊:
……………………………………………………………………………………………………………………………………………………………………………………………………………hhhhhhhhhhhhhhhh1234567890-0987654321qawsderfgtyhjuiklop;[poiuytredsxzaqwedcvfrtgbnhyujmkilo
*** stack smashing detected ***: ./a.out terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x50)[0x1f2390]
/lib/tls/i686/cmov/libc.so.6(+0xe233a)[0x1f233a]
./a.out[0x80484fa]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x126bd6]
======= Memory map: ========
00110000-00263000 r-xp 00000000 08:01 263984 /lib/tls/i686/cmov/libc-2.11.1.so
00263000-00264000 ---p 00153000 08:01 263984 /lib/tls/i686/cmov/libc-2.11.1.so
00264000-00266000 r--p 00153000 08:01 263984 /lib/tls/i686/cmov/libc-2.11.1.so
00266000-00267000 rw-p 00155000 08:01 263984 /lib/tls/i686/cmov/libc-2.11.1.so
00267000-0026a000 rw-p 00000000 00:00 0
00a60000-00a7d000 r-xp 00000000 08:01 133451 /lib/libgcc_s.so.1
00a7d000-00a7e000 r--p 0001c000 08:01 133451 /lib/libgcc_s.so.1
00a7e000-00a7f000 rw-p 0001d000 08:01 133451 /lib/libgcc_s.so.1
00bf0000-00bf1000 r-xp 00000000 00:00 0 [vdso]
00c9a000-00cb5000 r-xp 00000000 08:01 133393 /lib/ld-2.11.1.so
00cb5000-00cb6000 r--p 0001a000 08:01 133393 /lib/ld-2.11.1.so
00cb6000-00cb7000 rw-p 0001b000 08:01 133393 /lib/ld-2.11.1.so
08048000-08049000 r-xp 00000000 08:01 925403 /home/fuyi/a.out
08049000-0804a000 r--p 00000000 08:01 925403 /home/fuyi/a.out
0804a000-0804b000 rw-p 00001000 08:01 925403 /home/fuyi/a.out
09426000-09447000 rw-p 00000000 00:00 0 [heap]
b788f000-b7890000 rw-p 00000000 00:00 0
b789c000-b78a0000 rw-p 00000000 00:00 0
bfaa8000-bfabd000 rw-p 00000000 00:00 0 [stack]
hhhhhhhhhhhhhhhh1234567890-0987654321qawsderfgtyhjuiklop;[poiuytredsxzaqwedcvfrtAborted