Merged in default ENGINE-434
authorKrista Bennett <krista@pep-project.org>
Thu, 24 May 2018 11:12:10 +0200
branchENGINE-434
changeset 2695187afdcf3e51
parent 2691 6e7b73c67701
parent 2694 dbbf5b09f2ea
child 2696 54689953f3bd
Merged in default
src/pgp_gpg.c
     1.1 --- a/build-mac/pEpEngine.xcodeproj/project.pbxproj	Thu May 24 10:50:10 2018 +0200
     1.2 +++ b/build-mac/pEpEngine.xcodeproj/project.pbxproj	Thu May 24 11:12:10 2018 +0200
     1.3 @@ -977,7 +977,7 @@
     1.4  				ONLY_ACTIVE_ARCH = YES;
     1.5  				OTHER_CFLAGS = "-DSQLITE3_FROM_OS";
     1.6  				SDKROOT = iphoneos;
     1.7 -				VALID_ARCHS = arm64;
     1.8 +				VALID_ARCHS = "arm64 armv7s armv7";
     1.9  			};
    1.10  			name = Debug;
    1.11  		};
    1.12 @@ -1033,7 +1033,7 @@
    1.13  				OTHER_CFLAGS = "-DSQLITE3_FROM_OS";
    1.14  				SDKROOT = iphoneos;
    1.15  				VALIDATE_PRODUCT = YES;
    1.16 -				VALID_ARCHS = arm64;
    1.17 +				VALID_ARCHS = "arm64 armv7s armv7";
    1.18  			};
    1.19  			name = Release;
    1.20  		};
     2.1 --- a/src/pgp_gpg.c	Thu May 24 10:50:10 2018 +0200
     2.2 +++ b/src/pgp_gpg.c	Thu May 24 11:12:10 2018 +0200
     2.3 @@ -22,6 +22,17 @@
     2.4      stringlist_t *_v;
     2.5      unsigned int i;
     2.6      unsigned int found = 0;
     2.7 +    bool eof_nl = 0;
     2.8 +    char * rest;
     2.9 +    char * token;
    2.10 +    char * s;
    2.11 +    const char* line_end;
    2.12 +
    2.13 +#ifdef WIN32
    2.14 +    line_end = "\r\n";
    2.15 +#else
    2.16 +    line_end = "\n";
    2.17 +#endif    
    2.18  
    2.19      FILE *f = Fopen(config_file_path, "r");
    2.20      if (f == NULL && errno == ENOMEM)
    2.21 @@ -29,7 +40,6 @@
    2.22  
    2.23      if (f != NULL) {
    2.24          int length = stringlist_length(keys);
    2.25 -        unsigned int n = (1 << length) - 1;
    2.26  
    2.27          // make sure we 1) have the same number of keys and values
    2.28          // and 2) we don't have more key/value pairs than
    2.29 @@ -44,31 +54,26 @@
    2.30              return false;
    2.31          }
    2.32  
    2.33 -        do {
    2.34 -            char * s = Fgets(buf, MAX_LINELENGTH, f);
    2.35 -            if (!feof(f)) {
    2.36 -                assert(s);
    2.37 -                if (s == NULL)
    2.38 -                    return false;
    2.39 -
    2.40 -                if (s && !feof(f)) {
    2.41 -                    char * rest;
    2.42 -                    char * t = strtok_r(s, " ", &rest);
    2.43 -                    for (i = 1, _k = keys, _v = values; _k != NULL;
    2.44 -                            _k = _k->next, _v = _v->next, i <<= 1) {
    2.45 -                        if (t && strncmp(t, _k->value, strlen(_k->value)) == 0)
    2.46 -                            found |= i;
    2.47 -
    2.48 -                        if (i == n) {
    2.49 -                            r = Fclose(f);
    2.50 -                            if (r != 0)
    2.51 -                                return false;
    2.52 -                            return true;
    2.53 -                        }
    2.54 -                    }
    2.55 +        while ((s = Fgets(buf, MAX_LINELENGTH, f))) {
    2.56 +            token = strtok_r(s, " \t\r\n", &rest);
    2.57 +            for (_k = keys, _v = values, i = 1;
    2.58 +                 _k != NULL;
    2.59 +                 _k = _k->next, _v = _v->next, i <<= 1) {
    2.60 +
    2.61 +                if (((found & i) != i) && token &&
    2.62 +                    (strncmp(token, _k->value, strlen(_k->value)) == 0)) {
    2.63 +                    found |= i;
    2.64 +                    break;
    2.65                  }
    2.66              }
    2.67 -        } while (!feof(f));
    2.68 +            if (feof(f)) {
    2.69 +                eof_nl = 1;
    2.70 +                break;
    2.71 +            }
    2.72 +        }
    2.73 +
    2.74 +        if (!s && ferror(f))
    2.75 +            return false;
    2.76          f = Freopen(config_file_path, "a", f);
    2.77      }
    2.78      else {
    2.79 @@ -78,13 +83,16 @@
    2.80      assert(f);
    2.81      if (f == NULL)
    2.82          return false;
    2.83 +    
    2.84 +    if (eof_nl)
    2.85 +        r = Fprintf(f, line_end);
    2.86  
    2.87      for (i = 1, _k = keys, _v = values; _k != NULL; _k = _k->next,
    2.88              _v = _v->next, i <<= 1) {
    2.89          if ((found & i) == 0) {
    2.90 -            r = Fprintf(f, "%s %s\n", _k->value, _v->value);
    2.91 +            r = Fprintf(f, "%s %s%s", _k->value, _v->value, line_end);
    2.92              assert(r >= 0);
    2.93 -            if(r<0)
    2.94 +            if (r < 0)
    2.95                  return false;
    2.96          }
    2.97      }