Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!decuac!avolio From: avolio@gildor.dec.com (Frederick M. Avolio) Newsgroups: comp.unix.questions Subject: A couple questions Message-ID: <1296@decuac.DEC.COM> Date: Tue, 14-Apr-87 12:52:26 EST Article-I.D.: decuac.1296 Posted: Tue Apr 14 12:52:26 1987 Date-Received: Sun, 19-Apr-87 00:01:57 EST References: <3164@jade.BERKELEY.EDU> Sender: daemon@decuac.DEC.COM Organization: DEC SWS, Landover, MD Lines: 58 In article <3164@jade.BERKELEY.EDU> marcp@beryl.berkeley.edu (Marc M. Pack) writes: >Hello! I've a couple of questions in UNIX 4.2. > >1. How do programs like "more" distinguish between text files and > executable files? More 1) checks the file type to make sure it is not a directory. 2) Looks for a magic number at the head of the file. For example, 0407 (exec.), 0410 (pure exec.), 0413(demand paged), 0177545(old archive)..... it knows that it should not more it and gives an appropriate message. It does not know about data files, as far as I can tell and more will go ahead and show them to you if you want. Not too good for a printer either... The file program samples data. If it finds characters greater than 255 it decides it is a "data" file. A filter such as this is maybe your best bet if you have a "simple" lineprinter... >2. Is it possible to, while in a C program, call another program and > put it into the background? Actually, I know it's possible, > 'cause I can do it with a line like: > system("cat textfile &"); > > This won't work, however, if I try to "more" the file instead. > What determines what can be put in the background and what can't? > Is there some way to run a program from within a program, and have > it return upon completion to the original program besides "system"? > (The execl series, of course, doesn't return.) You're on the right track... execl et al. don't return but overlays so what you do is a fork and execl. The parent can wait for the child to finish or not. BAsically this is what system is. It ... FORK EXEC SHELL WITH COMMAND LINE PARENT WAITS As in: /* FORK RETURNS THE PID OF THE CHILD TO THE PARENT AND 0 TO THE CHILD */ if ((pid = fork()) == 0) /* IF CHILD */ { execl("/bin/sh", "sh", "-c", arg, 0); printf("HELP! SOMEONE STOLE THE SHELL!\n"); exit(1); } wait(0); /* WAIT SATISFIED WHEN CHILD FINISHES So this kind of thing will do what you want. See manual page on fork (section 2) for more details. (If on a 4.*BSD ort Ultrix system use cfork instead... Why? I don't know, the same reason you type 'sync' three times before halting :-).) BTW, "more" doesn't work too well in the background as you did it because it is not associated at that point with the tty anymore. Cat doesn't care what it cats to. Fred