473,890 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Append to char *

Hi,

I would like to recursively force to empty and delete a directory. As
such I would like to append the char *.

Here is the code:

char * directory = "Directory" ;
char * command = "rm -rf ";

How can I append the two to get: "rm -rf Directory"???

Following this, I would like to do the system command:

if(system(comma nd) == 0)
{
printf("Success fully removed directory\n");
}

Thanks,
Marcia
Nov 14 '05
16 34754
Richard Heathfield wrote:
Marcia Hon wrote:
char * directory = "Directory" ;
char * command = "rm -rf ";

How can I append the two to get: "rm -rf Directory"???


char command[FILENAME_MAX + sizeof "rm -rf "] = "rm -rf ";
strcat(command, directory);


FILENAME_MAX is either too large or too small here. Too small,
because there's no guarantee (as far as I know) that it's large enough
for the buffer to hold "Directory" ; it's certainly not large enough
for arbitrary user input, which will overflow the buffer. Too large,
because if this code is supposed to only handle the specific string
"Directory" then FILENAME_MAX is unnecessarily large on the
implementations I'm aware of.

Dynamic memory allocation is the only sensible strategy here.

Jeremy.
Nov 14 '05 #11
Derk Gwen wrote:
Ma********@hotm ail.com (Marcia Hon) wrote:
# Hi,
#
# I would like to recursively force to empty and delete a directory. As
# such I would like to append the char *.
#
# Here is the code:
#
# char * directory = "Directory" ;
# char * command = "rm -rf ";

char * commandformat = "rm -rf '%s'";

# How can I append the two to get: "rm -rf Directory"???

char * command = malloc(strlen(d irectory)+strle n(commandformat )+1);
sprintf(command ,commandformat, directory);

The command string is passed to a shell; putting single quotes around
the directory name protects it from shell interpretation if you happen
to have spaces or other shell-interpretted characters in the name.


That's only part of the job, though: the user input may contain single
quotes.

Jeremy.
Nov 14 '05 #12
Jeremy Yallop wrote:
Richard Heathfield wrote:
Marcia Hon wrote:
char * directory = "Directory" ;
char * command = "rm -rf ";

How can I append the two to get: "rm -rf Directory"???
char command[FILENAME_MAX + sizeof "rm -rf "] = "rm -rf ";
strcat(command, directory);


FILENAME_MAX is either too large or too small here. Too small,
because there's no guarantee (as far as I know) that it's large enough
for the buffer to hold "Directory" ;


<shrug> The Standard guarantees it's big enough to hold any filename. I
agree that the Standard doesn't talk about directory names, but one would
have thought a directory name would have to fit into FILENAME_MAX, wouldn't
one?
it's certainly not large enough
for arbitrary user input, which will overflow the buffer. Too large,
because if this code is supposed to only handle the specific string
"Directory" then FILENAME_MAX is unnecessarily large on the
implementations I'm aware of.

Dynamic memory allocation is the only sensible strategy here.


That is /always/ a reasonable approach, in my opinion. (Call me lazy on this
occasion.)

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #13
Johan Lindh wrote:
Duff wrote:
Just include <string.h> header file, and use "strcat()" function:

char * directory = "Directory" ;
char * command = "rm -rf ";
strcat(command, directory);
system(command) ;


That's a pretty good way to make demons fly out your nose, anyway...


Nonsense!
The ANSI/ISO C [89]9 standard
does *not* specify anything about nasal demons.
According to my Linux man page:
STRCAT(3) Linux Programmerís Manual STRCAT(3)

NAME
strcat, strncat - concatenate two strings
Nov 14 '05 #14
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Jeremy Yallop wrote:
Richard Heathfield wrote:
Marcia Hon wrote:

char * directory = "Directory" ;
char * command = "rm -rf ";

How can I append the two to get: "rm -rf Directory"???

char command[FILENAME_MAX + sizeof "rm -rf "] = "rm -rf ";
strcat(command, directory);


FILENAME_MAX is either too large or too small here. Too small,
because there's no guarantee (as far as I know) that it's large enough
for the buffer to hold "Directory" ;


<shrug> The Standard guarantees it's big enough to hold any filename. I
agree that the Standard doesn't talk about directory names, but one would
have thought a directory name would have to fit into FILENAME_MAX, wouldn't
one?


No, the Standard doesn't guarantee that it's big enough to hold any
filename. FILENAME_MAX is "the size needed for an array of char large
enough to hold the longest file name string that the implementation
guarantees can be opened". The implementation bay be able to
succesfully open files with names longer than FILENAME_MAX, and
there's no limit on the length of an *invalid* file name.

If you're writing real-world code, rather than a toy example, you
should at least check strlen(director y) before calling strcat().

If you're being lazy, you might as well have used 200 rather than
FILENAME_MAX; using FILENAME_MAX doesn't really buy you anything.

So what's FILENAME_MAX good for? A program that gets file names from
some external source can reasonably reject names longer than
FILENAME_MAX, but it needs to check explicitly rather than assuming
everything is ok.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #15
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Johan Lindh wrote:

[..]
That's a pretty good way to make demons fly out your nose, anyway...


Nonsense!
The ANSI/ISO C [89]9 standard
does *not* specify anything about nasal demons.
According to my Linux man page:

[snip]

ERT, you've been hanging around here long enough to know what the
whole "nasal demons" thing is about.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #16
Keith Thompson <ks***@mib.or g> scribbled the following:
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Johan Lindh wrote: [..]
> That's a pretty good way to make demons fly out your nose, anyway...


Nonsense!
The ANSI/ISO C [89]9 standard
does *not* specify anything about nasal demons.
According to my Linux man page:

[snip]

ERT, you've been hanging around here long enough to know what the
whole "nasal demons" thing is about.


Sinc when were Linux man pages an authoritative source of what the
ANSI/ISO C standard says, anyway?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
Nov 14 '05 #17

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

Similar topics

3
1560
by: waters | last post by:
I seem to have hit a snag. I am trying to add the list (l_local) as an item in the output list (l_output). "l_local" is a modified copy of the format-list "l_format", which will be updated by index position within the function. The problem occurs in the following statement block: def create_apid_list(l_format,l_values): l_output = for t_curr_line in l_values: # ...for each list in the values list
12
4069
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and compare string lengths afterwards, you would know whether or not the line contained invalid characters. The new method is more than 10x faster than the standard 'if char in string' test! So - here's the code plus sample timings: ''' Translate...
10
873
by: Michael R. Copeland | last post by:
How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data types...) TIA
17
2393
by: Rick | last post by:
Hi, Before copying something into a character array, do we need to initialize it with something? For example: char a; strcpy(a, "hey"); Can we do:
10
1955
by: Amit | last post by:
char *str1="amit"; char str2="mehta" strcat(str1,str2); It will crash, I feel str1 will be stored in code section. Once memory is allocated, you cannot change or append into this. Please correct me If I am wrong..
3
15557
by: some one | last post by:
I have kind of wired problem, I using httpwebrequest to post form data to server , in the GetResponse stage a WebException occurred, after tracing the actual error that occurs on the server, I found the follows. Invalid length for a Base-64 char array. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about
8
1960
by: aeo3 | last post by:
Hello Everybody; I would like to append a number to set of char, I do not like to use string, because In my project I am using several files. What I did but there is a problem with it is as follows 1. char filename; 2. int x=2; 3. filename="xxxxx"; But i do not know how to append 2 to filename. Thanks
13
7982
by: xzzy | last post by:
None of the following properly do the VB.net double quote conversion because all of the following in csharp convert to \" instead of just a double quote: " I have tried: char myDoubleQuote = (char)34; string myDoubleQuote = "" + (char)34;
1
13109
by: gilgil | last post by:
Hi, I'm trying to add some strings inside a file in specific locations. I'm opening a file with fopen and "r+a" and after a while i'm trying to add strings that way: char *line = (char *) malloc(MAX_LINE); while (fgets(line, sizeof(line), theFile) != NULL) if (strstr(line, "I want to append here") != NULL) fputs("My new string", theFile);
0
9970
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9810
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11207
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10794
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10896
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7153
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6031
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4251
muto222
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.