event.test: Fix test on Haiku
[jimtcl.git] / jim-file.c
blob6d10a2bdb703a5429288a9a0e85b67f6768d807a
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
75 *----------------------------------------------------------------------
77 * JimGetFileType --
79 * Given a mode word, returns a string identifying the type of a
80 * file.
82 * Results:
83 * A static text string giving the file type from mode.
85 * Side effects:
86 * None.
88 *----------------------------------------------------------------------
91 static const char *JimGetFileType(int mode)
93 if (S_ISREG(mode)) {
94 return "file";
96 else if (S_ISDIR(mode)) {
97 return "directory";
99 #ifdef S_ISCHR
100 else if (S_ISCHR(mode)) {
101 return "characterSpecial";
103 #endif
104 #ifdef S_ISBLK
105 else if (S_ISBLK(mode)) {
106 return "blockSpecial";
108 #endif
109 #ifdef S_ISFIFO
110 else if (S_ISFIFO(mode)) {
111 return "fifo";
113 #endif
114 #ifdef S_ISLNK
115 else if (S_ISLNK(mode)) {
116 return "link";
118 #endif
119 #ifdef S_ISSOCK
120 else if (S_ISSOCK(mode)) {
121 return "socket";
123 #endif
124 return "unknown";
128 *----------------------------------------------------------------------
130 * StoreStatData --
132 * This is a utility procedure that breaks out the fields of a
133 * "stat" structure and stores them in textual form into the
134 * elements of an associative array.
136 * Results:
137 * Returns a standard Tcl return value. If an error occurs then
138 * a message is left in interp->result.
140 * Side effects:
141 * Elements of the associative array given by "varName" are modified.
143 *----------------------------------------------------------------------
145 static void AppendStatElement(Jim_Interp *interp, Jim_Obj *listObj, const char *key, jim_wide value)
147 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, key, -1));
148 Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, value));
151 static int StoreStatData(Jim_Interp *interp, Jim_Obj *varName, const struct stat *sb)
153 /* Just use a list to store the data */
154 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
156 AppendStatElement(interp, listObj, "dev", sb->st_dev);
157 AppendStatElement(interp, listObj, "ino", sb->st_ino);
158 AppendStatElement(interp, listObj, "mode", sb->st_mode);
159 AppendStatElement(interp, listObj, "nlink", sb->st_nlink);
160 AppendStatElement(interp, listObj, "uid", sb->st_uid);
161 AppendStatElement(interp, listObj, "gid", sb->st_gid);
162 AppendStatElement(interp, listObj, "size", sb->st_size);
163 AppendStatElement(interp, listObj, "atime", sb->st_atime);
164 AppendStatElement(interp, listObj, "mtime", sb->st_mtime);
165 AppendStatElement(interp, listObj, "ctime", sb->st_ctime);
166 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "type", -1));
167 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, JimGetFileType((int)sb->st_mode), -1));
169 /* Was a variable specified? */
170 if (varName) {
171 Jim_Obj *objPtr = Jim_GetVariable(interp, varName, JIM_NONE);
172 if (objPtr) {
173 if (Jim_DictSize(interp, objPtr) < 0) {
174 /* This message matches the one from Tcl */
175 Jim_SetResultFormatted(interp, "can't set \"%#s(dev)\": variable isn't array", varName);
176 Jim_FreeNewObj(interp, listObj);
177 return JIM_ERR;
180 if (Jim_IsShared(objPtr))
181 objPtr = Jim_DuplicateObj(interp, objPtr);
183 /* Just cheat here and append as a list and convert to a dict */
184 Jim_ListAppendList(interp, objPtr, listObj);
185 Jim_DictSize(interp, objPtr);
186 Jim_InvalidateStringRep(objPtr);
188 Jim_FreeNewObj(interp, listObj);
189 listObj = objPtr;
191 Jim_SetVariable(interp, varName, listObj);
194 /* And also return the value */
195 Jim_SetResult(interp, listObj);
197 return JIM_OK;
200 static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
202 const char *path = Jim_String(argv[0]);
203 const char *p = strrchr(path, '/');
205 if (!p && path[0] == '.' && path[1] == '.' && path[2] == '\0') {
206 Jim_SetResultString(interp, "..", -1);
207 } else if (!p) {
208 Jim_SetResultString(interp, ".", -1);
210 else if (p == path) {
211 Jim_SetResultString(interp, "/", -1);
213 #if defined(__MINGW32__) || defined(_MSC_VER)
214 else if (p[-1] == ':') {
215 /* z:/dir => z:/ */
216 Jim_SetResultString(interp, path, p - path + 1);
218 #endif
219 else {
220 Jim_SetResultString(interp, path, p - path);
222 return JIM_OK;
225 static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
227 const char *path = Jim_String(argv[0]);
228 const char *lastSlash = strrchr(path, '/');
229 const char *p = strrchr(path, '.');
231 if (p == NULL || (lastSlash != NULL && lastSlash > p)) {
232 Jim_SetResult(interp, argv[0]);
234 else {
235 Jim_SetResultString(interp, path, p - path);
237 return JIM_OK;
240 static int file_cmd_extension(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
242 const char *path = Jim_String(argv[0]);
243 const char *lastSlash = strrchr(path, '/');
244 const char *p = strrchr(path, '.');
246 if (p == NULL || (lastSlash != NULL && lastSlash >= p)) {
247 p = "";
249 Jim_SetResultString(interp, p, -1);
250 return JIM_OK;
253 static int file_cmd_tail(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
255 const char *path = Jim_String(argv[0]);
256 const char *lastSlash = strrchr(path, '/');
258 if (lastSlash) {
259 Jim_SetResultString(interp, lastSlash + 1, -1);
261 else {
262 Jim_SetResult(interp, argv[0]);
264 return JIM_OK;
267 static int file_cmd_normalize(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
269 #ifdef HAVE_REALPATH
270 const char *path = Jim_String(argv[0]);
271 char *newname = Jim_Alloc(MAXPATHLEN + 1);
273 if (realpath(path, newname)) {
274 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, -1));
275 return JIM_OK;
277 else {
278 Jim_Free(newname);
279 Jim_SetResultFormatted(interp, "can't normalize \"%#s\": %s", argv[0], strerror(errno));
280 return JIM_ERR;
282 #else
283 Jim_SetResultString(interp, "Not implemented", -1);
284 return JIM_ERR;
285 #endif
288 static int file_cmd_join(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
290 int i;
291 char *newname = Jim_Alloc(MAXPATHLEN + 1);
292 char *last = newname;
294 *newname = 0;
296 /* Simple implementation for now */
297 for (i = 0; i < argc; i++) {
298 int len;
299 const char *part = Jim_GetString(argv[i], &len);
301 if (*part == '/') {
302 /* Absolute component, so go back to the start */
303 last = newname;
305 #if defined(__MINGW32__) || defined(_MSC_VER)
306 else if (strchr(part, ':')) {
307 /* Absolute compontent on mingw, so go back to the start */
308 last = newname;
310 #endif
311 else if (part[0] == '.') {
312 if (part[1] == '/') {
313 part += 2;
314 len -= 2;
316 else if (part[1] == 0 && last != newname) {
317 /* Adding '.' to an existing path does nothing */
318 continue;
322 /* Add a slash if needed */
323 if (last != newname && last[-1] != '/') {
324 *last++ = '/';
327 if (len) {
328 if (last + len - newname >= MAXPATHLEN) {
329 Jim_Free(newname);
330 Jim_SetResultString(interp, "Path too long", -1);
331 return JIM_ERR;
333 memcpy(last, part, len);
334 last += len;
337 /* Remove a slash if needed */
338 if (last > newname + 1 && last[-1] == '/') {
339 *--last = 0;
343 *last = 0;
345 /* Probably need to handle some special cases ... */
347 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, last - newname));
349 return JIM_OK;
352 static int file_access(Jim_Interp *interp, Jim_Obj *filename, int mode)
354 Jim_SetResultBool(interp, access(Jim_String(filename), mode) != -1);
356 return JIM_OK;
359 static int file_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
361 return file_access(interp, argv[0], R_OK);
364 static int file_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
366 return file_access(interp, argv[0], W_OK);
369 static int file_cmd_executable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
371 #ifdef X_OK
372 return file_access(interp, argv[0], X_OK);
373 #else
374 /* If no X_OK, just assume true. */
375 Jim_SetResultBool(interp, 1);
376 return JIM_OK;
377 #endif
380 static int file_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
382 return file_access(interp, argv[0], F_OK);
385 static int file_cmd_delete(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
387 int force = Jim_CompareStringImmediate(interp, argv[0], "-force");
389 if (force || Jim_CompareStringImmediate(interp, argv[0], "--")) {
390 argc++;
391 argv--;
394 while (argc--) {
395 const char *path = Jim_String(argv[0]);
397 if (unlink(path) == -1 && errno != ENOENT) {
398 if (rmdir(path) == -1) {
399 /* Maybe try using the script helper */
400 if (!force || Jim_EvalPrefix(interp, "file delete force", 1, argv) != JIM_OK) {
401 Jim_SetResultFormatted(interp, "couldn't delete file \"%s\": %s", path,
402 strerror(errno));
403 return JIM_ERR;
407 argv++;
409 return JIM_OK;
412 #ifdef HAVE_MKDIR_ONE_ARG
413 #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME)
414 #else
415 #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME, 0755)
416 #endif
419 * Create directory, creating all intermediate paths if necessary.
421 * Returns 0 if OK or -1 on failure (and sets errno)
423 * Note: The path may be modified.
425 static int mkdir_all(char *path)
427 int ok = 1;
429 /* First time just try to make the dir */
430 goto first;
432 while (ok--) {
433 /* Must have failed the first time, so recursively make the parent and try again */
435 char *slash = strrchr(path, '/');
437 if (slash && slash != path) {
438 *slash = 0;
439 if (mkdir_all(path) != 0) {
440 return -1;
442 *slash = '/';
445 first:
446 if (MKDIR_DEFAULT(path) == 0) {
447 return 0;
449 if (errno == ENOENT) {
450 /* Create the parent and try again */
451 continue;
453 /* Maybe it already exists as a directory */
454 if (errno == EEXIST) {
455 struct stat sb;
457 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
458 return 0;
460 /* Restore errno */
461 errno = EEXIST;
463 /* Failed */
464 break;
466 return -1;
469 static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
471 while (argc--) {
472 char *path = Jim_StrDup(Jim_String(argv[0]));
473 int rc = mkdir_all(path);
475 Jim_Free(path);
476 if (rc != 0) {
477 Jim_SetResultFormatted(interp, "can't create directory \"%#s\": %s", argv[0],
478 strerror(errno));
479 return JIM_ERR;
481 argv++;
483 return JIM_OK;
486 #ifdef HAVE_MKSTEMP
487 static int file_cmd_tempfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
489 int fd;
490 char *filename;
491 const char *template = "/tmp/tcl.tmp.XXXXXX";
492 mode_t mask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
494 if (argc >= 1) {
495 template = Jim_String(argv[0]);
497 filename = Jim_StrDup(template);
499 fd = mkstemp(filename);
500 umask(mask);
501 if (fd < 0) {
502 Jim_SetResultString(interp, "Failed to create tempfile", -1);
503 Jim_Free(filename);
504 return JIM_ERR;
506 close(fd);
508 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, filename, -1));
509 return JIM_OK;
511 #endif
513 static int file_cmd_rename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
515 const char *source;
516 const char *dest;
517 int force = 0;
519 if (argc == 3) {
520 if (!Jim_CompareStringImmediate(interp, argv[0], "-force")) {
521 return -1;
523 force++;
524 argv++;
525 argc--;
528 source = Jim_String(argv[0]);
529 dest = Jim_String(argv[1]);
531 if (!force && access(dest, F_OK) == 0) {
532 Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": target exists", argv[0],
533 argv[1]);
534 return JIM_ERR;
537 if (rename(source, dest) != 0) {
538 Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": %s", argv[0], argv[1],
539 strerror(errno));
540 return JIM_ERR;
543 return JIM_OK;
546 static int file_stat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
548 const char *path = Jim_String(filename);
550 if (stat(path, sb) == -1) {
551 Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
552 return JIM_ERR;
554 return JIM_OK;
557 #ifdef HAVE_LSTAT
558 static int file_lstat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
560 const char *path = Jim_String(filename);
562 if (lstat(path, sb) == -1) {
563 Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
564 return JIM_ERR;
566 return JIM_OK;
568 #else
569 #define file_lstat file_stat
570 #endif
572 static int file_cmd_atime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
574 struct stat sb;
576 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
577 return JIM_ERR;
579 Jim_SetResultInt(interp, sb.st_atime);
580 return JIM_OK;
583 static int file_cmd_mtime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
585 struct stat sb;
587 if (argc == 2) {
588 #ifdef HAVE_UTIMES
589 jim_wide newtime;
590 struct timeval times[2];
592 if (Jim_GetWide(interp, argv[1], &newtime) != JIM_OK) {
593 return JIM_ERR;
596 times[1].tv_sec = times[0].tv_sec = newtime;
597 times[1].tv_usec = times[0].tv_usec = 0;
599 if (utimes(Jim_String(argv[0]), times) != 0) {
600 Jim_SetResultFormatted(interp, "can't set time on \"%#s\": %s", argv[0], strerror(errno));
601 return JIM_ERR;
603 #else
604 Jim_SetResultString(interp, "Not implemented", -1);
605 return JIM_ERR;
606 #endif
608 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
609 return JIM_ERR;
611 Jim_SetResultInt(interp, sb.st_mtime);
612 return JIM_OK;
615 static int file_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
617 return Jim_EvalPrefix(interp, "file copy", argc, argv);
620 static int file_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
622 struct stat sb;
624 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
625 return JIM_ERR;
627 Jim_SetResultInt(interp, sb.st_size);
628 return JIM_OK;
631 static int file_cmd_isdirectory(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
633 struct stat sb;
634 int ret = 0;
636 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
637 ret = S_ISDIR(sb.st_mode);
639 Jim_SetResultInt(interp, ret);
640 return JIM_OK;
643 static int file_cmd_isfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
645 struct stat sb;
646 int ret = 0;
648 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
649 ret = S_ISREG(sb.st_mode);
651 Jim_SetResultInt(interp, ret);
652 return JIM_OK;
655 #ifdef HAVE_GETEUID
656 static int file_cmd_owned(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
658 struct stat sb;
659 int ret = 0;
661 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
662 ret = (geteuid() == sb.st_uid);
664 Jim_SetResultInt(interp, ret);
665 return JIM_OK;
667 #endif
669 #if defined(HAVE_READLINK)
670 static int file_cmd_readlink(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
672 const char *path = Jim_String(argv[0]);
673 char *linkValue = Jim_Alloc(MAXPATHLEN + 1);
675 int linkLength = readlink(path, linkValue, MAXPATHLEN);
677 if (linkLength == -1) {
678 Jim_Free(linkValue);
679 Jim_SetResultFormatted(interp, "couldn't readlink \"%#s\": %s", argv[0], strerror(errno));
680 return JIM_ERR;
682 linkValue[linkLength] = 0;
683 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, linkValue, linkLength));
684 return JIM_OK;
686 #endif
688 static int file_cmd_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
690 struct stat sb;
692 if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
693 return JIM_ERR;
695 Jim_SetResultString(interp, JimGetFileType((int)sb.st_mode), -1);
696 return JIM_OK;
699 #ifdef HAVE_LSTAT
700 static int file_cmd_lstat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
702 struct stat sb;
704 if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
705 return JIM_ERR;
707 return StoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb);
709 #else
710 #define file_cmd_lstat file_cmd_stat
711 #endif
713 static int file_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
715 struct stat sb;
717 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
718 return JIM_ERR;
720 return StoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb);
723 static const jim_subcmd_type file_command_table[] = {
724 { "atime",
725 "name",
726 file_cmd_atime,
729 /* Description: Last access time */
731 { "mtime",
732 "name ?time?",
733 file_cmd_mtime,
736 /* Description: Get or set last modification time */
738 { "copy",
739 "?-force? source dest",
740 file_cmd_copy,
743 /* Description: Copy source file to destination file */
745 { "dirname",
746 "name",
747 file_cmd_dirname,
750 /* Description: Directory part of the name */
752 { "rootname",
753 "name",
754 file_cmd_rootname,
757 /* Description: Name without any extension */
759 { "extension",
760 "name",
761 file_cmd_extension,
764 /* Description: Last extension including the dot */
766 { "tail",
767 "name",
768 file_cmd_tail,
771 /* Description: Last component of the name */
773 { "normalize",
774 "name",
775 file_cmd_normalize,
778 /* Description: Normalized path of name */
780 { "join",
781 "name ?name ...?",
782 file_cmd_join,
785 /* Description: Join multiple path components */
787 { "readable",
788 "name",
789 file_cmd_readable,
792 /* Description: Is file readable */
794 { "writable",
795 "name",
796 file_cmd_writable,
799 /* Description: Is file writable */
801 { "executable",
802 "name",
803 file_cmd_executable,
806 /* Description: Is file executable */
808 { "exists",
809 "name",
810 file_cmd_exists,
813 /* Description: Does file exist */
815 { "delete",
816 "?-force|--? name ...",
817 file_cmd_delete,
820 /* Description: Deletes the files or directories (must be empty unless -force) */
822 { "mkdir",
823 "dir ...",
824 file_cmd_mkdir,
827 /* Description: Creates the directories */
829 #ifdef HAVE_MKSTEMP
830 { "tempfile",
831 "?template?",
832 file_cmd_tempfile,
835 /* Description: Creates a temporary filename */
837 #endif
838 { "rename",
839 "?-force? source dest",
840 file_cmd_rename,
843 /* Description: Renames a file */
845 #if defined(HAVE_READLINK)
846 { "readlink",
847 "name",
848 file_cmd_readlink,
851 /* Description: Value of the symbolic link */
853 #endif
854 { "size",
855 "name",
856 file_cmd_size,
859 /* Description: Size of file */
861 { "stat",
862 "name ?var?",
863 file_cmd_stat,
866 /* Description: Returns results of stat, and may store in var array */
868 { "lstat",
869 "name ?var?",
870 file_cmd_lstat,
873 /* Description: Returns results of lstat, and may store in var array */
875 { "type",
876 "name",
877 file_cmd_type,
880 /* Description: Returns type of the file */
882 #ifdef HAVE_GETEUID
883 { "owned",
884 "name",
885 file_cmd_owned,
888 /* Description: Returns 1 if owned by the current owner */
890 #endif
891 { "isdirectory",
892 "name",
893 file_cmd_isdirectory,
896 /* Description: Returns 1 if name is a directory */
898 { "isfile",
899 "name",
900 file_cmd_isfile,
903 /* Description: Returns 1 if name is a file */
906 NULL
910 static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
912 const char *path;
914 if (argc != 2) {
915 Jim_WrongNumArgs(interp, 1, argv, "dirname");
916 return JIM_ERR;
919 path = Jim_String(argv[1]);
921 if (chdir(path) != 0) {
922 Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path,
923 strerror(errno));
924 return JIM_ERR;
926 return JIM_OK;
929 static int Jim_PwdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
931 char *cwd = Jim_Alloc(MAXPATHLEN);
933 if (getcwd(cwd, MAXPATHLEN) == NULL) {
934 Jim_SetResultString(interp, "Failed to get pwd", -1);
935 Jim_Free(cwd);
936 return JIM_ERR;
938 #if defined(__MINGW32__) || defined(_MSC_VER)
940 /* Try to keep backlashes out of paths */
941 char *p = cwd;
942 while ((p = strchr(p, '\\')) != NULL) {
943 *p++ = '/';
946 #endif
948 Jim_SetResultString(interp, cwd, -1);
950 Jim_Free(cwd);
951 return JIM_OK;
954 int Jim_fileInit(Jim_Interp *interp)
956 if (Jim_PackageProvide(interp, "file", "1.0", JIM_ERRMSG))
957 return JIM_ERR;
959 Jim_CreateCommand(interp, "file", Jim_SubCmdProc, (void *)file_command_table, NULL);
960 Jim_CreateCommand(interp, "pwd", Jim_PwdCmd, NULL, NULL);
961 Jim_CreateCommand(interp, "cd", Jim_CdCmd, NULL, NULL);
962 return JIM_OK;