examples/tip.tcl: Lock the device
[jimtcl.git] / jim-file.c
blob07fe699121e201f6857ad879455b2942c08acedb
1 /*
2 * Implements the file command for jim
4 * (c) 2008 Steve Bennett <steveb@workware.net.au>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as representing
32 * official policies, either expressed or implied, of the Jim Tcl Project.
34 * Based on code originally from Tcl 6.7:
36 * Copyright 1987-1991 Regents of the University of California
37 * Permission to use, copy, modify, and distribute this
38 * software and its documentation for any purpose and without
39 * fee is hereby granted, provided that the above copyright
40 * notice appear in all copies. The University of California
41 * makes no representations about the suitability of this
42 * software for any purpose. It is provided "as is" without
43 * express or implied warranty.
46 #include <limits.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <sys/stat.h>
53 #include <jimautoconf.h>
54 #include <jim-subcmd.h>
56 #ifdef HAVE_UTIMES
57 #include <sys/time.h>
58 #endif
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #elif defined(_MSC_VER)
62 #include <direct.h>
63 #define F_OK 0
64 #define W_OK 2
65 #define R_OK 4
66 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
67 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
68 #endif
70 # ifndef MAXPATHLEN
71 # define MAXPATHLEN JIM_PATH_LEN
72 # endif
74 #if defined(__MINGW32__) || defined(_MSC_VER)
75 #define ISWINDOWS 1
76 #else
77 #define ISWINDOWS 0
78 #endif
81 *----------------------------------------------------------------------
83 * JimGetFileType --
85 * Given a mode word, returns a string identifying the type of a
86 * file.
88 * Results:
89 * A static text string giving the file type from mode.
91 * Side effects:
92 * None.
94 *----------------------------------------------------------------------
97 static const char *JimGetFileType(int mode)
99 if (S_ISREG(mode)) {
100 return "file";
102 else if (S_ISDIR(mode)) {
103 return "directory";
105 #ifdef S_ISCHR
106 else if (S_ISCHR(mode)) {
107 return "characterSpecial";
109 #endif
110 #ifdef S_ISBLK
111 else if (S_ISBLK(mode)) {
112 return "blockSpecial";
114 #endif
115 #ifdef S_ISFIFO
116 else if (S_ISFIFO(mode)) {
117 return "fifo";
119 #endif
120 #ifdef S_ISLNK
121 else if (S_ISLNK(mode)) {
122 return "link";
124 #endif
125 #ifdef S_ISSOCK
126 else if (S_ISSOCK(mode)) {
127 return "socket";
129 #endif
130 return "unknown";
134 *----------------------------------------------------------------------
136 * StoreStatData --
138 * This is a utility procedure that breaks out the fields of a
139 * "stat" structure and stores them in textual form into the
140 * elements of an associative array.
142 * Results:
143 * Returns a standard Tcl return value. If an error occurs then
144 * a message is left in interp->result.
146 * Side effects:
147 * Elements of the associative array given by "varName" are modified.
149 *----------------------------------------------------------------------
151 static void AppendStatElement(Jim_Interp *interp, Jim_Obj *listObj, const char *key, jim_wide value)
153 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, key, -1));
154 Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, value));
157 static int StoreStatData(Jim_Interp *interp, Jim_Obj *varName, const struct stat *sb)
159 /* Just use a list to store the data */
160 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
162 AppendStatElement(interp, listObj, "dev", sb->st_dev);
163 AppendStatElement(interp, listObj, "ino", sb->st_ino);
164 AppendStatElement(interp, listObj, "mode", sb->st_mode);
165 AppendStatElement(interp, listObj, "nlink", sb->st_nlink);
166 AppendStatElement(interp, listObj, "uid", sb->st_uid);
167 AppendStatElement(interp, listObj, "gid", sb->st_gid);
168 AppendStatElement(interp, listObj, "size", sb->st_size);
169 AppendStatElement(interp, listObj, "atime", sb->st_atime);
170 AppendStatElement(interp, listObj, "mtime", sb->st_mtime);
171 AppendStatElement(interp, listObj, "ctime", sb->st_ctime);
172 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "type", -1));
173 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, JimGetFileType((int)sb->st_mode), -1));
175 /* Was a variable specified? */
176 if (varName) {
177 Jim_Obj *objPtr;
178 objPtr = Jim_GetVariable(interp, varName, JIM_NONE);
180 if (objPtr) {
181 Jim_Obj *objv[2];
183 objv[0] = objPtr;
184 objv[1] = listObj;
186 objPtr = Jim_DictMerge(interp, 2, objv);
187 if (objPtr == NULL) {
188 /* This message matches the one from Tcl */
189 Jim_SetResultFormatted(interp, "can't set \"%#s(dev)\": variable isn't array", varName);
190 Jim_FreeNewObj(interp, listObj);
191 return JIM_ERR;
194 Jim_InvalidateStringRep(objPtr);
196 Jim_FreeNewObj(interp, listObj);
197 listObj = objPtr;
199 Jim_SetVariable(interp, varName, listObj);
202 /* And also return the value */
203 Jim_SetResult(interp, listObj);
205 return JIM_OK;
208 static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
210 const char *path = Jim_String(argv[0]);
211 const char *p = strrchr(path, '/');
213 if (!p && path[0] == '.' && path[1] == '.' && path[2] == '\0') {
214 Jim_SetResultString(interp, "..", -1);
215 } else if (!p) {
216 Jim_SetResultString(interp, ".", -1);
218 else if (p == path) {
219 Jim_SetResultString(interp, "/", -1);
221 else if (ISWINDOWS && p[-1] == ':') {
222 /* z:/dir => z:/ */
223 Jim_SetResultString(interp, path, p - path + 1);
225 else {
226 Jim_SetResultString(interp, path, p - path);
228 return JIM_OK;
231 static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
233 const char *path = Jim_String(argv[0]);
234 const char *lastSlash = strrchr(path, '/');
235 const char *p = strrchr(path, '.');
237 if (p == NULL || (lastSlash != NULL && lastSlash > p)) {
238 Jim_SetResult(interp, argv[0]);
240 else {
241 Jim_SetResultString(interp, path, p - path);
243 return JIM_OK;
246 static int file_cmd_extension(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
248 const char *path = Jim_String(argv[0]);
249 const char *lastSlash = strrchr(path, '/');
250 const char *p = strrchr(path, '.');
252 if (p == NULL || (lastSlash != NULL && lastSlash >= p)) {
253 p = "";
255 Jim_SetResultString(interp, p, -1);
256 return JIM_OK;
259 static int file_cmd_tail(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
261 const char *path = Jim_String(argv[0]);
262 const char *lastSlash = strrchr(path, '/');
264 if (lastSlash) {
265 Jim_SetResultString(interp, lastSlash + 1, -1);
267 else {
268 Jim_SetResult(interp, argv[0]);
270 return JIM_OK;
273 static int file_cmd_normalize(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
275 #ifdef HAVE_REALPATH
276 const char *path = Jim_String(argv[0]);
277 char *newname = Jim_Alloc(MAXPATHLEN + 1);
279 if (realpath(path, newname)) {
280 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, -1));
281 return JIM_OK;
283 else {
284 Jim_Free(newname);
285 Jim_SetResultFormatted(interp, "can't normalize \"%#s\": %s", argv[0], strerror(errno));
286 return JIM_ERR;
288 #else
289 Jim_SetResultString(interp, "Not implemented", -1);
290 return JIM_ERR;
291 #endif
294 static int file_cmd_join(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
296 int i;
297 char *newname = Jim_Alloc(MAXPATHLEN + 1);
298 char *last = newname;
300 *newname = 0;
302 /* Simple implementation for now */
303 for (i = 0; i < argc; i++) {
304 int len;
305 const char *part = Jim_GetString(argv[i], &len);
307 if (*part == '/') {
308 /* Absolute component, so go back to the start */
309 last = newname;
311 else if (ISWINDOWS && strchr(part, ':')) {
312 /* Absolute component on mingw, so go back to the start */
313 last = newname;
315 else if (part[0] == '.') {
316 if (part[1] == '/') {
317 part += 2;
318 len -= 2;
320 else if (part[1] == 0 && last != newname) {
321 /* Adding '.' to an existing path does nothing */
322 continue;
326 /* Add a slash if needed */
327 if (last != newname && last[-1] != '/') {
328 *last++ = '/';
331 if (len) {
332 if (last + len - newname >= MAXPATHLEN) {
333 Jim_Free(newname);
334 Jim_SetResultString(interp, "Path too long", -1);
335 return JIM_ERR;
337 memcpy(last, part, len);
338 last += len;
341 /* Remove a slash if needed */
342 if (last > newname + 1 && last[-1] == '/') {
343 /* but on on Windows, leave the trailing slash on "c:/ " */
344 if (!ISWINDOWS || !(last > newname + 2 && last[-2] == ':')) {
345 *--last = 0;
350 *last = 0;
352 /* Probably need to handle some special cases ... */
354 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, last - newname));
356 return JIM_OK;
359 static int file_access(Jim_Interp *interp, Jim_Obj *filename, int mode)
361 Jim_SetResultBool(interp, access(Jim_String(filename), mode) != -1);
363 return JIM_OK;
366 static int file_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
368 return file_access(interp, argv[0], R_OK);
371 static int file_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
373 return file_access(interp, argv[0], W_OK);
376 static int file_cmd_executable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
378 #ifdef X_OK
379 return file_access(interp, argv[0], X_OK);
380 #else
381 /* If no X_OK, just assume true. */
382 Jim_SetResultBool(interp, 1);
383 return JIM_OK;
384 #endif
387 static int file_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
389 return file_access(interp, argv[0], F_OK);
392 static int file_cmd_delete(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
394 int force = Jim_CompareStringImmediate(interp, argv[0], "-force");
396 if (force || Jim_CompareStringImmediate(interp, argv[0], "--")) {
397 argc++;
398 argv--;
401 while (argc--) {
402 const char *path = Jim_String(argv[0]);
404 if (unlink(path) == -1 && errno != ENOENT) {
405 if (rmdir(path) == -1) {
406 /* Maybe try using the script helper */
407 if (!force || Jim_EvalPrefix(interp, "file delete force", 1, argv) != JIM_OK) {
408 Jim_SetResultFormatted(interp, "couldn't delete file \"%s\": %s", path,
409 strerror(errno));
410 return JIM_ERR;
414 argv++;
416 return JIM_OK;
419 #ifdef HAVE_MKDIR_ONE_ARG
420 #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME)
421 #else
422 #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME, 0755)
423 #endif
426 * Create directory, creating all intermediate paths if necessary.
428 * Returns 0 if OK or -1 on failure (and sets errno)
430 * Note: The path may be modified.
432 static int mkdir_all(char *path)
434 int ok = 1;
436 /* First time just try to make the dir */
437 goto first;
439 while (ok--) {
440 /* Must have failed the first time, so recursively make the parent and try again */
442 char *slash = strrchr(path, '/');
444 if (slash && slash != path) {
445 *slash = 0;
446 if (mkdir_all(path) != 0) {
447 return -1;
449 *slash = '/';
452 first:
453 if (MKDIR_DEFAULT(path) == 0) {
454 return 0;
456 if (errno == ENOENT) {
457 /* Create the parent and try again */
458 continue;
460 /* Maybe it already exists as a directory */
461 if (errno == EEXIST) {
462 struct stat sb;
464 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
465 return 0;
467 /* Restore errno */
468 errno = EEXIST;
470 /* Failed */
471 break;
473 return -1;
476 static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
478 while (argc--) {
479 char *path = Jim_StrDup(Jim_String(argv[0]));
480 int rc = mkdir_all(path);
482 Jim_Free(path);
483 if (rc != 0) {
484 Jim_SetResultFormatted(interp, "can't create directory \"%#s\": %s", argv[0],
485 strerror(errno));
486 return JIM_ERR;
488 argv++;
490 return JIM_OK;
493 static int file_cmd_tempfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
495 int fd = Jim_MakeTempFile(interp, (argc >= 1) ? Jim_String(argv[0]) : NULL);
497 if (fd < 0) {
498 return JIM_ERR;
500 close(fd);
502 return JIM_OK;
505 static int file_cmd_rename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
507 const char *source;
508 const char *dest;
509 int force = 0;
511 if (argc == 3) {
512 if (!Jim_CompareStringImmediate(interp, argv[0], "-force")) {
513 return -1;
515 force++;
516 argv++;
517 argc--;
520 source = Jim_String(argv[0]);
521 dest = Jim_String(argv[1]);
523 if (!force && access(dest, F_OK) == 0) {
524 Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": target exists", argv[0],
525 argv[1]);
526 return JIM_ERR;
529 if (rename(source, dest) != 0) {
530 Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": %s", argv[0], argv[1],
531 strerror(errno));
532 return JIM_ERR;
535 return JIM_OK;
538 #if defined(HAVE_LINK) && defined(HAVE_SYMLINK)
539 static int file_cmd_link(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
541 int ret;
542 const char *source;
543 const char *dest;
544 static const char * const options[] = { "-hard", "-symbolic", NULL };
545 enum { OPT_HARD, OPT_SYMBOLIC, };
546 int option = OPT_HARD;
548 if (argc == 3) {
549 if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ENUM_ABBREV | JIM_ERRMSG) != JIM_OK) {
550 return JIM_ERR;
552 argv++;
553 argc--;
556 dest = Jim_String(argv[0]);
557 source = Jim_String(argv[1]);
559 if (option == OPT_HARD) {
560 ret = link(source, dest);
562 else {
563 ret = symlink(source, dest);
566 if (ret != 0) {
567 Jim_SetResultFormatted(interp, "error linking \"%#s\" to \"%#s\": %s", argv[0], argv[1],
568 strerror(errno));
569 return JIM_ERR;
572 return JIM_OK;
574 #endif
576 static int file_stat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
578 const char *path = Jim_String(filename);
580 if (stat(path, sb) == -1) {
581 Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
582 return JIM_ERR;
584 return JIM_OK;
587 #ifdef HAVE_LSTAT
588 static int file_lstat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
590 const char *path = Jim_String(filename);
592 if (lstat(path, sb) == -1) {
593 Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
594 return JIM_ERR;
596 return JIM_OK;
598 #else
599 #define file_lstat file_stat
600 #endif
602 static int file_cmd_atime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
604 struct stat sb;
606 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
607 return JIM_ERR;
609 Jim_SetResultInt(interp, sb.st_atime);
610 return JIM_OK;
613 static int file_cmd_mtime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
615 struct stat sb;
617 if (argc == 2) {
618 #ifdef HAVE_UTIMES
619 jim_wide newtime;
620 struct timeval times[2];
622 if (Jim_GetWide(interp, argv[1], &newtime) != JIM_OK) {
623 return JIM_ERR;
626 times[1].tv_sec = times[0].tv_sec = newtime;
627 times[1].tv_usec = times[0].tv_usec = 0;
629 if (utimes(Jim_String(argv[0]), times) != 0) {
630 Jim_SetResultFormatted(interp, "can't set time on \"%#s\": %s", argv[0], strerror(errno));
631 return JIM_ERR;
633 #else
634 Jim_SetResultString(interp, "Not implemented", -1);
635 return JIM_ERR;
636 #endif
638 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
639 return JIM_ERR;
641 Jim_SetResultInt(interp, sb.st_mtime);
642 return JIM_OK;
645 static int file_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
647 return Jim_EvalPrefix(interp, "file copy", argc, argv);
650 static int file_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
652 struct stat sb;
654 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
655 return JIM_ERR;
657 Jim_SetResultInt(interp, sb.st_size);
658 return JIM_OK;
661 static int file_cmd_isdirectory(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
663 struct stat sb;
664 int ret = 0;
666 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
667 ret = S_ISDIR(sb.st_mode);
669 Jim_SetResultInt(interp, ret);
670 return JIM_OK;
673 static int file_cmd_isfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
675 struct stat sb;
676 int ret = 0;
678 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
679 ret = S_ISREG(sb.st_mode);
681 Jim_SetResultInt(interp, ret);
682 return JIM_OK;
685 #ifdef HAVE_GETEUID
686 static int file_cmd_owned(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
688 struct stat sb;
689 int ret = 0;
691 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
692 ret = (geteuid() == sb.st_uid);
694 Jim_SetResultInt(interp, ret);
695 return JIM_OK;
697 #endif
699 #if defined(HAVE_READLINK)
700 static int file_cmd_readlink(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
702 const char *path = Jim_String(argv[0]);
703 char *linkValue = Jim_Alloc(MAXPATHLEN + 1);
705 int linkLength = readlink(path, linkValue, MAXPATHLEN);
707 if (linkLength == -1) {
708 Jim_Free(linkValue);
709 Jim_SetResultFormatted(interp, "couldn't readlink \"%#s\": %s", argv[0], strerror(errno));
710 return JIM_ERR;
712 linkValue[linkLength] = 0;
713 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, linkValue, linkLength));
714 return JIM_OK;
716 #endif
718 static int file_cmd_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
720 struct stat sb;
722 if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
723 return JIM_ERR;
725 Jim_SetResultString(interp, JimGetFileType((int)sb.st_mode), -1);
726 return JIM_OK;
729 #ifdef HAVE_LSTAT
730 static int file_cmd_lstat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
732 struct stat sb;
734 if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
735 return JIM_ERR;
737 return StoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb);
739 #else
740 #define file_cmd_lstat file_cmd_stat
741 #endif
743 static int file_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
745 struct stat sb;
747 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
748 return JIM_ERR;
750 return StoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb);
753 static const jim_subcmd_type file_command_table[] = {
754 { "atime",
755 "name",
756 file_cmd_atime,
759 /* Description: Last access time */
761 { "mtime",
762 "name ?time?",
763 file_cmd_mtime,
766 /* Description: Get or set last modification time */
768 { "copy",
769 "?-force? source dest",
770 file_cmd_copy,
773 /* Description: Copy source file to destination file */
775 { "dirname",
776 "name",
777 file_cmd_dirname,
780 /* Description: Directory part of the name */
782 { "rootname",
783 "name",
784 file_cmd_rootname,
787 /* Description: Name without any extension */
789 { "extension",
790 "name",
791 file_cmd_extension,
794 /* Description: Last extension including the dot */
796 { "tail",
797 "name",
798 file_cmd_tail,
801 /* Description: Last component of the name */
803 { "normalize",
804 "name",
805 file_cmd_normalize,
808 /* Description: Normalized path of name */
810 { "join",
811 "name ?name ...?",
812 file_cmd_join,
815 /* Description: Join multiple path components */
817 { "readable",
818 "name",
819 file_cmd_readable,
822 /* Description: Is file readable */
824 { "writable",
825 "name",
826 file_cmd_writable,
829 /* Description: Is file writable */
831 { "executable",
832 "name",
833 file_cmd_executable,
836 /* Description: Is file executable */
838 { "exists",
839 "name",
840 file_cmd_exists,
843 /* Description: Does file exist */
845 { "delete",
846 "?-force|--? name ...",
847 file_cmd_delete,
850 /* Description: Deletes the files or directories (must be empty unless -force) */
852 { "mkdir",
853 "dir ...",
854 file_cmd_mkdir,
857 /* Description: Creates the directories */
859 { "tempfile",
860 "?template?",
861 file_cmd_tempfile,
864 /* Description: Creates a temporary filename */
866 { "rename",
867 "?-force? source dest",
868 file_cmd_rename,
871 /* Description: Renames a file */
873 #if defined(HAVE_LINK) && defined(HAVE_SYMLINK)
874 { "link",
875 "?-symbolic|-hard? newname target",
876 file_cmd_link,
879 /* Description: Creates a hard or soft link */
881 #endif
882 #if defined(HAVE_READLINK)
883 { "readlink",
884 "name",
885 file_cmd_readlink,
888 /* Description: Value of the symbolic link */
890 #endif
891 { "size",
892 "name",
893 file_cmd_size,
896 /* Description: Size of file */
898 { "stat",
899 "name ?var?",
900 file_cmd_stat,
903 /* Description: Returns results of stat, and may store in var array */
905 { "lstat",
906 "name ?var?",
907 file_cmd_lstat,
910 /* Description: Returns results of lstat, and may store in var array */
912 { "type",
913 "name",
914 file_cmd_type,
917 /* Description: Returns type of the file */
919 #ifdef HAVE_GETEUID
920 { "owned",
921 "name",
922 file_cmd_owned,
925 /* Description: Returns 1 if owned by the current owner */
927 #endif
928 { "isdirectory",
929 "name",
930 file_cmd_isdirectory,
933 /* Description: Returns 1 if name is a directory */
935 { "isfile",
936 "name",
937 file_cmd_isfile,
940 /* Description: Returns 1 if name is a file */
943 NULL
947 static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
949 const char *path;
951 if (argc != 2) {
952 Jim_WrongNumArgs(interp, 1, argv, "dirname");
953 return JIM_ERR;
956 path = Jim_String(argv[1]);
958 if (chdir(path) != 0) {
959 Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path,
960 strerror(errno));
961 return JIM_ERR;
963 return JIM_OK;
966 static int Jim_PwdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
968 char *cwd = Jim_Alloc(MAXPATHLEN);
970 if (getcwd(cwd, MAXPATHLEN) == NULL) {
971 Jim_SetResultString(interp, "Failed to get pwd", -1);
972 Jim_Free(cwd);
973 return JIM_ERR;
975 else if (ISWINDOWS) {
976 /* Try to keep backslashes out of paths */
977 char *p = cwd;
978 while ((p = strchr(p, '\\')) != NULL) {
979 *p++ = '/';
983 Jim_SetResultString(interp, cwd, -1);
985 Jim_Free(cwd);
986 return JIM_OK;
989 int Jim_fileInit(Jim_Interp *interp)
991 if (Jim_PackageProvide(interp, "file", "1.0", JIM_ERRMSG))
992 return JIM_ERR;
994 Jim_CreateCommand(interp, "file", Jim_SubCmdProc, (void *)file_command_table, NULL);
995 Jim_CreateCommand(interp, "pwd", Jim_PwdCmd, NULL, NULL);
996 Jim_CreateCommand(interp, "cd", Jim_CdCmd, NULL, NULL);
997 return JIM_OK;