I am suppose to make a MakeFile for the 2 source files named p4a.c and p4b.c
Am i doing this makefile correctly? Any suggestions would be so helpful
my MakeFile so far
.SUFFIXES: .c .o
CC - gcc
CFLAGS = -g
.c.o:
$(CC) $(CFLAGS) -c $,
sample: p4a.c p4b.c
gcc p4a.c p4b.c -o sample
p4a.c:
p4b.c:
clean:
rm -f *.o core
p4a.c
#include<stdio.h>
#include<string.h>
main(int argc,char* argv[])
{
short int a[100][5];
short int res;
FILE *fp1;
if(!(fp1=fopen(argv[1],"r")))
printf("Error opening file\n");
else
{
FILE *fp2;
int i,j,k,l;
i=j=0;
while(!feof(fp1))
{
for(j=0;j<4;j++)
fscanf(fp1,"%d",&a[i][j]);
i++;
}
for(k=0;k<i;k++)
{
unsigned short int musk=!0; //musk=11...111
musk=musk|15; //musk=000..001111
musk=musk<<12; //musk=111100..000
res=0;
for(l=0;l<4;l++)
{
short int temp=(a[k][l]&musk);
res=res|temp;
musk=musk>>4;
}
a[k][4]=res;
}
if(!(fp2=fopen(argv[2],"w")))
printf("Error:cannot open file");
else
{
for(k=0;k<i;k++)
{
for(j=0;j<5;j++)
fprintf(fp2,"%d%s",a[k][j],"\t");
fprintf(fp2,"%s","\n");
}
fclose(fp1);
fclose(fp2);
}
}
return 0;
}
p4b.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct token
{
char name[15];
int def,usecount,use[100];
}identifier;
identifier id[1000];
int idcounter;
void search(char* token,int linenum)
{
int i;
for(i=0;i<idcounter;i++) //check if already exist
{
if(!strcmp(token,id[i].name))
{
id[i].use[id[i].usecount]=linenum;
id[i].usecount++;
return;
}
}
//insert
strcpy(id[idcounter].name,token);
id[idcounter].def=linenum;
id[idcounter].usecount=0;
idcounter++;
}
void check(char line[81],int linenum)
{
char token1[15];
char* token=(char*)malloc(sizeof(char)*15);
if(line[0]=='_' || (line[0]>='a' && line[0]<='z') || (line[0]>='A' && line[0]<='Z'))
{
token=strtok(line," ,\t"); //take label
strcpy(token1,token);
if(token1[strlen(token1)-1]==':')
token1[strlen(token1)-1]='\0';
search(token1,linenum);
token=strtok(0," ,\t"); //skip opcode\datatype
strcpy(token1,token);
if(token1[0]=='.') //check data type
return;
while(token)
{
token=strtok(0," ,\t");
if(token)
{
strcpy(token1,token);
if(token1[0]=='#')
return; //start of comment
else if(token1[0]=='_' || (token1[0]>='a' && token1[0]<='z') || (token1[0]>='A' && token1[0]<='Z'))
{
if(token1[strlen(token)-1]=='\n')
token1[strlen(token)-1]='\0';
search(token1,linenum);
}
}
}
}
else
{
token=strtok(line," ,\t"); //skip opcode
token=strtok(0," ,\t"); //search for optional operands
while(token)
{
strcpy(token1,token);
if(token1[0]=='#')
return; //start of comment
else if(token1[0]=='_' || (token1[0]>='a' && token1[0]<='z') || (token1[0]>='A' && token1[0]<='Z'))
{
if(token1[strlen(token)-1]=='\n')
token1[strlen(token)-1]='\0';
search(token1,linenum);
}
token=strtok(0," ,\t");
}
}
}
main(int argc,char* argv[])
{
char line[81],line2[81];
int i,linenum,p,q;
FILE *fp1,*fp2;
if(argc!=3) //check invalid command
{
printf("Invalid command argument number\n");
return 1;
}
if(!(fp1=fopen(argv[1],"r")))
{
printf("Error opening inputfile\n");
return 1;
}
if(!(fp2=fopen(argv[2],"w")))
{
printf("Error opening outputfile\n");
return 1;
}
linenum=i=1;
while(fgets(line,81,fp1))
{
if(line[0]=='#')
{
//fputs(strcat(strcat(itoa(i,str,100),". "),line),fp2);
//fputs("\n",fp2);
fprintf(fp2,"%d%s",i,". ");
fputs(line,fp2);
linenum++;
i++;
}
else if(strlen(line)>1)
{
strcpy(line2,line);
fprintf(fp2,"%d%s",i,". ");
fputs(line2,fp2);
//fputs(strcat(strcat(itoa(i,str,100),". "),line2),fp2);
check(line,linenum);
i++;
linenum++;
//printf("%d.\t%s",i,line);
}
else
{
fputs(line,fp2);
//fputs("\n",fp2);
linenum++;
}
}
fprintf(fp2,"%s","\n\n\tCross Reference Table\n");
fprintf(fp2,"%s","\n\nname\t\t def\t \tuse\n\n");
for(p=0;p<idcounter;p++)
{
fprintf(fp2,"%s\t\t%d\t\t",id[p].name,id[p].def);
for(q=0;q<id[p].usecount;q++)
{
if(q==0)
fprintf(fp2,"%d",id[p].use[q]);
else
fprintf(fp2,"%c%d",',',id[p].use[q]);
}
fprintf(fp2,"%s","\n");
}
fclose(fp1);
fclose(fp1);
return 0;
}