I got hacked around the end of last year. That is why I'm asking this question here. Ever since then, I've been using the users command every time I log into the root account, just to make sure I'm the only one here. I've been working through a programming book, Sam's Teach Yourself C++ For Linux In 21 Days. I read a chapter one day, and over the course of the next week or so I enter the listings. Listing12.10 gave me an output I didn't expect. Can anyone here tell me why this output is so...WRONG?
Code:
michael@caitlyn 12 $ cat listing12.10.cpp
//Listing 12.10 - strcpy, strncpy, strlen, and strcat
//2020-09-03
#include <iostream>
#include <string.h>
int main()
{
char String1[] = "No man is an island";
char String2[] = "";
char String3[] = "";
std::cout << "String1: " << String1 << std::endl;
strcpy(String2, String1);
std::cout << "String2: " << String2 << std::endl;
strncpy(String3, String1, 5); //not entire string
String3[5] = '\0'; //you need a null terminator
std::cout << "String3 after strcpy: " << String3 << std::endl;
std::cout << "String1 is " << strlen(String1) << " bytes long.\nString2 is " << strlen(String2)
<< " bytes long.\nString 3 is " << strlen(String3) << " bytes long." << std::endl;
strcat(String3, String1);
std::cout << "String3 after strcat: " << String3 << std::endl;
std::cout << "String1 is still " << strlen(String1) << " bytes long.\nString2 is still " << strlen(String2)
<< " bytes long\nString 3 is now " << strlen(String3) << " bytes long.\n";
return 0;
}
michael@caitlyn 12 $
Here is the output:
Code:
michael@caitlyn 12 $ ./listing12.10
String1: No man is an island
String2: No man is an island
String3 after strcpy: o man
String1 is 3 bytes long.
String2 is 4 bytes long.
String 3 is 5 bytes long.
String3 after strcat: o manman
String1 is still 6 bytes long.
String2 is still 7 bytes long
String 3 is now 8 bytes long.
And here is the expected output (from the book):
Code:
String1: No man is an island
String2: No man is an island
String3 after strncpy: No ma
String1 is 19 bytes long, String2 is 19 bytes long, and String3 is 5 bytes long.
String3 after strcat: No maNo man is an island
String1 is still 19 bytes long,
String25 is still 19 bytes long,
and String3 is now 24 bytes long.
If it is a Linux virus, (I've heard that Linux is virus-free,) can anyone tell me how to find it, ID it, and get rid of it? Otherwise, why is my program giving me such erroneous output? Thank you in advance to anyone who can help me solve this.
If it matters, this is the line I used to compile the code:
Code:
michael@caitlyn 12 $ g++ -Wall listing12.10.cpp -o listing12.10