From owner-freebsd-questions Thu Nov 18 12:50:22 1999 Delivered-To: freebsd-questions@freebsd.org Received: from yana.lemis.com (yana.lemis.com [192.109.197.140]) by hub.freebsd.org (Postfix) with ESMTP id 3D60615188 for ; Thu, 18 Nov 1999 12:50:14 -0800 (PST) (envelope-from grog@mojave.sitaranetworks.com) Received: from mojave.sitaranetworks.com (mojave.sitaranetworks.com [199.103.141.157]) by yana.lemis.com (8.8.8/8.8.8) with ESMTP id HAA25316; Fri, 19 Nov 1999 07:18:09 +1030 (CST) (envelope-from grog@mojave.sitaranetworks.com) Message-ID: <19991118154736.22915@mojave.sitaranetworks.com> Date: Thu, 18 Nov 1999 15:47:36 -0500 From: Greg Lehey To: rover@lglobus.ru, freebsd-questions@FreeBSD.ORG Subject: Re: Is there correct way for program to read from itself? Reply-To: Greg Lehey References: <19991118065815.B89755@fly.lglobus.ru> <19991118102421.09370@mojave.sitaranetworks.com> <19991118223426.A62913@fly.lglobus.ru> <19991118152324.37840@mojave.sitaranetworks.com> <19991118232846.A63288@fly.lglobus.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii In-Reply-To: <19991118232846.A63288@fly.lglobus.ru>; from Oleg V. Volkov on Thu, Nov 18, 1999 at 11:28:46PM +0300 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Thursday, 18 November 1999 at 23:28:46 +0300, Oleg V. Volkov wrote: > On Thu, Nov 18, 1999 at 03:23:24PM -0500, Greg Lehey wrote: >>>>> Is there correct way for porgram to read from it's own file? >>>> I'm not sure I understand. What do you mean by "it's own file"? >>>> If you mean the object file, sure. Where's the problem? >>> I mean this situation: >>> I have some program /usr/local/bin/someprog. Is there a way for it >>> to read from itself (from /usr/local/bin/someprog). >> Sure, that's what I said. What do you expect to find? > > Could you give me short example? OK, here's copyme.c: #include #include #include #include extern int errno; main (int argc, char *argv []) { int me = open (argv [0], O_RDONLY); int you; char buffer [4096]; int count; if (argc < 2) { fprintf (stderr, "No output file specified\n"); return 1; } if (me < 0) { fprintf (stderr, "Can't open %s: %s\n", argv [0], strerror (errno)); return 1; } you = open (argv [1], O_RDWR | O_APPEND | O_CREAT, S_IRUSR); if (you < 0) { fprintf (stderr, "Can't open %s: %s\n", argv [1], strerror (errno)); return 1; } while ((count = read (me, buffer, 4096)) > 0) { if (write (you, buffer, count) < 0) { perror ("Can't write"); return 1; } } if (count < 0) { perror ("Can't read"); return 1; } return ; } And here's what happens when I run it: $ copyme foo $ cmp copyme foo $ ls -l copyme foo -rwxrwxrwx 1 grog eng 4197 Nov 18 15:44 copyme -r-------- 1 grog eng 4197 Nov 18 15:44 foo $ Not much use, is it? Was that your class assignment? Greg -- When replying to this message, please copy the original recipients. For more information, see http://www.lemis.com/questions.html Finger grog@lemis.com for PGP public key See complete headers for address and phone numbers To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message