[Git][cmucl/cmucl][issue-130-file-author-in-c] os_file_author returns a new string for the author

Raymond Toy (@rtoy) gitlab at common-lisp.net
Mon Aug 29 14:52:58 UTC 2022



Raymond Toy pushed to branch issue-130-file-author-in-c at cmucl / cmucl


Commits:
82e485f4 by Raymond Toy at 2022-08-29T07:52:27-07:00
os_file_author returns a new string for the author

Update file-author to call os_file_author correctly.

- - - - -


2 changed files:

- src/code/filesys.lisp
- src/lisp/os-common.c


Changes:

=====================================
src/code/filesys.lisp
=====================================
@@ -1098,16 +1098,22 @@ optionally keeping some of the most recent old versions."
 		 :pathname file
 		 :format-control (intl:gettext "~S doesn't exist.")
 		 :format-arguments (list file)))
-	(alien:with-alien ((author (array c-call:char 1024)))
-	  (unix::syscall* ("os_file_author" c-call:c-string
-					    (alien:array c-call:char 1024)
-					    c-call:int)
-			  ;; Return
-			  (alien:cast author c-call:c-string)
-			  ;; Args
-			  (unix::%name->file name)
-			  author
-			  1024)))))
+	;; 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 "")
+	  (setf name "."))
+	(let (author)
+	  (unwind-protect
+	       (progn
+		 (setf author (alien:alien-funcall
+			       (alien:extern-alien "os_file_author"
+						   (function (alien:* c-call:c-string) c-call:c-string))
+			       (unix::%name->file name)))
+		 (unless (zerop (sap-int (alien:alien-sap author)))
+		   (alien:cast author c-call:c-string)))
+	    (when author
+	      (alien:free-alien author)))))))
 
 
 ;;;; DIRECTORY.


=====================================
src/lisp/os-common.c
=====================================
@@ -717,36 +717,35 @@ os_lstat(const char* path, u_int64_t *dev, u_int64_t *ino, unsigned int *mode, u
     return rc;
 }
 
-int
-os_file_author(const char *path, char* author, int len)
+/*
+ * Interface for file-author.  Given a pathname, returns a new string
+ * holding the author of the file or NULL if some error occurred.  The
+ * caller is responsible for freeing the memory used by the string.
+ */
+char *
+os_file_author(const char *path)
 {
     int rc;
     struct stat statbuf;
     char buf[16384];
+    char* author = NULL;
     struct passwd pwd;
-    struct passwd *result;
+    struct passwd *result = NULL;
     
     rc = stat(path, &statbuf);
 
     if (rc != 0) {
-        return rc;
+        return NULL;
     }
 
     rc = getpwuid_r(statbuf.st_uid, &pwd, buf, sizeof(buf), &result);
 
     if (result) {
-        if (strlen(result->pw_name) < len) {
+        author = malloc(strlen(result->pw_name + 1));
+        if (author) {
             strcpy(author, result->pw_name);
-        } else {
-            strcpy(author, "");
-            rc = -1;
         }
     }
 
-    return rc;
+    return author;
 }
-
-    
-        
-    
-    



View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/82e485f4e866c2c87643a17146e528f2c1f5a44e

-- 
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/82e485f4e866c2c87643a17146e528f2c1f5a44e
You're receiving this email because of your account on gitlab.common-lisp.net.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cmucl-cvs/attachments/20220829/3b545676/attachment-0001.html>


More information about the cmucl-cvs mailing list