format: fix format %hd on some platforms
[jimtcl.git] / jim-file.c
blob23b431fc6b0e206705e4997af8c8a6bbcdeb2fbb
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, int 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 const char *path = Jim_String(filename);
355 int rc = access(path, mode);
357 Jim_SetResultBool(interp, rc != -1);
359 return JIM_OK;
362 static int file_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
364 return file_access(interp, argv[0], R_OK);
367 static int file_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
369 return file_access(interp, argv[0], W_OK);
372 static int file_cmd_executable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
374 #ifdef X_OK
375 return file_access(interp, argv[0], X_OK);
376 #else
377 /* XXX: X_OK doesn't work under Windows.
378 * In any case, may need to add .exe, etc. so just lie!
380 Jim_SetResultBool(interp, 1);
381 return JIM_OK;
382 #endif
385 static int file_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
387 return file_access(interp, argv[0], F_OK);
390 static int file_cmd_delete(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
392 int force = Jim_CompareStringImmediate(interp, argv[0], "-force");
394 if (force || Jim_CompareStringImmediate(interp, argv[0], "--")) {
395 argc++;
396 argv--;
399 while (argc--) {
400 const char *path = Jim_String(argv[0]);
402 if (unlink(path) == -1 && errno != ENOENT) {
403 if (rmdir(path) == -1) {
404 /* Maybe try using the script helper */
405 if (!force || Jim_EvalPrefix(interp, "file delete force", 1, argv) != JIM_OK) {
406 Jim_SetResultFormatted(interp, "couldn't delete file \"%s\": %s", path,
407 strerror(errno));
408 return JIM_ERR;
412 argv++;
414 return JIM_OK;
417 #ifdef HAVE_MKDIR_ONE_ARG
418 #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME)
419 #else
420 #define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME, 0755)
421 #endif
424 * Create directory, creating all intermediate paths if necessary.
426 * Returns 0 if OK or -1 on failure (and sets errno)
428 * Note: The path may be modified.
430 static int mkdir_all(char *path)
432 int ok = 1;
434 /* First time just try to make the dir */
435 goto first;
437 while (ok--) {
438 /* Must have failed the first time, so recursively make the parent and try again */
440 char *slash = strrchr(path, '/');
442 if (slash && slash != path) {
443 *slash = 0;
444 if (mkdir_all(path) != 0) {
445 return -1;
447 *slash = '/';
450 first:
451 if (MKDIR_DEFAULT(path) == 0) {
452 return 0;
454 if (errno == ENOENT) {
455 /* Create the parent and try again */
456 continue;
458 /* Maybe it already exists as a directory */
459 if (errno == EEXIST) {
460 struct stat sb;
462 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
463 return 0;
465 /* Restore errno */
466 errno = EEXIST;
468 /* Failed */
469 break;
471 return -1;
474 static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
476 while (argc--) {
477 char *path = Jim_StrDup(Jim_String(argv[0]));
478 int rc = mkdir_all(path);
480 Jim_Free(path);
481 if (rc != 0) {
482 Jim_SetResultFormatted(interp, "can't create directory \"%#s\": %s", argv[0],
483 strerror(errno));
484 return JIM_ERR;
486 argv++;
488 return JIM_OK;
491 #ifdef HAVE_MKSTEMP
492 static int file_cmd_tempfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
494 int fd;
495 char *filename;
496 const char *template = "/tmp/tcl.tmp.XXXXXX";
498 if (argc >= 1) {
499 template = Jim_String(argv[0]);
501 filename = Jim_StrDup(template);
503 fd = mkstemp(filename);
504 if (fd < 0) {
505 Jim_SetResultString(interp, "Failed to create tempfile", -1);
506 return JIM_ERR;
508 close(fd);
510 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, filename, -1));
511 return JIM_OK;
513 #endif
515 static int file_cmd_rename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
517 const char *source;
518 const char *dest;
519 int force = 0;
521 if (argc == 3) {
522 if (!Jim_CompareStringImmediate(interp, argv[0], "-force")) {
523 return -1;
525 force++;
526 argv++;
527 argc--;
530 source = Jim_String(argv[0]);
531 dest = Jim_String(argv[1]);
533 if (!force && access(dest, F_OK) == 0) {
534 Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": target exists", argv[0],
535 argv[1]);
536 return JIM_ERR;
539 if (rename(source, dest) != 0) {
540 Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": %s", argv[0], argv[1],
541 strerror(errno));
542 return JIM_ERR;
545 return JIM_OK;
548 static int file_stat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
550 const char *path = Jim_String(filename);
552 if (stat(path, sb) == -1) {
553 Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
554 return JIM_ERR;
556 return JIM_OK;
559 #ifdef HAVE_LSTAT
560 static int file_lstat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
562 const char *path = Jim_String(filename);
564 if (lstat(path, sb) == -1) {
565 Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
566 return JIM_ERR;
568 return JIM_OK;
570 #else
571 #define file_lstat file_stat
572 #endif
574 static int file_cmd_atime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
576 struct stat sb;
578 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
579 return JIM_ERR;
581 Jim_SetResultInt(interp, sb.st_atime);
582 return JIM_OK;
585 static int file_cmd_mtime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
587 struct stat sb;
589 if (argc == 2) {
590 #ifdef HAVE_UTIMES
591 jim_wide newtime;
592 struct timeval times[2];
594 if (Jim_GetWide(interp, argv[1], &newtime) != JIM_OK) {
595 return JIM_ERR;
598 times[1].tv_sec = times[0].tv_sec = newtime;
599 times[1].tv_usec = times[0].tv_usec = 0;
601 if (utimes(Jim_String(argv[0]), times) != 0) {
602 Jim_SetResultFormatted(interp, "can't set time on \"%#s\": %s", argv[0], strerror(errno));
603 return JIM_ERR;
605 #else
606 Jim_SetResultString(interp, "Not implemented", -1);
607 return JIM_ERR;
608 #endif
610 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
611 return JIM_ERR;
613 Jim_SetResultInt(interp, sb.st_mtime);
614 return JIM_OK;
617 static int file_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
619 return Jim_EvalPrefix(interp, "file copy", argc, argv);
622 static int file_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
624 struct stat sb;
626 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
627 return JIM_ERR;
629 Jim_SetResultInt(interp, sb.st_size);
630 return JIM_OK;
633 static int file_cmd_isdirectory(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
635 struct stat sb;
636 int ret = 0;
638 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
639 ret = S_ISDIR(sb.st_mode);
641 Jim_SetResultInt(interp, ret);
642 return JIM_OK;
645 static int file_cmd_isfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
647 struct stat sb;
648 int ret = 0;
650 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
651 ret = S_ISREG(sb.st_mode);
653 Jim_SetResultInt(interp, ret);
654 return JIM_OK;
657 #ifdef HAVE_GETEUID
658 static int file_cmd_owned(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
660 struct stat sb;
661 int ret = 0;
663 if (file_stat(interp, argv[0], &sb) == JIM_OK) {
664 ret = (geteuid() == sb.st_uid);
666 Jim_SetResultInt(interp, ret);
667 return JIM_OK;
669 #endif
671 #if defined(HAVE_READLINK)
672 static int file_cmd_readlink(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
674 const char *path = Jim_String(argv[0]);
675 char *linkValue = Jim_Alloc(MAXPATHLEN + 1);
677 int linkLength = readlink(path, linkValue, MAXPATHLEN);
679 if (linkLength == -1) {
680 Jim_Free(linkValue);
681 Jim_SetResultFormatted(interp, "couldn't readlink \"%#s\": %s", argv[0], strerror(errno));
682 return JIM_ERR;
684 linkValue[linkLength] = 0;
685 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, linkValue, linkLength));
686 return JIM_OK;
688 #endif
690 static int file_cmd_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
692 struct stat sb;
694 if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
695 return JIM_ERR;
697 Jim_SetResultString(interp, JimGetFileType((int)sb.st_mode), -1);
698 return JIM_OK;
701 #ifdef HAVE_LSTAT
702 static int file_cmd_lstat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
704 struct stat sb;
706 if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
707 return JIM_ERR;
709 return StoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb);
711 #else
712 #define file_cmd_lstat file_cmd_stat
713 #endif
715 static int file_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
717 struct stat sb;
719 if (file_stat(interp, argv[0], &sb) != JIM_OK) {
720 return JIM_ERR;
722 return StoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb);
725 static const jim_subcmd_type file_command_table[] = {
726 { "atime",
727 "name",
728 file_cmd_atime,
731 /* Description: Last access time */
733 { "mtime",
734 "name ?time?",
735 file_cmd_mtime,
738 /* Description: Get or set last modification time */
740 { "copy",
741 "?-force? source dest",
742 file_cmd_copy,
745 /* Description: Copy source file to destination file */
747 { "dirname",
748 "name",
749 file_cmd_dirname,
752 /* Description: Directory part of the name */
754 { "rootname",
755 "name",
756 file_cmd_rootname,
759 /* Description: Name without any extension */
761 { "extension",
762 "name",
763 file_cmd_extension,
766 /* Description: Last extension including the dot */
768 { "tail",
769 "name",
770 file_cmd_tail,
773 /* Description: Last component of the name */
775 { "normalize",
776 "name",
777 file_cmd_normalize,
780 /* Description: Normalized path of name */
782 { "join",
783 "name ?name ...?",
784 file_cmd_join,
787 /* Description: Join multiple path components */
789 { "readable",
790 "name",
791 file_cmd_readable,
794 /* Description: Is file readable */
796 { "writable",
797 "name",
798 file_cmd_writable,
801 /* Description: Is file writable */
803 { "executable",
804 "name",
805 file_cmd_executable,
808 /* Description: Is file executable */
810 { "exists",
811 "name",
812 file_cmd_exists,
815 /* Description: Does file exist */
817 { "delete",
818 "?-force|--? name ...",
819 file_cmd_delete,
822 /* Description: Deletes the files or directories (must be empty unless -force) */
824 { "mkdir",
825 "dir ...",
826 file_cmd_mkdir,
829 /* Description: Creates the directories */
831 #ifdef HAVE_MKSTEMP
832 { "tempfile",
833 "?template?",
834 file_cmd_tempfile,
837 /* Description: Creates a temporary filename */
839 #endif
840 { "rename",
841 "?-force? source dest",
842 file_cmd_rename,
845 /* Description: Renames a file */
847 #if defined(HAVE_READLINK)
848 { "readlink",
849 "name",
850 file_cmd_readlink,
853 /* Description: Value of the symbolic link */
855 #endif
856 { "size",
857 "name",
858 file_cmd_size,
861 /* Description: Size of file */
863 { "stat",
864 "name ?var?",
865 file_cmd_stat,
868 /* Description: Returns results of stat, and may store in var array */
870 { "lstat",
871 "name ?var?",
872 file_cmd_lstat,
875 /* Description: Returns results of lstat, and may store in var array */
877 { "type",
878 "name",
879 file_cmd_type,
882 /* Description: Returns type of the file */
884 #ifdef HAVE_GETEUID
885 { "owned",
886 "name",
887 file_cmd_owned,
890 /* Description: Returns 1 if owned by the current owner */
892 #endif
893 { "isdirectory",
894 "name",
895 file_cmd_isdirectory,
898 /* Description: Returns 1 if name is a directory */
900 { "isfile",
901 "name",
902 file_cmd_isfile,
905 /* Description: Returns 1 if name is a file */
908 NULL
912 static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
914 const char *path;
916 if (argc != 2) {
917 Jim_WrongNumArgs(interp, 1, argv, "dirname");
918 return JIM_ERR;
921 path = Jim_String(argv[1]);
923 if (chdir(path) != 0) {
924 Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path,
925 strerror(errno));
926 return JIM_ERR;
928 return JIM_OK;
931 static int Jim_PwdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
933 const int cwd_len = 2048;
934 char *cwd = malloc(cwd_len);
936 if (getcwd(cwd, cwd_len) == NULL) {
937 Jim_SetResultString(interp, "Failed to get pwd", -1);
938 return JIM_ERR;
940 #if defined(__MINGW32__) || defined(_MSC_VER)
942 /* Try to keep backlashes out of paths */
943 char *p = cwd;
944 while ((p = strchr(p, '\\')) != NULL) {
945 *p++ = '/';
948 #endif
950 Jim_SetResultString(interp, cwd, -1);
952 free(cwd);
953 return JIM_OK;
956 int Jim_fileInit(Jim_Interp *interp)
958 if (Jim_PackageProvide(interp, "file", "1.0", JIM_ERRMSG))
959 return JIM_ERR;
961 Jim_CreateCommand(interp, "file", Jim_SubCmdProc, (void *)file_command_table, NULL);
962 Jim_CreateCommand(interp, "pwd", Jim_PwdCmd, NULL, NULL);
963 Jim_CreateCommand(interp, "cd", Jim_CdCmd, NULL, NULL);
964 return JIM_OK;