Ejemplo n.º 1
0
//Determines which token(s) from the list that should be in the replacement
char *getTokenSequenceForList (struct token *list,
                               int *iPointer,
                               const char *oldLine,
                               int *replacementStatus) {
    if (oldLine[iPointer[0]] == '^'){
        return getTokenForPos (list, 1);
    }
    else if (oldLine[iPointer[0]] == '$'){
        return getLastToken (list);
    }
    else if (oldLine[iPointer[0]] == ':'){
        if (strlen(&oldLine[iPointer[0]]) > 1) {
            if (oldLine[iPointer[0] + 1] >= 48 && oldLine[iPointer[0] + 1] <= 57) {
                char *endptr;
                int tokenNum = strtol (&oldLine[iPointer[0] + 1], &endptr, 10);
                char *tokenReturned = getTokenForPos (list, tokenNum);
                int numLength = getIntLength (tokenNum);
                iPointer[0] += numLength;
                return tokenReturned;
            }
        }
    }
    else if (oldLine[iPointer[0]] == '*'){
        return convertAllButFirst (list);
    }
    iPointer[0] --;
    return convertTokensToString(list);
}
Ejemplo n.º 2
0
//Skips to the end of an escape sequence
void skipToEnd (const char *oldLine, int *iPointer) {
    char *characters = "^*$";
    int foundChar = 0;
    for (int i = 0; i < 4; i++) {
        if(oldLine[iPointer[0] + 1] == characters[i]) {
            foundChar = 1;
        }
    }
    if (foundChar){
        iPointer[0] ++;
    }
    else if (oldLine[iPointer[0] + 1] == ':' ){
        if (strlen(&oldLine[iPointer[0]]) > 1) {
            if (oldLine[iPointer[0] + 2] >= 48 && oldLine[iPointer[0] + 2] <= 57) {
                char *endptr;
                int tokenNum = strtol (&oldLine[iPointer[0] + 2], &endptr, 10);
                int numLength = getIntLength (tokenNum);
                iPointer[0] += numLength + 1;
                return;
            }
        }
    }
    else {
        return;
    }
}
Ejemplo n.º 3
0
/**
 * @brief Sets the value of str to the value of the integer n.
 *	(i.e. if n=7 than str should contain ‘7’)
 * @param str the MyString to set.
 * @param n the int to set from.
 * RETURN VALUE:
 *  @return MYSTRING_SUCCESS on success, MYSTRING_ERROR on failure.
 */
MyStringRetVal myStringSetFromInt(MyString *str, int n)
{
    unsigned int length = getIntLength(n);

    if (str == NULL)
    {
        return MYSTRING_ERROR;
    }

    if (mystringRealloc(str, length) == MYSTRING_ERROR)
    {
        return MYSTRING_ERROR;
    }

    char *tempStr = (char *)malloc(sizeof(char) * C_STRING_LENGTH);

    if (tempStr == NULL)
    {
        return MYSTRING_ERROR;
    }

    snprintf(tempStr, length, "%d", n);

    if (myStringSetFromCString(str, tempStr) == MYSTRING_ERROR)
    {
        return MYSTRING_ERROR;
    }

    free(tempStr);
    tempStr = NULL;

    return MYSTRING_SUCCESS;
}
Ejemplo n.º 4
0
Length getLength(const FTSENT *pChild) {
//	char *pColumns = NULL;
	Length s_length = {0};
	int temp_len = 0;
	char* pBuf = NULL;
	char aBuf[1024];
	struct stat *tempStat = {0};
	struct passwd *pUid = NULL;
	struct group *pGid = NULL;
	struct winsize w;
	ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
	int hasDevice = 0;

	uint64_t maxSize = 0;
	uint64_t maxBlock = 0;
	bzero(aBuf, 1024);
	
	//pColumns = getenv("COLUMNS");
//	if(pColumns != NULL && atoi(pColumns) > 0) 
		windowWidth = w.ws_col;
//	printf("Column: %d\n", w.ws_col);
	while(pChild != NULL) {
		if(!flg_dot && pChild->fts_name[0] == '.') {
			pChild = pChild->fts_link;
			continue;
		}
			
		
		s_length.count++;
		
		// get access path (NAME)
		if(strlen(pChild->fts_accpath) > s_length.l_name)
			s_length.l_name = strlen(pChild->fts_accpath);			
		
		if(pChild->fts_info != FTS_NS)	{
			tempStat = pChild->fts_statp;

			// get inode 
			//(void)snfrintf(pBuf, sizeof(pBuf),"%lu",tempStat->st_ino);
			if(tempStat->st_ino > s_length.l_ino)
				s_length.l_ino = tempStat->st_ino;

			// get nlink
			if(tempStat->st_nlink > s_length.l_nlink)
				s_length.l_nlink = tempStat->st_nlink;

			// get uid
			pUid = getpwuid(tempStat->st_uid);
			if(flg_display == in_n || pUid == NULL)	{
				(void)snprintf(aBuf, sizeof(aBuf),"%u",
					tempStat->st_uid);
				temp_len = strlen(pBuf);
			}
			else 
				temp_len = strlen(pUid->pw_name);
			if(temp_len > s_length.l_uid)
				s_length.l_uid = temp_len;
			
			//get gid
			pGid = getgrgid(tempStat->st_gid);			
			if(flg_display == in_n || pUid == NULL)	{
				(void)snprintf(aBuf, sizeof(aBuf),"%u",
					tempStat->st_gid);
				temp_len = strlen(pBuf);
			}
			else 
				temp_len = strlen(pGid->gr_name);
			if(temp_len > s_length.l_gid)
				s_length.l_gid = temp_len;


			// get size
			hasDevice = 0; 
			if((tempStat->st_mode & S_IFMT) == S_IFBLK 
				|| (tempStat->st_mode & S_IFMT) == S_IFCHR) 
				hasDevice = 1;

			if(hasDevice) {
				if(major(tempStat->st_rdev) > s_length.l_major)
					s_length.l_major = major(tempStat->st_rdev);
				
				if(minor(tempStat->st_rdev) > s_length.l_minor)
					s_length.l_minor = minor(tempStat->st_rdev);
			
			}
			else {
				if(tempStat->st_size > maxSize)
					maxSize = tempStat->st_size;
			}

			// get block
			s_length.total_b += tempStat->st_blocks;
			if(tempStat->st_blocks > maxBlock) 
				maxBlock = tempStat->st_blocks;

			//
	//		resetSize(pBuf, tempStat->st_size);
	//		printf("%s: %s ", pChild->fts_accpath,
	//				pBuf);
		//	printf("after resize: %0.1f\n", resizeBlock(tempStat->st_blocks));
		}
		//printf
		pChild = pChild->fts_link;
	}// end while
	
	// count length
	s_length.l_ino = getIntLength(s_length.l_ino);
	s_length.l_nlink = getIntLength(s_length.l_nlink);
	
	//resize size
	//printf("origin size: %zu\n",maxSize);
	if(s_length.l_major != 0 || s_length.l_minor != 0) {
		s_length.l_size = getIntLength(s_length.l_major) +
				 getIntLength(s_length.l_minor) + 2;
	}
	else if(flg_display == in_l || flg_display == in_n) {
		if(flg_h) {
			pBuf = resetSize(maxSize);
			s_length.l_size = strlen(pBuf) + 1;
			free(pBuf);
		}
		else {
			s_length.l_size = 
				snprintf(aBuf, sizeof(aBuf), "%zu", maxSize);
		}
	}
		
	//resize block 
	 if (flg_s) {
		pBuf = resetBlock(maxBlock);
		//printf("MaxBlockSize: %s\n", pBuf);		
		s_length.l_blocks = strlen(pBuf);
		free(pBuf);
	}
		
	
	s_length.column = s_length.l_name;
	if(flg_i)
		s_length.column += s_length.l_ino + 1;
	if(flg_s)
		s_length.column += s_length.l_blocks + 1;
	if(flg_F)
		s_length.column += 1;
	
	//printf("finish!\n");
	return s_length;
}
Ejemplo n.º 5
0
//Determines if a replacement designator has been passed in and
//replases and locates the appropriate list
char *getReplacement (int *iPointer, const char *oldLine, int *replacementStatus) {
    int lineLength = strlen(&oldLine[iPointer[0]]);
    char *endptr;
    struct token *listToSearch;
    int listNum;
    if (oldLine[iPointer[0]] == '!') {
        if (!tokenHistory){
            if (oldLine[iPointer[0] + 1]) {
                skipToEnd(oldLine, iPointer);
            }
            return NULL;
        }
        iPointer[0] += 1;
        return getTokenSequenceForList (tokenHistory[currentPossition - 1],
                                        iPointer, oldLine,
                                        replacementStatus);

        replacementStatus[0] = 1;
    }
    else if (oldLine[iPointer[0]] == '-'){
        if (lineLength > 1) {
            if (oldLine[iPointer[0] + 1] >= 48 && oldLine[iPointer[0] + 1] <= 57) {
                listNum = strtol (&oldLine[iPointer[0] + 1], &endptr, 10);
                int index = getIndexHistoryPossition (historyTotal + 1 - listNum);
                int numLength = getIntLength (listNum);
                iPointer[0] += numLength + 1;
                if (!tokenHistory){
                    if (oldLine[iPointer[0] + 1]) {
                        skipToEnd(oldLine, iPointer);
                    }
                    return NULL;
                }
                if (index >= 0){
                    listToSearch = tokenHistory[index];
                    return getTokenSequenceForList (listToSearch,
                                                    iPointer,
                                                    oldLine,
                                                    replacementStatus);
                }
            }
        }
        replacementStatus[0] = 1;
    }
    else if (oldLine[iPointer[0]] >= 48 && oldLine[iPointer[0]] <= 57) {
        listNum = strtol (&oldLine[iPointer[0]], &endptr, 10);
        int index = getIndexHistoryPossition(listNum);
        int numLength = getIntLength (listNum);
        iPointer[0] += numLength;
        if (!tokenHistory){
            if (oldLine[iPointer[0] + 1]) {
                if (oldLine[iPointer[0] + 1] ==':') {
                    iPointer[0] ++;
                }
                skipToEnd(oldLine, iPointer);
            }
            return NULL;
        }
        if (index >= 0){
            listToSearch = tokenHistory[index];
            return getTokenSequenceForList (listToSearch,
                                            iPointer,
                                            oldLine,
                                            replacementStatus);
        }
        replacementStatus[0] = 1;
    }
    else if (oldLine[iPointer[0]] == '?') {
        if (lineLength > 1) {
            char *searchString = getString (&oldLine[iPointer[0] + 1]);
            listToSearch = findStringInHistory (searchString);
            iPointer[0] += strlen(searchString) + 1;
            if (oldLine[iPointer[0]] == '?'){
                iPointer[0] ++;
            }
            if (!tokenHistory || !listToSearch){
                if (oldLine[iPointer[0] + 1]) {
                    skipToEnd(oldLine, iPointer);
                }
                return NULL;
            }
            free(searchString);
            return getTokenSequenceForList (listToSearch,
                                            iPointer,
                                            oldLine,
                                            replacementStatus);
        }
        replacementStatus[0] = 1;
    }
    if (replacementStatus[0] != 1) {
        replacementStatus[0] = -1;
    }
    return NULL;
}