Copy a file using UNIX-system calls | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Copy a file using UNIX-system calls

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-25-2009
c_d c_d is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 11 May 2009, 1:42 AM EDT
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Copy a file using UNIX-system calls


Code:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>

int main( int argc,char *argv[] )
{
    char buf;
    int sourcefile,destfile,n;
    if(argc!=3)
    {
        write(STDOUT_FILENO,"prgm1 <sourcefile> <destination file>\n",50);   
        exit(1);
    }
    else
    {
         sourcefile=open(argv[1],O_RDONLY);
         if(sourcefile==-1)
         {
            perror("SOURCE FILE ERROR");
            exit(0);
         }
         else
         {
            destfile=open(argv[2],O_WRONLY | O_CREAT , 0641);
            if(destfile==-1)
            {
                perror("DESTINATION FILE ERROR");
                exit(0);
            }
            else
            {
                while((n=read(sourcefile,&buf,1)) != -1)
                {
                    write( destfile, &buf, 1 );
                }
                write(STDOUT_FILENO, "FILES COPIED\n" , 15);    
                close(sourcefile);
                close(destfile);
            }
        }
    }
    return 0;
}

the value returned by read() (n) never becomes -1 ? why?
Sponsored Links
    #2  
Old 03-25-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 August 2014, 5:38 PM EDT
Location: NM
Posts: 10,180
Thanks: 275
Thanked 785 Times in 734 Posts
read returns 0 on EOF, -1 on error. EOF is not an error.
Sponsored Links
    #3  
Old 03-25-2009
c_d c_d is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 11 May 2009, 1:42 AM EDT
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
read returns 0 on EOF, -1 on error. EOF is not an error.
well in my textbook it says

Quote:
if successful, read() returns the number of bytes that it read, otherwise , it returns -1
so , should'nt it return -1 once it reaches the EOF and tries to read the next byte?

so , does it mean that even after the EOF is reached and it tries to read the next byte, it continues to "read" from the "stream"? eventhough there is nothing to read?

i guess i misunderstood this...
thanks,problem solved

Last edited by c_d; 03-25-2009 at 10:32 AM..
    #4  
Old 03-25-2009
c_d c_d is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 11 May 2009, 1:42 AM EDT
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
ok, i have another doubt on open()
i change my c-code
from

Code:
sourcefile=open(argv[1],O_RDONLY);

to

Code:
sourcefile=open(argv[1],O_RDONLY,0664);

shouldn't open() be able to override the default permissions already set?
i tried and apparently it does not. is there any reason, besides enforcing security(that the only thing that i could think of)? and is there any way around this? (besides giving the file permissions using the system("chmod "); function?)
Sponsored Links
    #5  
Old 03-25-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 31 August 2014, 11:02 AM EDT
Location: /dev/ph
Posts: 4,787
Thanks: 63
Thanked 424 Times in 393 Posts
The third option to open() - which is shown in man pages as "..." is only used when a new file is being created. In your case you are opening an existing file as read only.

"..." is a way of saying that the number and types of the remaining arguments may vary. There may be none or there may be one or more.
Sponsored Links
    #6  
Old 03-25-2009
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 28 August 2014, 12:45 PM EDT
Location: USA
Posts: 1,441
Thanks: 19
Thanked 136 Times in 128 Posts
Quote:
Originally Posted by c_d View Post

Code:
sourcefile=open(argv[1],O_RDONLY,0664);

That's not the preferred way to specify file modes in UNIX. By not giving it the O_CREAT file it means that the file exists and the open call will have no affect on it and instead of giving 0644 for the file mode bits use...

Code:
sourcefile=open(argv[1], O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);

That's for creating and opening a file that does not exist because if it does then the open call fails so should check its return value.
Sponsored Links
    #7  
Old 03-25-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 August 2014, 5:38 PM EDT
Location: NM
Posts: 10,180
Thanks: 275
Thanked 785 Times in 734 Posts
Quote:
well in my textbook it says
Do not use a textbook, reference the man page for read. There are a lot of reasons for doing using the man page, you've discovered the first one.

Code:
man 2 read

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
system calls sangramdas BSD 1 05-08-2008 08:48 PM
System calls joshighanshyam Programming 1 03-19-2008 08:57 AM
System calls? PlunderBunny UNIX for Dummies Questions & Answers 2 03-26-2006 08:12 PM
System calls for cp and mv gaurava99 UNIX for Dummies Questions & Answers 2 04-25-2005 10:04 AM
System Calls clickonline1 UNIX for Dummies Questions & Answers 2 10-06-2001 05:30 PM



All times are GMT -4. The time now is 07:26 PM.