LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2004, 02:08 AM   #1
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Rep: Reputation: 15
setting the default file permissoins


I am trying to copy files from one folder to another folder. i am doing this using the functions open,read,write. what is the problem is that the file permissions are changed. how can i ensure that the file must have the same permissions as the original file. for example if the source file has permissions 644 then the copied file in the other folder must have the same permissios. I know that chmod function can be used to change the permission. But how can i know the permission of original file.

Thanx in advance,
rajesh
 
Old 09-23-2004, 02:11 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Check out 'man stat'. Particularly, the st_mode member of struct stat.
 
Old 09-23-2004, 02:19 AM   #3
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Original Poster
Rep: Reputation: 15
Thanx for u r immediate reply. Finally i got the solution.
I am changing the file permission of the newfile with the statement
chmod(filename,p_stat->st_mode&0777);

thanx a lot.
 
Old 09-23-2004, 02:36 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
This is another way to do it:
Code:
itsme@dreams:~/C$ cat mycp.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  int fdin, fdout;
  struct stat st;
  char buf[BUFSIZ];
  int n_read;

  if(argc != 3)
  {
    puts("Usage: mycp <source> <destination>");
    exit(1);
  }
  if((fdin = open(argv[1], O_RDONLY)) == -1)
  {
    printf("open(): %s\n", strerror(errno));
    exit(1);
  }
  if(fstat(fdin, &st) == -1)
  {
    printf("stat(): %s\n", strerror(errno));
    exit(1);
  }

  umask(0);
  if((fdout = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, st.st_mode)) == -1)
  {
    printf("open(): %s\n", strerror(errno));
    exit(1);
  }

  while((n_read = read(fdin, buf, sizeof(buf))) > 0)
  {
    if(write(fdout, buf, n_read) == -1)
    {
      printf("write(): %s\n", strerror(errno));
      exit(1);
    }
  }

  close(fdin);
  close(fdout);

  if(n_read == -1)
  {
    printf("read(): %s\n", strerror(errno));
    exit(1);
  }

  return 0;
}
Example:
Code:
itsme@dreams:~/C$ head -c 50000 /dev/urandom > file1.garbage
itsme@dreams:~/C$ ls -l file1.garbage
-rw-r--r--    1 itsme    users       50000 Sep 23 00:34 file1.garbage
itsme@dreams:~/C$ ./mycp file1.garbage file2.garbage
itsme@dreams:~/C$ ls -l file2.garbage
-rw-r--r--    1 itsme    users       50000 Sep 23 00:34 file2.garbage
itsme@dreams:~/C$ chmod a+w file2.garbage
itsme@dreams:~/C$ ls -l file2.garbage
-rw-rw-rw-    1 itsme    users       50000 Sep 23 00:34 file2.garbage
itsme@dreams:~/C$ ./mycp file2.garbage file3.garbage
itsme@dreams:~/C$ ls -l file3.garbage
-rw-rw-rw-    1 itsme    users       50000 Sep 23 00:35 file3.garbage
itsme@dreams:~/C$
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting a default program to open certain file types in Gnome danran Debian 3 10-12-2005 10:07 PM
setting default page size and default tray lived4eva Linux - General 1 11-04-2003 02:39 PM
setting default browser? tombomb300 Linux - Software 1 10-19-2003 07:14 PM
Permissoins and YaST2 susesarus Linux - General 0 08-21-2003 09:34 PM
Setting a default browser freezinbutt Linux - Newbie 14 03-30-2002 01:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration