Raymond Toy pushed to branch issue-130-file-author-in-c at cmucl / cmucl
Commits:
- 
6557e832
by Raymond Toy at 2022-09-05T14:45:35-07:00
- 
08b38701
by Raymond Toy at 2022-09-05T14:50:40-07:00
- 
3a41b05c
by Raymond Toy at 2022-09-05T14:54:16-07:00
- 
c4a9d0d8
by Raymond Toy at 2022-09-05T15:16:43-07:00
2 changed files:
Changes:
| ... | ... | @@ -1078,7 +1078,7 @@ optionally keeping some of the most recent old versions." | 
| 1078 | 1078 |  	;; unix-namestring converts "." to "".  Convert it back to
 | 
| 1079 | 1079 |  	;; "." so we can stat the current directory.  (Perhaps
 | 
| 1080 | 1080 |  	;; that's a bug in unix-namestring?)
 | 
| 1081 | -	(when (string= name "")
 | |
| 1081 | +	(when (zerop (length name))
 | |
| 1082 | 1082 |  	  (setf name "."))
 | 
| 1083 | 1083 |  	(let (author)
 | 
| 1084 | 1084 |  	  (unwind-protect
 | 
| ... | ... | @@ -5,6 +5,7 @@ | 
| 5 | 5 | |
| 6 | 6 |  */
 | 
| 7 | 7 | |
| 8 | +#include <assert.h>
 | |
| 8 | 9 |  #include <errno.h>
 | 
| 9 | 10 |  #include <math.h>
 | 
| 10 | 11 |  #include <netdb.h>
 | 
| ... | ... | @@ -738,31 +739,43 @@ os_file_author(const char *path) | 
| 738 | 739 |      if (stat(path, &sb) != 0) {
 | 
| 739 | 740 |          return NULL;
 | 
| 740 | 741 |      }
 | 
| 742 | + | |
| 741 | 743 |      result = NULL;
 | 
| 742 | 744 |      buffer = initial;
 | 
| 743 | -    size = ARRAYSIZE(initial);
 | |
| 744 | -    assert(sysconf(_SC_GETPW_R_SIZE_MAX) <= 16384));
 | |
| 745 | +    size = sizeof(initial) / sizeof(initial[0]);
 | |
| 746 | + | |
| 747 | +    /*
 | |
| 748 | +     * Assume a buffer of size 16384 is enough to for getpwuid_r to do
 | |
| 749 | +     * it's thing.
 | |
| 750 | +     */
 | |
| 751 | +    assert(sysconf(_SC_GETPW_R_SIZE_MAX) <= 16384);
 | |
| 752 | + | |
| 753 | +    /*
 | |
| 754 | +     * Keep trying with larger buffers until a maximum is reached.
 | |
| 755 | +     */
 | |
| 745 | 756 |      while (size <= 16384) {
 | 
| 746 | 757 |          switch (getpwuid_r(sb.st_uid, &pwd, buffer, size, &ppwd)) {
 | 
| 747 | -        case 0:
 | |
| 748 | -            /* Success, though we might not have a matching entry */
 | |
| 749 | -            result = (ppwd == NULL) ? NULL : strdup(pwd.pw_name);
 | |
| 750 | -            goto exit;
 | |
| 751 | -        case ERANGE:
 | |
| 752 | -            /* Buffer is too small, double its size and try again */
 | |
| 753 | -            size *= 2;
 | |
| 754 | -            obuffer = (buffer == initial) ? NULL : buffer;
 | |
| 755 | -            if ((buffer = realloc(obuffer, size)) == NULL) {
 | |
| 756 | -                free(obuffer); 
 | |
| 757 | -                goto exit;
 | |
| 758 | -            }
 | |
| 759 | -            continue;
 | |
| 760 | -        default:
 | |
| 761 | -            /* All other errors */
 | |
| 762 | -            goto exit;
 | |
| 758 | +          case 0:
 | |
| 759 | +              /* Success, though we might not have a matching entry */
 | |
| 760 | +              result = (ppwd == NULL) ? NULL : strdup(pwd.pw_name);
 | |
| 761 | +              goto exit;
 | |
| 762 | +          case ERANGE:
 | |
| 763 | +              /* Buffer is too small, double its size and try again */
 | |
| 764 | +              size *= 2;
 | |
| 765 | +              obuffer = (buffer == initial) ? NULL : buffer;
 | |
| 766 | +              if ((buffer = realloc(obuffer, size)) == NULL) {
 | |
| 767 | +                  free(obuffer); 
 | |
| 768 | +                  goto exit;
 | |
| 769 | +              }
 | |
| 770 | +              continue;
 | |
| 771 | +          default:
 | |
| 772 | +              /* All other errors */
 | |
| 773 | +              goto exit;
 | |
| 763 | 774 |          }
 | 
| 764 | 775 |      }
 | 
| 765 | 776 |  exit:
 | 
| 777 | +    fprintf(stderr, "buffer, initial = %p %p\n", buffer, initial);
 | |
| 778 | +    
 | |
| 766 | 779 |      free(buffer);
 | 
| 767 | 780 |      return result;
 | 
| 768 | 781 |  } |