Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
(if (> len max-len) (setq max-len len))
(incf cnt)
(push slash-name names)))
(setq names (nreverse names))
;;
;; Do the output.
(let* ((col-width (1+ max-len))
(cols (max (truncate width col-width) 1))
(lines (ceiling cnt cols)))
(declare (fixnum cols lines))
(format t "Directory of ~A :~%" pattern)
(dotimes (i lines)
(declare (fixnum i))
(dotimes (j cols)
(declare (fixnum j))
(let ((name (nth (+ i (the fixnum (* j lines))) names)))
(when name
(write-string name)
(unless (eql j (1- cols))
(tab-over
(- col-width (length (the simple-string name))))))))
(terpri))))
(when return-list (nreverse result))))
;;;; Translating uid's and gid's.
(defvar *uid-hash-table* (make-hash-table)
"Hash table for keeping track of uid's and login names.")
;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
;;; lookups are cached in a hash table since groveling the passwd(s) files
;;; is somewhat expensive. The table may hold nil for id's that cannot
;;; be looked up since this means the files are searched in their entirety
;;; each time this id is translated.
;;;
(defun lookup-login-name (uid)
(multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
(if foundp
login-name
(setf (gethash uid *uid-hash-table*)
(get-group-or-user-name :user uid)))))
(defvar *gid-hash-table* (make-hash-table)
"Hash table for keeping track of gid's and group names.")
;;; LOOKUP-GROUP-NAME translates a group id into a group name. Previous
;;; lookups are cached in a hash table since groveling the group(s) files
;;; is somewhat expensive. The table may hold nil for id's that cannot
;;; be looked up since this means the files are searched in their entirety
;;; each time this id is translated.
;;;
(defun lookup-group-name (gid)
(multiple-value-bind (group-name foundp) (gethash gid *gid-hash-table*)
(if foundp
group-name
(setf (gethash gid *gid-hash-table*)
(get-group-or-user-name :group gid)))))
;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group") since it is
;;; a much smaller file, contains all the local id's, and most uses probably
;;; involve id's on machines one would login into. Then if necessary, we look
;;; in "/etc/passwds" ("/etc/groups") which is really long and has to be
;;; fetched over the net.
;;;
(defun get-group-or-user-name (group-or-user id)
"Returns the simple-string user or group name of the user whose uid or gid
is id, or NIL if no such user or group exists. Group-or-user is either
:group or :user."
(let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
(declare (simple-string id-string))
(multiple-value-bind (file1 file2)
(ecase group-or-user
(:group (values "/etc/group" "/etc/groups"))
(:user (values "/etc/passwd" "/etc/passwd")))
(or (get-group-or-user-name-aux id-string file1)
(get-group-or-user-name-aux id-string file2)))))
(defun get-group-or-user-name-aux (id-string passwd-file)
(with-open-file (stream passwd-file)
(loop
(let ((entry (read-line stream nil)))
(unless entry (return nil))
(let ((name-end (position #\: (the simple-string entry)
:test #'char=)))
(when name-end
(let ((id-start (position #\: (the simple-string entry)
:start (1+ name-end) :test #'char=)))
(when id-start
(incf id-start)
(let ((id-end (position #\: (the simple-string entry)
:start id-start :test #'char=)))
(when (and id-end
(string= id-string entry
:start2 id-start :end2 id-end))
(return (subseq entry 0 name-end))))))))))))
;;; Complete-One-File -- Internal
;;;
;;; Return as values a string and the greatest common prefix of all
;;; the files corresponding to pattern.
;;;
(defun complete-one-file (pattern default-type ignore-types)
(let ((first nil)
(length nil))
(do-directory (name etype pattern nil)
(declare (ignore etype))
(let* ((last-dot (position #\. name :from-end t))
(type (if last-dot (subseq name (1+ last-dot)))))
(cond ((and (not (string= type default-type))
(member type ignore-types :test #'string=)))
(first
(let ((msm (string-not-equal name first :end2 length)))
(when (and msm (< msm length))
(setq length msm))))
(t
(setq first name)
(setq length (length name))))))
(values first length)))
;;; Complete-File -- Public
;;;
;;; If the pathname is absolute, just call Complete-One-File on and test
;;; whether the result is a file. If a relative pathname, do it on each
;;; directory, accumulating the result.
;;;
(defun complete-file (pathname &key defaults ignore-types)
"Attempt to complete Pathname as the name of a file. If the resulting
completion is unique, return T as the second value. If there is no
possible completion, return both values NIL."
(setq pathname (pathname pathname))
(setq defaults (if defaults (pathname defaults) *default-pathname-defaults*))
(flet ((pathnamify (res len ambiguous pathname)
(values
(make-pathname :device (%pathname-device pathname)
:directory (%pathname-directory pathname)
:defaults (parse-namestring (subseq res 0 len)))
(not ambiguous))))
(let ((dev (or (%pathname-device pathname) "default"))
(default-type (%pathname-type defaults)))
(if (eq dev :absolute)
(multiple-value-bind
(res len)
(complete-one-file (concatenate 'simple-string
(namestring pathname)
"*")
default-type ignore-types)
(declare (fixnum len))
(if res
(pathnamify res len
(/= (the fixnum (length res)) len)
pathname)
(values nil nil)))
(let ((namestring (%ses-get-useful-name pathname))
(dirs (if (and (%pathname-directory defaults)
(string-equal dev "default"))
(list (directory-namestring defaults))
()))
(max most-positive-fixnum)
(max-str nil)
(ambiguous nil))
(declare (simple-string namestring))
(do-search-list (entry dev)
(pushnew entry dirs :test #'string-equal))
(dolist (entry dirs)
(let ((str (concatenate 'simple-string entry namestring "*")))
(declare (simple-string str))
(multiple-value-bind
(res len)
(complete-one-file str default-type ignore-types)
(when res
(unless ambiguous
(setq ambiguous (or max-str (/= (length res) len))))
(if max-str
(setq max (or (string-not-equal res max-str :end1 len
:end2 max)
max))
(setq max len max-str res))))))
(if max-str
(pathnamify max-str max ambiguous pathname)
(values nil nil)))))))
;;; File-writable -- exported from extensions.
;;;
;;; Determines whether the single argument (which should be a pathname)
;;; can be written by the the current task.
(defun file-writable (name)
"File-writable accepts a pathname and returns T if the current
process can write it, and NIL otherwise."
(multiple-value-bind (tn exists) (predict-name name nil)
(if exists
(values (mach:unix-access tn mach:w_ok))
(values (mach:unix-access (directory-namestring tn)
(logior mach:w_ok mach:x_ok))))))
;;; Pathname-Order -- Internal
;;;
;;; Predicate to order pathnames by. Goes by name.
;;;
(defun pathname-order (x y)
(let ((xn (%pathname-name x))
(yn (%pathname-name y)))
(if (and xn yn)
(let ((res (string-lessp xn yn)))
(cond ((not res) nil)
((= res (length (the simple-string xn))) t)
((= res (length (the simple-string yn))) nil)
(t t)))
xn)))
;;; Ambiguous-Files -- Public
;;;
;;; If the pathname is absolute, just do a directory. If it is relative,
;;; do a directory on each directory in the search-list and merge the results.
;;;
(defun ambiguous-files (pathname &optional defaults)
"Return a list of all files which are possible completions of Pathname.
We look in the directory specified by Defaults as well as looking down
the search list."
(setq pathname (pathname pathname)
defaults (if defaults (pathname defaults) *default-pathname-defaults*))
(let ((dev (or (%pathname-device pathname) "default")))
(if (eq dev :absolute)
(directory (concatenate 'simple-string (namestring pathname) "*"))
(let ((namestring (%ses-get-useful-name pathname))
(dirs (if (and (%pathname-directory defaults)
(string-equal dev "default"))
(list (directory-namestring defaults))
()))
(res ()))
(declare (simple-string namestring))
(do-search-list (entry dev) (pushnew entry dirs :test #'string-equal))
(dolist (entry dirs)
(let ((str (concatenate 'simple-string entry namestring "*")))
(declare (simple-string str))
(setq res (merge 'list res (directory str)
#'pathname-order))))
res))))
;;; Default-Directory -- Public
;;;
;;; This fills in a hole in Common Lisp. We return the first thing we
;;; find by doing a ResolveSearchList on Default.
;;;
(defun default-directory ()
"Returns the pathname for the default directory. This is the place where
a file will be written if no directory is specified. This may be changed
with setf."
(multiple-value-bind (gr dir-or-error)
(mach:unix-current-directory)
(if gr
dir-or-error
(error (mach:get-unix-error-msg dir-or-error)))))
;;;
;;; Maybe this shouldn't go here...
(defsetf default-directory %set-default-directory)
;;; %Set-Default-Directory -- Internal
;;;
;;; The setf method for Default-Directory. We actually set the environment
;;; variable Current which is by convention the head of the search list.
;;;
(defun %set-default-directory (new-val)
(multiple-value-bind (gr error)
(mach:unix-chdir (predict-name new-val nil))
(if gr
(car (setf (search-list "default:")
(cdr (multiple-value-list (mach:unix-current-directory)))))
(error (mach:get-unix-error-msg error)))))