Sponsored Content
Full Discussion: Copying Folder, C program
Homework and Emergencies Homework & Coursework Questions Copying Folder, C program Post 302392297 by MW99 on Thursday 4th of February 2010 03:41:19 PM
Old 02-04-2010
Copying Folder, C program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Copying all the normal files from 1 folder to another on a unix platform. I pass in 1 or 2 arguments, the folder to be copied and folder copied to.
I don't know why my code is not working. I open up a directory, and copying file by file. I don't know why it won't work.


2. Relevant commands, code, scripts, algorithms:
some cp files you can find easily all over the internet, and part of a ls file.


3. The attempts at a solution (include all code and scripts):
//here is my code

Code:
#include	<stdio.h>
#include	<sys/types.h>
#include	<dirent.h>
#include	<sys/stat.h>
#include	<pwd.h>
#include	<grp.h>
#include    <string.h>
#include	<unistd.h>
#include	<stdlib.h>
#include	<fcntl.h>

#define BUFFERSIZE      4096
#define COPYMODE        0644


void open_dir(char[], char[]);
void copy_file(char *, char[]);
void oops(char *, char *);

main(int ac, char *av[]){
	if ( ac == 2 )
		open_dir( ".", av[1]);
	if ( ac == 3)
		open_dir( av[1], av[2]);
}		
						
/*******************************************
 *	list files in directory called dirname */
 void open_dir( char dirname[], char tardir[] ){
	DIR		*dir_ptr;		/* the directory */
	struct dirent	*direntp;	/* each entry	 */

	if ( ( dir_ptr = opendir( dirname ) ) == NULL )
		fprintf(stderr,"ls1: cannot open %s\n", dirname);
	else {
		while ( ( direntp = readdir( dir_ptr ) ) != NULL )
			copy_file( direntp->d_name, tardir);
		closedir(dir_ptr);
	}
}

/********************************************
 *  copys file								*/
void copy_file( char *filename, char dirname[]){
		
		int     in_fd, out_fd, n_chars;
        char    buf[BUFFERSIZE];
		DIR		*dir_ptr;		/* the directory */
		struct dirent	*direntp;	/* each entry	 */      						
						/* open files	*/
        if ( (in_fd=open(filename, O_RDONLY)) == -1 )
                oops("Cannot open ", filename);
		if ( ( dir_ptr = opendir( dirname ) ) == NULL )
		fprintf(stderr,"ls1: cannot open %s\n", dirname);
		else {
			if ( (out_fd=creat( filename, COPYMODE)) == -1 )
                oops( "Cannot creat", filename);
		}
						/* copy files	*/

        while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
                if ( write( out_fd, buf, n_chars ) != n_chars )
                        oops("Write error to ", filename);
	if ( n_chars == -1 )
			oops("Read error from ", filename);

						/* close files	*/

        if ( close(in_fd) == -1 || close(out_fd) == -1 )
                oops("Error closing files","");
}
void oops(char *s1, char *s2)
{
        fprintf(stderr,"Error: %s ", s1);
        perror(s2);
        exit(1);
}


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
CSUEB, Hayward, CA, USA, Prof. Lee, CS3640

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

---------- Post updated at 03:41 PM ---------- Previous update was at 01:51 AM ----------

I've found out that I have to use malloc() to put it in another directory, but I'm very confused to how to use it for this program.

Last edited by MW99; 02-04-2010 at 05:26 AM.. Reason: code tags, please...
This User Gave Thanks to MW99 For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

2. Shell Programming and Scripting

Copying latest file into a folder

Hello all, this is my first post while i am trying to understand unix. I would basically like to know if i can do this: Lets say i have a folderA and folderB And i save something in folderA Can i make a script that checks folderA latest file, then compares it with the date of latest file in... (16 Replies)
Discussion started by: takissd
16 Replies

3. Shell Programming and Scripting

mkdir: copying a folder's permissions

Hi, I wanted to know how to create a folder using mkdir and then have it copy the permissions from another specified folder on the system. For example if I did this: mkdir /Volumes/USBSTICK/System How can I make it copy the permissions from the /System folder ? Thanks (4 Replies)
Discussion started by: pcwiz
4 Replies

4. Shell Programming and Scripting

Bash folder manipulation - selecting/copying specified files

Bash/scripting newbie here - I feel this might be a trivial problem, but I'm not sure how to tackle it. I've got a folder of a year's worth of files, with some random number of files generated every day of the year (but at least one per day). I'm writing a script to automatically grab the file with... (6 Replies)
Discussion started by: WildGooseChased
6 Replies

5. UNIX for Advanced & Expert Users

Help Required for copying a folder from one server to another

Hi all, I have a requirement in which i have to check the latest folder in server A and move the same to Server B with all the subdirectories and files. This has to be done automatically without providing password at each execution. Help me in the same. Thanks in Advance!!! (2 Replies)
Discussion started by: mick_000
2 Replies

6. Shell Programming and Scripting

copying random Jpg files to different folder

Hi, I have 200 pictures in a folder and I would like move 10 random pictures every week to given folder automatically. I have this server on 1and1.com. So I tried the following using Bash script for manual copy and paste for testing #!/bin/bash mapfile -t -n 3 files < <(find... (13 Replies)
Discussion started by: raamkum
13 Replies

7. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

8. UNIX for Dummies Questions & Answers

Copying a file based on a incremented folder name

I need to routinely cp a file out of the highest incremented folder named PROD_2_6.1_xxx where xxx (3 numerical values) is incremented only once a night starting at 100 and ending 999 over a period of time. I am not worried about copying a subset of a complete file while it is being created by some... (5 Replies)
Discussion started by: sf_cb
5 Replies

9. Red Hat

Copy mismatch while copying RHEL DVD to folder

Hi, Here is this weird thing happening here. I mounted RHEL 6.6 DVD on a directoy /a, I am trying to copy it's content to another folder by using command: cp -pr /a/* /new/folder But while I run ls -lrt on both locations it show me difference in number of files. Any specific reason for that.... (5 Replies)
Discussion started by: nixhead
5 Replies
All times are GMT -4. The time now is 02:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy