Is there a way to make a printing string literally in C, it has? [duplicate] - c

This question already has answers here:
How to escape the % (percent) sign in C's printf
(13 answers)
Closed 1 year ago.
fprintf(fptr, "#NoEnv SetWorkingDir %A_ScriptDir% CoordMode, Mouse, Window SendMode Input #SingleInstance Force SetTitleMatchMode 2 #WinActivateForce SetControlDelay 1 SetWinDelay 0 SetKeyDelay -1 SetMouseDelay -1 SetBatchLines -1 Loop { Sleep, 10 CoordMode, Pixel, Screen PixelSearch, FoundX, FoundY, 1324, 589, 1324, 589, 0x00786A, 0, RGB If ErrorLevel = 0 { Sleep, 1000 Click, 696, 728 Left, 1 Sleep, 10 Sleep, 300 Click, 775, 726 Left, 1 Sleep, 10 Sleep, 300 Click, 1273, 590 Left, 1 Sleep, 10 Return } else { sleep, 4000 Send {f5} sleep, 3000 Click, 1324, 589, 0 sleep, 10 }} Return")"");
Here is my code, not sure if it would work for what I'm doing, but it believes that the % and other stuff in the quotations are meant to be variables or whatever, how do I make it so it ignores all of that and just prints it?

fprintf stands for File Print Formatted.
In order to print unformatted, consider using fputs

You need to escape the %. You can do this by writing %% instead of %.

Related

SWI-Prolog, Read the file and sum the numbers in the file

I am learning prolog and have the following problem:
Reads an input file, line by line. Then write the sum of each line to the output file.
Given an input.txt input file of the following form:
1 2 7 4 5
3 1 0 7 9
Each entry line are integers separated by a space.
? - calculate (‘input.txt’, ’output.txt’).
Here is the content of the output.txt file:
19
20
I have tried many ways but still not working, hope someone can help me
go :-
setup_call_cleanup(
open('output.txt', write, Out),
forall(file_line_sum('input.txt', Sum), writeln(Out, Sum)),
close(Out)
).
file_line_sum(File, Sum) :-
file_line(File, Line),
line_sum(Line, Sum).
line_sum(Line, Sum) :-
split_string(Line, " ", "", NumsStr),
maplist(string_number, NumsStr, Nums),
sum_list(Nums, Sum).
string_number(Str, Num) :-
number_string(Num, Str).
file_line(File, Line) :-
setup_call_cleanup(
open(File, read, In),
stream_line(In, Line),
close(In)
).
stream_line(In, Line) :-
repeat,
read_line_to_string(In, Line1),
(Line1 == end_of_file -> !, fail ; Line = Line1).
Contents of input.txt:
1 2 7 4 5
3 1 0 7 9
123 456 7890
Result in swi-prolog:
?- time(go).
% 115 inferences, 0.001 CPU in 0.001 seconds (90% CPU, 195568 Lips)
Generated output.txt:
19
20
8469

Why does fork() fail on MacOs Big Sur if the executable that runs it is deleted?

If a running process's executable is deleted, I've noticed fork fails where the child process is never executed.
For example, consider the code below:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
sleep(5);
pid_t forkResult;
forkResult = fork();
printf("after fork %d \n", forkResult);
return 0;
}
If I compile this and delete the resulting executable before fork is called, I never see fork return a pid of 0, meaning the child process never starts. I only have a Mac running Big Sur, so not sure if this repros on other OS's.
Does anyone know why this would be? My understanding is an executable should work just fine even if it's deleted while still running.
The expectation that the process should continue even if the binary was deleted is correct, however not fully correct in case of macOS. The example is tripping on a side-effect of the System Integrity Protection (SIP) mechanism inside the macOS kernel, however before explaining what is exactly going on, we need to make several experiments which will help us to better understand the whole scenario.
Modified example to better demonstrate the issue
To demonstrate what is going on, I had modified the example to count to 9, than do the fork, after the fork, the child will print a message "I am done", wait 1 second and exit by printing the 0 as the PID. The parent will continue to count to 14 and print the child PID. The code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
for(int i=0; i <10; i++)
{
sleep(1);
printf("%i ", i);
}
pid_t forkResult;
forkResult = fork();
if (forkResult != 0) {
for(int i=10; i < 15; i++) {
sleep(1);
printf("%i ", i);
}
} else {
sleep(1);
printf("I am done ");
}
printf("after fork %d \n", forkResult);
return 0;
}
After compiling it, I have started the normal scenario:
╰> ./a.out
0 1 2 3 4 5 6 7 8 9 I am done after fork 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 after fork 4385
So, the normal scenario works as expected. The fact that we see the count from 0 to 9 two times, is due to the copy of the buffers for stdout that was done in the fork call.
Tracing the failing example
Now is time to do the negative scenario, we will wait for 5 seconds after the start and remove the binary.
╰> ./a.out & (sleep 5 && rm a.out)
[4] 8555
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 after fork 8677
[4] 8555 done ./a.out
We see that the output is only from the parent. Since the parent had counted to 14, and shows valid PID for the child, however the child is missing, it never printed anything. So, the child creation failed after the fork() was performed, otherwise fork() would have received and error instead of a valid PID. Traces from ktrace reveal that the child was created under the pid and was waken up:
test5-ko.txt:2021-04-07 13:34:26.623783 +04 0.3 MACH_DISPATCH 1bc 0 84 4 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623783 +04 0.2 TMR_TimerCallEnter 9931ba49ead1bd17 0 330e7e4e9a59 41 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623783 +04 0.0(0.0) TMR_TimerCallEnter 9931ba49ead1bd17 0 330e7e4e9a59 0 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623783 +04 0.0 TMR_TimerCallEnter 9931ba49ead1bd17 0 330e7e4e9a59 0 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623854 +04 0.0 imp_thread_qos_and_relprio 88775d 20000 20200 6 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623854 +04 0.0 imp_update_thread 88775d 811200 140000100 1f 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623855 +04 0.1(0.8) imp_update_thread 88775d c15200 140000100 25 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623855 +04 0.0(1.1) imp_thread_qos_and_relprio 88775d 30000 20200 40 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623855 +04 0.0 imp_thread_qos_workq_override 88775d 30000 20200 0 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623855 +04 0.0 imp_update_thread 88775d c15200 140000100 25 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623855 +04 0.1(0.1) imp_update_thread 88775d c15200 140000100 25 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623855 +04 0.0(0.2) imp_thread_qos_workq_override 88775d 30000 20200 40 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623857 +04 1.3 TURNSTILE_turnstile_added_to_thread_heap 88775d 9931ba6049ddcc77 0 0 888065 2 a.out(8677)
test5-ko.txt:2021-04-07 13:34:26.623858 +04 1.0 MACH_MKRUNNABLE 88775d 25 0 5 888065 2 a.out(8677)
t
So the child's process was dispatched with MACH_DISPATCH and made runnable with MACH_MKRUNNABLE. This is the reason the parent got valid PID after the fork().
Further more the ktrace for the normal scenario shows that the process had issued BSC_exit and and imp_task_terminated system call occurred, which is the normal way for a process to exit. However, in the second scenario where we had deleted the file, the trace doesn't show BSC_exit. This means that the child was terminated by the kernel, not by a normal termination. And we know that the termination happend after the child was created properly, since the parent had received the valid PID and the PID was made runnable.
This bring us closer to the understanding of what is going on here. But, before we have the conclusion, let's show another even more "twisted" example.
Even more strange example
What if we replace the binary on the filesystem after we started the process?
Here is the test to answer this question: we will start the process, remove the binary and create an empty file with the same name on his place with touch.
╰> ./a.out & (sleep 5 && rm a.out; touch a.out)
[1] 6264
0 1 2 3 4 5 6 7 8 9 I am done after fork 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 after fork 6851
[1] + 6722 done ./a.out
Wait a minute, this works!? What is going on here!?!?
This strange example gives us important clue that will help us to explain what is going on.
The root-cause of the issue
The reason why the third example works, while the second one is failing, reveals a lot of what is going on here. As mentioned on the beginning, we are tripping on a side-effect of SIP, more precisely on the runtime protection mechanism.
To protect the system integrity, SIP will examine the running processes for the system protection and special entitlement. From the apple documentation: ...When a process is started, the kernel checks to see whether the main executable is protected on disk or is signed with an special system entitlement. If either is true, then a flag is set to denote that it is protected against modification. Any attempt to attach to a protected process is denied by the kernel...
When we had removed the binary from the filesystem, the protection mechanism was not able to identify the type of process for the child nor the special system entitlements since the binary file was missing from the disk. This triggered the protection mechanism to treat this process as an intruder in the system and terminate it, hanse we had not seen the BSC_exit for the child process.
In the third example, when we created dummy entry on the file system with touch, the SIP was able to detect that this is not a special process nor it has special entitlements and allowed the process to continue. This is a very solid indication that we ware tripping on the SIP realtime protection mechanism.
To prove that this is the case, I have disabled the SIP which requires a restart in the recovery mode and executed the test
╰> csrutil status
System Integrity Protection status: disabled.
╰> ./a.out & (sleep 5 && rm a.out)
[1] 1504
0 1 2 3 4 5 6 7 8 9 I am done after fork 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 after fork 1626
Conclusion
So, the whole issue was caused by the System Integrity Protection. More details can be fond in the documentation
All the SIP needed was to have a file on the filesystem with the process name, so the mechanism can run the verification and decide to allow the child to continue the execution. This is showing us that we are observing a side-effect, rather than designed behavior, since the empty file was not even a valid dwarf, yet the execution had proceed.

autohotkey mouse click selecting wrong place

I made this code to copy and paste some data with autohotkey and according to the documentation I have done it correctly but the mouse click start from y=0 rather than the number i sat it before. the loop works fine it increments by 30px each time but I need to make it start from the number specified.
^j::
x_increment := 100
y_increment := 30
Loop, 15
{
clipboard := "" ; Start off empty to allow ClipWait to detect when the text has arrived.
y:= 175
y=y_offset
x:= 173
WinActivate, ahk_exe GoogleMapExtractor.exe
sleep 500
WinMaximize, ahk_exe GoogleMapExtractor.exe
Sleep, 500
MouseClick, left, x, y_offset, 1, 0, ,
Sleep, 100
MouseClick, left, x, y_offset, 1, 0, ,
Sleep, 100
MouseClick, left, x, y_offset, 1, 0, ,
Sleep, 500
Send ^a
sleep 500
Send ^c
sleep 500
;;;;;;;;;;;;;; EXCEL;;;;;;;;;;;;;;;;
WinActivate, ahk_exe Excel.exe
WinWaitActive, ahk_exe Excel.exe
Sleep 500
Send ^v
sleep 500
Send {Down}
sleep 500
y_offset+=y_increment
}
Return
The problem is with this line:
y=y_offset
Perhaps you meant to do this?
y += y_offset

Why rand() returns the same values when I run my program in a script ? [duplicate]

This question already has answers here:
Run rand() in C language and loop with Shell Scripts
(3 answers)
srand(time(NULL)) doesn't change seed value quick enough [duplicate]
(6 answers)
Closed 6 years ago.
I have a srand(time(NULL)) at the beginning of the execution, then I am doing
*pos_x = (rand() % HEIGHT);
*pos_y = (rand() % WIDTH);
to generate random numbers.
This works fine when I run my program manually, but when I lunch my program with this script :
#!/bin/bash
i="0"
team_number="0"
while [ $i -lt 30 ]
do
./lemipc `pwd` team_number
i=$[$i+1]
if (( $i % 10 == 0 ))
then
team_number=$[$team_number+1]
fi
echo "create process $i"
usleep 10000
done
I always get the same numbers for all the processes.
I even tried to add a usleep to fix this but it still doesn't work.
I get this for exemple :
97 51
create process 1
97 51
create process 2
97 51
create process 3
97 51
where 97 is pos_x and 51 pos_y.
Have an idea why ?
The time() call has only whole-second precision. If your programs all run the same second, they will all use the same seed.
You must add more entropy. Consider using the return value of getpid() if you have it, or else investigate the platform's random sources.
This is cause usleep is delaying the execution of the next script the amount of microseconds (10000 = 0.01 seconds). time(NULL) returns SECONDS, thus making each time(NULL) returning value the same. Increase the amount of time it's delayed

Correct usage of subroutine in commodore basic 4.0?

I have a subroutine which fills an array with "."s
In my main program I am trying to call this subroutine and then print the array; however, it doesn't seem to be working. I think I am incorrectly calling the subroutine?
This is my code:
subroutine:
1070 dim a$(x,x)
1080 for aa = 0 to x
1090 for bb = 0 to x
2000 a$(x,x)="."
2010 next
2020 next
main code:
10 input "please enter a number"; x
20 gosub 1070
30 for i = 1 to x
40 for j = 1 to x
50 print a$(i,j);
60 next
70 print
80 next
Nothing happens when run; but when I run it all in one program (not calling gosub) it works?
Any help?
In line #2000, I believe you want a$(aa,bb)=".", otherwise you're just hammering the same location with the initialization.
Also, and probably more important to your question, every GOSUB needs a RETURN to get back to the main line of execution. In your case, that's probably line 2030.

Resources