473,788 Members | 2,857 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 #1
39 2825
On 13 Aug 2008 at 21:33, CBFalconer wrote:
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'.
Just leave it calling exit.
Brilliant.

Remind me never to use any library you write.

Oh, yes, hell would have frozen over first anyway, even without this
latest idiotic post.

Aug 13 '08 #2
CBFalconer <cb********@yah oo.comwrites:
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:

7.20.4.3 The exit function
[C standard's description of the exit function snipped]

And how does this answer the OP's question?

He has a main program; a call to exit terminates the program.

He wants to turn this main program into a function within a larger
program. Wherever there's a call to exit, he wants to terminate the
function, not the entire (now larger) program.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 13 '08 #3
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
>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:

7.20.4.3 The exit function
[C standard's description of the exit function snipped]

And how does this answer the OP's question?

He has a main program; a call to exit terminates the program.

He wants to turn this main program into a function within a larger
program. Wherever there's a call to exit, he wants to terminate the
function, not the entire (now larger) program.
If that's what he wants to do that is another case. But the
previous effect was to terminate the program, whch is what exit
does.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

Aug 14 '08 #4
CBFalconer <cb********@yah oo.comwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
>>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:

7.20.4.3 The exit function
[C standard's description of the exit function snipped]

And how does this answer the OP's question?

He has a main program; a call to exit terminates the program.

He wants to turn this main program into a function within a larger
program. Wherever there's a call to exit, he wants to terminate the
function, not the entire (now larger) program.

If that's what he wants to do that is another case. But the
previous effect was to terminate the program, whch is what exit
does.
He knew that.

If he wanted the existing exit calls to terminate the program, why
would he have asked what to do about them?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 14 '08 #5
CBFalconer said:
Keith Thompson wrote:
<snip>
>>
He has a main program; a call to exit terminates the program.

He wants to turn this main program into a function within a larger
program. Wherever there's a call to exit, he wants to terminate the
function, not the entire (now larger) program.

If that's what he wants to do that is another case.
No, it's the original question. To avoid making such mistakes in future, I
recommend that you save up your replies for an hour or so, and then:

(1) Re-read the original question, and then re-read your original reply. At
this stage, it will sometimes become clear that your reply was based on a
mis-reading of the question which your second reading makes obvious.
Solution: delete your reply without sending it.

(2) By this time, there may well be one or two other replies to the
question, by people whose opinion you respect. Read them.

If they are, in essence, the same as your own reply, delete your reply
without sending it, since it would be pointlessly duplicative (unless your
reply adds something useful that was overlooked by the others).

If they are *not* in essence the same as your reply, consider seriously the
possibility that you have mis-read the question. Read their replies, and
the original question, again, and find out why they have replied as they
did. If you genuinely interpret the question differently to them, then
edit your reply so that it says something like "I've read A's and B's
replies, and they've assumed you meant such-and-such. If so, fine. But it
seems to me that you might mean /this/ instead. If so, then my advice
is..." Then, having changed your reply, save it up for an hour or so, and
then start back at (1) again.

(3) Now send any undeleted replies.

I suggest that using this strategy could save you from a huge amount of
embarrassment.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 14 '08 #6
mathieu said:
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 ?
Eric has already given you some excellent advice, and there is no point in
duplicating it, obviously.

My comment is more in the nature of a smug, satisfied grin, I'm afraid. You
see, I don't use exit() - or at least, if I do, I consider it to be a bug
that has to be fixed. I've just searched my personal code base for exit()
calls. I found 24 altogether in 14 different C files (out of - well,
thousands), dating back in some cases to the mid-1990s. That's 24 bugs I
have to fix, which isn't too huge a job, really.

Why don't I use exit()? Well, it's a style thing, I suppose. I like to
think of big chunks being made up of little chunks, and that means that
little chunks have to behave like little chunks and not take arbitrary
decisions like "quit the program", leaving those decisions to bigger
chunks. And whilst I will grant a big chunk (think "main()") the right to
make such a decision, it is easy to revoke it (think "edit and rename one
function") if that big chunk becomes a mere part of an enormous chunk.

Unfortunately, it is not always possible to persuade other people of the
advantages of this kind of modularity. In 1994-5, I was working at V, and
the code I was working on was littered with mallocs, which were never
freed. (No, it wasn't me that did that, and I didn't have time to fix it
because I was too busy doing other stuff.) It was no big deal, said the
principal culprit, because the memory was in any case released when the
program exited. In due course, I left, and went on to work at W, X, and Y.
And then I was hired by Z, who had bought the code from X. And guess what?
They had to do precisely what you need to do - turn a program into a
function. And suddenly all these leaks mattered a great deal.

Fellow regulars in comp.lang.c often smile indulgently when I wax lyrical
about my strange stylistic habits - such as one entry point and one exit
point for each function and each loop, always initialise, always give
control back to your caller, and so on. But I have my reasons, and they
are good reasons. In this case: if the original programmer had been me,
your task would have been trivial, and we would never even have heard
about it because you wouldn't have had to ask.

Why am I telling you all this? Well, think about it the next time you write
a program. What if, one day, you need *this* program to become a function?
How hard would it be to do that? How can you make it easier?

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 14 '08 #7
On Aug 14, 9:55 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
mathieu said:
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 ?

Eric has already given you some excellent advice, and there is no point in
duplicating it, obviously.

My comment is more in the nature of a smug, satisfied grin, I'm afraid. You
see, I don't use exit() - or at least, if I do, I consider it to be a bug
that has to be fixed. I've just searched my personal code base for exit()
calls. I found 24 altogether in 14 different C files (out of - well,
thousands), dating back in some cases to the mid-1990s. That's 24 bugs I
have to fix, which isn't too huge a job, really.

Why don't I use exit()? Well, it's a style thing, I suppose. I like to
think of big chunks being made up of little chunks, and that means that
little chunks have to behave like little chunks and not take arbitrary
decisions like "quit the program", leaving those decisions to bigger
chunks. And whilst I will grant a big chunk (think "main()") the right to
make such a decision, it is easy to revoke it (think "edit and rename one
function") if that big chunk becomes a mere part of an enormous chunk.

Unfortunately, it is not always possible to persuade other people of the
advantages of this kind of modularity. In 1994-5, I was working at V, and
the code I was working on was littered with mallocs, which were never
freed. (No, it wasn't me that did that, and I didn't have time to fix it
because I was too busy doing other stuff.) It was no big deal, said the
principal culprit, because the memory was in any case released when the
program exited. In due course, I left, and went on to work at W, X, and Y.
And then I was hired by Z, who had bought the code from X. And guess what?
They had to do precisely what you need to do - turn a program into a
function. And suddenly all these leaks mattered a great deal.

Fellow regulars in comp.lang.c often smile indulgently when I wax lyrical
about my strange stylistic habits - such as one entry point and one exit
point for each function and each loop, always initialise, always give
control back to your caller, and so on. But I have my reasons, and they
are good reasons. In this case: if the original programmer had been me,
your task would have been trivial, and we would never even have heard
about it because you wouldn't have had to ask.

Why am I telling you all this? Well, think about it the next time you write
a program. What if, one day, you need *this* program to become a function?
How hard would it be to do that? How can you make it easier?
So to paraphrase you, I should have said:

....
While working at X, I am trying to reuse a piece of code that was
designed by
Y as an application, in mid 1990. The code is covered with 'exit'
calls...
....

:)

Anyway to put names, I am talking about the PVRG JPEG library (google
it if needed). And it is producing a bizarre JPEG compression for 16
bits lossless that I simply *cannot* reproduce using IJG (google). So
the only way I can support those damned lossless JPEG is to link to
PVRG. I am not very keen on 'system' call, as I do not know how
portable they are. So yes I am stuck on reusing this piece of code,
and my goal is to spent little time as possible.

-Mathieu
Aug 14 '08 #8
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote in a splendid little chick fight
leading up to:
....
>He knew that.

If he wanted the existing exit calls to terminate the program, why
would he have asked what to do about them?
Looks like Keith is getting caught up in his own little web.

Are you guys beginning to see how pointless these little pedantic
quibbles are? How silly it looks when all you are doing is redefining
OP's questions to suit your own petty little agendas?

Aug 14 '08 #9
CBFalconer wrote:
Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
>>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:
....
>He wants to turn this main program into a function within a larger
program. Wherever there's a call to exit, he wants to terminate the
function, not the entire (now larger) program.

If that's what he wants to do that is another case.
No, that's the original case, not another one.
... But the
previous effect was to terminate the program, whch is what exit
does.
And he made it abundantly clear, I thought, that the point of his
question was about not wanting to terminate the program.
Aug 14 '08 #10

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

Similar topics

5
2608
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
3814
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
2423
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
18337
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
11581
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
1827
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
1567
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
9656
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
10366
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...
1
10112
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
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7518
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
6750
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();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
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
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.