Thread: Garbage data

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

    Garbage data

    Im getting some garbage data after 2 runs, i have no idea why. It always seems to throw in a extra '0'.

    Code:
    int getpackagename(char *package, char **dest) {
    	int end = ( strlen(package) - 1 );
    	
        while(end > 0) {	
            if( package[end] > 47 && package[end] < 58 ) {
                while(package[end] != '-' && end > 0) {
                    --end;
                }
                
                if(package[end+1] == 'r' && package[end+2] == 'c') {
                    --end;
                    
                    while(package[end] != '-' && end > 0) {
                        --end;
                    }
                }
                
                break;
            }
            
            --end;
        }
        
        *dest = malloc(end + 1);
        strncpy(*dest, package, end);
    		
    	return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Without the context in which you are using the function, it is a bit difficult to debug.

    Sorry if I'm bypassing the question a little, but assuming you're still doing this, would it be easier to make sscanf do the parsing? (The fields you are after may be different from my guesses; this is just an example.)
    Code:
    #include <stdio.h>
    
    struct sPackage
    {
       char name[32];
       int  version[4];
       int  rc;
       char ext[4];
    };
    
    struct sPackage *package_parse(struct sPackage *parsed, const char *package)
    {
       char format[64];
       sprintf(format, "%%%d[^-]-%%d.%%d.%%d.%%d_rc%%d.%%%ds",
               (int)(sizeof(parsed->name) - 1), (int)(sizeof(parsed->ext) - 1));
    
       if ( sscanf(package, format,
                   parsed->name,
                   &parsed->version[0],
                   &parsed->version[1],
                   &parsed->version[2],
                   &parsed->version[3],
                   &parsed->rc,
                   parsed->ext) == 7 )
       {
          return parsed;
       }
       return NULL;
    }
    
    void package_print(struct sPackage *parsed)
    {
       printf("name = %s\n", parsed->name);
       printf("version: release = %d, ", parsed->version[0]);
       printf("major = %d, ", parsed->version[1]);
       printf("minor = %d, ", parsed->version[2]);
       printf("build = %d\n", parsed->version[3]);
       printf("rc   = %d\n", parsed->rc);
       printf("ext  = %s\n", parsed->ext);
    }
    
    int main(void)
    {
       const char package[] = "no1willno-4.5.1.24_rc4.tar";
       struct sPackage parsed;
       if ( package_parse(&parsed, package) != NULL )
       {
          package_print(&parsed);
       }
       return 0;
    }
    
    /* my output
    name = no1willno
    version: release = 4, major = 5, minor = 1, build = 24
    rc   = 4
    ext  = tar
    */
    Or are you going through the exercise of parsing out the 'package' string?
    Or are 'package' strings not using a set format?

    I'm just presenting a possible alternative.
    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.*

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Or are you going through the exercise of parsing out the 'package' string?
    Yup.

    Im trying to take something like "test-1.0.0" and get back "test". Im not sure where my problem is. By itself it seems to work fine.

    I was trying to keep the code short, looks like ill have to post alot. Sorry about that.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include "package/package.h"
    
    int isdir(struct dirent **check_dirent) {
        struct stat     check_stat;
        
        if( stat((*check_dirent)->d_name, &check_stat) == 0 ) {
            if( S_ISDIR(check_stat.st_mode) != 0 ) {
                if( strcmp( (*check_dirent)->d_name , "." ) != 0 ) {
                    if( strcmp( (*check_dirent)->d_name , ".." ) != 0 ) {
                        return 0;
                    }
                }
            }
        }
        
        return 1;
    }
    
    int makepackagedatabase(char *folder, char *name) {
        DIR             *master = opendir(folder);
        struct  dirent  *master_dirent;
        DIR             *crawl;
        struct  dirent  *crawl_dirent;
        
        char *open = NULL;
        FILE *database = fopen(name, "w");
        
        char *package = NULL;
        char *version = NULL;
        
        if(master != NULL) {
            while( (master_dirent = readdir(master)) != 0 ) {
                if( isdir(&master_dirent) == 0 ) {
                    open = malloc( strlen(folder) + strlen(master_dirent->d_name) + 3 );
                    sprintf(open, "%s\\%s", folder, master_dirent->d_name);
                    chdir(open);
                    
                    crawl = opendir(open);
                    
                    while( (crawl_dirent = readdir(crawl)) != 0 ) {
                        if( isdir(&crawl_dirent) == 0 ) {                        
                            getpackagename(crawl_dirent->d_name, &package);
                            getpackageversion(crawl_dirent->d_name, &version);
                            
                            printf("%s\t%s\n", package, version);
                            fprintf(database, "%s\t%s\n", package, version);
                            
                            free(package);
                            free(version);
                        }
                    }
                    
                    closedir(crawl);
                    chdir("..");
                }
            }
            
            closedir(master);
            fclose(database);
        } else {
            perror("Error: ");
        }
    
        return 0;
    }
    
    int main() {
        makepackagedatabase("I:\\packager", "I:\\packager\\database.txt");
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM