/* Retrieve the size and mode of a file * filestr directory name of directory entry * de directory entry */ int rosh_ls_de_size_mode(struct dirent *de, mode_t * st_mode) { int de_size; // char filestr2[ROSH_PATH_SZ]; // int file2pos; struct stat fdstat; int status; /* filestr2[0] = 0; file2pos = -1;*/ fdstat.st_size = 0; fdstat.st_mode = 0; /* if (filestr) { file2pos = strlen(filestr); memcpy(filestr2, filestr, file2pos); filestr2[file2pos] = '/'; } strcpy(filestr2 + file2pos + 1, de->d_name);*/ status = stat(de->d_name, &fdstat); ROSH_DEBUG2("\t--stat()=%d\terr=%d\n", status, errno); if (errno) { rosh_error(errno, "ls:szmd.stat", de->d_name); errno = 0; } de_size = (int)fdstat.st_size; *st_mode = fdstat.st_mode; return de_size; } /* rosh_ls_de_size_mode */
/* Change PWD (Present Working Directory) * cmdstr command string to process * ipwdstr Initial PWD */ void rosh_cd(const char *cmdstr, const char *ipwdstr) { int rv; char filestr[ROSH_PATH_SZ]; int cmdpos; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; rv = 0; /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); if (strlen(filestr) != 0) rv = chdir(filestr); else rv = chdir(ipwdstr); if (rv != 0) { rosh_error(errno, "cd", filestr); errno = 0; } else { #ifdef DO_DEBUG if (getcwd(filestr, ROSH_PATH_SZ)) ROSH_DEBUG(" %s\n", filestr); #endif /* DO_DEBUG */ } } /* rosh_cd */
/* Concatenate multiple files to stdout * cmdstr command string to process */ void rosh_cat(const char *cmdstr) { FILE *f; char filestr[ROSH_PATH_SZ]; char buf[ROSH_BUF_SZ]; int numrd; int cmdpos; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); while (strlen(filestr) > 0) { printf("--File = '%s'\n", filestr); f = fopen(filestr, "r"); if (f != NULL) { numrd = fread(buf, 1, ROSH_BUF_SZ, f); while (numrd > 0) { fwrite(buf, 1, numrd, stdout); numrd = fread(buf, 1, ROSH_BUF_SZ, f); } fclose(f); } else { rosh_error(errno, "cat", filestr); errno = 0; } cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); } } /* rosh_cat */
/* Page through a file like the more command * cmdstr command string to process * ipwdstr Initial PWD */ void rosh_more(const char *cmdstr) { int fd; char filestr[ROSH_PATH_SZ]; int cmdpos; int rows, cols; char *scrbuf; int ret; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; ret = getscreensize(1, &rows, &cols); if (ret) { ROSH_DEBUG("getscreensize() fail(%d); fall back\n", ret); ROSH_DEBUG("\tROWS='%d'\tCOLS='%d'\n", rows, cols); /* If either fail, go under normal size, just in case */ if (!rows) rows = 20; if (!cols) cols = 75; } ROSH_DEBUG("\tUSE ROWS='%d'\tCOLS='%d'\n", rows, cols); /* 32 bit align beginning of row and over allocate */ scrbuf = malloc(rows * ((cols+3)&(INT_MAX - 3))); if (!scrbuf) return; /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); if (strlen(filestr) > 0) { /* There is no need to mess up the console if we don't have a file */ rosh_console_raw(); while (strlen(filestr) > 0) { printf("--File = '%s'\n", filestr); fd = open(filestr, O_RDONLY); if (fd != -1) { rosh_more_fd(fd, rows, cols, scrbuf); close(fd); } else { rosh_error(errno, "more", filestr); errno = 0; } cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); } rosh_console_std(); } free(scrbuf); } /* rosh_more */
/* Output listing of a regular directory * filestr directory name to list * d the open DIR * optarr Array of options NOTE:This is where I could use qsort */ void rosh_ls_arg_dir(const char *filestr, DIR * d, const int *optarr) { struct dirent *de; int filepos; filepos = 0; while ((de = readdir(d))) { filepos++; rosh_ls_arg_dir_de(de, optarr); } if (errno) rosh_error(errno, "ls:arg_dir", filestr); else if (filepos == 0) ROSH_DEBUG("0 files found"); } /* rosh_ls_arg_dir */
/* Simple directory listing for one argument (file/directory) based on * filestr and pwdstr * ifilstr input filename/directory name to list * pwdstr Present Working Directory string * optarr Option Array */ void rosh_ls_arg(const char *filestr, const int *optarr) { struct stat fdstat; int status; // char filestr[ROSH_PATH_SZ]; // int filepos; DIR *d; struct dirent de; /* Initialization; make filestr based on leading character of ifilstr and pwdstr */ // rosh_qualify_filestr(filestr, ifilstr, pwdstr); fdstat.st_mode = 0; fdstat.st_size = 0; ROSH_DEBUG("\topt[0]=%d\topt[1]=%d\topt[2]=%d\n", optarr[0], optarr[1], optarr[2]); /* Now, the real work */ errno = 0; status = stat(filestr, &fdstat); if (status == 0) { if (S_ISDIR(fdstat.st_mode)) { ROSH_DEBUG("PATH '%s' is a directory\n", filestr); d = opendir(filestr); rosh_ls_arg_dir(filestr, d, optarr); closedir(d); } else { de.d_ino = rosh_ls_d_ino(&fdstat); de.d_type = (IFTODT(fdstat.st_mode)); strcpy(de.d_name, filestr); if (S_ISREG(fdstat.st_mode)) { ROSH_DEBUG("PATH '%s' is a regular file\n", filestr); } else { ROSH_DEBUG("PATH '%s' is some other file\n", filestr); } rosh_ls_arg_dir_de(&de, optarr); /* if (ifilstr[0] == SEP) rosh_ls_arg_dir_de(NULL, &de, optarr); else rosh_ls_arg_dir_de(pwdstr, &de, optarr);*/ } } else { rosh_error(errno, "ls", filestr); errno = 0; } return; } /* rosh_ls_arg */
/* Show PWD * cmdstr command string to process */ void rosh_pwd(const char *cmdstr) { int istr; char pwdstr[ROSH_PATH_SZ]; if (cmdstr) istr = 0; ROSH_DEBUG("CMD: '%s'\n", cmdstr); errno = 0; if (getcwd(pwdstr, ROSH_PATH_SZ)) { printf("%s\n", pwdstr); } else { rosh_error(errno, "pwd", ""); errno = 0; } istr = htonl(*(int *)pwdstr); ROSH_DEBUG2(" --%08X\n", istr); } /* rosh_pwd */
/* Page through a file like the more command * cmdstr command string to process * ipwdstr Initial PWD */ void rosh_more(const char *cmdstr) { int fd; char filestr[ROSH_PATH_SZ]; int cmdpos; int rows, cols; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; if (getscreensize(1, &rows, &cols)) { ROSH_DEBUG("getscreensize() fail; fall back\n"); ROSH_DEBUG("\tROWS='%d'\tCOLS='%d'\n", rows, cols); /* If either fail, go under normal size, just in case */ if (!rows) rows = 20; if (!cols) cols = 75; } ROSH_DEBUG("\tUSE ROWS='%d'\tCOLS='%d'\n", rows, cols); /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); if (strlen(filestr) > 0) { /* There is no need to mess up the console if we don't have a file */ rosh_console_raw(); while (strlen(filestr) > 0) { printf("--File = '%s'\n", filestr); fd = open(filestr, O_RDONLY); if (fd != -1) { rosh_more_fd(fd, rows, cols); close(fd); } else { rosh_error(errno, "more", filestr); errno = 0; } cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); } rosh_console_std(); } } /* rosh_more */