/* insert a label into the extrnal tables */ int insertExternalLabel(char *label, int label_dec_address) { strcpy((g_externalTable[g_externalTableSize]).label, label); (g_externalTable[g_externalTableSize]).type = EXT_LABEL; /*not in use */ (g_externalTable[g_externalTableSize]).decimal = label_dec_address; (g_externalTable[g_externalTableSize]).octal = getOctal(label_dec_address); g_externalTableSize++; return NORMAL; }
/* insert a label into the label tables */ int insertLabel(char *label, int tableType, int label_dec_address) { switch (tableType){ case EXT_LABEL: case DATA_LABEL: case CODE_LABEL: /*check for duplicate definition*/ if (KNF != getSymbolOctall(label)){ char msg[MSG_MAX_SIZE]; sprintf(msg, "Error! duplicate defintion of label [%s]\n", label); return reportError(msg, ERROR); } strcpy((g_symbolTable[g_symbolTableSize]).label, label); (g_symbolTable[g_symbolTableSize]).type = tableType; (g_symbolTable[g_symbolTableSize]).decimal = label_dec_address; (g_symbolTable[g_symbolTableSize]).octal = getOctal(label_dec_address); g_symbolTableSize++; break; case ENT_LABEL: /*check for duplicate definition*/ if (KNF != getEntryLabelOctall(label)){ char msg[MSG_MAX_SIZE]; sprintf(msg, "Error! duplicate defintion of entry label [%s]\n", label); return reportError(msg, ERROR); } strcpy((g_entryTable[g_entryTableSize]).label, label); (g_entryTable[g_entryTableSize]).type = tableType; (g_entryTable[g_entryTableSize]).decimal = label_dec_address; (g_entryTable[g_entryTableSize]).octal = getOctal(label_dec_address); g_entryTableSize++; break; } return NORMAL; }
/* increment all data labels by value of increment parameter*/ void incrementDataLabels(int increment){ int i = 0; while (i < g_symbolTableSize) { if (DATA_LABEL == g_symbolTable[i].type) { g_symbolTable[i].decimal += increment; g_symbolTable[i].octal = getOctal(g_symbolTable[i].decimal); } i++; } }
char *TKGetNextToken( TokenizerT * tk ) { if(tk->str[tk->pindex] == ' ' || tk->str[tk->pindex] == '\t' || tk->str[tk->pindex] == '\v' || tk->str[tk->pindex] == '\f' || tk->str[tk->pindex] == '\n' || tk->str[tk->pindex] == '\r'){ tk->pindex++; return NULL; } int charIndex = 0; char *tmp = (char*)malloc(sizeof(char)*strlen(tk->str)); if(isalnum(tk->str[tk->pindex])){ if(isalpha(tk->str[tk->pindex])){ //checks for word tmp[charIndex] = tk->str[tk->pindex]; charIndex++; tk->pindex++; tmp = getWord(tk, tmp, charIndex); return tmp; }else{ if(tk->str[tk->pindex] == '0' && isalnum(tk->str[tk->pindex + 1])){ //checks for hex if(tk->str[tk->pindex + 1] == 'x' || tk->str[tk->pindex + 1] == 'X'){ if(isdigit(tk->str[tk->pindex + 2]) || tk->str[tk->pindex + 2] == 'a' || tk->str[tk->pindex + 2] == 'b' || tk->str[tk->pindex + 2] == 'c' || tk->str[tk->pindex + 2] == 'd' || tk->str[tk->pindex + 2] == 'e' || tk->str[tk->pindex + 2] == 'f'){ tmp[charIndex] = tk->str[tk->pindex]; charIndex++; tk->pindex++; tmp = getHex(tk, tmp, charIndex); return tmp; } }else if(tk->str[tk->pindex + 1] == '0' || tk->str[tk->pindex + 1] == '1' || tk->str[tk->pindex + 1] == '2' || tk->str[tk->pindex + 1] == '3' || tk->str[tk->pindex + 1] == '4' || tk->str[tk->pindex + 1] == '5' || tk->str[tk->pindex + 1] == '6' || tk->str[tk->pindex + 1] == '7'){ // checks for octal tmp[charIndex] = tk->str[tk->pindex]; charIndex++; tk->pindex++; tmp = getOctal(tk, tmp, charIndex); return tmp; } } } //if not sends to number(decimal or floats) tmp[charIndex] = tk->str[tk->pindex]; charIndex++; tk->pindex++; tmp = getNumber(tk, tmp, charIndex); return tmp; }else{ // C-Operators tmp = getOp(tk, tmp, charIndex); } return tmp; }
/* * Examine the header block that was just read. * This can specify the information for another file, or it can mark * the end of the tar file. */ static void readHeader(const TarHeader * hp, int fileCount, const char ** fileTable) { int mode; int uid; int gid; //int checkSum; long size; time_t mtime; const char * name; int cc; BOOL hardLink; BOOL softLink; /* * If the block is completely empty, then this is the end of the * archive file. If the name is null, then just skip this header. */ name = hp->name; if (*name == '\0') { for (cc = TAR_BLOCK_SIZE; cc > 0; cc--) { if (*name++) return; } eofFlag = TRUE; return; } /* * There is another file in the archive to examine. * Extract the encoded information and check it. */ mode = getOctal(hp->mode, sizeof(hp->mode)); uid = getOctal(hp->uid, sizeof(hp->uid)); gid = getOctal(hp->gid, sizeof(hp->gid)); size = getOctal(hp->size, sizeof(hp->size)); mtime = getOctal(hp->mtime, sizeof(hp->mtime)); //checkSum = getOctal(hp->checkSum, sizeof(hp->checkSum)); if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) { if (!badHeader) fprintf(stderr, "Bad tar header, skipping\n"); badHeader = TRUE; return; } badHeader = FALSE; skipFileFlag = FALSE; /* * Check for the file modes. */ hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) || (hp->typeFlag == TAR_TYPE_HARD_LINK - '0')); softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) || (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0')); /* * Check for a directory or a regular file. */ if (name[strlen(name) - 1] == '/') mode |= S_IFDIR; else if ((mode & S_IFMT) == 0) mode |= S_IFREG; /* * Check for absolute paths in the file. * If we find any, then warn the user and make them relative. */ if (*name == '/') { while (*name == '/') name++; if (!warnedRoot) { fprintf(stderr, "Absolute path detected, removing leading slashes\n"); } warnedRoot = TRUE; } /* * See if we want this file to be restored. * If not, then set up to skip it. */ if (!wantFileName(name, fileCount, fileTable)) { if (!hardLink && !softLink && S_ISREG(mode)) { inHeader = (size == 0); dataCc = size; } skipFileFlag = TRUE; return; } /* * This file is to be handled. * If we aren't extracting then just list information about the file. */ if (!extractFlag) { if (verboseFlag) { printf("%s %3d/%-d %9ld %s %s", modeString(mode), uid, gid, size, timeString(mtime), name); } else printf("%s", name); if (hardLink) printf(" (link to \"%s\")", hp->linkName); else if (softLink) printf(" (symlink to \"%s\")", hp->linkName); else if (S_ISREG(mode)) { inHeader = (size == 0); dataCc = size; } printf("\n"); return; } /* * We really want to extract the file. */ if (verboseFlag) printf("x %s\n", name); if (hardLink) { if (link(hp->linkName, name) < 0) perror(name); return; } if (softLink) { #ifdef S_ISLNK if (symlink(hp->linkName, name) < 0) perror(name); #else fprintf(stderr, "Cannot create symbolic links\n"); #endif return; } /* * If the file is a directory, then just create the path. */ if (S_ISDIR(mode)) { createPath(name, mode); return; } /* * There is a file to write. * First create the path to it if necessary with a default permission. */ createPath(name, 0777); inHeader = (size == 0); dataCc = size; /* * Start the output file. */ outFd = open(name, O_WRONLY | O_CREAT | O_TRUNC, mode); if (outFd < 0) { perror(name); skipFileFlag = TRUE; return; } /* * If the file is empty, then that's all we need to do. */ if (size == 0) { (void) close(outFd); outFd = -1; } }