ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to make a c program in linux. let's say that i will name my file test.cc
In this program i want to show the use of the system commands mkdir and rmdir, so a user can come and execute the commands in any order he wants and the output of the program must show the usage of this command and prove the correct operation. What is the code for this program. Thanks.
You call system("command goes here") to execute shell commands from isude a program.
If this isn't a school assignment post back and we'll try to help.
Also, if this is not a school assignment, consider using shell script.
Quote:
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h>
int system (const char * string);
DESCRIPTION
system() executes a command specified in string by calling
/bin/sh -c string, and returns after the command has been
completed. During execution of the command, SIGCHLD will
be blocked, and SIGINT and SIGQUIT will be ignored.
Anyone who cant solve the problem just stay quiet nurds its not a school assignment. jim thanks for replying, the main problem is how i can convert the commands of the user in the C program so they will become system calls? if u can give me an example about the mkdir command. Thanks
/* my_mkdir.c
get the name of a directory to create
usage: my_mkdir newdirectoryname
to compile:
gcc -o my_mkdir my_mkdir.c
chmod +x my_mkdir -- if the file isn't already executable
*/
#include <stdlib.h>
#include <string.h>
#define zout(z) memset(&z,0x00,sizeof(z))
void help(void);
int main(int argc, char *argv[]){
int retval=0;
char commandline[256];
if(argc <2 || argc > 3) help(); /* the user typed the wrong number of arguments */
/* assume that argv[1] is the name of
the directory to create */
zout(commandline); /* set all characters in commandline to ASCII 0 */
strcpy(commandline,"mkdir "); /* copy the work mkdir to the start of the command */
strcat(commandline,argv[1]); /* add the directory name */
retval=system(commandline); /* execute the command */
if(retval){ /* check to see if it worked */
printf("Command: %s failed.\n",commandline); /* we bombed */
return 1; /* return failure */
}
printf("Command: %s succeeded.\n",commandline); /* we did okay */
return 0; /* return success */
}
void help(void){ /* give the user some help and then return an error */
printf("Invalid number of arguments\n");
printf("Usage: my_mkdir <directory name> \n");
exit(EXIT_FAILURE);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.