ryan nuada
ryan nuada

Reputation: 59

How to use variable in system() in c

Please help me with my code. I am using the system() function in c. Let's say I want to make a new directory using C code.

char name[];

printf("Enter the name of directory: ");
scanf("%s", &name);

Then using system(), I want to use the variable name[] instead of using or putting a fixed value like system("mkdir ryan"); which makes a new directory ryan; what I want to happen is that, to create any name of directory and store it to variable name[] and implement that value instead of ryan. Your answers are highly appreciated.

Upvotes: 5

Views: 14091

Answers (5)

Ahmed Masud
Ahmed Masud

Reputation: 22422

here is an example of how it could be implemented:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

int main() {
    char *dirname = NULL;
    char *cmdline = NULL;
    size_t len;
    size_t dirlen = 0;
    int rv = 0;

    printf("Enter directory: ");
    if ( (len = getline(&dirname, &dirlen, stdin)) < 0) {
        perror("getline");
        exit(-1);
    }

    dirname[len-1] = 0;
    cmdline = malloc(len+8);
    snprintf(cmdline, dirlen+8, "mkdir %s", dirname);
    rv = system(cmdline);
    free(cmdline);
    free(dirname);
    rv = WEXITSTATUS(rv);
    return rv;
}

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882806

First off (and this probably doesn't matter if your code is just an example), don't ever use an unbounded %s scanf - that opens you up to buffer overflows.

If you have a string like:

char name[] = "paxdiablo";

you can just use that to construct your own string for execution.

char cmd[1000];
strcpy (cmd, "mkdir ");
strcat (cmd, name);
system (cmd);

And make sure you know (or check with strlen, or dynamically allocate the buffer so it's big enough) the size of name so that you don't end up with a buffer overflow there as well.

An example of the dynamic allocation one:

void tryMkdir (char *dir) {
    static char prefix[] = "mkdir ";
    // Use sizeof to allow for null char at end.
    char *cmd = malloc (sizeof (prefix) + strlen (dir));
    if (cmd != NULL) {
        strcpy (cmd, prefix);
        strcat (cmd, dir);
        system (cmd);
        free (cmd);
    }
}

(although you'd probably want some error checking in there in case the mkdir or malloc failed).

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 755114

You should be using something like:

char name[100];
printf("Enter the name of the directory: ");
if (scanf("%99s", name) == 1)  // Not &name
{
    char command[120];
    sprintf("%s %s", "mkdir", name);
    if (system(command) != 0)
        ...oops...
}

Upvotes: 6

Scott Hunter
Scott Hunter

Reputation: 49920

You'll need to build up a string w/ your full command to send to system() in it, which means allocating space for it (which you didn't do for name in your original code, so that your call to scanf would likely fail). For something like your mkdir example, you could create a string that starts with mkdir & has enough room after that for your directory name, and when you call scanf, point it to the location within that string you want the name to go; saves you having to do a strcat or somesuch afterward, and if this is the only thing you need the directory name for, why store it twice?

Upvotes: 0

wrongusername
wrongusername

Reputation: 18918

Try:

char command[80];
strcpy(command, "mkdir ");
strcat(command, name);
system(command);

Upvotes: -3

Related Questions