I'm trying to get commands from the keyboard in a similiar fashion as command line args int main( int argc, char *argv[] )but in a separate function. When I parse and print them within the scope of the getCmd() function all looks and behaves as intended, but as soon as they return to the main function they become a bunch of garbage. My questions are below the code.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
void getCmd(char *cmd, char *args[])
{
char input[81] = { 0 };
char *next_token = NULL;
printf("$ ");
fgets(input, 81, stdin);
input[strcspn(input, "\n")] = 0;
cmd = strtok_s(input, " ", &next_token);
if (!strcmp(cmd, "mv"))
{
args[0] = strtok_s(NULL, " ", &next_token);
args[1] = strtok_s(NULL, " ", &next_token);
printf("\n\n%s\n%s\n%s\n\n", cmd, args[0], args[1]);
}
}
int main(void)
{
char *cmd = NULL, *args[5];
cmd = (char *)calloc(20,sizeof(char));
for (size_t i = 0; i < (size_t)5; i++)
{
args[i] = (char *)calloc(20,sizeof(char));
}
getCmd(cmd, args);
printf("\n\n%s \n%s\n%s", cmd, args[0], args[1]);
return 0;
}
I don't think its relevant but I'm using VS 2015 Community with the Visual C++ compiler on a 64 bit processor, Windows 7 OS.
My questions:
How should I pass the cmd and args[] by reference?
Are there any widely accepted idioms that deal with this sort of situations?
I've looked trough a few of the similiar questions and couldn't find a solution that works in this context, if the question is a duplicate, tell me and I'll close it.Since I'm new to stackoverflow any question formatting tips would be greatly appreciated. Cheers! (:
There are a number of different ways to approach the problem. While you are free to dynamically allocate memory for cmd and your args array, there is really no need, for this limited amount of memory you can use a static declaration for all. There is no need for a separate input array, you cause use cmd for that purpose and then tokenize cmd. This provides the benefit of leaving the first token nul-terminated in cmd after strtok is called.
note: in the example below, strtok is used, strtok_s was an optional compiler addition in C11, and unfortunately, I don't have a compiler that implements that option, so I test with strtok. You can easily make the change for VS.
#include <stdio.h>
#include <string.h>
enum { NARGS = 5, MAXC = 128 };
size_t getcmd (char *cmd, char (*args)[MAXC]);
int main (void) {
char cmd[MAXC] = "", args[NARGS][MAXC] = { "" };
size_t i, n;
if (!(n = getcmd (cmd, args))) return 1;
printf (" %s", cmd);
for (i = 0; i < n; i++)
printf(" %s", args[i]);
putchar ('\n');
return 0;
}
size_t getcmd (char *cmd, char (*args)[MAXC])
{
char *delim = " ,.\t\n";
char *p = NULL;
size_t idx = 0;
printf ("$ ");
if (!fgets (cmd, MAXC, stdin)) {
fprintf (stderr, "error: invalid input.\n");
return 0;
}
strtok (cmd, delim); /* terminate after 1st token */
for (p = strtok (NULL, delim); p; p = strtok (NULL, delim)) {
strncpy (args[idx++], p, MAXC); /* limit to avail chars */
if (idx == NARGS) break; /* limit to available bounds */
}
return idx;
}
Note above, the return type of getcmd is size_t. Always choose a meaningful type to return an indication of success/failure as well as returning some needed information (the number of arguments here). Also note the C-Style Guide disfavors camelCase variable/function names preferring all lower-case instead. Leave camelCase names for C++. See e.g. NASA - C Style Guide, 1994
Example Use/Output
$ ./bin/getcmd
$ mv /this/here/file /that/there/file
mv /this/here/file /that/there/file
$ ./bin/getcmd
$ mv -i --strip-trailing-slashes /this/here/file /that/there/file
mv -i --strip-trailing-slashes /this/here/file /that/there/file
Look it over and let me know if you have any additional questions.
strtok_s() return pointer into the buffer it's parsing (input here).
input lives on getCmd() stack. It dies the moment getCmd() returns. From then on the addresses pointing into input and that had been stored in args's elements does not point to valid memory any more.
The code needs allocate fresh memory and copy what strtok_s() returned a pointer to.
Have a look on ow this can be done:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getCmd(char **pcmd, char *args[], size_t s)
{
char input[81] = { 0 };
char *next_token = NULL;
printf("$ ");
fgets(input, 81, stdin);
input[strcspn(input, "\n")] = 0;
(*pcmd) = _strdup(strtok_s(input, " ", &next_token));
if (!strcmp(*pcmd, "mv"))
{
args[0] = _strdup(strtok_s(NULL, " ", &next_token));
args[1] = _strdup(strtok_s(NULL, " ", &next_token));
printf("\n\n%s\n%s\n%s\n\n", *pcmd, args[0], args[1]);
}
}
#define ARGS_MAX (5)
int main(void)
{
char *cmd, *args[ARGS_MAX] = {0};
getCmd(&cmd, args, ARGS_MAX);
printf("\n\n%s \n%s\n%s", cmd, args[0], args[1]);
/* Clean up. */
free(cmd);
for (size_t i = 0; i < ARGS_MAX; ++i)
{
free(args[i]);
}
return 0;
}
Related
I want to convert a string to an array of strings and I get an error
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int count = 0;
char *str = argv[1];
char *token, *last;
char **arr_str = calloc(9999, sizeof(char*));
token = strtok_r(str, " ,", &last);
arr_str[count] = strcpy(calloc(strlen(token), sizeof(char)), token);
while (token != NULL) {
count++;
token = strtok_r(NULL, " ", &last);
arr_str[count] = strcpy(calloc(strlen(token), sizeof(char)), token);
printf("%s", arr_str[count - 1]);
}
printf("------------");
while(arr_str[count])
printf("%s", arr_str[count--]);
exit (0);
}
how to allocate memory for a string and make a pointer to it from an array?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/* always check argument count */
if(argc < 2) {
printf("Not enough arguments given\n");
return 1;
}
int count = 0;
char *str = malloc(strlen(argv[1] + 5));
memcpy(str, argv[1], strlen(argv[1]));
char *token, *last;
char **arr_str = calloc(9999, sizeof(char*));
token = strtok_r(str, " ,", &last);
while ((token = strtok_r(NULL, " ", &last)) != NULL) {
count++;
/* sizeof(char) is always 1 and is redundant unless you are on
a obscure platform that it returns other than 1
which shouldnt exist in modern world
*/
arr_str[count] = malloc(strlen(token) + 1);
strcpy(arr_str[count], token);
}
printf("------------");
while(arr_str[count])
printf("%s", arr_str[count--]);
exit (0);
}
strtok is destructive meaning it edits strings it encounters, it tried to edit argv which resulted in a segmentation error.
I also edited code to follow better practices and edited formatting.
You need memory for elements of the arr_str.
calloc(9999) while not great if this not going to end up in a serious application it's not a issue.
sizeof(char) should always return 1 on a normal modern system unless you are on extremely obscure system
Use puts(char* s) if you don't need string formatting.
You should do input validation.
I'm looking for the simplest way in standard C to parse a string. The number of words inside the string is fixed, but the length of each single word is not. The code will be running on a microprocessor with limited memory so I can't just allocate an overkill buffer, I'd like to allocate just the memory that I need.
The following code works, but I'd like the single words to be char* . Is there some way around this?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char * my_words = "foo bar 1 2";
char word1[20];
char word2[20];
char word3[20];
char word4[20];
int match = sscanf(my_words,"%s %s %s %s",word1,word2,word3,word4);
printf("Matches: %d\r\n",match);
printf("%s\r\n",word1);
printf("%s\r\n",word2);
printf("%s\r\n",word3);
printf("%s\r\n",word4);
return 0;
}
Thank you
For parsing you can use strtok() function. A simple approach can be like that also you can modify it
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char const *my_words = "foo bar 1 2";
char *str = malloc(1 + strlen(my_words));
strcpy(str, my_words);
int countWord = 0;
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
++countWord;
}
printf("Total words = %d\n", countWord);
return 0;
}
The answer depends on how simple and standard your code should be.
If your target supports POSIX 2008 (recent GNU libc does), then you could use m modifier as docs suggest to allocate just enough space to read data.
But if you have to stay with ANSI C, then probably you're stuck to functions like strtok/strtok_r or alike.
And in the event you must roll your own, the algorithm goes like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
/* your input, I'll presume its constant */
const char *input = " foo bar 1 2 ";
/* here is your array of char*.
* you indicate the number of words is fixed */
char *words[4];
/* the algo */
size_t b = 0;
size_t e = 0;
size_t l = strlen(input);
int w = 0;
while (b < l) {
b += strspn(input + b, " ");
e = b + strcspn(input + b, " ");
words[w] = malloc(e - b + 1);
strncpy(words[w], input + b, e - b);
w++;
b = e+1;
}
/* debugging, outputs in reverse order */
while (w--) {
printf("%s\n", words[w]);
free(words[w]);
}
exit(EXIT_SUCCESS);
}
Obviously, you'd want to add error checking.
I've a program which takes any number of words from the command-line arguments and replaces them with the word 'CENSORED'. I finally have the program working for the first argument passed in, and I am having trouble getting the program to censor all arguments, outputted in just a single string. The program rather functions individually on a given argument and does not take them all into account. How would I modify this?
How does one use/manipulate multiple command-line arguments collectively ?
My code follows.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *replace_str(char *str, char *orig, char *rep, int j, int argc)
{
static char buffer[4096];
char *p;
for ( j = 1; j <= argc; j++ )
{
if(!(p = strstr(str, orig))) // Check if 'orig' is not in 'str'
{
if ( j == argc ) { return str; } // return str once final argument is reached
else { continue; } // restart loop with next argument
}
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' str
buffer[p-str] = '\0';
if ( j == argc ) { return buffer; }
else { continue; }
}
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
}
int main( int argc, char* argv[] ) //argv: list of arguments; array of char pointers //argc: # of arguments.
{
long unsigned int c, i = 0, j = 1;
char str[4096];
while ( (c = getchar()) != EOF )
{
str[i] = c; // save input string to variable 'str'
i++;
}
puts(replace_str( str, argv[j], "CENSORED", j, argc ) );
return 0;
}
i.e.
$ cat Hello.txt
Hello, I am me.
$ ./replace Hello me < Hello.txt
CENSORED, I am CENSORED.
Two issues, you are not guaranteeing a null-terminated str and second, you are not iterating over the words on the command line to censor each. Try the following in main after your getchar() loop:
/* null-terminate str */
str[i] = 0;
/* you must check each command line word (i.e. argv[j]) */
for (j = 1; j < argc; j++)
{
puts(replace_str( str, argv[j], "CENSORED", j, argc ) );
}
Note: that will place each of the CENSORED words on a separate line. As noted in the comments, move puts (or preferably printf) outside the loop to keep on a single line.
Edit
I apologize. You have more issues than stated above. Attempting to check the fix, it became apparent that you would continue to have difficulty parsing the words depending on the order the bad words were entered on the command line.
While it is possible to do the pointer arithmetic to copy/expand/contract the original string regardless of the order the words appear on the command line, it is far easier to simply separate the words provided into an array, and then compare each of the bad words against each word in the original string.
This can be accomplished relatively easily with strtok or strsep. I put together a quick example showing this approach. (note: make a copy of the string before passing to strtok, as it will alter the original). I believe this is what you were attempting to do, but you were stumbling on not having the ability to compare each word (thus your use of strstr to test for a match).
Look over the example and let me know if you have further questions. Note: I replaced your hardcoded 4096 with a SMAX define and provided a word max WMAX for words entered on the command line. Also always initialize your strings/buffers. It will enable you to always be able to easily find the last char in the buffer and ensure the buffer is always null-terminated.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SMAX 4096
#define WMAX 50
char *replace_str (char *str, char **bad, char *rep)
{
static char buffer[SMAX] = {0};
char *p = buffer;
char *wp = NULL;
unsigned i = 0;
unsigned char censored = 0;
char *str2 = strdup (str); /* make copy of string for strtok */
char *savp = str2; /* and save start address to free */
if (!(wp = strtok (str2, " "))) /* get first word in string or bail */
{
if (savp) free (savp);
return str;
}
while (bad[i]) /* test against each bad word */
{
if (strcmp (wp, bad[i++]) == 0) /* if matched, copy rep to buffer */
{
memcpy (buffer, rep, strlen (rep));
censored = 1;
}
}
if (!censored) /* if no match, copy original word */
memcpy (buffer, wp, strlen (wp));
while ((wp = strtok (NULL, " "))) /* repeat for each word in str */
{
i = 0;
censored = 0;
memcpy (strchr (buffer, 0), " ", 1);
p = strchr (buffer, 0); /* (get address of null-term char) */
while (bad[i])
{
if (strcmp (wp, bad[i++]) == 0)
{
memcpy (p, rep, strlen (rep));
censored = 1;
}
}
if (!censored)
memcpy (p, wp, strlen (wp));
}
if (savp) free (savp); /* free copy of strtok string */
return buffer;
}
int main ( int argc, char** argv)
{
unsigned int i = 0;
char str[SMAX] = {0};
char *badwords[WMAX] = {0}; /* array to hold command line words */
for (i = 1; i < argc; i++) /* save command line in array */
badwords[i-1] = strdup (argv[i]);
i = 0; /* print out the censored words */
printf ("\nCensor words:");
while (badwords[i])
printf (" %s", badwords[i++]);
printf ("\n\n");
printf ("Enter string: "); /* promt to enter string to censor */
if (fgets (str, SMAX-1, stdin) == NULL)
{
fprintf (stderr, "error: failed to read str from stdin\n");
return 1;
}
str[strlen (str) - 1] = 0; /* strip linefeed from input str */
/* print out censored string */
printf ("\ncensored str: %s\n\n", replace_str (str, badwords, "CENSORED"));
i = 0; /* free all allocated memory */
while (badwords[i])
free (badwords[i++]);
return 0;
}
use/output
./bin/censorw bad realbad
Censor words: bad realbad
Enter string: It is not nice to say bad or realbad words.
censored str: It is not nice to say CENSORED or CENSORED words.
I am asked to implement my own shell for an Operating System class.
My shell runs every commands fine, except ls that won't return on execve, which is weird because cd, cp, mv, and all the others main commands are returning okay.
ls is still displaying the right output (the list of files in the folder), but just keep running after (execve hangs and needs a carriage return to finish).
All the options like -l, -a are also working correctly, with the same issue.
EDIT: I modified my code in order to completely avoid any memory leaks (I used valgrind to track them), added some comments so you can see what's going on, but ls is still not returning. Here is the updated version:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXPATHLEN 40
#define MAXSIZE 100
#define MAXARGS 10
static char cwd[MAXPATHLEN];
typedef void (*sighandler_t)(int);
void handle_signal(int signo);
void parse_command(char *command, char **arguments);
int main(int argc, char *argv[], char *envp[])
{
int status;
char *command;
char **arguments;
signal(SIGINT, SIG_IGN);
signal(SIGINT, handle_signal);
while(1) {
//Allocating memory
command = calloc(MAXSIZE, sizeof(char));
arguments = calloc(MAXARGS, sizeof(char *));
//Print shell name and cwd
getcwd(cwd,MAXPATHLEN);
printf("[MY_SHELL]:%s$ ", cwd);
parse_command(command, arguments);
//Displays command and arguments
printf("Command is %s\n", command);
int i;
for(i=0; arguments[i] != NULL; ++i){
printf("Argument %d is %s\n", i, arguments[i]);
}
//Fork exec code
if (fork() != 0){
waitpid(1, &status, 0);
} else{
execve(command, arguments, 0);
}
free(command);
for (i=0; arguments[i] != NULL; ++i) {
free(arguments[i]);
}
free(arguments);
}
return 0;
}
void handle_signal(int signo)
{
getcwd(cwd,MAXPATHLEN);
printf("\n[MY_SHELL]:%s$ ", cwd);
fflush(stdout);
}
void parse_command(char *command, char **arguments){
char buf[MAXSIZE];
char env[MAXPATHLEN];
char *tmp;
//Initiate array values to avoid buffer overflows
memset(buf, 0, sizeof(buf));
memset(env, 0, sizeof(env));
//Read command and put it in a buffer
char c = '\0';
int N = 0; //Number of chars in input - shouldn't be more than MAXSIZE
while(1) {
c = getchar();
if (c == '\n')
break;
else{
if (N == MAXSIZE)
break;
buf[N] = c;
}
++N;
}
//Extract command name (e.g "ls"), fetch path to command, append it to command name
tmp = strtok(buf, " ");
strcpy(env, "/bin/");
size_t len1 = strlen(env);
size_t len2 = strlen(tmp);
memcpy(command, env, len1);
memcpy(command + len1, tmp, len2);
//Extracts arguments array: arguments[0] is path+command name
arguments[0] = calloc(strlen(command) + 1, sizeof(char));
strcpy(arguments[0], command);
int i = 1;
while(1){
tmp = strtok(NULL, " ");
if (tmp == NULL)
break;
else{
arguments[i] = calloc(strlen(tmp) + 1, sizeof(char));
strcpy(arguments[i],tmp);
++i;
}
}
}
EDIT 2: This seems to have something to do with STDIN (or STDOUT): similarily than ls, cat makes execve hangs after executing, and I need to carriage return to have my shell line [MY_SHELL]current_working_directory$: line back. Any thoughts on why it is the case ?
In your code, in parse_command() function, you're doing
bzero(arguments, sizeof(char) * MAXARGS);
but at that point of time, arguments is not initialized or allocated memory. So essentially you're trying to write into uninitialized memory. This invokes undefined behaviour.
Same like that, without allocating memory to arguments, you're accessing arguments[0].
Note: As I already mentioned in my comments, do not cast the return value of malloc() and family.
C uses pass by value. That means that after the call to parse_command the value of arguments will still be undefined, since any assignments were made to the local copy. Instead of becoming a three-star programmer I would recommend that you have parse_command return the argument list instead:
char **parse_command(char *command){
char **arguments = malloc(...);
...
return arguments;
}
And in main:
arguments = parse_command(command);
Also look at Sourav Ghosh's answer as he points out some other bugs.
I have an array of structs, inside the while loop I add things to that array, however when I print out the array I get the wrong output?
(The last element added is printed out n times, n being the number of things I added)
I have googled this and I think it is because a while loop in Bash creates a subshell, not too sure.
Any help would be much appreciated
(please have patience I am only a student!!)
Using Mac OSX mountain lion
Xcode 4 gcc
Code:
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
typedef struct{
char* one;
char* two;
} Node;
Node nodes[100];
int count = 0;
void add(char *one,char*two){
Node newNode = {one,two};
nodes[count]= newNode;
printf("one: %s\n",one);
printf("two: %s\n",two);
count++;
}
void print(){
int x;
for (x = 0; x < 10; x++)
printf("%d : (%s, %s) \n",x,nodes[x].one, nodes[x].two);
}
void check(char **arg)
{
if(strcmp(*arg, "Add") == 0)
add(arg[1],arg[2]);
else if(strcmp(*arg,"print") == 0)
print();
else
printf("Error syntax Enter either: \n Add [item1][item2]\n OR \n print\n");
}
void readandParseInput(char *line,char **arg)
{
if (fgets (line, 512, stdin)!= NULL) {
char * pch;
pch = strtok (line," \n\t");
int count = 0;
arg[0] = pch;
while (pch != NULL)
{
count++;
pch = strtok (NULL, " \n\t");
arg[count] = pch;
}
}else{
printf("\n");
exit(0);
}
}
int main()
{
int i;
for(i = 0;i <100; i++){
nodes[i].one = ".";
nodes[i].two = ".";
}
char line[512]; /* the input line */
char *arg[50]; /* the command line argument */
while (1)
{
readandParseInput(line,arg);
if(arg[0] != NULL)
check(arg);
}
return(0);
}
strtok() returns a pointer to different elements within the buffer it was initially passed. This means that all entries in the array will be pointing to different elements of the same buffer, named line. You need to make a copy of the pointer returned by strtok():
use malloc(), strlen() and strcpy(). Or,
use the non-standard strdup()
in either case, the memory must be free()d when no longer required.
It's because you use the same buffer for all input.
You need to duplicate the strings you put into the structures. Either by using arrays for the strings and strcpy into them, or by using strdup to allocate new memory for the strings and do the copying in one function.