Changeset 35a35651 in mainline


Ignore:
Timestamp:
2012-05-04T11:31:47Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b00255a
Parents:
f2b3d3e
Message:

Previous work-dir handling only in Bdsh, not in libc

Location:
uspace
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/builtins/cd/cd.c

    rf2b3d3e r35a35651  
    4141static const char *cmdname = "cd";
    4242
     43/* Previous directory variables.
     44 *
     45 * Declaring them static to avoid many "== NULL" checks.
     46 * PATH_MAX is not that big to cause any problems with memory overhead.
     47 */
     48static char previous_directory[PATH_MAX] = "";
     49static char previous_directory_tmp[PATH_MAX];
     50static bool previous_directory_valid = true;
     51static bool previous_directory_set = false;
     52
     53static int chdir_and_remember(const char *new_dir) {
     54
     55        char *ok = getcwd(previous_directory_tmp, PATH_MAX);
     56        previous_directory_valid = ok != NULL;
     57        previous_directory_set = true;
     58
     59        int rc = chdir(new_dir);
     60        if (rc != EOK) {
     61                return rc;
     62        }
     63
     64        str_cpy(previous_directory, PATH_MAX, previous_directory_tmp);
     65        return EOK;
     66}
     67
    4368void help_cmd_cd(unsigned int level)
    4469{
     
    5580}
    5681
     82
    5783/* This is a very rudamentary 'cd' command. It is not 'link smart' (yet) */
    5884
     
    6591        /* Handle cd -- -. Override to switch to a directory named '-' */
    6692        bool hyphen_override = false;
     93        char *target_directory = argv[1];
    6794        if (argc == 3) {
    68                 if(!str_cmp(argv[1], "--")) {
     95                if (!str_cmp(argv[1], "--")) {
    6996                        hyphen_override = true;
    7097                        argc--;
     98                        target_directory = argv[2];
    7199                }
    72100        }
     
    92120
    93121        /* Handle 'cd -' first. */
    94         if (!str_cmp(argv[1], "-") && !hyphen_override) {
    95                 char *buffer = (char *) malloc(PATH_MAX);
    96                 if (!buffer) {
     122        if (!str_cmp(target_directory, "-") && !hyphen_override) {
     123                if (!previous_directory_valid) {
     124                        cli_error(CL_EFAIL, "Cannot switch to previous directory");
     125                        return CMD_FAILURE;
     126                }
     127                if (!previous_directory_set) {
     128                        cli_error(CL_EFAIL, "No previous directory to switch to");
     129                        return CMD_FAILURE;
     130                }
     131                char *prev_dup = str_dup(previous_directory);
     132                if (prev_dup == NULL) {
    97133                        cli_error(CL_ENOMEM, "Cannot switch to previous directory");
    98134                        return CMD_FAILURE;
    99135                }
    100                 memset(buffer, 0, PATH_MAX);
    101                 getprevwd(buffer, PATH_MAX);
    102                 if (*buffer == '\0') {
    103                         cli_error(CL_EFAIL, "No previous directory to switch to");
    104                         free(buffer);
    105                         return CMD_FAILURE;
    106                 } else {
    107                         rc = chdir(buffer);
    108                         free(buffer);
    109                 }
    110         } else if (hyphen_override) {
    111                 /* Handles 'cd -- <dirname>'.
    112                  * Override for directory named '-'.
    113                  */
    114                 rc = chdir(argv[2]);
     136                rc = chdir_and_remember(prev_dup);
     137                free(prev_dup);
    115138        } else {
    116                 rc = chdir(argv[1]);
     139                rc = chdir_and_remember(target_directory);
    117140        }
    118141
     
    126149                        break;
    127150                case ENOENT:
    128                         cli_error(CL_ENOENT, "Invalid directory `%s'", argv[1]);
     151                        cli_error(CL_ENOENT, "Invalid directory `%s'", target_directory);
    129152                        break;
    130153                default:
    131                         cli_error(CL_EFAIL, "Unable to change to `%s'", argv[1]);
     154                        cli_error(CL_EFAIL, "Unable to change to `%s'", target_directory);
    132155                        break;
    133156                }
  • uspace/lib/c/generic/vfs/vfs.c

    rf2b3d3e r35a35651  
    5858static async_sess_t *vfs_sess = NULL;
    5959
    60 /* Current (working) directory. */
    6160static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
     61
    6262static int cwd_fd = -1;
    6363static char *cwd_path = NULL;
    6464static size_t cwd_size = 0;
    65 
    66 /* Previous directory. */
    67 static FIBRIL_MUTEX_INITIALIZE(pwd_mutex);
    68 static int pwd_fd = -1;
    69 static char *pwd_path = NULL;
    70 static size_t pwd_size = 0;
    71 
    7265
    7366/** Start an async exchange on the VFS session.
     
    758751        fibril_mutex_lock(&cwd_mutex);
    759752       
    760 
    761         fibril_mutex_lock(&pwd_mutex);
    762 
    763         if (pwd_fd >= 0)
    764                 close(pwd_fd);
    765 
    766 
    767         if (pwd_path)
    768                 free(pwd_path);
    769 
    770 
    771         pwd_fd = cwd_fd;
    772         pwd_path = cwd_path;
    773         pwd_size = cwd_size;
    774 
    775         fibril_mutex_unlock(&pwd_mutex);
    776 
     753        if (cwd_fd >= 0)
     754                close(cwd_fd);
     755       
     756       
     757        if (cwd_path)
     758                free(cwd_path);
     759       
    777760        cwd_fd = fd;
    778761        cwd_path = abs;
     
    798781        fibril_mutex_unlock(&cwd_mutex);
    799782       
    800         return buf;
    801 }
    802 
    803 
    804 char *getprevwd(char *buf, size_t size)
    805 {
    806         if (size == 0)
    807                 return NULL;
    808 
    809         fibril_mutex_lock(&pwd_mutex);
    810 
    811         if ((pwd_size == 0) || (size < pwd_size + 1)) {
    812                 fibril_mutex_unlock(&pwd_mutex);
    813                 return NULL;
    814         }
    815 
    816         str_cpy(buf, size, pwd_path);
    817         fibril_mutex_unlock(&pwd_mutex);
    818 
    819783        return buf;
    820784}
  • uspace/lib/c/include/unistd.h

    rf2b3d3e r35a35651  
    7474
    7575extern char *getcwd(char *, size_t);
    76 extern char *getprevwd(char *, size_t);
    7776extern int rmdir(const char *);
    7877extern int chdir(const char *);
Note: See TracChangeset for help on using the changeset viewer.