The program is and has always been intellectual property. Therefore, only approaching this issue from this side can try to sue. But to prove that your code is almost impossible. Roughly speaking, you do not even have the right to use the simplest cycles that you found in the book in your programs, for example:
for(int i=0; i<var; i++) doSomth();
Regarding patents, you can always register your creation abroad. Naturally, the patent will not be valid in Russia or other countries, depending on where they patented, but Vasily Pupkin will not be able to sell this program in the country whose patent you have received.
With regard to trademarks and names, they can also be registered. Usually there are local registers (for example, per country), and there are global, as the example was given, Apple, a trademark, a trademark is already registered and cannot be used. But only for products of the same specifics. Ie, for example, if you pick apples and call yourself Apple, you can also register this trademark and it will be completely legal and will not go at all as opposed to the existing one.
I also consider registering the code itself meaningless, I will repeat it again, even if it is registered, it will be very difficult to prove that your code was broken.
UPD
Here is another question and answer to it, which illustrates well what is happening with the author's code. @quyse is absolutely right, I spoke with a lawyer on this issue.