Debian Codegraph @

unix/tclUnixTest.c

raw
1 /*
2 * tclUnixTest.c --
3 *
4 * Contains platform specific test commands for the Unix platform.
5 *
6 * Copyright (c) 1996-1997 Sun Microsystems, Inc.
7 * Copyright (c) 1998 by Scriptics Corporation.
8 *
9 * See the file "license.terms" for information on usage and redistribution of
10 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 */
12
13 #ifndef USE_TCL_STUBS
14 # define USE_TCL_STUBS
15 #endif
16 #include "tclInt.h"
17
18 /*
19 * The headers are needed for the testalarm command that verifies the use of
20 * SA_RESTART in signal handlers.
21 */
22
23 #include <signal.h>
24 #include <sys/resource.h>
25
26 /*
27 * The following macros convert between TclFile's and fd's. The conversion
28 * simple involves shifting fd's up by one to ensure that no valid fd is ever
29 * the same as NULL. Note that this code is duplicated from tclUnixPipe.c
30 */
31
32 #define MakeFile(fd) ((TclFile)INT2PTR(((int)(fd))+1))
33 #define GetFd(file) (PTR2INT(file)-1)
34
35 /*
36 * The stuff below is used to keep track of file handlers created and
37 * exercised by the "testfilehandler" command.
38 */
39
40 typedef struct {
41 TclFile readFile; /* File handle for reading from the pipe. NULL
42 * means pipe doesn't exist yet. */
43 TclFile writeFile; /* File handle for writing from the pipe. */
44 int readCount; /* Number of times the file handler for this
45 * file has triggered and the file was
46 * readable. */
47 int writeCount; /* Number of times the file handler for this
48 * file has triggered and the file was
49 * writable. */
50 } Pipe;
51
52 #define MAX_PIPES 10
53 static Pipe testPipes[MAX_PIPES];
54
55 /*
56 * The stuff below is used by the testalarm and testgotsig ommands.
57 */
58
59 static const char *gotsig = "0";
60
61 /*
62 * Forward declarations of functions defined later in this file:
63 */
64
65 static Tcl_CmdProc TestalarmCmd;
66 static Tcl_ObjCmdProc TestchmodCmd;
67 static Tcl_CmdProc TestfilehandlerCmd;
68 static Tcl_CmdProc TestfilewaitCmd;
69 static Tcl_ObjCmdProc TestfindexecutableCmd;
70 static Tcl_ObjCmdProc TestforkCmd;
71 static Tcl_CmdProc TestgetdefencdirCmd;
72 static Tcl_CmdProc TestgetopenfileCmd;
73 static Tcl_CmdProc TestgotsigCmd;
74 static Tcl_CmdProc TestsetdefencdirCmd;
75 static Tcl_FileProc TestFileHandlerProc;
76 static void AlarmHandler(int signum);
77
78 /*
79 *----------------------------------------------------------------------
80 *
81 * TclplatformtestInit --
82 *
83 * Defines commands that test platform specific functionality for Unix
84 * platforms.
85 *
86 * Results:
87 * A standard Tcl result.
88 *
89 * Side effects:
90 * Defines new commands.
91 *
92 *----------------------------------------------------------------------
93 */
94
95 int
96 TclplatformtestInit(
97 Tcl_Interp *interp) /* Interpreter to add commands to. */
98 {
99 Tcl_CreateObjCommand(interp, "testchmod", TestchmodCmd,
100 NULL, NULL);
101 Tcl_CreateCommand(interp, "testfilehandler", TestfilehandlerCmd,
102 NULL, NULL);
103 Tcl_CreateCommand(interp, "testfilewait", TestfilewaitCmd,
104 NULL, NULL);
105 Tcl_CreateObjCommand(interp, "testfindexecutable", TestfindexecutableCmd,
106 NULL, NULL);
107 Tcl_CreateObjCommand(interp, "testfork", TestforkCmd,
108 NULL, NULL);
109 Tcl_CreateCommand(interp, "testgetopenfile", TestgetopenfileCmd,
110 NULL, NULL);
111 Tcl_CreateCommand(interp, "testgetdefenc", TestgetdefencdirCmd,
112 NULL, NULL);
113 Tcl_CreateCommand(interp, "testsetdefenc", TestsetdefencdirCmd,
114 NULL, NULL);
115 Tcl_CreateCommand(interp, "testalarm", TestalarmCmd,
116 NULL, NULL);
117 Tcl_CreateCommand(interp, "testgotsig", TestgotsigCmd,
118 NULL, NULL);
119 return TCL_OK;
120 }
121
122 /*
123 *----------------------------------------------------------------------
124 *
125 * TestfilehandlerCmd --
126 *
127 * This function implements the "testfilehandler" command. It is used to
128 * test Tcl_CreateFileHandler, Tcl_DeleteFileHandler, and TclWaitForFile.
129 *
130 * Results:
131 * A standard Tcl result.
132 *
133 * Side effects:
134 * None.
135 *
136 *----------------------------------------------------------------------
137 */
138
139 static int
140 TestfilehandlerCmd(
141 void *clientData, /* Not used. */
142 Tcl_Interp *interp, /* Current interpreter. */
143 int argc, /* Number of arguments. */
144 const char **argv) /* Argument strings. */
145 {
146 Pipe *pipePtr;
147 int i, mask, timeout;
148 static int initialized = 0;
149 char buffer[4000];
150 TclFile file;
151
152 /*
153 * NOTE: When we make this code work on Windows also, the following
154 * variable needs to be made Unix-only.
155 */
156
157 if (!initialized) {
158 for (i = 0; i < MAX_PIPES; i++) {
159 testPipes[i].readFile = NULL;
160 }
161 initialized = 1;
162 }
163
164 if (argc < 2) {
165 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
166 " option ... \"", (char *)NULL);
167 return TCL_ERROR;
168 }
169 pipePtr = NULL;
170 if (argc >= 3) {
171 if (Tcl_GetInt(interp, argv[2], &i) != TCL_OK) {
172 return TCL_ERROR;
173 }
174 if (i >= MAX_PIPES) {
175 Tcl_AppendResult(interp, "bad index ", argv[2], (char *)NULL);
176 return TCL_ERROR;
177 }
178 pipePtr = &testPipes[i];
179 }
180
181 if (strcmp(argv[1], "close") == 0) {
182 for (i = 0; i < MAX_PIPES; i++) {
183 if (testPipes[i].readFile != NULL) {
184 TclpCloseFile(testPipes[i].readFile);
185 testPipes[i].readFile = NULL;
186 TclpCloseFile(testPipes[i].writeFile);
187 testPipes[i].writeFile = NULL;
188 }
189 }
190 } else if (strcmp(argv[1], "clear") == 0) {
191 if (argc != 3) {
192 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
193 argv[0], " clear index\"", (char *)NULL);
194 return TCL_ERROR;
195 }
196 pipePtr->readCount = pipePtr->writeCount = 0;
197 } else if (strcmp(argv[1], "counts") == 0) {
198 char buf[TCL_INTEGER_SPACE * 2];
199
200 if (argc != 3) {
201 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
202 argv[0], " counts index\"", (char *)NULL);
203 return TCL_ERROR;
204 }
205 snprintf(buf, sizeof(buf), "%d %d", pipePtr->readCount, pipePtr->writeCount);
206 Tcl_AppendResult(interp, buf, (char *)NULL);
207 } else if (strcmp(argv[1], "create") == 0) {
208 if (argc != 5) {
209 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
210 argv[0], " create index readMode writeMode\"", (char *)NULL);
211 return TCL_ERROR;
212 }
213 if (pipePtr->readFile == NULL) {
214 if (!TclpCreatePipe(&pipePtr->readFile, &pipePtr->writeFile)) {
215 Tcl_AppendResult(interp, "couldn't open pipe: ",
216 Tcl_PosixError(interp), (char *)NULL);
217 return TCL_ERROR;
218 }
219 #ifdef O_NONBLOCK
220 fcntl(GetFd(pipePtr->readFile), F_SETFL, O_NONBLOCK);
221 fcntl(GetFd(pipePtr->writeFile), F_SETFL, O_NONBLOCK);
222 #else
223 Tcl_AppendResult(interp, "can't make pipes non-blocking",
224 (char *)NULL);
225 return TCL_ERROR;
226 #endif
227 }
228 pipePtr->readCount = 0;
229 pipePtr->writeCount = 0;
230
231 if (strcmp(argv[3], "readable") == 0) {
232 Tcl_CreateFileHandler(GetFd(pipePtr->readFile), TCL_READABLE,
233 TestFileHandlerProc, pipePtr);
234 } else if (strcmp(argv[3], "off") == 0) {
235 Tcl_DeleteFileHandler(GetFd(pipePtr->readFile));
236 } else if (strcmp(argv[3], "disabled") == 0) {
237 Tcl_CreateFileHandler(GetFd(pipePtr->readFile), 0,
238 TestFileHandlerProc, pipePtr);
239 } else {
240 Tcl_AppendResult(interp, "bad read mode \"", argv[3], "\"", (char *)NULL);
241 return TCL_ERROR;
242 }
243 if (strcmp(argv[4], "writable") == 0) {
244 Tcl_CreateFileHandler(GetFd(pipePtr->writeFile), TCL_WRITABLE,
245 TestFileHandlerProc, pipePtr);
246 } else if (strcmp(argv[4], "off") == 0) {
247 Tcl_DeleteFileHandler(GetFd(pipePtr->writeFile));
248 } else if (strcmp(argv[4], "disabled") == 0) {
249 Tcl_CreateFileHandler(GetFd(pipePtr->writeFile), 0,
250 TestFileHandlerProc, pipePtr);
251 } else {
252 Tcl_AppendResult(interp, "bad read mode \"", argv[4], "\"", (char *)NULL);
253 return TCL_ERROR;
254 }
255 } else if (strcmp(argv[1], "empty") == 0) {
256 if (argc != 3) {
257 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
258 argv[0], " empty index\"", (char *)NULL);
259 return TCL_ERROR;
260 }
261
262 while (read(GetFd(pipePtr->readFile), buffer, 4000) > 0) {
263 /* Empty loop body. */
264 }
265 } else if (strcmp(argv[1], "fill") == 0) {
266 if (argc != 3) {
267 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
268 argv[0], " fill index\"", (char *)NULL);
269 return TCL_ERROR;
270 }
271
272 memset(buffer, 'a', 4000);
273 while (write(GetFd(pipePtr->writeFile), buffer, 4000) > 0) {
274 /* Empty loop body. */
275 }
276 } else if (strcmp(argv[1], "fillpartial") == 0) {
277 char buf[TCL_INTEGER_SPACE];
278
279 if (argc != 3) {
280 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
281 argv[0], " fillpartial index\"", (char *)NULL);
282 return TCL_ERROR;
283 }
284
285 memset(buffer, 'b', 10);
286 TclFormatInt(buf, write(GetFd(pipePtr->writeFile), buffer, 10));
287 Tcl_AppendResult(interp, buf, (char *)NULL);
288 } else if (strcmp(argv[1], "oneevent") == 0) {
289 Tcl_DoOneEvent(TCL_FILE_EVENTS|TCL_DONT_WAIT);
290 } else if (strcmp(argv[1], "wait") == 0) {
291 if (argc != 5) {
292 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
293 argv[0], " wait index readable|writable timeout\"", NULL);
294 return TCL_ERROR;
295 }
296 if (pipePtr->readFile == NULL) {
297 Tcl_AppendResult(interp, "pipe ", argv[2], " doesn't exist", (char *)NULL);
298 return TCL_ERROR;
299 }
300 if (strcmp(argv[3], "readable") == 0) {
301 mask = TCL_READABLE;
302 file = pipePtr->readFile;
303 } else {
304 mask = TCL_WRITABLE;
305 file = pipePtr->writeFile;
306 }
307 if (Tcl_GetInt(interp, argv[4], &timeout) != TCL_OK) {
308 return TCL_ERROR;
309 }
310 i = TclUnixWaitForFile(GetFd(file), mask, timeout);
311 if (i & TCL_READABLE) {
312 Tcl_AppendElement(interp, "readable");
313 }
314 if (i & TCL_WRITABLE) {
315 Tcl_AppendElement(interp, "writable");
316 }
317 } else if (strcmp(argv[1], "windowevent") == 0) {
318 Tcl_DoOneEvent(TCL_WINDOW_EVENTS|TCL_DONT_WAIT);
319 } else {
320 Tcl_AppendResult(interp, "bad option \"", argv[1],
321 "\": must be close, clear, counts, create, empty, fill, "
322 "fillpartial, oneevent, wait, or windowevent", (char *)NULL);
323 return TCL_ERROR;
324 }
325 return TCL_OK;
326 }
327
328 static void
329 TestFileHandlerProc(
330 void *clientData, /* Points to a Pipe structure. */
331 int mask) /* Indicates which events happened:
332 * TCL_READABLE or TCL_WRITABLE. */
333 {
334 Pipe *pipePtr = (Pipe *)clientData;
335
336 if (mask & TCL_READABLE) {
337 pipePtr->readCount++;
338 }
339 if (mask & TCL_WRITABLE) {
340 pipePtr->writeCount++;
341 }
342 }
343
344 /*
345 *----------------------------------------------------------------------
346 *
347 * TestfilewaitCmd --
348 *
349 * This function implements the "testfilewait" command. It is used to
350 * test TclUnixWaitForFile.
351 *
352 * Results:
353 * A standard Tcl result.
354 *
355 * Side effects:
356 * None.
357 *
358 *----------------------------------------------------------------------
359 */
360
361 static int
362 TestfilewaitCmd(
363 void *clientData, /* Not used. */
364 Tcl_Interp *interp, /* Current interpreter. */
365 int argc, /* Number of arguments. */
366 const char **argv) /* Argument strings. */
367 {
368 int mask, result, timeout;
369 Tcl_Channel channel;
370 int fd;
371 void *data;
372
373 if (argc != 4) {
374 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
375 " file readable|writable|both timeout\"", NULL);
376 return TCL_ERROR;
377 }
378 channel = Tcl_GetChannel(interp, argv[1], NULL);
379 if (channel == NULL) {
380 return TCL_ERROR;
381 }
382 if (strcmp(argv[2], "readable") == 0) {
383 mask = TCL_READABLE;
384 } else if (strcmp(argv[2], "writable") == 0){
385 mask = TCL_WRITABLE;
386 } else if (strcmp(argv[2], "both") == 0){
387 mask = TCL_WRITABLE|TCL_READABLE;
388 } else {
389 Tcl_AppendResult(interp, "bad argument \"", argv[2],
390 "\": must be readable, writable, or both", (char *)NULL);
391 return TCL_ERROR;
392 }
393 if (Tcl_GetChannelHandle(channel,
394 (mask & TCL_READABLE) ? TCL_READABLE : TCL_WRITABLE,
395 (void **) &data) != TCL_OK) {
396 Tcl_AppendResult(interp, "couldn't get channel file", (char *)NULL);
397 return TCL_ERROR;
398 }
399 fd = PTR2INT(data);
400 if (Tcl_GetInt(interp, argv[3], &timeout) != TCL_OK) {
401 return TCL_ERROR;
402 }
403 result = TclUnixWaitForFile(fd, mask, timeout);
404 if (result & TCL_READABLE) {
405 Tcl_AppendElement(interp, "readable");
406 }
407 if (result & TCL_WRITABLE) {
408 Tcl_AppendElement(interp, "writable");
409 }
410 return TCL_OK;
411 }
412
413 /*
414 *----------------------------------------------------------------------
415 *
416 * TestfindexecutableCmd --
417 *
418 * This function implements the "testfindexecutable" command. It is used
419 * to test TclpFindExecutable.
420 *
421 * Results:
422 * A standard Tcl result.
423 *
424 * Side effects:
425 * None.
426 *
427 *----------------------------------------------------------------------
428 */
429
430 static int
431 TestfindexecutableCmd(
432 void *clientData, /* Not used. */
433 Tcl_Interp *interp, /* Current interpreter. */
434 int objc, /* Number of arguments. */
435 Tcl_Obj *const *objv) /* Argument strings. */
436 {
437 Tcl_Obj *saveName;
438
439 if (objc != 2) {
440 Tcl_WrongNumArgs(interp, 1, objv, "argv0");
441 return TCL_ERROR;
442 }
443
444 saveName = TclGetObjNameOfExecutable();
445 Tcl_IncrRefCount(saveName);
446
447 TclpFindExecutable(Tcl_GetString(objv[1]));
448 Tcl_SetObjResult(interp, TclGetObjNameOfExecutable());
449
450 TclSetObjNameOfExecutable(saveName, NULL);
451 Tcl_DecrRefCount(saveName);
452 return TCL_OK;
453 }
454
455 /*
456 *----------------------------------------------------------------------
457 *
458 * TestgetopenfileCmd --
459 *
460 * This function implements the "testgetopenfile" command. It is used to
461 * get a FILE * value from a registered channel.
462 *
463 * Results:
464 * A standard Tcl result.
465 *
466 * Side effects:
467 * None.
468 *
469 *----------------------------------------------------------------------
470 */
471
472 static int
473 TestgetopenfileCmd(
474 void *clientData, /* Not used. */
475 Tcl_Interp *interp, /* Current interpreter. */
476 int argc, /* Number of arguments. */
477 const char **argv) /* Argument strings. */
478 {
479 void *filePtr;
480
481 if (argc != 3) {
482 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
483 " channelName forWriting\"", (void *)NULL);
484 return TCL_ERROR;
485 }
486 if (Tcl_GetOpenFile(interp, argv[1], atoi(argv[2]), 1, &filePtr)
487 == TCL_ERROR) {
488 return TCL_ERROR;
489 }
490 if (filePtr == NULL) {
491 Tcl_AppendResult(interp,
492 "Tcl_GetOpenFile succeeded but FILE * NULL!", (void *)NULL);
493 return TCL_ERROR;
494 }
495 return TCL_OK;
496 }
497
498 /*
499 *----------------------------------------------------------------------
500 *
501 * TestsetdefencdirCmd --
502 *
503 * This function implements the "testsetdefenc" command. It is used to
504 * test Tcl_SetDefaultEncodingDir().
505 *
506 * Results:
507 * A standard Tcl result.
508 *
509 * Side effects:
510 * None.
511 *
512 *----------------------------------------------------------------------
513 */
514
515 static int
516 TestsetdefencdirCmd(
517 void *clientData, /* Not used. */
518 Tcl_Interp *interp, /* Current interpreter. */
519 int argc, /* Number of arguments. */
520 const char **argv) /* Argument strings. */
521 {
522 if (argc != 2) {
523 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
524 " defaultDir\"", (void *)NULL);
525 return TCL_ERROR;
526 }
527
528 Tcl_SetDefaultEncodingDir(argv[1]);
529 return TCL_OK;
530 }
531
532 /*
533 *----------------------------------------------------------------------
534 *
535 * TestforkCmd --
536 *
537 * This function implements the "testfork" command. It is used to
538 * fork the Tcl process for specific test cases.
539 *
540 * Results:
541 * A standard Tcl result.
542 *
543 * Side effects:
544 * None.
545 *
546 *----------------------------------------------------------------------
547 */
548
549 static int
550 TestforkCmd(
551 void *clientData, /* Not used. */
552 Tcl_Interp *interp, /* Current interpreter. */
553 int objc, /* Number of arguments. */
554 Tcl_Obj *const *objv) /* Argument strings. */
555 {
556 pid_t pid;
557
558 if (objc != 1) {
559 Tcl_WrongNumArgs(interp, 1, objv, "");
560 return TCL_ERROR;
561 }
562 pid = fork();
563 if (pid == -1) {
564 Tcl_AppendResult(interp,
565 "Cannot fork", (char *)NULL);
566 return TCL_ERROR;
567 }
568 /* Only needed when pthread_atfork is not present,
569 * should not hurt otherwise. */
570 if (pid==0) {
571 Tcl_InitNotifier();
572 }
573 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(pid));
574 return TCL_OK;
575 }
576
577 /*
578 *----------------------------------------------------------------------
579 *
580 * TestgetdefencdirCmd --
581 *
582 * This function implements the "testgetdefenc" command. It is used to
583 * test Tcl_GetDefaultEncodingDir().
584 *
585 * Results:
586 * A standard Tcl result.
587 *
588 * Side effects:
589 * None.
590 *
591 *----------------------------------------------------------------------
592 */
593
594 static int
595 TestgetdefencdirCmd(
596 void *clientData, /* Not used. */
597 Tcl_Interp *interp, /* Current interpreter. */
598 int argc, /* Number of arguments. */
599 const char **argv) /* Argument strings. */
600 {
601 if (argc != 1) {
602 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], (void *)NULL);
603 return TCL_ERROR;
604 }
605
606 Tcl_AppendResult(interp, Tcl_GetDefaultEncodingDir(), (void *)NULL);
607 return TCL_OK;
608 }
609
610 /*
611 *----------------------------------------------------------------------
612 *
613 * TestalarmCmd --
614 *
615 * Test that EINTR is handled correctly by generating and handling a
616 * signal. This requires using the SA_RESTART flag when registering the
617 * signal handler.
618 *
619 * Results:
620 * None.
621 *
622 * Side Effects:
623 * Sets up an signal and async handlers.
624 *
625 *----------------------------------------------------------------------
626 */
627
628 static int
629 TestalarmCmd(
630 void *clientData, /* Not used. */
631 Tcl_Interp *interp, /* Current interpreter. */
632 int argc, /* Number of arguments. */
633 const char **argv) /* Argument strings. */
634 {
635 #ifdef SA_RESTART
636 unsigned int sec;
637 struct sigaction action;
638
639 if (argc > 1) {
640 Tcl_GetInt(interp, argv[1], (int *)&sec);
641 } else {
642 sec = 1;
643 }
644
645 /*
646 * Setup the signal handling that automatically retries any interrupted
647 * I/O system calls.
648 */
649
650 action.sa_handler = AlarmHandler;
651 memset((void *) &action.sa_mask, 0, sizeof(sigset_t));
652 action.sa_flags = SA_RESTART;
653
654 if (sigaction(SIGALRM, &action, NULL) < 0) {
655 Tcl_AppendResult(interp, "sigaction: ", Tcl_PosixError(interp), (char *)NULL);
656 return TCL_ERROR;
657 }
658 (void) alarm(sec);
659 return TCL_OK;
660 #else
661 Tcl_AppendResult(interp,
662 "warning: sigaction SA_RESTART not support on this platform",
663 (char *)NULL);
664 return TCL_ERROR;
665 #endif
666 }
667
668 /*
669 *----------------------------------------------------------------------
670 *
671 * AlarmHandler --
672 *
673 * Signal handler for the alarm command.
674 *
675 * Results:
676 * None.
677 *
678 * Side effects:
679 * Calls the Tcl Async handler.
680 *
681 *----------------------------------------------------------------------
682 */
683
684 static void
685 AlarmHandler(
686 int signum)
687 {
688 gotsig = "1";
689 }
690
691 /*
692 *----------------------------------------------------------------------
693 *
694 * TestgotsigCmd --
695 *
696 * Verify the signal was handled after the testalarm command.
697 *
698 * Results:
699 * None.
700 *
701 * Side Effects:
702 * Resets the value of gotsig back to '0'.
703 *
704 *----------------------------------------------------------------------
705 */
706
707 static int
708 TestgotsigCmd(
709 void *clientData, /* Not used. */
710 Tcl_Interp *interp, /* Current interpreter. */
711 int argc, /* Number of arguments. */
712 const char **argv) /* Argument strings. */
713 {
714 Tcl_AppendResult(interp, gotsig, (char *)NULL);
715 gotsig = "0";
716 return TCL_OK;
717 }
718
719 /*
720 *---------------------------------------------------------------------------
721 *
722 * TestchmodCmd --
723 *
724 * Implements the "testchmod" cmd. Used when testing "file" command.
725 * The only attribute used by the Windows platform is the user write
726 * flag; if this is not set, the file is made read-only. Otherwise, the
727 * file is made read-write.
728 *
729 * Results:
730 * A standard Tcl result.
731 *
732 * Side effects:
733 * Changes permissions of specified files.
734 *
735 *---------------------------------------------------------------------------
736 */
737
738 static int
739 TestchmodCmd(
740 void *dummy, /* Not used. */
741 Tcl_Interp *interp, /* Current interpreter. */
742 int objc, /* Number of arguments. */
743 Tcl_Obj *const *objv) /* Argument strings. */
744 {
745 int i, mode;
746 Tcl_DString ds;
747
748 if (objc < 2) {
749 Tcl_WrongNumArgs(interp, 1, objv, "mode file ?file ...?");
750 return TCL_ERROR;
751 }
752
753 if (Tcl_GetIntFromObj(interp, objv[1], &mode) != TCL_OK) {
754 return TCL_ERROR;
755 }
756
757 Tcl_DStringInit(&ds);
758 for (i = 2; i < objc; i++) {
759 Tcl_DString buffer;
760 const char *translated;
761
762 translated = Tcl_TranslateFileName(interp, Tcl_GetString(objv[i]), &buffer);
763 if (translated == NULL) {
764 Tcl_DStringFree(&ds);
765 return TCL_ERROR;
766 }
767 Tcl_UtfToExternalDString(NULL, translated, -1, &ds);
768 if (chmod(Tcl_DStringValue(&ds), mode) != 0) {
769 Tcl_AppendResult(interp, translated, ": ", Tcl_PosixError(interp),
770 (char *)NULL);
771 Tcl_DStringFree(&ds);
772 return TCL_ERROR;
773 }
774 Tcl_DStringFree(&buffer);
775 Tcl_DStringSetLength(&ds, 0);
776 }
777 Tcl_DStringFree(&ds);
778 return TCL_OK;
779 }
780
781 /*
782 * Local Variables:
783 * mode: c
784 * c-basic-offset: 4
785 * fill-column: 78
786 * tab-width: 8
787 * End:
788 */