11 #include <sys/statvfs.h>
13 #include <sys/queue.h>
19 #include "test_driver.h"
22 * ###################################################
24 * # These functions are used to test libsysio. #
25 * # Eventually, there should be one of these for #
26 * # every function document in sysio.h #
27 * ###################################################
34 * Attempt to set the cwd by getting it out of the
35 * user's environment. If that does not work, set
44 DBG(5, sprintf(output, "%schdir: errno %d\n", output, errno));
51 DBG(3, sprintf(output, "Your current working directory is %s\n", wd));
57 int sysio_list(char *path)
61 struct dirent *buf, *dp;
66 fd = open(path, O_RDONLY);
82 DBG(5, sprintf(output, "About to call getdirentries\n"));
83 while ((cc = getdirentries(fd, (char *)buf, n, &base)) > 0) {
86 DBG(4, fprintf(outfp, "\t%s: ino %#08x off %#08x type %#08x\n",
88 (unsigned int)dp->d_ino,
89 (unsigned int)dp->d_off,
92 sprintf(output, "%s\n", dp->d_name);
94 dp = (struct dirent *)((char *)dp + dp->d_reclen);
97 printf("Out of inner loop\n");
104 DBG(2, sprintf(output, "cc barfed\n"));
112 if (close(fd) != 0) {
113 DBG(2,sprintf(output, "close barfed\n"));
122 last_ret_val = numfiles;
128 int sysio_mount(char *from, char *to)
134 char *fstype, *source, *opts, *target;
139 * Copy everything to a buffer we can modify.
141 s = buf = malloc(strlen(from) + 1);
148 (void )strcpy(s, from);
153 while (*s && *s == ' ' && *s == '\t')
159 while (*cp && *cp != ':' && *cp != ' ' && *cp != '\t')
161 if (fstype == cp || *cp != ':') {
162 DBG(1, sprintf(output, "%s: Missing FS type\n", from));
172 while (*s && *s == ' ' && *s == '\t')
178 while (*cp && *cp != ' ' && *cp != '\t')
181 DBG(1, sprintf(output, "%s: Missing source\n", from));
192 while (*s && *s == ' ' && *s == '\t')
198 while (*cp && *cp != ' ' && *cp != '\t')
201 DBG(1,sprintf(output, "%s: Missing target\n", to));
212 while (*s && *s == ' ' && *s == '\t')
218 while (*cp && *cp != ' ' && *cp != '\t')
227 err = mount(source, target, fstype, 0, opts);
238 int sysio_chdir(char *newdir)
241 if (chdir(newdir) != 0) {
246 buf = getcwd(NULL, 0);
253 DBG(4, sprintf(output, "New dir is %s\n", buf));
260 static mode_t get_mode(char *arg, int type, int start_mode);
266 * Change the permissions on a given file
268 * sysio_chmod <filename> <permissions>
271 int sysio_chmod(char *mode_arg, const char *path)
277 /* Get the current mode */
278 err = stat(path, &st);
280 /* Is the new mode symbolic? */
281 if (isalpha(mode_arg[0])) {
282 /* Could be specifying defines */
283 if (mode_arg[0] == 'S')
284 mode = get_mode(mode_arg, DEFINED, st.st_mode);
286 mode = get_mode(mode_arg, SYMBOLIC, st.st_mode);
288 mode = get_mode(mode_arg, NUMERIC, st.st_mode);
289 DBG(3,sprintf(output, "Using a mode of %o and a file of %s\n", mode, path));
292 DBG(2,sprintf(output, "Invalid mode\n"));
296 last_ret_val = chmod(path, mode);
303 #define USER_STATE 0 /* Specifies that the users are still being listed */
304 #define MODE_STATE_ADD 1
305 #define MODE_STATE_REMOVE 2
309 #define EXECUTE 00111
317 get_mode(char *arg, int type, int start_mode)
319 int i, j,digit, total;
321 int state = USER_STATE;
322 int len = strlen(arg);
323 unsigned int users = 0;
324 unsigned int modes = 0;
327 if (type == DEFINED) {
332 DBG(4, sprintf(output, "len is %d\n", len));
333 for (i=0; i < len; i++) {
336 DBG(3, sprintf(output, "Got mode word %s\n", curr_word));
337 digit = get_obj(curr_word);
339 DBG(2, sprintf(output, "Unable to understand mode arg %s\n",
346 curr_word[j++] = arg[i];
349 DBG(3, sprintf(output, "Got mode word %s\n", curr_word));
350 digit = get_obj(curr_word);
352 DBG(3, sprintf(output, "Unable to understand mode arg %s\n",
360 if (type == SYMBOLIC) {
361 for (i=0; i < len; i++) {
363 if (state == USER_STATE) {
375 users |= (OWNER|GROUP|OTHER);
378 state = MODE_STATE_ADD;
381 state = MODE_STATE_REMOVE;
404 if (state == MODE_STATE_ADD) {
405 return (start_mode | (users & modes));
407 return (start_mode & ~(users & modes));
411 /* Digits should be octal digits, so should convert */
413 for (i=0; i < len; i++) {
418 for (j=len-i-1; j >0; j--)
428 * Changes the ownership of the file. The new_id
429 * is of the format owner:group. Either the owner
430 * or the group may be omitted, but, in order to
431 * change the group, the : must preced the group.
433 int sysio_chown(char *new_id, char *file)
437 uid_t o_id=-1, g_id=-1;
439 int state = 0; /* Correspond to getting owner name */
441 len = strlen(new_id);
442 for (i=0; i < len; i++) {
444 if (new_id[i] == ':') {
447 group = malloc(strlen(new_id) -i +2);
448 state = 1; /* Now getting group name */
455 /* Getting owner name */
457 owner = malloc(strlen(new_id) +1 );
458 owner[i] = new_id[i];
461 group[j] = new_id[i];
470 /* Are the owner and/or group symbolic or numeric? */
472 if (isdigit(owner[0])) {
473 /* Numeric -- just convert */
474 o_id = (uid_t) atoi(owner);
477 /* No longer support non-numeric ids */
479 DBG(2, sprintf(output, "Error: non-numeric ids unsupported\n"));
487 if (isdigit(group[0])) {
488 /* Numeric -- just convert */
489 g_id = (uid_t) atoi(group);
491 /* Don't support group names either */
492 DBG(2, sprintf(output, "Error: non-numeric ids unsupported\n"));
497 /* Now issue the syscall */
498 DBG(4, sprintf(output, "Changing owner of file %s to %d (group %d)\n",
501 last_ret_val = chown(file, o_id, g_id);
506 int sysio_open(char *path, int flags)
508 last_ret_val = open(path, flags);
510 DBG(3, sprintf(output, "Returning with errno set to %s (ret val is %d)\n",
511 strerror(my_errno), (int)last_ret_val));
515 int sysio_open3(char *path, int flags, char *mode_arg)
519 /* Is the new mode symbolic? */
520 if (isalpha(mode_arg[0])) {
521 /* Could be specifying defines */
522 if (mode_arg[0] == 'S')
523 mode = get_mode(mode_arg, DEFINED, 0);
525 mode = get_mode(mode_arg, SYMBOLIC, 0);
527 mode = get_mode(mode_arg, NUMERIC, 0);
529 last_ret_val = open(path, flags, mode);
535 int sysio_close(int fd)
538 last_ret_val = close(fd);
543 int sysio_fcntl(int fd, struct cmd_map* cmdptr, char *arg)
545 int fd_new, index, cmd, flag;
550 cmdname = cmdptr->cmd_name;
554 fd_new = get_obj(arg);
555 last_ret_val = fcntl(fd, F_DUPFD, fd_new);
566 last_ret_val= fcntl(fd, cmd);
578 last_ret_val = fcntl(fd, cmd, flag);
587 /* Get the buffer to hold the lock structure */
588 index = get_obj(arg);
590 sprintf(output, "Unable to find buffer %s\n", arg+1);
594 buf = buflist[index];
596 sprintf(output, "Buffer at index %d (mapped by %s) is null\n",
601 last_ret_val = fcntl(fd, cmd, (struct flock *)buf);
605 /* THis should be impossible */
612 void print_stat(struct stat *st)
614 DBG(3, sprintf(output, "%sstruct stat: \n", output));
615 DBG(3, sprintf(output, "%s st_dev: %#16x\n", output, (unsigned int)st->st_dev));
616 DBG(3, sprintf(output, "%s st_ino: %#16x\n", output, (unsigned int) st->st_ino));
617 DBG(3, sprintf(output, "%s st_mode: %#16x\n", output, st->st_mode));
618 DBG(3, sprintf(output, "%s st_nlink: %#16x\n", output, (int)st->st_nlink));
619 DBG(3, sprintf(output, "%s st_uid: %#16x\n", output, st->st_uid));
620 DBG(3, sprintf(output, "%s st_gid: %#16x\n", output, st->st_gid));
621 DBG(3, sprintf(output, "%s st_rdev: %#16x\n", output, (int)st->st_rdev));
622 DBG(3, sprintf(output, "%s st_size: %#16x\n", output, (int) st->st_size));
623 DBG(3, sprintf(output, "%s st_blksize: %#16x\n", output, (int) st->st_blksize));
624 DBG(3, sprintf(output, "%s st_blocks: %#16x\n", output, (int) st->st_blocks));
625 DBG(3, sprintf(output, "%s st_atime: %#16x\n", output, (unsigned int) st->st_atime));
626 DBG(3, sprintf(output, "%s st_mtime: %#16x\n", output, (unsigned int) st->st_mtime));
627 DBG(3, sprintf(output, "%s st_ctime: %#16x", output, (unsigned int) st->st_ctime));
630 int sysio_fstat(int fd, void *buf)
633 struct stat *st = (struct stat *)buf;
645 int sysio_lstat(char *filename, void *buf)
648 struct stat *st = (struct stat *)buf;
649 err = lstat(filename, st);
661 int sysio_stat(char *filename, void *buf)
664 struct stat *st = (struct stat *)buf;
666 err = stat(filename, st);
678 int sysio_getdirentries(int fd, char *buf, size_t nbytes, off_t *basep)
683 err = getdirentries(fd, buf, nbytes, basep);
686 DBG(4, sprintf(output, "%sRead %d bytes\n", output, err));
688 dp = (struct dirent *)buf;
690 DBG(3, sprintf(output, "%s\t%s: ino %llu off %llu len %x type %c\n",
693 (unsigned long long )dp->d_ino,
694 (unsigned long long )dp->d_off,
698 dp = (struct dirent *)((char *)dp + dp->d_reclen);
706 int sysio_mkdir(char *path, char *mode_arg)
712 /* Is the new mode symbolic? */
713 if (isalpha(mode_arg[0])) {
714 /* Could be specifying defines */
715 if (mode_arg[0] == 'S')
716 mode = get_mode(mode_arg, DEFINED, st.st_mode);
718 mode = get_mode(mode_arg, SYMBOLIC, st.st_mode);
720 mode = get_mode(mode_arg, NUMERIC, st.st_mode);
722 DBG(3, sprintf(output, "Using a mode of %o and a file of %s\n", mode, path));
725 DBG(2, sprintf(output, "Invalid mode\n"));
729 err = mkdir(path, mode);
736 int sysio_creat(char *path, char *mode_arg)
741 /* Is the new mode symbolic? */
742 if (isalpha(mode_arg[0])) {
743 /* Could be specifying defines */
744 if (mode_arg[0] == 'S')
745 mode = get_mode(mode_arg, DEFINED, 0);
747 mode = get_mode(mode_arg, SYMBOLIC, 0);
749 mode = get_mode(mode_arg, NUMERIC, 0);
751 DBG(3, sprintf(output, "Using a mode of %o and a file of %s\n", mode, path));
754 DBG(2, sprintf(output, "Invalid mode\n"));
758 err = creat(path, mode);
764 void print_statvfs(struct statvfs *st)
766 DBG(3, sprintf(output, "%sstruct statvfs: \n", output));
767 DBG(3, sprintf(output, "%s f_bsize: %x\n", output, (unsigned int) st->f_bsize));
768 DBG(3, sprintf(output, "%s f_frsize: %x\n", output, (unsigned int) st->f_frsize));
769 DBG(3, sprintf(output, "%s f_blocks: %x\n", output, (unsigned int) st->f_blocks));
770 DBG(3, sprintf(output, "%s f_bfree: %x\n", output, (unsigned int) st->f_bfree));
771 DBG(3, sprintf(output, "%s f_bavail: %x\n", output, (unsigned int) st->f_bavail));
772 DBG(3, sprintf(output, "%s f_files: %x\n", output, (unsigned int) st->f_files));
773 DBG(3, sprintf(output, "%s f_ffree: %x\n", output, (unsigned int) st->f_ffree));
774 DBG(3, sprintf(output, "%s f_favail: %x\n", output, (unsigned int) st->f_favail));
775 DBG(3, sprintf(output, "%s f_files: %x\n", output, (unsigned int) st->f_files));
776 #if (__GLIBC__ == 2 && __GLIBC_MINOR__ == 1)
777 DBG(3, sprintf(output, "%s f_fsid: %x\n", output, (unsigned int) st->f_fsid.__val[1]));
779 DBG(3, sprintf(output, "%s f_fsid: %x\n", output, (unsigned int) st->f_fsid));
781 DBG(3, sprintf(output, "%s f_flag: %x\n", output, (unsigned int) st->f_flag));
782 DBG(3, sprintf(output, "%s f_fnamemax: %x\n", output, (unsigned int) st->f_namemax));
786 int sysio_statvfs(char *filename, void *buf)
789 struct statvfs *st = (struct statvfs *)buf;
791 err = statvfs(filename, st);
793 my_perror("statvfs");
803 int sysio_fstatvfs(int fd, void *buf)
806 struct statvfs *st = (struct statvfs *)buf;
808 err = fstatvfs(fd, st);
810 my_perror("fstatvfs");
820 int sysio_umask(char *mode_arg)
824 /* Is the new mode symbolic? */
825 if (isalpha(mode_arg[0])) {
826 /* Could be specifying defines */
827 if (mode_arg[0] == 'S')
828 mode = get_mode(mode_arg, DEFINED, 0);
830 mode = get_mode(mode_arg, SYMBOLIC, 0);
832 mode = get_mode(mode_arg, NUMERIC, 0);
834 last_ret_val = umask(mode);
839 int sysio_mknod(char *path, char *mode_arg, dev_t dev)
844 mode = get_obj(mode_arg);
847 DBG(2,sprintf(output, "Cant get mode from %s\n", mode_arg));
848 fprintf(stderr, "Cant get mode from %s\n", mode_arg);
852 err = mknod(path, (mode_t) mode, dev);