stk/Lib/unix.stk

96 lines
2.7 KiB
Plaintext
Raw Normal View History

1996-09-27 06:29:02 -04:00
;;;;
;;;; u n i x . s t k -- Some unix stuff
;;;;
1999-09-05 07:16:41 -04:00
;;;; Copyright <20> 1993-1999 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
1996-09-27 06:29:02 -04:00
;;;;
1999-09-05 07:16:41 -04:00
;;;; Permission to use, copy, modify, distribute,and license this
;;;; software and its documentation for any purpose is hereby granted,
;;;; provided that existing copyright notices are retained in all
;;;; copies and that this notice is included verbatim in any
;;;; distributions. No written agreement, license, or royalty fee is
;;;; required for any of the authorized uses.
;;;; This software is provided ``AS IS'' without express or implied
;;;; warranty.
1996-09-27 06:29:02 -04:00
;;;;
;;;; Author: Erick Gallesio [eg@kaolin.unice.fr]
;;;; Creation date: 29-Mar-1994 17:36
1999-09-05 07:16:41 -04:00
;;;; Last file update: 3-Sep-1999 19:55 (eg)
1996-09-27 06:29:02 -04:00
;;;;
;;;; This file implements
;;;; (basename f)
;;;; (dirname f)
1999-09-05 07:16:41 -04:00
;;;; (decompose-file-name f) return f exploded in a list
1996-09-27 06:29:02 -04:00
(define basename '())
(define dirname '())
(define decompose-file-name '())
1999-09-05 07:16:41 -04:00
(let* ((slash (if (eqv? (os-kind) 'Unix) #\/ #\\))
(sslash (string slash)))
1996-09-27 06:29:02 -04:00
(define (delete-trailing-slashes s)
(let ((pos (- (string-length s) 1)))
1999-09-05 07:16:41 -04:00
(while (and (>= pos 0) (char=? (string-ref s pos) slash))
1996-09-27 06:29:02 -04:00
(set! pos (- pos 1)))
(if (= pos -1)
1999-09-05 07:16:41 -04:00
sslash
1996-09-27 06:29:02 -04:00
(substring s 0 (+ pos 1)))))
(define (decompose name)
1999-09-05 07:16:41 -04:00
(if (equal? name sslash)
(cons sslash "")
1996-09-27 06:29:02 -04:00
(begin
(let* ((f (delete-trailing-slashes name))
(len (string-length f))
(pos (- len 1)))
;; find last slash
1999-09-05 07:16:41 -04:00
(while (and (>= pos 0) (not (char=? (string-ref f pos) slash)))
1996-09-27 06:29:02 -04:00
(set! pos (- pos 1)))
(case pos
(-1 (cons "." (substring f 0 len)))
1999-09-05 07:16:41 -04:00
(0 (cons sslash (substring f 1 len)))
1996-09-27 06:29:02 -04:00
(else (cons (delete-trailing-slashes (substring f 0 pos))
(substring f (+ pos 1) len))))))))
(set! basename (lambda (file) (cdr (decompose file))))
(set! dirname (lambda (file) (car (decompose file))))
(set! decompose-file-name (lambda (file)
(letrec ((decomp (lambda (file res)
1999-09-05 07:16:41 -04:00
(if (or (equal? file sslash)
1996-09-27 06:29:02 -04:00
(equal? file "."))
(cons file res)
(let ((r (decompose file)))
(decomp (car r)
(cons (cdr r)
res)))))))
(decomp file '())))))
1999-09-05 07:16:41 -04:00
#|
This is another version which uses regexp. decompose-file-name is more
consistent than the above version (but could yiel compatibility problems)
(require "regexp")
(define (decompose-file-name str)
(cons (if (and (> (string-length str) 0)
(char=? (string-ref str 0) #\/))
"/"
".")
(split-string str "/")))
(define (dirname str)
(regexp-replace "^(.*)/(.+)$" str "\\1"))
(define (basename str)
(regexp-replace "^(.*)/(.*)$" str "\\2"))
|#
1996-09-27 06:29:02 -04:00
(provide "unix")