MESSAGE
DATE | 2015-03-08 |
FROM | Ruben Safir
|
SUBJECT | Subject: [LIU Comp Sci] Re: Operating systems HW
|
From owner-learn-outgoing-at-mrbrklyn.com Sun Mar 8 20:24:50 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id 8C914161190; Sun, 8 Mar 2015 20:24:50 -0400 (EDT) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id 74426161305; Sun, 8 Mar 2015 20:24:50 -0400 (EDT) Delivered-To: learn-at-nylxs.com Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by mrbrklyn.com (Postfix) with ESMTP id A1742161190 for ; Sun, 8 Mar 2015 20:24:49 -0400 (EDT) Received: from [10.0.0.19] (unknown [96.57.23.82]) by mailbackend.panix.com (Postfix) with ESMTPSA id 8897411ECB; Sun, 8 Mar 2015 20:24:49 -0400 (EDT) Message-ID: <54FCE851.70001-at-panix.com> Date: Sun, 08 Mar 2015 20:24:49 -0400 From: Ruben Safir User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: mohammed Ghriga Subject: [LIU Comp Sci] Re: Operating systems HW References: <54FCE7DB.3000700-at-panix.com> In-Reply-To: <54FCE7DB.3000700-at-panix.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: owner-learn-at-mrbrklyn.com Precedence: bulk Reply-To: learn-at-mrbrklyn.com
BTW this was the question: Programming Problems 2.26 In Section 2.3, we described a program that copies the contents of one file to a destination file. This program works by first prompting the user for the name of the source and destination files. Write this program using either the Windows or POSIX API . Be sure to include all necessary error checking, including ensuring that the source file exists. Once you have correctly designed and tested the program, if you used a system that supports it, run the program using a utility that traces system calls. Linux systems provide the strace utility, and Solaris and Mac OS X systems use the dtrace command. As Windows systems do not provide such features, you will have to trace through the Windows version of this program using a debugger.
One thing you notice is that the GNU optimizer is SMARTER than the C strings library.
On 03/08/2015 08:22 PM, Ruben Safir wrote: > I lost track of what the home work was, but before I moved forward > answering quiz questions, I wanted to tackle some of the coding > assignments in Chapter 2, because I thought I could learn a lot more > from them. So I did read Cpt 2 and 3, but I wanted to finish this which > was good because I completely forgot a number of things about fstat, > fopen and the use of strace, I had never done that before actually > although I am familiar and used extensively nm. I made a lot of > blunders on the way to finishing this, but others might benefit from > this as well. I'm going on to doing the kernel programming tonight, I > hope and tackle chapter 3 'quiz' questions at school in the AM before class. > > Ruben > > #include > #include > #include > #include > #include > > char buf[500] = {0}; > char buf2[500] = {0}; > char * buf3; > int fd, ret; > size_t len; > const char * PASSWD = "/etc/passwd"; > off_t tmpsize; > > struct stat filetmp; > > FILE * INPUT; > FILE * OUTPUT; > int main( int argc, char * argv[]) > { > puts ("Enter a file to copy\n"); > if ( ! fgets( buf, 500, stdin) ) > return EXIT_FAILURE; > len = strlen(buf); > buf[len-1] = 0; > > > puts ("Enter the file to copy to\n"); > if ( ! fgets( buf2, 500, stdin) ) > return EXIT_FAILURE; > len = strlen(buf2); > buf2[len-1] = 0; > > if( access( buf, F_OK|R_OK ) == 0 ) { > INPUT = fopen( buf, "r"); > if (INPUT == NULL){ > printf("Can't Open %s\n", buf); > exit(EXIT_FAILURE); > } > }else{ > perror("access INPUT file stat"); > exit (EXIT_FAILURE); > } > //File Opened for reading > fd = fileno(INPUT); > if (fstat(fd, &filetmp ) == -1){ > perror("stat on reading file:"); > exit(EXIT_FAILURE); > } > //FILE IS STATTED > tmpsize = filetmp.st_size; > if( (buf3 = (char *) malloc( sizeof(int) * tmpsize)) == NULL) > exit (EXIT_FAILURE); > ret = fread(buf3, tmpsize, 1, INPUT); > if( ret != 1){ > fprintf(stderr, "Read not complete: Req ==> %d Rec ==> %zu \n", > ret, tmpsize); > exit(EXIT_FAILURE); > } > > OUTPUT = fopen(buf2, "w"); > if(OUTPUT == NULL){ > printf("Can't Open %s\n ", buf2); > exit(EXIT_FAILURE); > } > //File Open for Writing > ret = fwrite(buf3, tmpsize, 1, OUTPUT); > if(ret != 1){ > perror("WRITE"); > fprintf(stderr, "WRITE not complete: Req ==> %d Rec ==> %zu\n", > ret, tmpsize); > exit(EXIT_FAILURE); > } > free(buf3); > fclose(INPUT); > fclose(OUTPUT); > return 1; > } > > execve("./a.out", ["./a.out"], [/* 55 vars */]) = 0 > brk(0) = 0x68b000 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) > = 0x7f71c0fdb000 > access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or > directory) > open("/home/ruben/GNUstep/Library/Libraries/tls/x86_64/libc.so.6", > O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) > stat("/home/ruben/GNUstep/Library/Libraries/tls/x86_64", 0x7fffd1634d10) > = -1 ENOENT (No such file or directory) > open("/home/ruben/GNUstep/Library/Libraries/tls/libc.so.6", > O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) > stat("/home/ruben/GNUstep/Library/Libraries/tls", 0x7fffd1634d10) = -1 > ENOENT (No such file or directory) > open("/home/ruben/GNUstep/Library/Libraries/x86_64/libc.so.6", > O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) > stat("/home/ruben/GNUstep/Library/Libraries/x86_64", 0x7fffd1634d10) = > -1 ENOENT (No such file or directory) > open("/home/ruben/GNUstep/Library/Libraries/libc.so.6", > O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) > stat("/home/ruben/GNUstep/Library/Libraries", 0x7fffd1634d10) = -1 > ENOENT (No such file or directory) > open("/root/GNUstep/Library/Libraries/tls/x86_64/libc.so.6", > O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) > stat("/root/GNUstep/Library/Libraries/tls/x86_64", 0x7fffd1634d10) = -1 > ENOENT (No such file or directory) > open("/root/GNUstep/Library/Libraries/tls/libc.so.6", > O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) > stat("/root/GNUstep/Library/Libraries/tls", 0x7fffd1634d10) = -1 ENOENT > (No such file or directory) > open("/root/GNUstep/Library/Libraries/x86_64/libc.so.6", > O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) > stat("/root/GNUstep/Library/Libraries/x86_64", 0x7fffd1634d10) = -1 > ENOENT (No such file or directory) > open("/root/GNUstep/Library/Libraries/libc.so.6", O_RDONLY|O_CLOEXEC) = > -1 ENOENT (No such file or directory) > stat("/root/GNUstep/Library/Libraries", 0x7fffd1634d10) = -1 ENOENT (No > such file or directory) > open("/usr/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT > (No such file or directory) > stat("/usr/lib/tls/x86_64", 0x7fffd1634d10) = -1 ENOENT (No such file or > directory) > open("/usr/lib/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such > file or directory) > stat("/usr/lib/tls", 0x7fffd1634d10) = -1 ENOENT (No such file or > directory) > open("/usr/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No > such file or directory) > stat("/usr/lib/x86_64", 0x7fffd1634d10) = -1 ENOENT (No such file or > directory) > open("/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 > read(3, > "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\1\2\0\0\0\0\0"..., 832) > = 832 > fstat(3, {st_mode=S_IFREG|0755, st_size=1984416, ...}) = 0 > mmap(NULL, 3813200, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, > 0) = 0x7f71c0a18000 > mprotect(0x7f71c0bb1000, 2097152, PROT_NONE) = 0 > mmap(0x7f71c0db1000, 24576, PROT_READ|PROT_WRITE, > MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x199000) = 0x7f71c0db1000 > mmap(0x7f71c0db7000, 16208, PROT_READ|PROT_WRITE, > MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f71c0db7000 > close(3) = 0 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) > = 0x7f71c0fda000 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) > = 0x7f71c0fd9000 > arch_prctl(ARCH_SET_FS, 0x7f71c0fda700) = 0 > mprotect(0x7f71c0db1000, 16384, PROT_READ) = 0 > mprotect(0x7f71c0fdc000, 4096, PROT_READ) = 0 > fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) > = 0x7f71c0fd8000 > write(1, "Enter a file to copy\n", 21) = 21 > write(1, "\n", 1) = 1 > fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) > = 0x7f71c0fd7000 > read(0, "/etc/passwd\n", 1024) = 12 > write(1, "Enter the file to copy to\n", 26) = 26 > write(1, "\n", 1) = 1 > read(0, "/home/ruben/ssadsa.tmp\n", 1024) = 23 > access("/etc/passwd", R_OK) = 0 > brk(0) = 0x68b000 > brk(0x6ac000) = 0x6ac000 > open("/etc/passwd", O_RDONLY) = 3 > fstat(3, {st_mode=S_IFREG|0644, st_size=1637, ...}) = 0 > fstat(3, {st_mode=S_IFREG|0644, st_size=1637, ...}) = 0 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) > = 0x7f71c0fd6000 > read(3, "root:x:0:0:root:/root:/bin/bash\n"..., 4096) = 1637 > open("/home/ruben/ssadsa.tmp", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 5 > fstat(5, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) > = 0x7f71c0fd5000 > close(3) = 0 > munmap(0x7f71c0fd6000, 4096) = 0 > write(5, "root:x:0:0:root:/root:/bin/bash\n"..., 1637) = 1637 > close(5) = 0 > munmap(0x7f71c0fd5000, 4096) = 0 > exit_group(1) = ? > +++ exited with 1 +++ >
|
|