Thread: why is strcmp not returning 0 despite matching strings

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    why is strcmp not returning 0 despite matching strings

    Given the following code

    Code:
        #include <stdlib.h>
        #include <stdio.h>
        #include <string.h>
        #include <strings.h>
        
        int main(void)
        {
        char buf[1024];
            char **arr1 = NULL;
            char **arr2 = NULL;
            char **arr3 = NULL;
            char **arr4 = NULL;
        
            int size1 = 0;
            int size2 = 0;
            int size3 = 0;
            int size4 = 0;
        
            FILE * f1, *f2;
            f1 = fopen("shadow.txt", "r");
            f2 = fopen("mytab24112.txt", "r");
        
            // Allocate memory for shadow.txt
            while(fgets(buf, 1024, f1))
            {
                size1++;
                arr1 = realloc(arr1, sizeof(char*) * size1);
                arr1[size1 - 1] = strdup(buf);
            }
        
            // Allocate memory for mytab2411.txt
            while(fgets(buf, 1024, f2))
            {
                size2++;
                arr2 = realloc(arr2, sizeof(char*) * size2);
                arr2[size2 - 1] = strdup(buf);
            }
        
                size3++;
                arr3 = realloc(arr3, sizeof(char*) * size3);
                arr3[size3 - 1] = strdup(buf);
        
                size4++;
                arr4 = realloc(arr4, sizeof(char*) * size4);
                arr4[size4 - 1] = strdup(buf);
        
            char line[1000]; //Allocate max number of characters in a line
        
            char hash[1000]; // Allocate max number of characters in a hash (substring of a line) for shadow.txt
            char * hash2; // Define hash for mytab24112.txt
        
            char md5[5]= "$1$$"; // Define string to be searched for md5 in shadow.txt
            char sha512[5]="$6$$"; // Define string to be searched for sha512 in shadow.txt
            char *ret; // Used for shadow.txt
            char *ret2; // Used for shadow.txt
        
            char del='$'; // Delimiter for displaying all hashes in mytab24112.txt
            char * del2; // Delimiter for displaying password text in mytab24112.txt
            char word[sizeof(line)];
        
            // Read shadow.txt line by line
            for(int i = 0; i < size1; i++) {
    
    
            memset(hash, '\0', sizeof(hash));
            strcpy(line, arr1[i]);
        
            // Search for md5 in shadow.txt
              md5[4]='\0';
              ret = strstr(line, md5);
        
            // Search for sha512 in shadow.txt
              sha512[4]='\0';
              ret2 = strstr(line, sha512);
        
            // Copies md5 hash to the variable hash if md5 is detected in shadow.txt
            if (ret){
               strncpy(hash, line+5, 26);
               hash[26] = '\0';
       
            }
            // Copies sha512 hash to the variable hash if sha512 is detected in shadow.txt
            else if (ret2){
        
              strncpy(hash, line+5, 90);
               hash[90] = '\0';
               
            }
      
         // Read mytab2411.txt line by line
            for(int j = 0; j < size2; j++){
        
            strcpy(line, arr2[j]);
           
            // Returns all hashes from mytab24112.txt using delimiter '$'
            hash2=strchr(line,del);
    
    
         for(int a = 0; a < size3; a++)
               for(int b = 0; b < size4; b++){
        
               strcpy(arr3[a],hash); 
               strcpy(arr4[b],hash2);
              
              printf("Hash in shadow:%s\n",arr3[a]);  
              printf("Hash in mytab24112:%s\n",arr4[b]);
              
              //Compare hash (arr3[a]) and hash 2 (arr4[b])
              int result = strcmp(arr3[a],arr4[b]);                   
                printf("%d\n",result);
    
    
        } //End of "for(int a = 0; a < size3; a++)" and "for(int b = 0; b < size4; b++)" loop
    
    
             }// End of "for(int j = 0; j < size2; j++)" loop  
      
          }// End of "for (int i = 0; i < size1; i++)" loop
       
            return 0;
    
    
        }//End of main
    and the following files


    shadow.txt

    Code:
        
    pyc1:$1$$Tnq7a6/C1wwyKyt0V/.BP/:17482:0:99999:7:::
        pyc2:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1:17482:0:99999:7:::
        
    pyc3:$1$$zZQKNjRd94GHyYOwXuStf0:17482:0:99999:7:::
        pyc4:$6$iKYSRG68$STdY8TCgoCaNfSUcyCwSBlVekdjs0P3qXtwxSbgpQpMUnHJRRSHOT5amoR24IqZBTPNWuIfO.uhZEnGLuE4q/.:17482:0:99999:7:::
        pyc5:$6$$FDqvMBbQOCyKP9uBL8E6TAEupCh72v.3/ow4fZ5HpZ/0NS7LBifFS9nJdzc/u2OEhUnRF9yC4Lw23hHjD1EmD.:17482:0:99999:7:::
        pyc6:$6$$LfCuhKecDtIfX77LOTWD1PjhF1IC0hBzjxckEthmoT8mVbxKH3qJzFgEi/P9GN1mptR4WPiwuh69X/41M6pHW1:17482:0:99999:7:::
    mytab24112.txt

    Code:
    apple:$1$$Tnq7a6/C1wwyKyt0V/.BP/    
    orange:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1  
    banana:$1$$zZQKNjRd94GHyYOwXuStf0   
    a:$1$$Ij31LCAysPM23KuPlm1wA/    
    a:$6$$ek/ucQg0IM8SQLyD2D66mpoW0vAF26eA0/pqoN95V.F0nZh1IFuENNo0OikacRkDBk5frNqziMYMdVVrQ0o.51 
    aah:$1$$bh5o1cAKfH43fc/B1.AjF0
    aah:$6$$jdxDww2LkNSlQPmWe5iDRAoBBT5IXa9241nN2bcm2Aukdrr4iH27Y6dj801MjLFaDQxbDBNxY4jvlXdIemiCY/
    aahed:$1$$zA2Ef92JR9W7kqAf8Km5B0
    aahed:$6$$mYQU7f3NL9Dysccvwv2BAyiCb/gxh9lN1gGnC9sa2uwHGX9VVb7jp3b7u/EZbdObqWrRFgiOLcRn7PEW1cOcz.
    why is it that "strcmp(arr3[a],arr4[b])" returns "-10" instead of "0" despite matching strings (First 3 lines from shadow.txt and mytab24112.txt should match)? How should I solve this problem?

    A few important lines from the output

    Code:
        
    Hash in shadow:$1$$Tnq7a6/C1wwyKyt0V/.BP/
    Hash in mytab24112:$1$$Tnq7a6/C1wwyKyt0V/.BP/
        
    -10
        
    Hash in shadow:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1
    Hash in mytab24112:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1
        
    -10
        
    Hash in shadow:$1$$zZQKNjRd94GHyYOwXuStf0
    Hash in mytab24112:$1$$zZQKNjRd94GHyYOwXuStf0
        
    -10
    Last edited by wei; 01-03-2018 at 01:27 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    ASCII 10 (the difference is -10) is LF so perhaps there is a LF character at the end of one of the strings and not the other

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp is always returning value == 0 :( help me (CLASSES & OBJECTS)
    By fredsilvester93 in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2012, 01:50 PM
  2. strcmp() not returning 0 when its suppose to
    By TaiL in forum C Programming
    Replies: 22
    Last Post: 12-11-2009, 08:32 PM
  3. pattern matching in strings
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 06:30 AM
  4. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  5. matching strings at end
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:27 AM

Tags for this Thread