Exit program in Eiffel - eiffel

Is there a way in Eiffel to exit a program, possible with a defined exit code like exit in C?
In my case I would like to just end the program like exit(0) would do.

Yes, you can terminate the program returning the exit code n with
{EXCEPTIONS}.die (n)
If you are using an older version of the compiler/libraries where the code above does not compile, the same can be achieved with
;(create {EXCEPTIONS}).die (n)

Related

Are there any alternatives to err() that do not terminate/exit program?

Is there an alternative to the err() family of functions found in <err.h> that displays the program name, a semicolon, a space, and the error message... without exiting the program?
Pseudocode
I want
print "<program name>: <error message>"
and not
print "<program name>: <error message>"
exit program
return <status>
EDIT: I cannot use argv[0] (as the program name) to write out the message myself, as I am writing a library
program_invocation_name works just as well for getting the name of the program, as Erdal Küçük mentioned. From this, we can print the full error message, program name included.
The variable is part of glibc and can be retrieved using:
extern char *program_invocation_name;
Because it is not a part of the C standard library, programs using this variable should not be expected to be portable.
See man error.
error is the same as err but will return if status == 0, exit otherwise.
CONFORMING TO
These functions and variables are GNU extensions, and should not be used in programs intended to be portable.
The warn/warnx functions from err.h will do the same thing as err, but will return.
For example:
warnx("message here");
puts("I'm still running!");
Output:
a.out: message here
I'm still running!

How to step C statements in GDB after going into bootmain() from bootasm.S in xv6?

I am using gdb to debug boot loader code in xv6. In bootasm.S, I can use gdb to debug assembly code as expected. Then at the end of bootasm.S, we will call bootmain (line 9168, call bootmain) and the execution will go into the bootmain function of the C code bootmain.c. Since it is a C code, I wish to step over the C statements instead of assembly. I know that the step command of gdb is for this purpose, but after I input 's', I received an error message: Cannot find bounds of current function. The same error shows up no matter I input 's' before or after the call bootmain instruction. So, is it possible to step C statements in gdb when the execution switches from an assembly source to a C source like the situation described above? If it is, how to do that? Thank you.
PS1: The list command only list the main() source of the kernel.
PS2: The remote terminal ran make qemu-gdb to start the QEMU, and the local terminal ran gdb kernel to start the debug session, following steps here. I think the C source code symbols like bootmain function should be included so I should be able to step the C code in it.
Thank you again for your help!

How to remove this line in language C : "Program ended with exit code: 0"

I wrote a code in C and it executed perfectly but at the end I got a line saying "Program ended with exit code: 0". What does this line signify and how do I get rid of this?
What it is?
an exit code of 0 indicates no error. If a program wants to
indicate there was something wrong when it exited it will exit with
a non-zero value.
How to get rid of it?
Do not use the IDE for program execution. – as BLUEPIXY said.
Basically what it means is that the program ran without any error. A 0 usually says that the program executed properly. Any nonzero number denotes some problem. Use text editors such as emacs or vim etc. and compile and execute your program.
The message doesn't come from your program itself, but from the IDE (e.g. Visual Studio) that launches the program. Try launching it from the command-line or by double-clicking the .exe file.
The meaning of "exit code 0" is usually: Everything went fine, the program exited successfully. Other exit codes usually stand for aborts because of errors and the actual number gives a hint about the type of error that occurred.

Exiting a C program

I am new to C programming. In my program below, I am simply trying to immediately, exit the C program without see any additional dialog, if the programs receives the input "quit".
I am trying to accomplish this using exit(0); however, before the program exits it outputs something like
success
process exited with return value 0
Press any key to continue...
I am trying to avoid this dialog and exit the program immediately. Is this possible?
I appreciate any help with this.
Many thanks in advance!
My C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char command1[256], command2[256];
printf("# ");
scanf("%s", command1);
if(strcmp(command1,"quit")==0){
printf("success");
exit(0);
}else{
printf("unknown command");
}
system("PAUSE");
return 0;
}
The message that you see is actually generated by the Visual Studio debugger. It's not really coming from your program.
If you would like to verify that your program is not actually displaying any message (nor waiting for a key press) just try running it from a windows command prompt. You may also try running the program in "Release" mode from withing Visual Studio. That will also confirm this.
The reason the debugger displays that information is just to help you understand what is going on with your program.
Can you post details of your execution environment? Seems like your process is being monitored for an exit code by another application (specialized shell perhaps) which is printing the "Press any key to continue" line
The process exited with return value 0 certainly isn't coming from your code, rather a program in the middle of your input and the output.
I compiled this on the command line (Mac OSX) and was presented with the following output:
James:Desktop iPhone$ gcc code.c
James:Desktop iPhone$ ./a.out
# quit
successJames:Desktop iPhone$
Note that I didn't reach the system("PAUSE"); either
That output doesn't come from YOUR program, it comes from the program that runs your program. Most likely "Visual Studio", but I expect some other types of IDE's may do similar things.
If you are using Dev-C++ and you would like to get rid of the message, do this:
Tools Menu -> Environment Options -> General tab
Then uncheck the Pause Program after return option.
Source: http://www.cplusplus.com/forum/general/89249/

How to set timeout for gcc compilation of .c file on linux

I am trying to compile a C code through my php script using gcc on a linux machine. But if the C code contains an infinite loop , the compiler gets stucked.
my PHP script goes like this ..
shell_exec('gcc input.c -o output 2> compile.txt');
$output=shell_exec('./output');
The 'input.c' file goes like this..
#include<stdio.h.
void main()
{
while(1)
prinf("This is infinite loop");
}
When I compile the PHP code , it gives an
**PHP Fatal error: Out of memory (allocated 403177472) (tried to allocate 804896768 bytes)**
I've tried timeout 5s :
$output=shell_exec('./output');
but it doesn't work although it works if i do ..
int i;
while(1)
{
scanf("%d",&i);
}
..in my input.c file. I have seached a lot but in vain..
What am I doing wrong.?
The first point is that, it is not the compiler which is getting stuck. The compiler compiles code fine. It is during execution that your code is going into a infinite loop. So, incase of your first code, it just keeps executing without any pause or exit and hogs the CPU all the time which gives a feeling of "being stuck".
With regard to how your second code works, it is because you are pausing for getting an input. But, again, in that case, you have failed provide an exit for your program. So, effectively, the second code is same as your first code, but it just doesn't hog the CPU.
There is only one way for you to correct this problem, that is by modifying your 'C' code not to have an infinite loop without any exit.
Example:
int i;
while(1)
{
scanf("%d",&i);
if(0 == i)
{
break; //Just an example as to how you can exit from an infinite loop
}
}
You can use ulimit in the invocation of the program. E.g. the following will limit output to 1 second of CPU time:
shell_exec("ulimit -t 1; ./output")
It might be a good idea to limit other resources too, so that output can't use all your memory etc.
(Note that this limits CPU time, not absolute time. If you want to do that, you should look at a program like timelimit, which would be called like shell_exec("timelimit -T 1 ./output"), I think.)
Jay explains much of it very well. The rest of it is that your first program not only goes into an infinite loop, but also produces an infinite amount of output, all of which you try to store in $output. I'd say you're lucky you didn't crash your computer, but PHP probably saved you with a built-in memory limit.
What you are looking for is possible theoretically:
You can read about the halting problem
However, you can probably use something like PHP's set_time_limit

Resources