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 Reindent according to cmucl style
- - - - - 08b38701 by Raymond Toy at 2022-09-05T14:50:40-07:00 Apply suggested change to test for zero length.
- - - - - 3a41b05c by Raymond Toy at 2022-09-05T14:54:16-07:00 Add a few comments
- - - - - c4a9d0d8 by Raymond Toy at 2022-09-05T15:16:43-07:00 Add assert.h and replace ARRAYSIZE with sizeof
We need to include assert.h to define the `assert` macro that we use.
`ARRAYSIZE` needs to be replaced with `sizeof` to compute the size of the `initial` buffer.
- - - - -
2 changed files:
- src/code/filesys.lisp - src/lisp/os-common.c
Changes:
===================================== src/code/filesys.lisp ===================================== @@ -1078,7 +1078,7 @@ optionally keeping some of the most recent old versions." ;; unix-namestring converts "." to "". Convert it back to ;; "." so we can stat the current directory. (Perhaps ;; that's a bug in unix-namestring?) - (when (string= name "") + (when (zerop (length name)) (setf name ".")) (let (author) (unwind-protect
===================================== src/lisp/os-common.c ===================================== @@ -5,6 +5,7 @@
*/
+#include <assert.h> #include <errno.h> #include <math.h> #include <netdb.h> @@ -738,31 +739,43 @@ os_file_author(const char *path) if (stat(path, &sb) != 0) { return NULL; } + result = NULL; buffer = initial; - size = ARRAYSIZE(initial); - assert(sysconf(_SC_GETPW_R_SIZE_MAX) <= 16384)); + size = sizeof(initial) / sizeof(initial[0]); + + /* + * Assume a buffer of size 16384 is enough to for getpwuid_r to do + * it's thing. + */ + assert(sysconf(_SC_GETPW_R_SIZE_MAX) <= 16384); + + /* + * Keep trying with larger buffers until a maximum is reached. + */ while (size <= 16384) { switch (getpwuid_r(sb.st_uid, &pwd, buffer, size, &ppwd)) { - case 0: - /* Success, though we might not have a matching entry */ - result = (ppwd == NULL) ? NULL : strdup(pwd.pw_name); - goto exit; - case ERANGE: - /* Buffer is too small, double its size and try again */ - size *= 2; - obuffer = (buffer == initial) ? NULL : buffer; - if ((buffer = realloc(obuffer, size)) == NULL) { - free(obuffer); - goto exit; - } - continue; - default: - /* All other errors */ - goto exit; + case 0: + /* Success, though we might not have a matching entry */ + result = (ppwd == NULL) ? NULL : strdup(pwd.pw_name); + goto exit; + case ERANGE: + /* Buffer is too small, double its size and try again */ + size *= 2; + obuffer = (buffer == initial) ? NULL : buffer; + if ((buffer = realloc(obuffer, size)) == NULL) { + free(obuffer); + goto exit; + } + continue; + default: + /* All other errors */ + goto exit; } } exit: + fprintf(stderr, "buffer, initial = %p %p\n", buffer, initial); + free(buffer); return result; }
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/518ef95922f48ff2e290878...