G
Grant Austin
I have a linked list where each node holds a couple numbers a char
and a const char array. I use strcpy to take an char array
argument passed to my insert function to copy the string into
the new node.
To some of these entries 0a(line feed?) is being appended.
I've looked through the faq and googled but haven't found much
info that applies.
The code follows this post(in a bit I'll split it into multiple
files for use in a larger project).
Regards,
Newbie Grant
<-------------------------------
#include <stdio.h>
#include <stdlib.h>
struct motNode {
struct motNode * next;
unsigned int opCode;
char type;
unsigned int numArgs;
char opName[];
};
typedef struct motNode motList;
motList *mot_head, * mot_cur;
int mot_insert(unsigned int opCode, char type, unsigned int numArgs,
const char * opName){
mot_cur = (motList *)malloc(sizeof(motList));
mot_cur->opCode = opCode;
mot_cur->type = type;
mot_cur->numArgs= numArgs;
strcpy(mot_cur->opName, opName);
mot_cur->next = mot_head;
mot_head = mot_cur;
}
mot_dump(){
while(mot_cur){
printf("%s\n",mot_cur->opName);
mot_cur = mot_cur->next;
}
}
generate_mot(){
mot_head = NULL;
mot_insert(0,'r',0,"hlt");
mot_insert(1,'r',3,"add");
mot_insert(2,'r',3,"sub");
mot_insert(3,'r',3,"mul");
mot_insert(4,'r',3,"div");
mot_insert(5,'r',3,"mod");
mot_insert(6,'r',2,"move");
mot_insert(7,'r',3,"and");
mot_insert(8,'r',3,"or");
mot_insert(9,'r',3,"xor");
mot_insert(10,'r',2,"com");
mot_insert(11,'r',3,"sll");
mot_insert(12,'r',3,"srl");
mot_insert(13,'r',3,"sra");
mot_insert(14,'r',3,"jr");
mot_insert(15,'r',1,"rdr");
mot_insert(16,'r',1,"prr");
mot_insert(17,'r',1,"prh");
/* I format */
mot_insert(18,'i',2,"li");
mot_insert(19,'i',3,"addi");
mot_insert(20,'i',3,"subi");
mot_insert(21,'i',3,"muli");
mot_insert(22,'i',3,"divi");
mot_insert(23,'i',3,"modi");
mot_insert(24,'i',3,"lwb");
mot_insert(25,'i',3,"swb");
/* J format */
mot_insert(26,'j',2,"lwa");
mot_insert(27,'i',2,"swa");
mot_insert(28,'i',1,"j");
mot_insert(29,'i',1,"jal");
mot_insert(30,'i',1,"jeq");
mot_insert(31,'i',1,"jne");
mot_insert(32,'i',1,"jlt");
mot_insert(33,'i',1,"jle");
mot_insert(34,'i',1,"jgt");
mot_insert(35,'i',1,"jge");
}
main(int argv, char * argc[]){
FILE * infle;
int
eofl,
fcount = 2;
if(argv < 3){
fprintf(stderr,"Too few arguments\n");
fprintf(stderr,"\tusage:\tp2 <flag> <input files:minimum of one>\n");
exit(1);
}
generate_mot();
mot_dump();
do{
printf("infle :: %s\n",argc[fcount]);
if((infle = fopen(argc[fcount],"r")) == NULL){
fprintf(stderr,"Unable to open data file %s\n",argc[fcount]);
}
else{
//do work
fclose(infle);
}
fcount++;
} while(fcount < argv);
}
--------------------------------->
and a const char array. I use strcpy to take an char array
argument passed to my insert function to copy the string into
the new node.
To some of these entries 0a(line feed?) is being appended.
I've looked through the faq and googled but haven't found much
info that applies.
The code follows this post(in a bit I'll split it into multiple
files for use in a larger project).
Regards,
Newbie Grant
<-------------------------------
#include <stdio.h>
#include <stdlib.h>
struct motNode {
struct motNode * next;
unsigned int opCode;
char type;
unsigned int numArgs;
char opName[];
};
typedef struct motNode motList;
motList *mot_head, * mot_cur;
int mot_insert(unsigned int opCode, char type, unsigned int numArgs,
const char * opName){
mot_cur = (motList *)malloc(sizeof(motList));
mot_cur->opCode = opCode;
mot_cur->type = type;
mot_cur->numArgs= numArgs;
strcpy(mot_cur->opName, opName);
mot_cur->next = mot_head;
mot_head = mot_cur;
}
mot_dump(){
while(mot_cur){
printf("%s\n",mot_cur->opName);
mot_cur = mot_cur->next;
}
}
generate_mot(){
mot_head = NULL;
mot_insert(0,'r',0,"hlt");
mot_insert(1,'r',3,"add");
mot_insert(2,'r',3,"sub");
mot_insert(3,'r',3,"mul");
mot_insert(4,'r',3,"div");
mot_insert(5,'r',3,"mod");
mot_insert(6,'r',2,"move");
mot_insert(7,'r',3,"and");
mot_insert(8,'r',3,"or");
mot_insert(9,'r',3,"xor");
mot_insert(10,'r',2,"com");
mot_insert(11,'r',3,"sll");
mot_insert(12,'r',3,"srl");
mot_insert(13,'r',3,"sra");
mot_insert(14,'r',3,"jr");
mot_insert(15,'r',1,"rdr");
mot_insert(16,'r',1,"prr");
mot_insert(17,'r',1,"prh");
/* I format */
mot_insert(18,'i',2,"li");
mot_insert(19,'i',3,"addi");
mot_insert(20,'i',3,"subi");
mot_insert(21,'i',3,"muli");
mot_insert(22,'i',3,"divi");
mot_insert(23,'i',3,"modi");
mot_insert(24,'i',3,"lwb");
mot_insert(25,'i',3,"swb");
/* J format */
mot_insert(26,'j',2,"lwa");
mot_insert(27,'i',2,"swa");
mot_insert(28,'i',1,"j");
mot_insert(29,'i',1,"jal");
mot_insert(30,'i',1,"jeq");
mot_insert(31,'i',1,"jne");
mot_insert(32,'i',1,"jlt");
mot_insert(33,'i',1,"jle");
mot_insert(34,'i',1,"jgt");
mot_insert(35,'i',1,"jge");
}
main(int argv, char * argc[]){
FILE * infle;
int
eofl,
fcount = 2;
if(argv < 3){
fprintf(stderr,"Too few arguments\n");
fprintf(stderr,"\tusage:\tp2 <flag> <input files:minimum of one>\n");
exit(1);
}
generate_mot();
mot_dump();
do{
printf("infle :: %s\n",argc[fcount]);
if((infle = fopen(argc[fcount],"r")) == NULL){
fprintf(stderr,"Unable to open data file %s\n",argc[fcount]);
}
else{
//do work
fclose(infle);
}
fcount++;
} while(fcount < argv);
}
--------------------------------->