LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2003, 07:03 AM   #1
homerz
LQ Newbie
 
Registered: Oct 2003
Posts: 9

Rep: Reputation: 0
Newbie C program


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.
 
Old 11-04-2003, 07:06 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
is this a joke? are you actually looking for someone to write this entire program for you???
 
Old 11-04-2003, 09:02 AM   #3
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Sounds like a homework assignment to me... figure it out yourself.
 
Old 11-04-2003, 09:18 AM   #4
smn
LQ Newbie
 
Registered: Nov 2003
Distribution: red hat 9
Posts: 3

Rep: Reputation: 0
woo...even lazier than me.
 
Old 11-04-2003, 09:36 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 11-04-2003, 10:57 AM   #6
homerz
LQ Newbie
 
Registered: Oct 2003
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 11-04-2003, 12:00 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
/* 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);
}
 
Old 11-05-2003, 07:18 PM   #8
HappyDude
Member
 
Registered: Jul 2003
Posts: 140

Rep: Reputation: 15
Man... Talk about assingments...
 
Old 11-05-2003, 11:31 PM   #9
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
now we should mention something about how god forsaken system calls really are...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie/program for calling BBS's Ltkpr49 Linux - Software 1 06-29-2005 09:45 AM
newbie needs help with simple program mastervampire Linux - Newbie 1 03-07-2005 05:26 AM
C++ Newbie stuck on first simple program webwolf70 Programming 8 09-14-2004 01:29 AM
Newbie: Gimp 2.0 Program doesn't run! asburyguy Linux - Software 4 04-05-2004 09:59 PM
c++ newbie - help debug simple program please mymojo Programming 4 11-27-2003 04:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration