/**
 * returns the data in the linked list at the specified index.
 *
 * @function   linkedListAt
 *
 * @date       2015-02-09
 *
 * @revision   none
 *
 * @designer   Eric Tsang
 *
 * @programmer Eric Tsang
 *
 * @note       none
 *
 * @signature  void* linkedListAt(LinkedList* dis, int index)
 *
 * @param      dis linked list pointer that operation is being performed on.
 * @param      index index in the linked list to get the data from. head being
 *   index 0, and tail being at index size-1
 */
void* linkedListAt(LinkedList* dis, int index)
{
    Node* curr;
    void* at = NULL;

    if(index/2 < linkedListSize(dis))
    {
        curr = dis->head;
        while(index > 0 && curr != 0)
        {
            curr = curr->next;
            --index;
        }
        if(index == 0)
        {
            at = curr->data;
        }
    }
    else
    {
        curr = dis->tail;
        while(index < linkedListSize(dis)-1 && curr != 0)
        {
            curr = curr->prev;
            ++index;
        }
        if(index == linkedListSize(dis)-1)
        {
            at = curr->data;
        }
    }

    return at;
}
Exemplo n.º 2
0
void commandContainerCleanup(CommandContainer commandContainer){
	CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer;
        lockLock(internal->lock);
        time_t currentTime;       // Stores seconds elapsed since 01-01-1970
        time(&currentTime);
        // Cleanup atomic commands
        if(linkedListSize(internal->atomicCommands)>0){
            int length=linkedListSize(internal->atomicCommands);
            Command atomicCommand;
            while(length>0){
                 linkedListGetFirst(internal->atomicCommands,(Object*)&atomicCommand);
                 int commandTime=commandGetInt(atomicCommand,SERVER_HANDLING_TIME);
                 int distributed=commandGetInt(atomicCommand,DISTRIBUTED);
                 if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){
                   commandFree(atomicCommand);
                   linkedListRemoveFirst(internal->atomicCommands);
                 }else{
                     break;
                 }
                 length=linkedListSize(internal->atomicCommands);
            }
        }
        
        // Cleanup commands
        int length=linkedListSize(internal->versions);
        int i;
        int* intBuffer;
        while(length>0){
            linkedListGetFirst(internal->versions,(Object*)&intBuffer);
            Command temp;
            hashmapGet(internal->versionMap,*intBuffer,(Object*)&temp);
            int commandTime=commandGetInt(temp,SERVER_HANDLING_TIME);            
            int persisted=commandGetInt(temp,PERSISTED);
            int distributed=commandGetInt(temp,DISTRIBUTED);
            if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&persisted==TRUE&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){
                linkedListRemoveFirst(internal->versions);
                hashmapRemove(internal->versionMap,*intBuffer);
                memoryFree(intBuffer);
                commandFree(temp);
            }else{
                break;
            }
            length=linkedListSize(internal->versions);
        }
        lockUnlock(internal->lock);
        
        

}
Exemplo n.º 3
0
Command* listToCommand(const List* list)
{
  ListNode* node;
  Command* command;

  if(list == NULL || linkedListSize(list) == 0)
    return NULL;

  command = (Command*) malloc( sizeof(Command) );
  command->command = newLinkedList();

  node = list->head;

  /* each argument is just inserted into the command's argument list */
  while(node != NULL)
  {
    linkedListInsert(command->command, node->data);
    node = node->next;
  }

  command->infile = NULL;
  command->outfile = NULL;

  return command;
}
Exemplo n.º 4
0
int commandChangeDirectory(Command* cmd)
{
  int size;
  int ret;
  char* errorMsg;

  if(cmd == NULL)
    return 0;

  size = linkedListSize(cmd->command);

  /* if the user specifies more than one argument it is an error */
  if(size > 2)
  {
    fprintf(stderr, "cd: Too many arguments.\n");
    return -1;
  }

  /* if the user did not specify a directory, the default behavior is to go to the user's home directory */
  if(size < 2)
    linkedListInsert(cmd->command, getenv("HOME"));

  ret = chdir((char*)cmd->command->head->next->data);

  /* check for error */
  if(ret == -1)
  {
    /* determine the specific type of error */
    switch(errno)
    {
      case EACCES: 
        errorMsg = "Search permission is denied.";
        break;
      case ELOOP:
        errorMsg = "A symbolic link loop was encountered.";
        break;
      case ENAMETOOLONG:
        errorMsg = "The pathname is too long.";
        break;
      case ENOENT:
        errorMsg = "The path does not exist.";
        break;
      case ENOTDIR:
        errorMsg = "The path is not a directory.";
        break;
      default:
        errorMsg = "Unknown error.";
        break;
    }

    printf("%s: %s\n",(char*)cmd->command->head->next->data,errorMsg);
    return -1;
  }

  /* otherwise we were successful */
  return 0;
}
/**
 * inserts a new node into the passed linked list at the specified index; the
 *   newly inserted node will have the specified index after the insertion.
 *
 * @function   linkedListInsert
 *
 * @date       2015-02-09
 *
 * @revision   none
 *
 * @designer   Eric Tsang
 *
 * @programmer Eric Tsang
 *
 * @note       none
 *
 * @signature  BOOL linkedListInsert(LinkedList* dis, void* data, int index)
 *
 * @param      dis pointer to the linked list that the new data is being
 *   inserted into.
 * @param      data void pointer to the data that will be inserted into the
 *   linked list.
 * @param      index index in the linked list to insert the data into.
 *
 * @return     TRUE if the insertion was successful; FALSE otherwise.
 */
BOOL linkedListInsert(LinkedList* dis, void* data, int index)
{
    Node* prev = 0;
    Node* next = 0;
    BOOL inserted = FALSE;

    if(index/2 < linkedListSize(dis))
    {
        next = dis->head;
        while(index > 0 && next != 0)
        {
            prev = next;
            next = next->next;
            --index;
        }
        if(index == 0)
        {
            nodeMalloc(dis, data, prev, next);
            inserted = TRUE;
        }
    }
    else
    {
        prev = dis->tail;
        while(index < linkedListSize(dis) && prev != 0)
        {
            next = prev;
            prev = prev->prev;
            ++index;
        }
        if(index == linkedListSize(dis))
        {
            nodeMalloc(dis, data, prev, next);
            inserted = TRUE;
        }
    }

    return inserted;
}
/**
 * removes the node at the specified element from the linked list.
 *
 * @function   linkedListRemoveByIndex
 *
 * @date       2015-02-09
 *
 * @revision   none
 *
 * @designer   Eric Tsang
 *
 * @programmer Eric Tsang
 *
 * @note       none
 *
 * @signature  BOOL linkedListRemoveByIndex(LinkedList* dis, int index)
 *
 * @param      dis linked list pointer that operation is being performed on.
 * @param      index index in the linked list to remove the node from.
 *
 * @return     TRUE if the operation succeeded, FALSE otherwise.
 */
BOOL linkedListRemoveByIndex(LinkedList* dis, int index)
{
    Node* curr;
    BOOL removed = FALSE;

    if(index/2 < linkedListSize(dis))
    {
        curr = dis->head;
        while(index > 0 && curr != 0)
        {
            curr = curr->next;
            --index;
        }
        if(index == 0)
        {
            nodeFree(dis, curr);
            removed = TRUE;
        }
    }
    else
    {
        curr = dis->tail;
        while(index < linkedListSize(dis)-1 && curr != 0)
        {
            curr = curr->prev;
            ++index;
        }
        if(index == linkedListSize(dis)-1)
        {
            nodeFree(dis, curr);
            removed = TRUE;
        }
    }

    return removed;
}
Exemplo n.º 7
0
void linkedListDump(List* list)
{
  ListNode* cur = list->head;

  printf("Size: %d\n", linkedListSize(list));

  if(cur != NULL)
  {
    while(cur != NULL)
    {
      printf("%s\n",(char*)cur->data);
      cur = cur->next;
    }
  }
}
Exemplo n.º 8
0
Properties propertiesCreate(char *filePath) {
    PropertiesInternal *m = (PropertiesInternal *) memoryAlloc(sizeof(PropertiesInternal));
    m->map = hashmapCreate(SMALL_CONTAINER_SIZE);
    FILE *fp;
    long len;
    char *buf;
    fp = fopen(filePath, "rt");
    if (fp == NULL) {
        printf("couldn't open the properties file");
    };
    fseek(fp, 0, SEEK_END);            //go to end
    len = ftell(fp);                    //get position at end (length)
    fseek(fp, 0, SEEK_SET);            //go to beg.
    buf = (char *) memoryAlloc(len + 1);        //malloc buffer
    long result = fread(buf, (size_t) (len + 1), 1, fp);            //read into buffer
    if (result < 0) {
        printf("Fail to read properties file .");
    }
    fclose(fp);
    buf[len] = 0;
    char *key;
    char *value;
    const char newLineDelimiter[] = "\n";
    const char spaseDelimiter[] = "=";
    char *token;
    token = strtok(buf, newLineDelimiter);
    LinkedList linkedList = linkedListCreate();
    while (token != NULL) {
        linkedListAddFirst(linkedList, token);
        token = strtok(NULL, newLineDelimiter);
    }
    while (linkedListSize(linkedList) > 0) {
        char *token = (char *) linkedListRemoveFirst(linkedList);
        key = strtok(token, spaseDelimiter);
        value = strtok(NULL, spaseDelimiter);
        unsigned int keyId = stringTransformToInt(key, strlen(key));
        hashmapPut(m->map, keyId, value);
    }
    linkedListFree(linkedList);

    return m;
}
/**
 * @see linkedListAt
 */
void* linkedListLast(LinkedList* dis)
{
    return linkedListAt(dis, linkedListSize(dis)-1);
}
/**
 * @see linkedListRemoveByIndex
 */
int linkedListRemoveLast(LinkedList* dis)
{
    return linkedListRemoveByIndex(dis, linkedListSize(dis)-1);
}
/**
 * @see linkedListInsert
 */
void linkedListAppend(LinkedList* dis, void* data)
{
    linkedListInsert(dis, data, linkedListSize(dis));
}
Exemplo n.º 12
0
int commandExternalCommand(Command* cmd)
{
  ListNode* node;
  pid_t pid;
  char* argv[linkedListSize(cmd->command) + 1];
  char* name;
  char* temp;
  List* tokens;
  struct stat statbuf;
  int i;
  int found;

  if(cmd == NULL || cmd->command == NULL || cmd->command->head == NULL)
    return 0;

  /* first we want to check if the command name contains a /, which
     tells us that the user is providing the direct path to the command */

  if( strchr((char*)cmd->command->head->data, '/') != NULL )
  {
    name = (char*) malloc( strlen((char*)cmd->command->head->data) + 1);
    strcpy(name, (char*)cmd->command->head->data);

    /* even though the user provided a full path to the command, we'll make sure it exists
       just in case! */
    if( stat(name, &statbuf) != 0 )
    {
      fprintf(stderr, "%s: Command not found.\n", (char*)cmd->command->head->data);
      return -1;
    }
  }
  /* it doesn't contain a /, so we have to search the PATH for the command
     ourselves */
  else
  {
    /* first we have to get a copy of the PATH to search, so we get the PATH from getenv() */
    name = getenv("PATH");

    /* then we copy it into a new string so we can search it with strtok */
    temp = (char*) malloc(strlen(name + 1));
    strcpy(temp,name);

    /* we split the path into tokens, then search each one for our command */
    tokens = tokenize(temp, ":");
    node = tokens->head;
    while(node != NULL)
    {
      /* first we have to build the name of our command by appending the name to each of
         the path's returned by PATH */
      name = (char*) malloc(strlen((char*)node->data) + strlen((char*)cmd->command->head->data) + 2);
      strcpy(name, (char*)node->data);
      strcat(name, "/");
      strcat(name, (char*)cmd->command->head->data);

      /* now name contains the full path, for instance, if /my/path is on the path, and the command is ls, 
         name will contain /my/path/ls */
      if( stat(name, &statbuf) == 0 )
      {
        found = 1;
        break;
      }

      found = 0;
      
      /* if we get here the current name wasn't found, so we free the memory and then try the next token */
      free(name);
      node = node->next;
    }
    
    /* always clean up after ourselves */
    free(temp);
    free(tokens);

    if(found == 0)
    {
      fprintf(stderr, "%s: Command not found.\n", (char*)cmd->command->head->data);
      return -1;
    }
  }

  /* we have to package the arguments to the command into an array */
  node = cmd->command->head;
  i = 0;
  while(node != NULL)
  {
    argv[i] = (char*) malloc( strlen((char*)node->data) + 1);
    strcpy(argv[i],(char*)node->data);
    ++i;
    node = node->next;
  }
  argv[i] = 0;
    
  /* fork our process */
  pid = fork();
  
  /* make sure the fork was successful, and if not print an error */
  if(pid < 0)
  {
    fprintf(stderr, "Failed to fork!\n");
    return -1;
  }
  
  /* the child process executes the external command */
  if(pid == 0)
  {
    /* execute the command */
    execv(name, argv);
    
    /* should never get here */
    fprintf(stderr, "Error attempting to execute external command!\n");
    return -1;
  }
  
  /* wait for the process to finish */
  waitpid(pid, NULL, 0);
  
  /* if we get here then name was allocated above so we must free it */
  free(name);
  
  /* we need to free every string allocated in argv, if any */
  while(--i >= 0)
    free(argv[i]);

  return 0;
}