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

Commits:

2 changed files:

Changes:

  • src/code/filesys.lisp
    ... ... @@ -1098,16 +1098,22 @@ optionally keeping some of the most recent old versions."
    1098 1098
     		 :pathname file
    
    1099 1099
     		 :format-control (intl:gettext "~S doesn't exist.")
    
    1100 1100
     		 :format-arguments (list file)))
    
    1101
    -	(alien:with-alien ((author (array c-call:char 1024)))
    
    1102
    -	  (unix::syscall* ("os_file_author" c-call:c-string
    
    1103
    -					    (alien:array c-call:char 1024)
    
    1104
    -					    c-call:int)
    
    1105
    -			  ;; Return
    
    1106
    -			  (alien:cast author c-call:c-string)
    
    1107
    -			  ;; Args
    
    1108
    -			  (unix::%name->file name)
    
    1109
    -			  author
    
    1110
    -			  1024)))))
    
    1101
    +	;; unix-namestring converts "." to "".  Convert it back to
    
    1102
    +	;; "." so we can stat the current directory.  (Perhaps
    
    1103
    +	;; that's a bug in unix-namestring?)
    
    1104
    +	(when (string= name "")
    
    1105
    +	  (setf name "."))
    
    1106
    +	(let (author)
    
    1107
    +	  (unwind-protect
    
    1108
    +	       (progn
    
    1109
    +		 (setf author (alien:alien-funcall
    
    1110
    +			       (alien:extern-alien "os_file_author"
    
    1111
    +						   (function (alien:* c-call:c-string) c-call:c-string))
    
    1112
    +			       (unix::%name->file name)))
    
    1113
    +		 (unless (zerop (sap-int (alien:alien-sap author)))
    
    1114
    +		   (alien:cast author c-call:c-string)))
    
    1115
    +	    (when author
    
    1116
    +	      (alien:free-alien author)))))))
    
    1111 1117
     
    
    1112 1118
     
    
    1113 1119
     ;;;; 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
    717 717
         return rc;
    
    718 718
     }
    
    719 719
     
    
    720
    -int
    
    721
    -os_file_author(const char *path, char* author, int len)
    
    720
    +/*
    
    721
    + * Interface for file-author.  Given a pathname, returns a new string
    
    722
    + * holding the author of the file or NULL if some error occurred.  The
    
    723
    + * caller is responsible for freeing the memory used by the string.
    
    724
    + */
    
    725
    +char *
    
    726
    +os_file_author(const char *path)
    
    722 727
     {
    
    723 728
         int rc;
    
    724 729
         struct stat statbuf;
    
    725 730
         char buf[16384];
    
    731
    +    char* author = NULL;
    
    726 732
         struct passwd pwd;
    
    727
    -    struct passwd *result;
    
    733
    +    struct passwd *result = NULL;
    
    728 734
         
    
    729 735
         rc = stat(path, &statbuf);
    
    730 736
     
    
    731 737
         if (rc != 0) {
    
    732
    -        return rc;
    
    738
    +        return NULL;
    
    733 739
         }
    
    734 740
     
    
    735 741
         rc = getpwuid_r(statbuf.st_uid, &pwd, buf, sizeof(buf), &result);
    
    736 742
     
    
    737 743
         if (result) {
    
    738
    -        if (strlen(result->pw_name) < len) {
    
    744
    +        author = malloc(strlen(result->pw_name + 1));
    
    745
    +        if (author) {
    
    739 746
                 strcpy(author, result->pw_name);
    
    740
    -        } else {
    
    741
    -            strcpy(author, "");
    
    742
    -            rc = -1;
    
    743 747
             }
    
    744 748
         }
    
    745 749
     
    
    746
    -    return rc;
    
    750
    +    return author;
    
    747 751
     }
    748
    -
    
    749
    -    
    
    750
    -        
    
    751
    -    
    
    752
    -