Changeset 089385e8 in mainline


Ignore:
Timestamp:
2012-04-07T17:08:06Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2662a1e9
Parents:
85d2fe2e
git-author:
Ketan Singh <> (2012-04-07 17:08:06)
git-committer:
Vojtech Horky <vojtechhorky@…> (2012-04-07 17:08:06)
Message:

Change to previous directory with `cd -' in bdsh

Location:
uspace
Files:
3 edited

Legend:

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

    r85d2fe2e r089385e8  
    6363        argc = cli_count_args(argv);
    6464
     65        /* Handle cd -- -. Override to switch to a directory named '-' */
     66        bool hyphen_override = false;
     67        if (argc == 3) {
     68                if(!str_cmp(argv[1], "--")) {
     69                        hyphen_override = true;
     70                        argc--;
     71                }
     72        }
     73
    6574        /* We don't yet play nice with whitespace, a getopt implementation should
    6675         * protect "quoted\ destination" as a single argument. Its not our job to
     
    7988        }
    8089
    81         /* We have the correct # of arguments
    82      * TODO: handle tidle (~) expansion? */
     90        /* We have the correct # of arguments */
     91        // TODO: handle tidle (~) expansion? */
    8392
    84         rc = chdir(argv[1]);
     93        /* Handle 'cd -' first. */
     94        if (!str_cmp(argv[1], "-") && !hyphen_override) {
     95                char *buffer = (char *) malloc(PATH_MAX);
     96                if (!buffer) {
     97                        cli_error(CL_ENOMEM, "Cannot switch to previous directory");
     98                        return CMD_FAILURE;
     99                }
     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]);
     115        } else {
     116                rc = chdir(argv[1]);
     117        }
    85118
    86119        if (rc == 0) {
  • uspace/lib/c/generic/vfs/vfs.c

    r85d2fe2e r089385e8  
    5858static async_sess_t *vfs_sess = NULL;
    5959
     60/* Current (working) directory. */
    6061static 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. */
     67static FIBRIL_MUTEX_INITIALIZE(pwd_mutex);
     68static int pwd_fd = -1;
     69static char *pwd_path = NULL;
     70static size_t pwd_size = 0;
     71
    6572
    6673/** Start an async exchange on the VFS session.
     
    751758        fibril_mutex_lock(&cwd_mutex);
    752759       
    753         if (cwd_fd >= 0)
    754                 close(cwd_fd);
    755        
    756        
    757         if (cwd_path)
    758                 free(cwd_path);
    759        
     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
    760777        cwd_fd = fd;
    761778        cwd_path = abs;
     
    781798        fibril_mutex_unlock(&cwd_mutex);
    782799       
     800        return buf;
     801}
     802
     803
     804char *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
    783819        return buf;
    784820}
  • uspace/lib/c/include/unistd.h

    r85d2fe2e r089385e8  
    7474
    7575extern char *getcwd(char *buf, size_t);
     76extern char *getprevwd(char *, size_t);
    7677extern int rmdir(const char *);
    7778extern int chdir(const char *);
Note: See TracChangeset for help on using the changeset viewer.