Thread: Function not reusable?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Function not reusable?

    Im attempting to figure out what a packages name is based on its file name(package-version-extension like format).

    I made a function to gets a package's version, i figure ill just use that to find its version and copy up to that point.

    Im getting a segfaul when i call version from name.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int getpackagename(char *package, char **dest);
    int getpackageversion(char *package, char **dest);
    
    int getpackageversion(char *package, char **dest) {
        int length = ( strlen(package) - 1 );
        int start = 0;
        int end = 0;
        
        while(length > 0) {
            if( package[length] > 47 && package[length] < 58 ) {
                end = length;
                
                while(package[length] != '-' && length > 0) {
                    --length;
                }
                
                if(package[length+1] == 'r' && package[length+2] == 'c') {
                    --length;
                    
                    while(package[length] != '-' && length > 0) {
                        --length;
                    }
                    
                    start = (length + 1);
                } else {
                    start = (length + 1);
                }
                
                break;
            }
        
            --length;
        }
        
        length = 0;
        
        *dest = malloc( end - start + 2 );
        
        while(start < end) {
            (*dest)[length] = package[start];
        
            ++length;
            ++start;
        }
        
        *dest[length] = '\0';
    
        return 0;
    }
    
    int getpackagename(char *package, char **dest) {
    	char *version = NULL;
    	char *nameend = NULL;
    	
    	getpackageversion(package, &version);
    	nameend = strstr(package, version);
        *dest = malloc( strlen(package) - strlen(nameend) + 1 );
        strncpy(*dest, package, (strlen(package) - strlen(nameend));
    		
    	return 0;
    }
    
    int main() {
        char package[] = "no1willno-4.5.1.24_rc4.tar";
        char *temp = NULL;
        
        getpackagename(package, &temp);
        
        printf("%s\n", temp);
        
        free(temp);
    
        return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >*dest[length] = '\0';
    Should be
    Code:
    (*dest)[length] = '\0';
    My best code is written with the delete key.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [nitpick]
    Code:
    if( package[length] > 47 && package[length] < 58 ) {
    Why not use a clearer, potentially more optimized, and certainly more portable method (assuming of course this code intends to check whether a character represents a[n ASCII] digit)?
    Code:
    if( isdigit(package[length]) ) {
    [/nitpick]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Thats what I shoudl do Dave_Sinkula, I just wrote the quickly. Ill change it someday....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM