jephthah 1,888 Posting Maven

strcpy(msg,"hello")

:)

jephthah 1,888 Posting Maven

Try setting a very large new buffer.

yah, that, or maybe just call a flea flicker and have the running back throw a hail mary to the kicker in the end zone.

jephthah 1,888 Posting Maven

i see now whats wrong you missed a couple points.

*msg needs to point /somewhere/... either declared as array[ ] or allocated.

you have to send the "sizeof" your message buffer to the "recv" function... not the length of the string (strlen)

i modified this to work with a public server. adjust your code accordingly

#define EXIT_SUCCESS 0
#define PORT 666            //BOFH Excuse Server
#define IP "193.202.115.241"  

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

char     msg[2000];
int      sockfd, connresult, recvresult, sendresult;
struct   sockaddr_in server_addr;
bool     Connected;

int main(int argc, char** argv) 
{
    server_addr.sin_family = AF_INET; 
    server_addr.sin_addr.s_addr = inet_addr(IP);
    server_addr.sin_port = htons(PORT);
        
    //Create the Socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    printf("sockfd = %d\n",sockfd);
    if (sockfd < 0) 
    {
        printf("errno %d: %s\n",errno,strerror(errno));
        exit(1);
    };
        
    //Connect
    errno=0;
    connresult = connect(sockfd,(struct sockaddr *)&server_addr, 
					  sizeof(server_addr));
    printf("connresult = %d\n",connresult);
    if (connresult < 0)
    {
        printf("errno %d: %s\n",errno,strerror(errno));
        exit(2);
    }    
    printf("Connected to host\n");
      
    // receive
    Connected = true;      
    while(Connected) 
    {
        errno = 0; 
        recvresult = recv(sockfd,msg,sizeof(msg), 0);
        //printf("recvresult = %d\n",recvresult);
        if (recvresult < 0)
        {
            printf("errno %d: %s\n",errno,strerror(errno));
            Connected=false;   
           
        }
        //Handle received messages here
        else if (recvresult > 0)
            printf("%s", msg);
        else
            Connected=false;   
    }
    printf("\n");

    close(sockfd);
    return (EXIT_SUCCESS);
}
jephthah 1,888 Posting Maven

change it to this:

server_addr.sin_family = AF_INET; 
	server_addr.sin_addr.s_addr = inet_aton(IP);
	server_addr.sin_port = htons(PORT);
jephthah 1,888 Posting Maven

I didn't get you. This is not the answer of my problem.Please be clear.

okay... how about, "You need to quit relying on others to do your work, and do it yourself. When you have put forth some effort, and then get stuck, come back with a sensible question."

is that clear?

jephthah 1,888 Posting Maven

wow.

now THAT was one helluva gift, serverdude.

you don't even know.

jephthah 1,888 Posting Maven

at the very least, you have to have an equal number of open parentheses and close parentheses '(' and ')' in any given statement.

and if youre going to have a conditional after "else", then it needs to be "else if" ... which then presumes another statement to follow for "else", otherwise you could have a condition where neither statements are true.

jephthah 1,888 Posting Maven

If a train leaves the station @ 8:30 p.m. full of clowns named bob and larry and one named steve and it takes 1 hour to get there, the number of licks it will take to get to the center of a tootsie roll tootsie pop

= 42.

QED.

Ancient Dragon commented: smartass :) +25
jephthah 1,888 Posting Maven

Currently I'm working like this:

gtsee.com/cgi-bin...

whoa, for a sec i thought you were pointing us to the goatse dude

:O

jephthah 1,888 Posting Maven

awesome.

now we might get this to four pages!

:-D

jephthah 1,888 Posting Maven

that sounds a bit ambiguous.

if you're doing a username, convention is to allow letters, numbers, and underscores only ... and to insist on a letter only for the first position

in regex, the '\w' will search for just that.

if youre doing a password, convention is to allow any and all characters to occur at any place within the string.

your first post indicated you were getting a username.


.

jephthah 1,888 Posting Maven

use some parentheses


.

jephthah 1,888 Posting Maven

so the same commands will work on both linux and windows ?

rarely, if ever, does windows and linux have the same commands for anything.

for compiling C on windows, you might typically use the MSVC compiler distributed for free from Microsoft, either as a standalone or as part of the Visual Studio. the basic compiler is invoked by "cl", and has totally different arguments than other compilers, such as gcc.

on linux you'll typically use "gcc" which is, imo, superior. windows uses a lot of non-standard libraries that do not port, and is the cause of much frustration.

whats the diffrence btw

gcc -o myprogram myprogram.c -lm

and gcc myprogram.c -lm

the -o argument allow you to name the "output" of the compiled binary. if you dont use -o, it will always name it "a.out"

jephthah 1,888 Posting Maven

page three!

lol.

i think Kevin's is the best solution. mine works, but is ugly.

jephthah 1,888 Posting Maven


what was wrong with my previous suggestion?

/\d/ &&  /^[a-zA-Z][a-zA-Z0-9]{3,7}$/

um... i don't know?

it's clearly more elegant than mine.

and doesn't pedantically force him to accept my idea of underscores.

i think yours is the winner

jephthah 1,888 Posting Maven

okay, bradleykirby, i see the problem.

you did not put the carat ^ at the beginning, where it should be.

jephthah 1,888 Posting Maven

Your second "if" condition contains some errors. "=!" is not a valid perl operator and "!=" is the wrong operator, they should all be "!~".

oops, dangit. you're right.

i thought that looked odd for some reason, but it was late, and i was in a hurry to go to bed.

that's two, now.

:embarrass:

jephthah 1,888 Posting Maven

mine should not in any way accept either & or ? or any other non characters, other than the _ underscore and then only after the first position.

ive used RegexBuddy to validate. theres no way it can allow it. i dont know why you're seeing what you're seeing if it is somehow allowing it.

/^     // start
[a-z]  // single letter
(?:     // match any one of the following:
\d\w{2,6}|   //either
\w{2,6}\d|   //or
\w\d\w{1,5}|   //or
\w{2}\d\w{0,4}|   //or
\w{3}\d\w{0,3}|   //or
\w{4}\d\w{0,2}|   //or
\w{5}\d\w?   //or
)
$/i  //end, case insensitive

this does allow, as i mentioned, the "_"... this is typically standard, as it seems you are testing for a valid username and/or password.

if you do not wish to allow underscores, replace the \w with [a-zA-Z0-9]


FULL DISCLAIMER: this is ugly, I don't like it, and i'm sure there's a better way. but it does work, and as always, TMTOWTDI.

jephthah 1,888 Posting Maven

The one offered by jephthah allows non-letters at the start, so &? etc allowed.

what are you talking about? it does not in any sense allow non letters at the start, or anywhere else for that matter ... it will allow the underscore "_" in any position other than the first, which is typically allowed (and desired) anywhere alphanumerics are allowed.

and i'm afraid to tell you, your last attempt won't work at all, for anything. maybe mine ain't pretty, but it works.

Curious about the /x modifier tho, because I have another regex thats taking up about two page widths - is /x a way to split your code over 2 or more lines?

yeah, narue just busted me. it means "ignore whitespace" ... so you can put a CR/LF in there for readability.

jephthah 1,888 Posting Maven

as for checking whether or not it is a function... each subroutine in C starts with one or more return type:

int, long, short, double, float, void, char, unsigned ...

then it has a single word for the function name (simply: \w+ )

then it will have an open parenthesis '(' followed eventually by a closed parenthesis ')' ....

and then there will be the opening bracket '{'

thats all you have to parse on to find that it is a function... of course be sure to ignore function "prototypes"... they will be distinguished by a semicolon ';' after the closing parenthesis

the problem for you will be that a function declaration can spread across multiple lines... very often that first opening bracket is on a line by itself. like so:

void myExample_Function (int argument, char * value)
{
    blah;
    blah;
    blah;
}
jephthah 1,888 Posting Maven

easiest way is to

(1) do a dir (ls) listing of directory contents, parse each one for a .c extention. start with the first .c file...

(2) open the .c file for READ, to a file handle.

(3) open a new .c file to WRITE to a different file handle and make it's filename similar to the original one being read from in step 2 (like using same name, but adding a "-mod" after the filename)

(4) read each line of original file.

(5) search for each function, and keep strict count of number of opening brackets, and number of closing brackets ( '{' and '}' )

(6) write each of the the lines read from the original file to the new file, exactly as read.

(7) write an additional "extra line 1" only after the first opening bracket '{', and write teh additional "extra line 2" right before the last closing bracket '}'

(8) repeat steps 5 - 7 until all functions are completed and end of file is reached

(9) close both the read file and write file, and then move to the next .c file in your directory.

(10) repeat steps #2 - #10 for the next .c file, until all .c files are completed.


.

jephthah 1,888 Posting Maven

hmm.... my long regex above is cut off by the webcode formatting.

that's kind of crappy. and all that after everyone bitches about people not using the webcode tags :P

anyhow, you'll have to click the "toggle plain text" to see it.

jephthah 1,888 Posting Maven

how about you take a stab at it, and when it doesnt work you post what you did here, then we can look and see where the error(s) might be.

jephthah 1,888 Posting Maven

so you were able to solve your problem?

because, it makes sense that I would have permission denied trying to bind as a server to your IP (I dont even know the correct port number) .... but was that the same result for you?

the fact that i got a "permission denied" error, tells me that the bind was being performed correctly, just rejected, which I would expect from my end.

did it give you the same error, and were you able to resolve it?

jephthah 1,888 Posting Maven

the problem doesnt seem to be in your code. the problem seems to be that your ip address is refusing the bind.

at least that's the error i get. your's may be different, so try this code and see what error code is returned.

errno=0;
handle = bind(localsock,(struct sockaddr*)&local,sizeof(local));
printf( "bind -- Error #%3d: %s\n", errno, strerror( errno ) );
if (handle < 0) 
{
    fprintf(stderr,"bind command failed\n", errno);
    exit(1);
}

because your code, as written (with the assumption that you included all appropriate headers and gave a meaningful port number) ... the code works. i dont see anything wrong with it.

here was additional debug results from my run:

[jezebel@localhost cprogs]$ ./ipv6test

ipv6 = 2001:0067:0000:0000:0000:0000:0000:0002
inet_pton -- Error #  0: Success

localsock = 4
socket open -- Error #  0: Success

bind -- Error # 13: Permission denied
bind command failed

[jezebel@localhost cprogs]$
jephthah 1,888 Posting Maven

you could do something like

if (str =~ /^[a-z](?:\d\w{2,6}|\w{2,6}\d|\w\d\w{1,5}|\w{2}\d\w{0,4}|\w{3}\d\w{0,3}|\w{4}\d\w{0,2}|\w{5}\d\w?)$/i)
{
     ... stuff ...
}

but i would rather

if (str =~ /^[a-z](\w{3,7})/ )
{
    if ($1 =! /\d/ || $1 =! /[A-Z]/ || $1 != /[a-z]/)
    { 
        die "must have one number, cap, and lowercase!\n";
    }

    ... stuff ...
}
jephthah 1,888 Posting Maven

I would try to help you but your code is unreadable. You need to use the "code" tags and include your indentations as described in the posting rules.

also, you need to explain what you have already done as far as debugging.

like Kevin said, this is not a service for you to demand urgent or immediate help. help us help you and you might get somewhere

jephthah 1,888 Posting Maven

your code is not compiling without a lot of effort on my end, and hence i havent been able to put much time into this while at work.

is this for a server i assume? do you have any more code along with this?

jephthah 1,888 Posting Maven

your post is the surest way to not get any help here.

if you want help, here are the steps you must follow:

(1) write part of your program
(2) when it doesn't work, post your code and specific questions here

jephthah 1,888 Posting Maven

you can make it a function:

void strToUppercase(char * myString)
{
   while(*myString)
      *myString = toupper(*myString++)
}

or perhaps a more "easy to understand" bit of inline code:

char myString[128];

strcpy(myString,"the quick brown fox jumped over the lazy dogs");

for(a = 0; a<strlen(myString); a++)
{
   if (myString[a] >= 'a' && myString[a] <= 'z')
      myString[a] -= 0x20;
}

try to understand exactly what is being done in either example

jephthah 1,888 Posting Maven

never use conio.h for anything. that just breaks your program for anythign other than windows.

and never,.ever, ever use "GOTO" statements. and by that i mean never. they're absolutely horrid. use a while or for loop instead.

jephthah 1,888 Posting Maven

sorry for any misunderstanding... im not saying malloc should never be used. its a very powerful tool that has its place.

im saying it shouldnt be used by beginners when there are other options, because they come to rely on its use without understanding the dangers.

and experienced programmers often make too much use of it themselves. its (mis)use is the cause of some of the worst errors from a debugging standpoint.

Pointers and memory leaks in C

jephthah 1,888 Posting Maven

My guess is that you have not done much professional programming

then your guess would be wrong.

jephthah 1,888 Posting Maven

well... the first 5 lines are okay...

on line 5 you fget the first line of the file and put it into the string "line"... that's fine too

but then on line 7, you start checking the string "readline" where does this come from? you should be checking the string "line", i think...

so you want to check if the first character of the line is the character "0", is that right? then change "readline" on line 7 to "line"

then on line 8 you are checking the string "s" to see if it contains exactly the string "input"... is that right? where did "s" come from? i have to assume that you must have filled it with its value previously.

jephthah 1,888 Posting Maven

giving new programmers "malloc" is like giving them rope to hang themselves with or a gun to shoot themselves in the foot with.

even experienced programmers should avoid the use of malloc unless absolutely necessary. which it usually isn't.

declare the local "tmp" string in the subroutine as a char array of some length, and do a check for the strlen( ) of the input argument if you want a check.

better yet, don't have the subroutine even make a copy, just modify the string that is passed into the function in place. if you need to keep an unmodified copy of the original, have the caller (main) make and keep the copy.

jephthah 1,888 Posting Maven

Yes, that's exactly what i was trying to tell you in my first post.

fgets( ) returns the newline character, which is why you believed the the string was was always +1 from what you "thought" it should be.

because that's what fgets( ) does, according to the C programming language.

you need to change the newline into a NULL using the line of code i provided, or use strncmp( ) to ignore the newline.

otherwise your "fix" will never allow you to ever parse a string from stdin.

jephthah 1,888 Posting Maven

my suspicion is that you are losing track of how many times you have opened / closed the file pointer, and that you are trying to read a pointer that is not opened.or close a pointer that is not opened.

this would explain why you're getting a segmentation fault. this is a common error that happens when you start using globals inside subroutines, it's just generally bad practice for that reason.

of course i could be wrong .... buti don't have anything else to go on, since you've only given subroutine.

it definitely is very suspicious though. i would rewrite the program to not use global file pointers.

jephthah 1,888 Posting Maven

so you're using global file pointers and accessing them inside a subroutine?

that's a bad idea. if you open a file outside a subroutine and then need to access it inside a subroutine, you should pass the file pointer to the subroutine.

otherwise it's hard to tell whats going on because you've only showed your sub

jephthah 1,888 Posting Maven

^ that works better than my first attempt, and much more elegant than my second.

nice job

jephthah 1,888 Posting Maven

Hummmm. Seems your function needs a tad bit more work. But its close.

int main()
{
    char str[] = "now \tis     the   time  for   all    good    men\n";
    capitalizeFirsts(str);
    puts(str);
    return 0;
}

Results

Now     is     The   Time  for   All    good    men

Press any key to continue . . .

good catch :P

i only planned for single spaces between words. as it happens, the function handles odd number of spaces, but not even number.

change the function to this, and it should be nearly bulletproof:

void capitalizeAll(char *buf)
{
	int capNext = 0;

	*buf=toupper(*buf);
	while (*buf++ != '\0')
	{
		if (capNext)
		{
			*buf=toupper(*buf);
			capNext = 0;
		}
		if (*buf <= ' ')
			capNext=1;
	}
}
jephthah 1,888 Posting Maven

your program, as posted in #7, with the changes i call in #8, is working just fine for me.

other than the fact i had to comment out #include lab7.h ... im using exactly what you posted.

EDIT:

i see what you did. in your latest program you removed the lines:

firstUpper(prodscrip);
puts(prodscrip);

just curious, how much help did you have writing the rest of the program? did you write it yourself?

jephthah 1,888 Posting Maven

your "getstring" function is buggering things up. your have a redeclaration for prodscrip that is local to that function, so when it returns back to "main( )" there's nothing for it to use, and you overrun the buffer.

"fprint" and "gets" work nice enough, i dont see what the benefit is for a separate function anyhow. you should always avoid scanf() whenever possible.

from your main( ) routine, remove the line

getstring(prodscrip,"product description");

and replace it with

printf("Enter the product description : ");
gets(prodscrip);

that will work.

jephthah 1,888 Posting Maven

call it from anywhere you want to.

if you have any string, lets call it "myString", and it has a bunch of text in it, when you make this call

captializeFirsts(myString);

after the function comes back, all the first characters in each word within "myString" will be capitalized.

if there's only one word in "myString", only the first letter of that word will be capitalized. if theres a hundred word, each of the hundred words will be capitalized.

jephthah 1,888 Posting Maven

How does gcc order bitfields on a little-endian machine? Is the first entry the most significant bit or what?

endianness referrs to byte ordering, not bits. bit ordering is always the same. endianness describes the direction that bytes are stored in increasing memory locations. big-endian means "big end first". little-endian means "little end first"

but really, it all comes down to the fact that the Blefuscudians are stupid fools who ruin perfectly good soft-boiled eggs.

jephthah 1,888 Posting Maven

it appears you're trying to close a file pointer before it has been opened.

jephthah 1,888 Posting Maven

strtok will work to be sure, but you have to watch out that it will stomp all over your original string.

another way is to use pointers. make sure you can understand, and explain, how this works

void capitalizeFirsts(char *buf)
{
    *buf=toupper(*buf);            // first char CAP
    while (*buf++ != '\0')
        if (*buf == ' ')           // look for space
            *buf=toupper(*++buf);  // CAP next char
}
jephthah 1,888 Posting Maven

so, i guess that means the <sys/socket.h> library?

post the code you're using to bind and we can see whats going on.

jephthah 1,888 Posting Maven

this is a tough problem. i actually tried to do this last night, and i could not get it to work right.

getch() from the <curses.h> library, did not work as advertised for me.

and im using the gcc compiler.

obviously the getchar( ) function requires an <ENTER> before reading the input buffer, so that won't work either.

i know i did something like this maybe 5 years ago. perhaps I was using the <conio.h> windows library, which wouldn't be helpful for someone on *nix, using <curses.h>

i mean, not that this is really a representative "real world" problem, but it's a head scratcher.

jephthah 1,888 Posting Maven

which socket library are you using?

jephthah 1,888 Posting Maven

printf, as narue said, is able to format quite well.

sumValue = firstValue + secondValue;
printf("   0x%04X\n", firstValue);
printf("+  0x%04X\n", secondValue);
printf("  --------\n");
printf("   0x%04X\n", sumValue);

i think we can just this one away for free.