Ejemplo n.º 1
0
Archivo: shell.c Proyecto: Rathcke/uni
int cmd_ls(int argc, char** argv) {
  char d[PATH_LENGTH];
  char* dir;
  if (argc == 2) {
    strcpy(d, argv[1]);
    dir = (char*) d;
  } else {
    dir = (char*) NULL;
  }
  int i;
  int n = syscall_filecount(dir);
  char buffer[PATH_LENGTH];

  if (n < 0) {
    printf("Error reading %s (Reason: %d)\n", dir, n);
    return n;
  }

  for (i = 0; i < n; ++i)
    {
      if (syscall_file(dir, i, buffer)) {
        return 2;
      }
      printf("%s\n", buffer);
    }
  return 0;
}
Ejemplo n.º 2
0
Archivo: shell.c Proyecto: Rotte/osm-k
int cmd_ls(int argc, char** argv) {
  char dir[PATH_LENGTH];
  if (argc == 1) {
    syscall_getwd(dir);
  } else if (argc == 2) {
    strcpy(dir, argv[1]);
  } else {
    printf("Usage: ls [dir]\n");
    return 1;
  }
  int i;
  int n = syscall_filecount(argv[1]);
  char buffer[PATH_LENGTH];

  for (i = 0; i < n; ++i)
    {
      if (!syscall_file(argv[1], i, buffer)) {
        return 2;
      }
      printf("%s\n", buffer);
    }
  return 0;
}
Ejemplo n.º 3
0
Archivo: shell.c Proyecto: Rotte/osm-k
/* This function is pretty gnarly and assumes that anything will fit
   in fixed buffers. */
size_t complete_at_point(char* s, size_t point) {
  /* Point points just at the terminating zero of s.  It might be
     interesting to change the entire readline stuff to support arrow
     keys and in-line editing, but that's neither here nor there. */
  char volume[BUFFER_SIZE];
  char filename[BUFFER_SIZE];
  /* Start by finding the file name. */
  int beg, end;
  int i;
  for (beg = point-1; beg >= 0; beg--) {
    if (s[beg] == ' ' || s[beg] == '/' || s[beg] == ']') {
      beg++;
      break;
    }
  }
  /* Beg now points at the beginning of the file name. */
  strncpy(filename, s+beg, point-beg);
  filename[point-beg] = '\0';
  /* Let's extract the volume/directory. */
  for (end = beg; beg >= 0; beg--) {
    if (s[beg] == ' ' || s[beg] == '[') {
      break;
    }
  }
  strncpy(volume, s+beg, end-beg);
  volume[end-beg+1] = '\0';
  /* Now let's look at all the files of the volume and see if we can
     find a match. */
  int n = syscall_filecount(volume);
  char file[PATH_LENGTH];
  char match[PATH_LENGTH];
  int anyfound = 0;

  for (i = 0; i < n; ++i) {
    if (!syscall_file(volume, i, file)) {
      continue;
    }
    if (strstr(file, filename) == file) {
      if (anyfound) {
        int j;
        /* Find where they differ. */
        for (j = 0; file[j] == match[j]; j++)
          ;
        match[j] = '\0';
      } else {
        anyfound = 1;
        /* Only match so far. */
        strcpy(match, file);
      }
    }
  }
  if (!anyfound) {
    return point;
  }
  /* The array 'match' now contains the best match - insert it into
     the input if we can. */
  if (strlen(match)-strlen(filename) < BUFFER_SIZE - point) {
    /*    printf("(inserting %s)", match+strlen(filename));*/
    strcpy(s+point, match+strlen(filename));
    return point + strlen(match) - strlen(filename);
  }
  return point;
}