473,800 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

catching exit

Hi there,

I am trying to reuse a piece of code that was designed as an
application. The code is covered with 'exit' calls. I would like to
reuse it as a library. For that I renamed the 'main' function into
'mymain', but I am stuck as to what I should do for the 'exit'.

AFAIK there is no portable way to catch the exit. The only thing I
can think of is atexit, but that does not work since 'exit' is still
called afterward.

What I am thinking now is that I need to replace all exit(val) with
longjmp(env, val). And make 'env' global.

Comments ?

Thanks
-Mathieu
Aug 13 '08
39 2831
CBFalconer wrote:
ja*********@ver izon.net wrote:
>CBFalconer wrote:
... snip ...
>>Which is easy. Simply set an error flag visible in the caller, and
return. That value may be the value returned by the function.
No, it's not that simple. Replacing

exit(status);
with
some_global = status;
return;

will not handle any of the numerous complications that have already
been mentioned elsewhere on this thread: memory leaks, files that were
not closed, at_exit() handlers (admittedly not a common problem). In
general, the kind of person who uses exit() doesn't merely ignore
memory leaks and unclosed files; such a person generally is actively
relying on exit() to handle those details. Such things can usually be
found in any program that calls exit().

Yes it will, provided that the calling function does those things.
I.e. the calling code is something like:

err = newfunct(...);
if (err & BADBITS) {
cleanupwhatever ();
decidewheretogo next();
}
else {
alliswellwithne wfunct();
...
}

It may be a pain in the butt to write 'cleanupwhateve r' and
'decidewheretog onext'. There is no problem replacing the calls to
exit with returning something, since the OP obviously has the
source code.
He didn't tell us how many different functions there were that called
exit(). He also didn't tell us how many different places in the program
that those functions are called from. However, if he's seriously
considering a myexit() function which calls longjmp(), I suspect that
there are probably at least a dozen such calls. If I understand what
you're suggesting properly (which is far from clear), he'll have to
figure out, at almost every place on the call chain connecting main() to
exit(), what the equivalent of cleanupwhatever (), decidewheretogo next()
and alliswellwithne wfunct() should be; in general, it will be different
at each of those locations.

It's certainly possible (I just recently got through doing something
similar), but I don't see how it can be described as "easy".
Aug 15 '08 #31
CBFalconer wrote:
mathieu wrote:
>CBFalconer <cbfalco...@yah oo.comwrote:
>>mathieu wrote:

I am trying to reuse a piece of code that was designed as an
applicatio n. The code is covered with 'exit' calls. I would like
to reuse it as a library. For that I renamed the 'main' function
into 'mymain', but I am stuck as to what I should do for the
'exit'.

AFAIK there is no portable way to catch the exit. The only thing
I can think of is atexit, but that does not work since 'exit' is
still called afterward.

What I am thinking now is that I need to replace all exit(val)
with longjmp(env, val). And make 'env' global.
Just leave it calling exit. The result is as follows:
... snip ...
>Thanks I also know how to read the man page of exit :)

That wasn't clear to me. Since the only things you can (portably)
return from main is EXIT_SUCCESS and EXIT_FAILURE (and 0), you have
much more latitude in a function. So simply return from the
function, returning a status, and let the caller decide what to do
about it. I see no point to the exit calls. Maybe you do.
I don't quite follow the point of your criticism. The exit() calls were
not put into the code by mathieu. He's trying to figure out what to
replace them with. What did he say to make you think he didn't
understand how exit() works? Are you assuming that the exit() calls are
coming from within main() itself? That's the only way I can make your
objections seem to make sense, but I can't figure out why you'd assume that.
Aug 15 '08 #32
James Kuyper wrote:
CBFalconer wrote:
>mathieu wrote:
>>CBFalconer <cbfalco...@yah oo.comwrote:
mathieu wrote:

I am trying to reuse a piece of code that was designed as an
application . The code is covered with 'exit' calls. I would
like to reuse it as a library. For that I renamed the 'main'
function into 'mymain', but I am stuck as to what I should
do for the 'exit'.
>
AFAIK there is no portable way to catch the exit. The only
thing I can think of is atexit, but that does not work since
'exit' is still called afterward.
>
What I am thinking now is that I need to replace all
exit(val) with longjmp(env, val). And make 'env' global.

Just leave it calling exit. The result is as follows:

... snip ...
>>Thanks I also know how to read the man page of exit :)

That wasn't clear to me. Since the only things you can
(portably) return from main is EXIT_SUCCESS and EXIT_FAILURE
(and 0), you have much more latitude in a function. So simply
return from the function, returning a status, and let the
caller decide what to do about it. I see no point to the exit
calls. Maybe you do.

I don't quite follow the point of your criticism. The exit()
calls were not put into the code by mathieu. He's trying to
figure out what to replace them with. What did he say to make
you think he didn't understand how exit() works? Are you
assuming that the exit() calls are coming from within main()
itself? That's the only way I can make your objections seem to
make sense, but I can't figure out why you'd assume that.
I didn't criticize, at least I don't think so. I made suggestions
about how to perform his port. If some of my assumptions about the
(never shown) code he wants to replace are false, then some of the
suggestions are pure blooey.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 15 '08 #33
On Aug 15, 8:26*am, CBFalconer <cbfalco...@yah oo.comwrote:
James Kuyper wrote:
CBFalconer wrote:
mathieu wrote:
CBFalconer <cbfalco...@yah oo.comwrote:
mathieu wrote:
>>>I am trying to reuse a piece of code that was designed as an
applicatio n. The code is covered with 'exit' calls. I would
like to reuse it as a library. For that I renamed the 'main'
function into 'mymain', but I am stuck as to what I should
do for the 'exit'.
>>>AFAIK there is no portable way to catch the exit. The only
thing I can think of is atexit, but that does not work since
'exit' is still called afterward.
>>>What I am thinking now is that I need to replace all
exit(val) with longjmp(env, val). And make 'env' global.
>>Just leave it calling exit. *The result is as follows:
... snip ...
>Thanks I also know how to read the man page of exit :)
That wasn't clear to me. *Since the only things you can
(portably) return from main is EXIT_SUCCESS and EXIT_FAILURE
(and 0), you have much more latitude in a function. *So simply
return from the function, returning a status, and let the
caller decide what to do about it. *I see no point to the exit
calls. *Maybe you do.
I don't quite follow the point of your criticism. The exit()
calls were not put into the code by mathieu. He's trying to
figure out what to replace them with. What did he say to make
you think he didn't understand how exit() works? Are you
assuming that the exit() calls are coming from within main()
itself? That's the only way I can make your objections seem to
make sense, but I can't figure out why you'd assume that.

I didn't criticize, at least I don't think so. *I made suggestions
about how to perform his port. *If some of my assumptions about the
(never shown) code he wants to replace are false, then some of the
suggestions are pure blooey.
As I said before it's is called 'PVRG JPEG 1.2.1' you can google for
it. Or see my integration in GDCM:

http://gdcm.svn.sourceforge.net/view...tilities/pvrg/

And yes there are *plenty* of exit all over the place (read: in
functions other than the original 'main').

$ grep exit\( * | wc
75 152 2479

Which are spread in different files:

$ grep -l exit\( * | wc
8 8 68

And no this code is not easy to follow (slightly easier to follow than
IJG, since less complex)

-M
Aug 15 '08 #34
CBFalconer wrote:
James Kuyper wrote:
>CBFalconer wrote:
>>mathieu wrote:
....
>>>Thanks I also know how to read the man page of exit :)
That wasn't clear to me. ...
....
I didn't criticize, at least I don't think so.
I think that the your comment cited above sounds fairly critical.

... I made suggestions
about how to perform his port. If some of my assumptions about the
(never shown) code he wants to replace are false, then some of the
suggestions are pure blooey.
He gave us a link to the code at <http://gdcm.sourceforg e.neton Thu,
14 Aug 2008 13:53:51 -0700 (PDT).

I don't know for sure what assumptions you made, but it has seemed to me
during this discussion that your comments made sense only if you were
assuming that all of the exit() calls were from within main(). I don't
think that assumption was ever plausible, given that his very first
message asked about the feasibility of solving his problem by replacing
exit() with longjmp(). That he asked that question doesn't prove that
exit() was being called from a subroutine - but it does make it pretty
likely. If he was just thinking about about using longjmp() to move
around main() itself, then replacing setjmp() with a statement label,
and longjmp() with a 'goto' would be a lot simpler.
Aug 15 '08 #35
mathieu wrote:
CBFalconer <cbfalco...@yah oo.comwrote:
>James Kuyper wrote:
>>CBFalconer wrote:
mathieu wrote:
CBFalcone r <cbfalco...@yah oo.comwrote:
>mathieu wrote:
>>
>>I am trying to reuse a piece of code that was designed as an
>>applicati on. The code is covered with 'exit' calls. I would
>>like to reuse it as a library. For that I renamed the 'main'
>>functio n into 'mymain', but I am stuck as to what I should
>>do for the 'exit'.
>>>
>>AFAIK there is no portable way to catch the exit. The only
>>thing I can think of is atexit, but that does not work since
>>'exit' is still called afterward.
>>>
>>What I am thinking now is that I need to replace all
>>exit(va l) with longjmp(env, val). And make 'env' global.
>>
>Just leave it calling exit. The result is as follows:

... snip ...
>
Thanks I also know how to read the man page of exit :)

That wasn't clear to me. Since the only things you can
(portably) return from main is EXIT_SUCCESS and EXIT_FAILURE
(and 0), you have much more latitude in a function. So simply
return from the function, returning a status, and let the
caller decide what to do about it. I see no point to the exit
calls. Maybe you do.

I don't quite follow the point of your criticism. The exit()
calls were not put into the code by mathieu. He's trying to
figure out what to replace them with. What did he say to make
you think he didn't understand how exit() works? Are you
assuming that the exit() calls are coming from within main()
itself? That's the only way I can make your objections seem to
make sense, but I can't figure out why you'd assume that.

I didn't criticize, at least I don't think so. I made
suggestions about how to perform his port. If some of my
assumptions about the (never shown) code he wants to replace
are false, then some of the suggestions are pure blooey.

As I said before it's is called 'PVRG JPEG 1.2.1' you can google
for it. Or see my integration in GDCM:

http://gdcm.svn.sourceforge.net/view...tilities/pvrg/

And yes there are *plenty* of exit all over the place (read: in
functions other than the original 'main').

$ grep exit\( * | wc
75 152 2479

Which are spread in different files:

$ grep -l exit\( * | wc
8 8 68

And no this code is not easy to follow (slightly easier to follow
than IJG, since less complex)
My reaction is UGH. I suspect writing your function from scratch
will be easier.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 15 '08 #36
James Kuyper wrote:
CBFalconer wrote:
>James Kuyper wrote:
>>CBFalconer wrote:
mathieu wrote:
...
>>>>Thanks I also know how to read the man page of exit :)

That wasn't clear to me. ...
...
>I didn't criticize, at least I don't think so.

I think that the your comment cited above sounds fairly critical.
Good lord. You consider "That wasn't clear to me." to be
critical? I am amazed.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 15 '08 #37
CBFalconer wrote:
James Kuyper wrote:
>CBFalconer wrote:
>>James Kuyper wrote:
CBFalconer wrote:
mathieu wrote:
...
>>>>>Thanks I also know how to read the man page of exit :)
That wasn't clear to me. ...
...
>>I didn't criticize, at least I don't think so.
I think that the your comment cited above sounds fairly critical.

Good lord. You consider "That wasn't clear to me." to be
critical? I am amazed.
That depends upon the thing you're not clear about. In this case, you
were not clear about whether he had basic reading skills and a
sufficiently strong understanding of C to use those reading skills to
understand the man page for exit(). In this group, suggesting that
someone might be lacking such elementary skills constitutes criticism,
at least, even if it doesn't quite qualify as an outright insult.
Someone who's even thinking about using longjmp() had better have a much
higher skill level than that.

Aug 15 '08 #38
On 15 Aug 2008 at 13:54, James Kuyper wrote:
CBFalconer wrote:
[same old same old]
In this case, you were not clear about whether he had basic reading
skills and a sufficiently strong understanding of C to use those
reading skills to understand the man page for exit().
Maybe CBF just assumes everyone else has the same problems he obviously
does with basic reading comprehension.

Aug 15 '08 #39
On Thu, 14 Aug 2008 09:48:44 -0700, Keith Thompson <ks***@mib.or g>
wrote:
mathieu <ma************ ***@gmail.comwr ites:
I am trying to reuse a piece of code that was designed as an
application. The code is covered with 'exit' calls. <snip>
Keeping it as a separate program and invoking it with system() might
be your best bet. <snip>
On the evidence seen so far, concur.
<OT>
Here's another possibility. I happen to know that there's another
language, whose name is that of our local favorite language with a
couple of plus signs appended to it, which supports the kind of
control flow you're looking for. Rather than calling exit, you can
"throw" an "exception" and "catch" it at whatever level you like
rather than letting it propagate out and terminate the program. It's
not inconceivable that you could recompile this program using a
compiler for this Other Language and make use of the OL's features.
This approach could be fraught (fraught, I say!) with peril. There
are subtle differences between C and the OL, some of which may prevent
valid C code from compiling, and some of which may silently change its
behavior. And if you take that approach, we can't help you here, but
the folks over in comp.lang.thato therlanguage or
comp.lang.thato therlanguage.mo derated will be glad to do so.
</OT>
That will give you the nonlocal jump -- like setjmp/longjmp already
discussed, in a slightly nicer form -- but it won't help clean up
resources allocated the C way (malloc, fopen, etc.), which the
existing code presumably uses given it is C. You would have to convert
to OL-style 'RAII' manager classes. And rewriting the now-library to
that extent is almost certainly more work than just rewriting it in
library-friendly C.

- formerly david.thompson1 || achar(64) || worldnet.att.ne t
Aug 25 '08 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2609
by: lord.zoltar | last post by:
How can I prevent the big close button in the top of the window from closing the window? I want to have and "are you sure?" confirmation so the user must press "Yes" before the program ends. Right now, I've tried catching the FormClosing and FormClosed events. The message box appears at the right time, but since the form is already closing, it doesn't matter if the user presses "Yes" or "No". how do I cancel the FormClosing?
4
1810
by: lovecreatesbea... | last post by:
Is the following code (without any code at lines x and x + 3) correct? Is it better to call exit() or re-throw the exceptions at line x and line x + 3?. What is the better code should we place at line x and line x + 3? try { cnn = env->createConnection(user, pwd, db); } catch (SQLException &esql){ cerr << "DB Exception: " << esql.getMessage(); /* line x ? */
5
3816
by: Stefan Bellon | last post by:
Hi all! I am embedding Python into a GUI application in a way that the GUI is scriptable using Python. Now I have come to a problem that when the user puts a "sys.exit(0)" into his script to end the script, not only the script is terminated, but also the GUI application itself. This is not the intended behaviour. As in Python itself you can catch SystemExit, I think this should be
1
2426
by: Marty | last post by:
I need to catch exceptions thrown by programs started by the os.system function, as indicated by a non-zero return code (e.g. the mount utility). For example, if I get the following results in a bash shell: $mount test mount: can't find /home/marty/test in /etc/fstab or /etc/mtab then I want to catch the same exception from the corresponding os.system() call, i.e. "os.system('mount test')", but it doesn't work as expected:
12
18343
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
9
11584
by: titanandrews | last post by:
Hi All, Is there any way to catch the exit code from someone calling exit(status) and check the value before the program terminates? Just for an idea, the code I'm thinking of is something like this: void exithandler() { DWORD exitCode = 0;
3
1830
by: Anthony P. | last post by:
Hello Everyone, I am writing a Windows Mobile 5.0 application using VS 2..8 in VB.NET. When someone clicks the "Exit" menu option, which generates a button_click() event, I find it easy to display an "Are you sure?" MessgeBox before the application closes. That way, if they accidentally selected exit, they have a chance to back out. But I also want to catch the _closing() event on the form in case the user clicks the application close X...
16
1570
by: cmdolcet69 | last post by:
I have the below if statement, that should catch if any of the conditions are met.....however for some reasons if my boolDSIFlushGapReading = true and MuxClass.DSIValues.count =1 and my muxclass.COM1Active =1 it will not exit the program??? Why is that can anyone help me Urgent!!!!!!
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.