OK, thanks!
OK, thanks!
Out of curiosity I've looked through your code and my alarms went off after seeing this part:
Please get rid of gets() as soon as possible. There is already enough broken code on this planet.Code:while(c) { printf("\n> "); char str[65536]; gets(str); ...
FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
Bye, Andreas
PS: I don't think writing a BASIC interpreter is a beginner's project.
This. Times 1000.PS: I don't think writing a BASIC interpreter is a beginner's project.
Think about learning the at least the basics of the language (and really, you should have a good grasp of the entire language) before doing this.
Yep, I know... I'm so used to languages with C syntax that the transition to C isn't all that bad.
Anyways, I've got another question... I'm using this code (still not perfectly complete) to try to pass a command to the system.
Here's the program output.Code:else if(tok[0]=='s' && tok[1]=='t' && tok[2]=='a' && tok[3]=='r' && tok[4]=='t' && (strlen(tok)) == 5) { //printf ("chrs"); char *q1 = strchr (comStr,'"'); if(q1==NULL) { //char *q4 = strchr(comStr, " "); printf("Non-literal string encountered; code has not yet been implemented"); } //if (q4==NULL) //{ //q4 = strlen(comStr); //printf("Here"); //printf("%.*s", q4-q1-1, q1 + 1); //} //} // if not NULL, q1 points to the first quote if (q1) { char *q2 = strchr(q1 + 1, '"'); // if not NULL, q2 points to the second quote if (q2) { //printf (("%.*s", q2-q1-1, q1)); int result = (system (("%.*s", q2-q1-1, q1))); if(result == 32512) { printf("Error: %.*s cannot be started", q2-q1-1, q1); } else if(result == 32256) { } else if(result == 0) return 1; else { printf ("Error: An unknown error code %i was encountered", result); } } } return 1; }
Obviously, the code isn't properly extracting the string. My question is: why? Almost the exact same code works on the print statement, but for some reason it fails on the start statement.Code:Portable BASIC v1.39, rev2 Copyright (C) Waterfront Software 2012 Approx. 2130509825 bytes free to C > start "sh" app_166@android:/sdcard/C $ exit > start "ifconfig" > start "ifconfig eth0" /system/bin/sh: ifconfig eth0: not found Error: "ifconfig eth cannot be started >
Last edited by WMH; 09-19-2012 at 11:22 AM.
I can guarantee that this line does not do what you think it does.
This statement passes one argument to the system function. That argument is the result of the expression ("%.*s", q2-q1-1, q1), which is the last value (q1). The other operands to the comma operator ("%.*s" and q2-q1-1) are evaluated but their values are discarded. Since those operands have no side effects, the statement is the same as "int result = system(q1)".
The long and short of it is that system() and printf() are totally different functions, and don't take the same type of parameters. The mere fact that they both typically have some sort of string as a parameter doesn't give you carte blanche to do whatever you want with some double quotes. You should read the documentation for the functions you're using, or at least find a good tutorial.Obviously, the code isn't properly extracting the string. My question is: why? Almost the exact same code works on the print statement, but for some reason it fails on the start statement.
I'm pretty sure rags_to_riches was referring to the basics of how the language works, not merely it's syntax. You have some pretty fundamental issues to work out still, and a large-scale project like this isn't the place to do it, too many factors at play when there's a problem. Java has an almost identical syntax, and is a totally different language. Take for example your misunderstanding of the comma operator (see christop's reply in post #20 and mine in post #14 several days ago). It does not behave like the + operator in Java, which intelligently concatenates strings in the right format. Also, you appear unaware of extremely fundamental functions like strcmp. And what is up with the following:
What are those hideous magic numbers? At the very least, you should define a constant. But better yet, read the documentation from your implementation for the system() function to see how to properly handle the return code and determine success or failure. For example, on my system (Linux), system() returns a value of the same format as the wait/waitpid funcitons. Use the macros you see in the waitpid man page (like WEXITSTATUS) if you are using Linux.Code:int result = (system (("%.*s", q2-q1-1, q1))); if(result == 32512) ... else if(result == 32256)
One more thing, your indentation is pretty awful in that post. If your actual code is that messy, you don't stand much of a chance. If your code is clear and easy to read, then it's less likely you make mistakes, and easier to find and fix them when you do. If it's not that messy, but just posted that way, be kind to those helping you and reformat it so it's easy for us to read.
Seriously, please take our advice, and spend a few weeks to really work on your C fundamentals, until you understand pointers, strings, arrays, function calls and all the basics (read: not just syntax). If you have lots of free time (part time job, light class load at school, etc), then you could probably learn what you need in a few long days to a week. Then, at the very least, you wont spend so much time waiting for replies to basic questions on forums or listening to us pontificate.
Last edited by anduril462; 09-19-2012 at 04:58 PM. Reason: Add punctuation and a few words