473,472 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Linux Customized Commands

Hello All, I have been working on some customized linux commands. I wrote
the command mycpd dir1 dir2 that is supposed to copy a directory to
another. I couldnt debug it in time. But here is wat it does. It reads one
directory recursively, cats the output to a file and then reads that file
(temporary) and call cp for each of its tokens to dir2.

If anyone needs it... check it out. The mycp method is working if u need
it. I can post it.

#include<stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

main(int argc, char *argv[]){

DIR *dir_p;
struct dirent *dir_entry_p;

dir_p= opendir(argv[1]);
FILE *f1 =fopen(argv[2],"w");

if( argv[1]!=NULL && argv[2]!= NULL){

// if(strcmp(argv[1],c1) ==0 && strcmp(argv[2],c2)==0)
while(NULL != (dir_entry_p=readdir(dir_p))){

fprintf(f1,"%s\n",dir_entry_p->d_name);
}
closedir(dir_p);
fclose(f1);
}
else
printf("Illegal Number of Arguments");

FILE *f2 = fopen(argv[2],"r");

char test_string[1000];
char *sub_string;
char c;
int i=0;
while(i<10){

c=getc(f2);
test_string[i]=c;
i++;
}

printf("%s\n",strtok(test_string," "));

while((sub_string=strtok(NULL," "))!=NULL){
printf("%s\n",sub_string);
/*copy each token to location execv("mycp sub_string argv[3]");
while(sub_string !=NULL){
execv("./mycp","sub_string","argv[3]");
i.e. Read the contents of file that displays the content of the directory,
and copy them to lovation argv[3]*/
}

}


Nov 14 '05 #1
7 1617
Afifov <af****@yahoo.com> spoke thus:
Hello All, I have been working on some customized linux commands. I wrote
the command mycpd dir1 dir2 that is supposed to copy a directory to
another. I couldnt debug it in time. But here is wat it does. It reads one
directory recursively, cats the output to a file and then reads that file
(temporary) and call cp for each of its tokens to dir2.
This would have been better posted to comp.unix.programmer:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html
main(int argc, char *argv[]){
main() returns int; as of C99, the return type must be specified.
FILE *f1 =fopen(argv[2],"w"); if( argv[1]!=NULL && argv[2]!= NULL){
Huh? This is both illegal and unnecessary; far better to simply check
the value of argc, preferably before attempting to invoke fopen() on
argv[2].
// if(strcmp(argv[1],c1) ==0 && strcmp(argv[2],c2)==0)
while(NULL != (dir_entry_p=readdir(dir_p))){
fprintf(f1,"%s\n",dir_entry_p->d_name);
And what if fopen() failed? Kablooie!
}
closedir(dir_p);
fclose(f1);
}
else
printf("Illegal Number of Arguments");
If it's illegal, why continue execution? exit( EXIT_FAILURE ) sounds
reasonable.

FILE *f2 = fopen(argv[2],"r");
Are you sure you didn't mean to open argv[1] earlier?
char test_string[1000];
char *sub_string;
char c;
int i=0;
while(i<10){
c=getc(f2);
What if c == EOF? Your code will be none the wiser, and still more
broken.
test_string[i]=c;
i++;
} printf("%s\n",strtok(test_string," ")); while((sub_string=strtok(NULL," "))!=NULL){
printf("%s\n",sub_string);
/*copy each token to location execv("mycp sub_string argv[3]");
while(sub_string !=NULL){
execv("./mycp","sub_string","argv[3]");
i.e. Read the contents of file that displays the content of the directory,
and copy them to lovation argv[3]*/
}


Among other problems that may exist here, you haven't ensured that
there *is* an argv[3], which is likely to lead to tragedy.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
# while(NULL != (dir_entry_p=readdir(dir_p))){
#
# fprintf(f1,"%s\n",dir_entry_p->d_name);

I would suggest you echo this entry and verify what you are telling
the copier to copy. It may include things you are not expecting.

# while((sub_string=strtok(NULL," "))!=NULL){
# printf("%s\n",sub_string);
# /*copy each token to location execv("mycp sub_string argv[3]");
# while(sub_string !=NULL){
# execv("./mycp","sub_string","argv[3]");

A successful exec does not return. It replaces the current program
with the named program; if you want to call other program and continue
after with your current program, you can use the system() function
or unix specific calls involving fork() and waitpid() and other things.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
What kind of convenience store do you run here?
Nov 14 '05 #3
# > FILE *f1 =fopen(argv[2],"w");

# > FILE *f2 = fopen(argv[2],"r");
#
# Are you sure you didn't mean to open argv[1] earlier?

You snipped the open of argv[1]: dir_p= opendir(argv[1]);

argv[2] is the name of the temporary file, openned write, written, closed,
and reopenned read.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
Nov 14 '05 #4
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
# while(NULL != (dir_entry_p=readdir(dir_p))){
#
# fprintf(f1,"%s\n",dir_entry_p->d_name);

I would suggest you echo this entry and verify what you are telling
the copier to copy. It may include things you are not expecting.

# while((sub_string=strtok(NULL," "))!=NULL){
# printf("%s\n",sub_string);
# /*copy each token to location execv("mycp sub_string argv[3]");
# while(sub_string !=NULL){
# execv("./mycp","sub_string","argv[3]");

A successful exec does not return. It replaces the current program
with the named program; if you want to call other program and continue
after with your current program, you can use the system() function
or unix specific calls involving fork() and waitpid() and other things.


We don't know whether a successful (or unsuccessful) call to exec() or
execv() returns or not. There is no such function in standard C.

To put it another way, this whole discussion belongs in another
newsgroup, most likely comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Keith Thompson <ks***@mib.org> wrote:

# To put it another way, this whole discussion belongs in another
# newsgroup, most likely comp.unix.programmer.

You need to renew your alt.religion.scientology cancelbot license.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I ASSURE YOU WE'RE OPEN!
Nov 14 '05 #6
On Tue, 10 Aug 2004 02:10:29 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
comp.lang.c:
Keith Thompson <ks***@mib.org> wrote:

# To put it another way, this whole discussion belongs in another
# newsgroup, most likely comp.unix.programmer.

You need to renew your alt.religion.scientology cancelbot license.


You need to get a clue. We don't need time wasters posting off-topic
material here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
I tried EOF in both cases and it doesnt seem to stop. Could it be my linux
version? About arg3 , it is supposed to be implicit, as in a temporary
buffer.

Anyways,sorry for the post. But its C language anyways. will post at
unix.programmer next time.

Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Google Mike | last post by:
I guess I'm talking to get this off my chest. I guess I'm talking to the newbies out there wondering if they want to remain on ASP.NET, C#, JSP, or whatever, or whether they want to do their next...
7
by: Edward Diener | last post by:
I can install Python 2.4 on the Fedora 3 Linux system, but after I do a number of Linux utilities and commands, like yum, stop working because they were dependent on the Python 2.3 installation....
6
by: cantabile | last post by:
Hi, I'd like to get drives and partitions (and their size too) with python under Linux. So far, I thought of reading /proc/partitions but maybe i could use fdsik also ? How would I do that in...
1
by: Adriaan Renting | last post by:
I have the problem that I need to interact with a CD/DVD burning program called gear. I do this by running it in a pseudo terminal. I also need to log what I'm doing, so I copy all output I get...
3
by: Bruno LIVERNAIS | last post by:
Hi, We are currently installing a DB2 V9 ESE on a Linux server (RHEL4U4-x86_64). Installation runs successfully on each node. Database user environment is OK and the instance is well created. To...
1
by: nirmalarasu | last post by:
Hi, Iam look looking some standalone python package for both in windows and linux. In windows py2exe tool serving my purpose,is there any similar tool i can find in linux.? Though most of...
2
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.