📄 faqcatea63.html
字号:
</p><p>See also question <a href="faqcat1d60.html?sec=stdio#constrpath">12.28b</a>.<hr><hr><hr><a name="systemret"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/systemret.html"><!-- qtag -->Question 19.29</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How do I get an accurate error status return from <TT>system</TT> on MS-DOS?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>You can't;COMMAND.COMdoesn'ttend toprovide one.If you don't needCOMMAND.COM'sservices(i.e. if you're just trying to invoke a simple program,without I/O redirectionand such)tryone of the <TT>spawn</TT> routines, instead.<hr><hr><hr><a name="popen"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/popen.html"><!-- qtag -->Question 19.30</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I invokeanother programor commandandtrapits output?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Unixand some other systems provide a <TT>popen</TT>function,which sets upa stdio streamon a pipeconnected to the processrunning a command,so that thecalling program can read the output(oralternativelysupply the input).Using <TT>popen</TT>,the last example fromquestion <a href="faqcatea63.html?sec=osdep#system2">19.28</a>would look like<pre> extern FILE *popen(); sprintf(cmdbuf, "sort < %s", datafile); fp = popen(cmdbuf, "r"); /* ...now read sorted data from fp... */ pclose(fp);</pre>(Do be sureto call <TT>pclose</TT>,as shown;leaving it out will seem to work at firstbut may eventually run you out of processesorfile descriptors.)</p><p>If you can't use <TT>popen</TT>,you may be able to use <TT>system</TT>,with the output going to a filewhich you then open and read,as the code in question <a href="faqcatea63.html?sec=osdep#system2">19.28</a> was doing already.<a href="../../osdep/concur.html" rel=subdocument>[footnote]</a></p><p>If you're using Unix and <TT>popen</TT> isn't sufficient,you can learn about<TT>pipe</TT>,<TT>dup</TT>, <TT>fork</TT>,and<TT>exec</TT>.</p><p>(One thing thatprobably would <em>not</em> work,by the way,would beto use <TT>freopen</TT>.)</p><p>References:PCS Sec. 11 p. 169<hr><hr><hr><a name="exepath"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/exepath.html"><!-- qtag -->Question 19.31</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can my program discover the complete pathname to the executablefromwhich it was invoked?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font><TT>argv[0]</TT> may contain all or part of thepathname,or it may contain nothing.You maybe able to duplicate the command language interpreter'ssearch path logic to locate theexecutable if the name in <TT>argv[0]</TT>ispresent butincomplete.However, there is no guaranteedsolution.</p><p>References:K&R1 Sec. 5.11 p. 111<br>K&R2 Sec. 5.10 p. 115<br>ISO Sec. 5.1.2.2.1<br>H&S Sec. 20.1 p. 416<hr><hr><hr><a name="auxfiles"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/auxfiles.html"><!-- qtag -->Question 19.32</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I automatically locatea program'sconfiguration filesin the same directory astheexecutable?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's hard,in general;it's an equivalent problem to the one inquestion <a href="faqcatea63.html?sec=osdep#exepath">19.31</a>.Even if you canfigure out a workable way to do it,you might want to consider making the program'sauxiliary (library) directory configurable,perhaps with an environment variable.(It's especially important to allow variable placementof a program's configuration fileswhen the program will be used by several people,e.g. on a multiuser system.)<hr><hr><hr><a name="setenv"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/setenv.html"><!-- qtag -->Question 19.33</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can a process change an environment variable in its caller?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It may or may not be possible to do so at all.Different operating systems implementglobalname/value functionalitysimilar to theUnixenvironmentin different ways.Whether the ``environment'' can be usefully altered by arunning program, and if so, how, is system-dependent.</p><p>UnderUnix,a process can modify its own environment(some systems provide <TT>setenv</TT>or <TT>putenv</TT> functionsfor the purpose),and themodified environment isgenerallypassed on tochild processes,but it is<em>not</em>propagated back to the parent process.(The environment of the parent process can only be altered if theparent is explicitly set upto listen for some kind of change requests.The conventional execution of the BSD ``tset'' program in .profileand .login files effects such a scheme.)Under MS-DOS, it's possible tomanipulate themaster copy of the environment,but the required techniques are arcane.(See an MS-DOS FAQ list.)<hr><hr><hr><a name="argv"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/argv.html"><!-- qtag -->Question 19.34</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I open files mentioned on the command line,and parse option flags?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat38c2.html?sec=misc#argv">20.3</a>.<hr><hr><hr><a name="exitvsreturn"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/exitvsreturn.html"><!-- qtag -->Question 19.35</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is <TT>exit(status)</TT> truly equivalentto returning the same <TT>status</TT> from <TT>main</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#exitvsreturn">11.16</a>.<hr><hr><hr><a name="dynlink"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/dynlink.html"><!-- qtag -->Question 19.36</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I read in an object file and jump tolocationsin it?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>You want a dynamic linker or loader.It may be possible to <TT>malloc</TT> some spaceand read in object files,but you have to know an awful lotabout object file formats, relocation, etc.,and this approach can't work if code and datareside in separate address spacesorif code is otherwise privileged.</p><p>Under BSDUnix,you could use <TT>system</TT> and <TT>ld -A</TT>to do the linking for you.Manyversions of SunOS and System Vhave the -ldl librarycontaining routines like<TT>dlopen</TT> and<TT>dlsym</TT>whichallowobject files to be dynamicallyloaded.Under VMS, use LIB$FIND_IMAGE_SYMBOL.GNUhasapackage called ``dld''.See also question<a href="faqcat744e.html?sec=varargs#invvarargs">15.13</a>.<hr><hr><hr><a name="subsecond"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/subsecond.html"><!-- qtag -->Question 19.37</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I implement a delay,or time a user's response,with sub-second resolution?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Unfortunately, there is no portable way.Routinesyou might look for on your system include<TT>clock</TT>,<TT>delay</TT>,<TT>ftime</TT>,<TT>gettimeofday</TT>,<TT>msleep</TT>,<TT>nap</TT>,<TT>napms</TT>,<TT>nanosleep</TT>,<TT>setitimer</TT>,<TT>sleep</TT>,<TT>Sleep</TT>,<TT>times</TT>,and<TT>usleep</TT>.(A function called <TT>wait</TT>,however,is at least under Unix <em>not</em> what you want.)The <TT>select</TT> and <TT>poll</TT> calls(if available)can be pressed intoservice to implement simple delays.On MS-DOS machines, it is possible to reprogram the system timerand timer interrupts.</p><p>Of these,only<TT>clock</TT> is part of the ANSI Standard.The difference between two calls to <TT>clock</TT>gives elapsedexecutiontime,andmay evenhave subsecond resolution,if CLOCKS_PER_SECis greater than 1.However, <TT>clock</TT> giveselapsedprocessor timeused by the current program,whichon a multitasking system(or in a non-CPU-intensive program)maydiffer considerably fromreal time.</p><p>If you're trying to implement a delayand all you have availableis a time-reporting function,you can implement aCPU-intensivebusy-wait,but this is onlyanoption on a single-user, single-tasking machine,as it is terribly antisocialto any other processes.Under a multitasking operating system,be sure to useacallwhich puts your process to sleep for the duration,such as <TT>sleep</TT> or <TT>select</TT>,or <TT>pause</TT>in conjunction with <TT>alarm</TT> or <TT>setitimer</TT>.</p><p>For really brief delays,it's tempting to use a do-nothing loop like<pre> long int i; for(i = 0; i < 1000000; i++) ;</pre>but resist this temptation if at all possible!For one thing,your carefully-calculated delay loopswillstop workingproperlynext month when a faster processor comes out.Perhapsworse,aclevercompiler may notice that the loop does nothingand optimize it away completely.</p><p>Additional links:A<a href="../../osdep/sd25.html" rel=subdocument>technique involving I/O instructions</a>described by Pedro Zorzenon Neto</p><p>References:H&S Sec. 18.1 pp. 398-9<br>PCS Sec. 12 pp. 197-8,215-6<br>POSIX Sec. 4.5.2<hr><hr><hr><a name="sigint"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/sigint.html"><!-- qtag -->Question 19.38</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I trap or ignorekeyboard interruptslikecontrol-C?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The basic step is to call <TT>signal</TT>,either as<pre> #include <signal.h> signal(SIGINT, SIG_IGN);</pre>to ignorethe interrupt signal,or as<pre> extern void func(int); signal(SIGINT, func);</pre>to cause control to transfer to function <TT>func</TT>on receipt ofan interrupt signal.<a href="../../osdep/keybintr.html" rel=subdocument>[footnote]</a></p><p>On a multi-tasking system such as Unix,it's best to use a slightly moreinvolvedtechnique:<pre> extern void func(int); if(signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, func);</pre>The test and extra callensure that akeyboard interrupttyped in the foreground won't inadvertently interrupt a program running in the background(and it doesn't hurt to code calls to <TT>signal</TT> this way on any system).<a href="../../osdep/pgrps.html" rel=subdocument>[footnote]</a></p><p>On some systems,keyboard interrupthandling is also a function ofthe mode ofthe terminal-input subsystem;see question <a href="faqcatea63.html?sec=osdep#cbreak">19.1</a>.On somesystems,checking forkeyboard interruptsis only performed when the program is reading input,andkeyboard interrupthandling may therefore depend on which input routines are being called(and <em>whether</em> any input routines are active at all).On MS-DOS systems, <TT>setcbrk</TT>or <TT>ctrlbrk</TT>functionsmay also be involved.</p><p>References:ISO Secs. 7.7,7.7.1<br>H&S Sec. 19.6 pp. 411-3<br>PCS Sec. 12 pp. 210-2<br>POSIX Secs. 3.3.1,3.3.4<hr><hr><hr><a name="fpexcepts"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/fpexcepts.html"><!-- qtag -->Question 19.39</a></h1><p><f
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -