Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Is this C any good?

  1. #11
    Join Date
    May 2005
    Beans
    7

    Re: Is this C any good?

    How very hard it is to tell the difference between someone honestly asking for help, and someone trying to get their homework done for them....

    I'll merely suggest a C group for C questions, find lots of well written C and read it, and turn on all the warnings that your compiler has, read the warnings, understand the warnings, and correct the warnings if warranted.

  2. #12
    Join Date
    Jun 2008
    Beans
    1

    Re: Is this C any good?

    you should malloc an extra byte and set the last byte of the buffer to 0 to null terminate the string.

  3. #13
    Join Date
    Apr 2008
    Beans
    2
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Is this C any good?

    The code looks good, but one thing to remember is that in C, character strings are NULL terminated (\0). The printf ("%s\n", buffer) statement expects a NULL terminator in buffer to delimit the end of the character string (i.e. password).

    I do not see that you are setting a NULL terminator after the password characters are generated (to indicate the end of the string), so a core dump could occur of buffer does not happen to be initialized to zeros. One approach you could take is to call calloc instead of malloc to initialize the buffer to zero before using it.

    Also, you will need to allocate "buffer" to be one char larger than your password size (bytes) to accommodate the null terminator at the end of the string.

  4. #14
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Is this C any good?

    Quote Originally Posted by Jessehk View Post
    Don't assume that the user will enter correct input. Rather than use scanf(), read in a line with fgets() and convert it to an integer with sscanf(). Check return values for input or format errors. See manpages for details.
    Or even better, take them in as command-line arguments.


    @bobbocanfly
    You need to seed the random number generator with srand(). A common way is to use time() (in <time.h>)
    Code:
        srand( time(NULL) );
    And also, the man-page for rand(3) states:
    Code:
              "If  you  want  to  generate a random integer between 1 and 10, you
               should always do it by using high-order bits, as in
    
                   j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));
    
               and never by anything resembling
    
                   j = 1 + (rand() % 10);
    
               (which uses lower-order bits)."

  5. #15
    Join Date
    Feb 2007
    Location
    Edinburgh, Scotland
    Beans
    391

    Re: Is this C any good?

    Thanks for your feedback everyone. Was laughing at the person who thought this was homework help, I only with i was learning C at school. Currently we are 'learning' how to make websites in HTML, complete with <font> tag.

    Also, to the person who suggested Python, thanks but as you said this is just a practise in learning C. I already have a Python password generator script (which interestingly enough ran just 0.03 seconds slower than my C code when generating 100, 1000 char passwords).

    Anyway here is a 'fixed' version that should address some of the problems you guys raised:

    Code:
    /* passgen.c: Generate pseudo-random passwords 
       Copyright (C) David Futcher (bobbo) 2008
       <bobbocanfly@gmail.com> - rev 2
    
       Release under the GNU GPL License v2 or above */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char** argv)
    {
        int bytes, pass, n, i;
        char * buffer;
    
        /* Get user args */
        if (argc >= 3)
        {
            pass = atoi(argv[1]);
            bytes = atoi(argv[2]);
    
            /* Detects incorrect or stupid input */
            if (pass < 1) 
            {
                printf("%s <no. passwords to generate> <length of passwords>\n", argv[0]);
                return 1;
            }
            else if (bytes < 1)
            {
                printf("%s <no. passwords to generate> <length of passwords>\n", argv[0]);
                return 1;
            }
        }
        else
        {
            printf("%s <no. passwords to generate> <length of passwords>\n", argv[0]);
            return 1;
        }
    
        /* Set up dynamic memory + rand() seed */
        buffer = malloc(bytes+1);
    
        if (buffer == NULL)
        {
            printf("Could not allocate memory...exiting\n");
            return 1;
        }
    
        srand(time(NULL));
    
        /* Actual generation loop */
        for (i=0; i<pass; i++)
        {
            for (n=0; n<bytes; n++)
            {
                buffer[n] = 'a' + (31.0 * (rand() / (RAND_MAX + 1.0)));
            }
            
            buffer[bytes] = '\0';
        
            printf ("%s\n", buffer);
        }
    
        /* It worked! Get ready to exit */
        free(buffer);
        return 0;
    
    }
    Today we have 15 minutes of fame, tomorrow 15 minutes of anonymity.
    My Blog | Veza - Opensource TinyURL clone with sensible URLs

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •