Progress bar test segfaults

This is a discussion on Progress bar test segfaults within the C Programming forums, part of the General Programming Boards category; Ok, so this is supposed to display a wget-style progress bar. It segfaults and I don't know where. Any help ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    52

    Progress bar test segfaults

    Ok, so this is supposed to display a wget-style progress bar. It segfaults and I don't know where. Any help finding it is appreciated. Thanks!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    inline int getpercent(int a, int b) {
      return (int)((a / b) * 100);
    }
    
    /*Dest should be the length of the progress bar*/
    /*Returns 0 on error, 1 on success*/
    int pbar(const int percent, char *dest) {
      int cur, size, tostop, destlen = strlen(dest);
      size = destlen - 3;
      dest[0] = '|';
      dest[destlen - 2] = '|';
      dest[destlen - 1] = '\0';
      tostop = ((int)((percent / size) * 100));
      
      for (cur = 1; cur <= size; cur++) {
        if (cur <= tostop) dest[cur] = '=';
        else dest[cur] = ' ';
      }
      return 1;
    }
    
    void progbar(const int amount) {
      int current, cols = atoi(getenv("COLUMNS"));
      char *bstr = malloc(cols + 1);
      for (current = 0; current < cols; current++) bstr[current] = ' ';
      bstr[cols] = '\0';
      for (current = 0; current < amount; current++) {
        printf("\r%s", pbar(getpercent(current, amount), bstr));
        usleep(500000);
      }
      free(bstr);
      return;
    }  
    
    int main(int argc, char **argv) {
      int total;
      scanf("%d", &total);
      progbar(total);
      return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Are you sure the environment variable COLUMNS is defined?

    EDIT: Also, pbar() returns an int, but you're trying to print its return value as a string. That could also cause a segfault.
    Last edited by itsme86; 06-30-2006 at 08:10 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    yeah, apparently that's it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	printf("%d\n", atoi(getenv("COLUMNS")));
    	return 0;
    }
    that segfaults.

    any idea how to do a wget-style progress bar then?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Set the columns in the environment?
    Code:
    itsme@itsme:~/C$ COLUMNS=80
    itsme@itsme:~/C$ set | grep COLUMNS
    COLUMNS=80
    itsme@itsme:~/C$
    But you're also going to have to the fix the printf() where you call pbar(). You can't print an int as a string.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Another option would be to check if COLUMNS exists and if it doesn't, choose a default value. Something like:
    Code:
    {
      char *colstr = getenv("COLUMNS");
    
      if(colstr)
        cols = atoi(colstr);
      else
        cols = 40;
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    oops yeah lol pbar originally returned a char*
    still.... how do i manage to do it without setting them in advance?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > how do i manage to do it without setting them in advance?
    Probably with great difficulty.
    I mean, what it stdout is redirected to another file (or another program). There is no longer the concept of a terminal with a width.

    If there is no COLUMNS, then perhaps look to see if there is a TERM and use that to look up information in the termcap database.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    yeah that's what i tried to do the other day (using termcap)
    i'm just using this in case my program is run with -v... thought progress bars would be cool :-D

    anybody know a really good termcap tutorial? all I need is to get term width.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating a progress bar
    By NewGuy100 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2006, 12:00 PM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. Is comctl32.lib required for progress bar controls?
    By JasonD in forum Windows Programming
    Replies: 4
    Last Post: 08-28-2004, 02:30 PM
  5. progress bar newbie
    By WaterNut in forum Windows Programming
    Replies: 18
    Last Post: 08-09-2004, 01:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21