11 #include <sys/statvfs.h>
13 #include <sys/queue.h>
16 #include <sys/mount.h>
21 #include "test_driver.h"
24 * ###################################################
26 * # These functions are used to test libsysio. #
27 * # Eventually, there should be one of these for #
28 * # every function document in sysio.h #
29 * ###################################################
36 * Attempt to set the cwd by getting it out of the
37 * user's environment. If that does not work, set
46 DBG(5, sprintf(output, "%schdir: errno %d\n", output, errno));
53 DBG(3, sprintf(output, "Your current working directory is %s\n", wd));
59 int sysio_list(char *path)
63 struct dirent *buf, *dp;
68 fd = open(path, O_RDONLY);
84 DBG(5, sprintf(output, "About to call getdirentries\n"));
85 while ((cc = getdirentries(fd, (char *)buf, n, &base)) > 0) {
88 DBG(4, fprintf(outfp, "\t%s: ino %#08x off %#08x type %#08x\n",
90 (unsigned int)dp->d_ino,
91 (unsigned int)dp->d_off,
94 sprintf(output, "%s\n", dp->d_name);
96 dp = (struct dirent *)((char *)dp + dp->d_reclen);
99 printf("Out of inner loop\n");
106 DBG(2, sprintf(output, "cc barfed\n"));
114 if (close(fd) != 0) {
115 DBG(2,sprintf(output, "close barfed\n"));
124 last_ret_val = numfiles;
130 int sysio_mount(char *from, char *to)
136 char *fstype, *source, *opts, *target;
141 * Copy everything to a buffer we can modify.
143 s = buf = malloc(strlen(from) + 1);
150 (void )strcpy(s, from);
155 while (*s && *s == ' ' && *s == '\t')
161 while (*cp && *cp != ':' && *cp != ' ' && *cp != '\t')
163 if (fstype == cp || *cp != ':') {
164 DBG(1, sprintf(output, "%s: Missing FS type\n", from));
174 while (*s && *s == ' ' && *s == '\t')
180 while (*cp && *cp != ' ' && *cp != '\t')
183 DBG(1, sprintf(output, "%s: Missing source\n", from));
194 while (*s && *s == ' ' && *s == '\t')
200 while (*cp && *cp != ' ' && *cp != '\t')
203 DBG(1,sprintf(output, "%s: Missing target\n", to));
214 while (*s && *s == ' ' && *s == '\t')
220 while (*cp && *cp != ' ' && *cp != '\t')
229 err = mount(source, target, fstype, 0, opts);
240 int sysio_chdir(char *newdir)
243 if (chdir(newdir) != 0) {
248 buf = getcwd(NULL, 0);
255 DBG(4, sprintf(output, "New dir is %s\n", buf));
262 static mode_t get_mode(char *arg, int type, int start_mode);
268 * Change the permissions on a given file
270 * sysio_chmod <filename> <permissions>
273 int sysio_chmod(char *mode_arg, const char *path)
279 /* Get the current mode */
280 err = stat(path, &st);
282 /* Is the new mode symbolic? */
283 if (isalpha(mode_arg[0])) {
284 /* Could be specifying defines */
285 if (mode_arg[0] == 'S')
286 mode = get_mode(mode_arg, DEFINED, st.st_mode);
288 mode = get_mode(mode_arg, SYMBOLIC, st.st_mode);
290 mode = get_mode(mode_arg, NUMERIC, st.st_mode);
291 DBG(3,sprintf(output, "Using a mode of %o and a file of %s\n", mode, path));
294 DBG(2,sprintf(output, "Invalid mode\n"));
298 last_ret_val = chmod(path, mode);
305 #define USER_STATE 0 /* Specifies that the users are still being listed */
306 #define MODE_STATE_ADD 1
307 #define MODE_STATE_REMOVE 2
311 #define EXECUTE 00111
319 get_mode(char *arg, int type, int start_mode)
321 int i, j,digit, total;
323 int state = USER_STATE;
324 int len = strlen(arg);
325 unsigned int users = 0;
326 unsigned int modes = 0;
329 if (type == DEFINED) {
334 DBG(4, sprintf(output, "len is %d\n", len));
335 for (i=0; i < len; i++) {
338 DBG(3, sprintf(output, "Got mode word %s\n", curr_word));
339 digit = get_obj(curr_word);
341 DBG(2, sprintf(output, "Unable to understand mode arg %s\n",
348 curr_word[j++] = arg[i];
351 DBG(3, sprintf(output, "Got mode word %s\n", curr_word));
352 digit = get_obj(curr_word);
354 DBG(3, sprintf(output, "Unable to understand mode arg %s\n",
362 if (type == SYMBOLIC) {
363 for (i=0; i < len; i++) {
365 if (state == USER_STATE) {
377 users |= (OWNER|GROUP|OTHER);
380 state = MODE_STATE_ADD;
383 state = MODE_STATE_REMOVE;
406 if (state == MODE_STATE_ADD) {
407 return (start_mode | (users & modes));
409 return (start_mode & ~(users & modes));
413 /* Digits should be octal digits, so should convert */
415 for (i=0; i < len; i++) {
420 for (j=len-i-1; j >0; j--)
430 * Changes the ownership of the file. The new_id
431 * is of the format owner:group. Either the owner
432 * or the group may be omitted, but, in order to
433 * change the group, the : must preced the group.
435 int sysio_chown(char *new_id, char *file)
439 uid_t o_id=-1, g_id=-1;
441 int state = 0; /* Correspond to getting owner name */
443 len = strlen(new_id);
444 for (i=0; i < len; i++) {
446 if (new_id[i] == ':') {
449 group = malloc(strlen(new_id) -i +2);
450 state = 1; /* Now getting group name */
457 /* Getting owner name */
459 owner = malloc(strlen(new_id) +1 );
460 owner[i] = new_id[i];
463 group[j] = new_id[i];
472 /* Are the owner and/or group symbolic or numeric? */
474 if (isdigit(owner[0])) {
475 /* Numeric -- just convert */
476 o_id = (uid_t) atoi(owner);
479 /* No longer support non-numeric ids */
481 DBG(2, sprintf(output, "Error: non-numeric ids unsupported\n"));
489 if (isdigit(group[0])) {
490 /* Numeric -- just convert */
491 g_id = (uid_t) atoi(group);
493 /* Don't support group names either */
494 DBG(2, sprintf(output, "Error: non-numeric ids unsupported\n"));
499 /* Now issue the syscall */
500 DBG(4, sprintf(output, "Changing owner of file %s to %d (group %d)\n",
503 last_ret_val = chown(file, o_id, g_id);
508 int sysio_open(char *path, int flags)
510 last_ret_val = open(path, flags);
512 DBG(3, sprintf(output, "Returning with errno set to %s (ret val is %d)\n",
513 strerror(my_errno), (int)last_ret_val));
517 int sysio_open3(char *path, int flags, char *mode_arg)
521 /* Is the new mode symbolic? */
522 if (isalpha(mode_arg[0])) {
523 /* Could be specifying defines */
524 if (mode_arg[0] == 'S')
525 mode = get_mode(mode_arg, DEFINED, 0);
527 mode = get_mode(mode_arg, SYMBOLIC, 0);
529 mode = get_mode(mode_arg, NUMERIC, 0);
531 last_ret_val = open(path, flags, mode);
537 int sysio_close(int fd)
540 last_ret_val = close(fd);
545 int sysio_fcntl(int fd, struct cmd_map* cmdptr, char *arg)
547 int fd_new, index, cmd, flag;
552 cmdname = cmdptr->cmd_name;
556 fd_new = get_obj(arg);
557 last_ret_val = fcntl(fd, F_DUPFD, fd_new);
568 last_ret_val= fcntl(fd, cmd);
580 last_ret_val = fcntl(fd, cmd, flag);
589 /* Get the buffer to hold the lock structure */
590 index = get_obj(arg);
592 sprintf(output, "Unable to find buffer %s\n", arg+1);
596 buf = buflist[index];
598 sprintf(output, "Buffer at index %d (mapped by %s) is null\n",
603 last_ret_val = fcntl(fd, cmd, (struct flock *)buf);
607 /* THis should be impossible */
614 void print_stat(struct stat *st)
616 DBG(3, sprintf(output, "%sstruct stat: \n", output));
617 DBG(3, sprintf(output, "%s st_dev: %#16x\n", output, (unsigned int)st->st_dev));
618 DBG(3, sprintf(output, "%s st_ino: %#16x\n", output, (unsigned int) st->st_ino));
619 DBG(3, sprintf(output, "%s st_mode: %#16x\n", output, st->st_mode));
620 DBG(3, sprintf(output, "%s st_nlink: %#16x\n", output, (int)st->st_nlink));
621 DBG(3, sprintf(output, "%s st_uid: %#16x\n", output, st->st_uid));
622 DBG(3, sprintf(output, "%s st_gid: %#16x\n", output, st->st_gid));
623 DBG(3, sprintf(output, "%s st_rdev: %#16x\n", output, (int)st->st_rdev));
624 DBG(3, sprintf(output, "%s st_size: %#16x\n", output, (int) st->st_size));
625 DBG(3, sprintf(output, "%s st_blksize: %#16x\n", output, (int) st->st_blksize));
626 DBG(3, sprintf(output, "%s st_blocks: %#16x\n", output, (int) st->st_blocks));
627 DBG(3, sprintf(output, "%s st_atime: %#16x\n", output, (unsigned int) st->st_atime));
628 DBG(3, sprintf(output, "%s st_mtime: %#16x\n", output, (unsigned int) st->st_mtime));
629 DBG(3, sprintf(output, "%s st_ctime: %#16x", output, (unsigned int) st->st_ctime));
632 int sysio_fstat(int fd, void *buf)
635 struct stat *st = (struct stat *)buf;
647 int sysio_lstat(char *filename, void *buf)
650 struct stat *st = (struct stat *)buf;
651 err = lstat(filename, st);
663 int sysio_stat(char *filename, void *buf)
666 struct stat *st = (struct stat *)buf;
668 err = stat(filename, st);
680 int sysio_getdirentries(int fd, char *buf, size_t nbytes, off_t *basep)
685 err = getdirentries(fd, buf, nbytes, basep);
688 DBG(4, sprintf(output, "%sRead %d bytes\n", output, err));
690 dp = (struct dirent *)buf;
692 DBG(3, sprintf(output, "%s\t%s: ino %llu off %llu len %x type %c\n",
695 (unsigned long long )dp->d_ino,
696 (unsigned long long )dp->d_off,
700 dp = (struct dirent *)((char *)dp + dp->d_reclen);
708 int sysio_mkdir(char *path, char *mode_arg)
714 /* Is the new mode symbolic? */
715 if (isalpha(mode_arg[0])) {
716 /* Could be specifying defines */
717 if (mode_arg[0] == 'S')
718 mode = get_mode(mode_arg, DEFINED, st.st_mode);
720 mode = get_mode(mode_arg, SYMBOLIC, st.st_mode);
722 mode = get_mode(mode_arg, NUMERIC, st.st_mode);
724 DBG(3, sprintf(output, "Using a mode of %o and a file of %s\n", mode, path));
727 DBG(2, sprintf(output, "Invalid mode\n"));
731 err = mkdir(path, mode);
738 int sysio_creat(char *path, char *mode_arg)
743 /* Is the new mode symbolic? */
744 if (isalpha(mode_arg[0])) {
745 /* Could be specifying defines */
746 if (mode_arg[0] == 'S')
747 mode = get_mode(mode_arg, DEFINED, 0);
749 mode = get_mode(mode_arg, SYMBOLIC, 0);
751 mode = get_mode(mode_arg, NUMERIC, 0);
753 DBG(3, sprintf(output, "Using a mode of %o and a file of %s\n", mode, path));
756 DBG(2, sprintf(output, "Invalid mode\n"));
760 err = creat(path, mode);
766 void print_statvfs(struct statvfs *st)
768 DBG(3, sprintf(output, "%sstruct statvfs: \n", output));
769 DBG(3, sprintf(output, "%s f_bsize: %x\n", output, (unsigned int) st->f_bsize));
770 DBG(3, sprintf(output, "%s f_frsize: %x\n", output, (unsigned int) st->f_frsize));
771 DBG(3, sprintf(output, "%s f_blocks: %x\n", output, (unsigned int) st->f_blocks));
772 DBG(3, sprintf(output, "%s f_bfree: %x\n", output, (unsigned int) st->f_bfree));
773 DBG(3, sprintf(output, "%s f_bavail: %x\n", output, (unsigned int) st->f_bavail));
774 DBG(3, sprintf(output, "%s f_files: %x\n", output, (unsigned int) st->f_files));
775 DBG(3, sprintf(output, "%s f_ffree: %x\n", output, (unsigned int) st->f_ffree));
776 DBG(3, sprintf(output, "%s f_favail: %x\n", output, (unsigned int) st->f_favail));
777 DBG(3, sprintf(output, "%s f_files: %x\n", output, (unsigned int) st->f_files));
778 #if (__GLIBC__ == 2 && __GLIBC_MINOR__ == 1)
779 DBG(3, sprintf(output, "%s f_fsid: %x\n", output, (unsigned int) st->f_fsid.__val[1]));
781 DBG(3, sprintf(output, "%s f_fsid: %x\n", output, (unsigned int) st->f_fsid));
783 DBG(3, sprintf(output, "%s f_flag: %x\n", output, (unsigned int) st->f_flag));
784 DBG(3, sprintf(output, "%s f_fnamemax: %x\n", output, (unsigned int) st->f_namemax));
788 int sysio_statvfs(char *filename, void *buf)
791 struct statvfs *st = (struct statvfs *)buf;
793 err = statvfs(filename, st);
795 my_perror("statvfs");
805 int sysio_fstatvfs(int fd, void *buf)
808 struct statvfs *st = (struct statvfs *)buf;
810 err = fstatvfs(fd, st);
812 my_perror("fstatvfs");
822 int sysio_umask(char *mode_arg)
826 /* Is the new mode symbolic? */
827 if (isalpha(mode_arg[0])) {
828 /* Could be specifying defines */
829 if (mode_arg[0] == 'S')
830 mode = get_mode(mode_arg, DEFINED, 0);
832 mode = get_mode(mode_arg, SYMBOLIC, 0);
834 mode = get_mode(mode_arg, NUMERIC, 0);
836 last_ret_val = umask(mode);
841 int sysio_mknod(char *path, char *mode_arg, dev_t dev)
846 mode = get_obj(mode_arg);
849 DBG(2,sprintf(output, "Cant get mode from %s\n", mode_arg));
850 fprintf(stderr, "Cant get mode from %s\n", mode_arg);
854 err = mknod(path, (mode_t) mode, dev);