char* pch = strtok(line, " :");
while(pch != NULL){
printf("%s\n", pch);
pch = strtok(NULL, " :");
}
}
int main(int argc, char *argv[]){
if (argc == 1){
fprintf(stderr, "memake: *** No targets specified and no makefile found. Stop.\n");
return 2;
}
else{
char* file_to_read = argv[1];
FILE* fr = fopen(file_to_read, "r");
if (fr == NULL){
fprintf(stderr, "%s: trying to open file %s for read\n", strerror(errno), file_to_read);
exit(3);
}
char* target = NULL;
while(1){
char* line = NULL;
size_t line_length;
int chars_read = getline(&line, &line_length, fr);
if (chars_read == -1){
if (feof(fr)){
free(line);
break;
}
perror("getline");
exit(4);
}
if (line[0] == '\t'){
printf("%s", line);
}
else{
char* pch = strtok(line, ":");
int h;
for (h = 0; pch != NULL; h++){
if (h == 0){
target = pch;
}
else{
printf("Target %s\n", target);
printf("Dependencies %s\n", pch);
}
pch = strtok(NULL, " :");
}
}
free(line);
}
fclose(fr);
struct _rule_t_{
char** dependency;
char** buildcmd;
};
typedef struct _rule_t_ rule_t;
/*a struct as a representation of a list*/
struct _list_t_ {
struct rule *rule;
char *key;
struct _list_t_ *next;
};
struct _rule_t_{
char** dependency;
char** buildcmd;
char* target;
};
typedef struct _rule_t_ rule_t;
/*a struct as a representation of a list*/
struct _list_t_ {
rule_t *rule;
struct _list_t_ *next;
};
typedef struct _list_t_ list_t;
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "hash.h"
int main(int argc, char *argv[]){
if (argc == 1){
fprintf(stderr, "memake: *** No targets specified and no makefile found. Stop.\n");
return 2;
}
else{
HashTable* h = HashTableConstruct(50);
char* file_to_read = argv[1];
FILE* fr = fopen(file_to_read, "r");
if (fr == NULL){
fprintf(stderr, "%s: trying to open file %s for read\n", strerror(errno), file_to_read);
exit(3);
}
rule_t rules = null;
char* target = NULL;
char** bld = NULL;
char** dep = NULL;
int bldIndex = 0;
int depIndex = 0;
while(1){
char* line = NULL;
size_t line_length;
int chars_read = getline(&line, &line_length, fr);
if (chars_read == -1){
if (feof(fr)){
free(line);
break;
}
perror("getline");
exit(4);
}
if (line[0] == '\t'){
bld[bldIndex] = malloc(strlen(line) + 1);
strcpy(bld[bldIndex], line);
bldIndex++;
}
else{
char* pch = strtok(line, ":");
int h;
for (h = 0; pch != NULL; h++){
if (h == 0){
target = pch;
}
else{
dep[depIndex] = malloc(strlen(pch) + 1);
strcpy(dep[depIndex], pch);
depIndex++;
printf("Target %s\n", target);
printf("Dependencies %s\n", pch);
}
pch = strtok(NULL, " :");
}
}
bldIndex = 0;
depIndex = 0;
rules->dependency = dep;
rules->buildcmd = bld;
rules->target = target;
HashTableInsert(h, target, rules);
free(line);
}
fclose(fr);
}
return 0;
}
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "hash.h"
int main(int argc, char *argv[]){
if (argc == 1){
fprintf(stderr, "memake: *** No targets specified and no makefile found. Stop.\n");
return 2;
}
else{
HashTable* t = HashTableConstruct(50);
char* file_to_read = argv[1];
FILE* fr = fopen(file_to_read, "r");
if (fr == NULL){
fprintf(stderr, "%s: trying to open file %s for read\n", strerror(errno), file_to_read);
exit(3);
}
rule_t rules;
char** bld = rules.dependency;
char** dep = rules.buildcmd;
int bldIndex = 0;
int depIndex = 0;
while(1){
char* line = NULL;
size_t line_length;
int chars_read = getline(&line, &line_length, fr);
if (chars_read == -1){
if (feof(fr)){
free(line);
break;
}
perror("getline");
exit(4);
}
if (line[0] != '\t'){
char* pch = strtok(line, ":");
int h;
for (h = 0; pch != NULL; h++){
if (h == 0){
printf("Target %s\n", pch);
HashTableInsert(t, pch, &rules);
rules.target = pch;
bldIndex = 0;
depIndex = 0;
}
else{
dep = realloc(dep, sizeof(char *) * depIndex + 1);
dep[depIndex] = malloc(strlen(pch) + 1);
strcpy(dep[depIndex], pch);
rules.depSize = depIndex + 1;
depIndex++;
printf("Dependencies %s\n", pch);
}
pch = strtok(NULL, " ");
}
}
else{
bld = realloc(bld, sizeof(char *) * bldIndex + 1);
bld[bldIndex] = malloc(strlen(line) + 1);
strcpy(bld[bldIndex], line);
rules.bldSize = bldIndex + 1;
bldIndex++;
}
free(line);
}
fclose(fr);
}
return 0;
}
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "hash.h"
int main(int argc, char *argv[]){
if (argc == 1){
fprintf(stderr, "memake: *** No targets specified and no makefile found. Stop.\n");
return 2;
}
else{
HashTable* t = HashTableConstruct(50);
char* file_to_read = argv[1];
FILE* fr = fopen(file_to_read, "r");
if (fr == NULL){
fprintf(stderr, "%s: trying to open file %s for read\n", strerror(errno), file_to_read);
exit(3);
}
rule_t* rule = malloc(sizeof(rule_t));
char** bld = NULL;
char** dep = NULL;
int bldIndex = 0;
int depIndex = 0;
int flag = 0;
while(1){
char* line = NULL;
size_t line_length;
int chars_read = getline(&line, &line_length, fr);
if (chars_read == -1){
if (feof(fr)){
free(line);
}
perror("getline");
exit(4);
}
if (line[0] != '\t'){
if (flag == 1){
rule->dependency = dep;
rule->buildcmd = bld;
rule->bldSize = bldIndex + 1;
rule->depSize = depIndex + 1;
rule_t* ru = malloc(sizeof(rule_t));
RuleConstruct(rule, ru);
HashTableInsert(t, rule->target, ru);
RuleDestruct(rule);
flag = 0;
bldIndex = 0;
depIndex = 0;
}
char* pch = strtok(line, ":");
int h;
for (h = 0; pch != NULL; h++){
if (h == 0){
rule->target = pch;
printf("Target %s\n", pch);
}
else{
dep = realloc(dep, sizeof(char *) * (depIndex + 1));
dep[depIndex] = malloc(strlen(pch) + 1);
strcpy(dep[depIndex], pch);
printf("Dependencies %s\n", dep[depIndex] );
depIndex++;
}
pch = strtok(NULL, " ");
}
}
else if (line[0] == '\t'){
flag = 1;
bld = realloc(bld, sizeof(char *) * (bldIndex + 1));
bld[bldIndex] = malloc(strlen(line) + 1);
strcpy(bld[bldIndex], line);
printf("%s\n", bld[bldIndex]);
bldIndex++;
}
free(line);
}
fclose(fr);
}
return 0;
}
rule_t* RuleConstruct(rule_t* rule, rule_t* newrule)
{
char** dep = (char**) malloc(sizeof(char*) * rule->depSize);
int h;
for (h = 0; h < rule->depSize; h++){
dep[h] = malloc((strlen(rule->dependency[h]) + 1) * sizeof(char));
strcpy(dep[h], rule->dependency[h]);
}
char** bld = (char**) malloc(sizeof(char*) * rule->bldSize);
int i;
for (i = 0; i < rule->bldSize; i++){
dep[i] = malloc((strlen(rule->buildcmd[i]) + 1) * sizeof(char));
strcpy(dep[i], rule->buildcmd[i]);
}
char* target = (char*) malloc((strlen(rule->target) + 1) * sizeof(char));
strcpy(target, rule->target);
newrule->dependency = dep;
newrule->buildcmd = bld;
newrule->bldSize = rule->bldSize;
newrule->depSize = rule->depSize;
newrule->target = target;
return newrule;
}
boolean HashTableInsert(HashTable *this, const char* key, rule_t* rule)
{
list_t *temp;
int_u4 hashval = StringHash(key) % this->size;
/* Does item already exist? */
for (temp = this->table[hashval]; temp != NULL; temp = temp->next){
if (strcmp(key, temp->rule->target) == 0){
RuleDestruct(temp->rule);
rule_t* newrule;
temp->rule = RuleConstruct(rule, newrule);
return false;
}
}
list_t *new_list;
if ((new_list = malloc(sizeof(list_t))) == NULL)
outOfMemory_(__LINE__, __FILE__, "struct node creation");
rule_t* newrule = malloc(sizeof(rule_t));
new_list->rule = RuleConstruct(rule, newrule);
new_list->next = this->table[hashval];
this->table[hashval] = new_list;
this->length = this->length + 1;
return true;
}
void recursive(rule_t* r){
int index = 0;
if (r->depSize == index)
return;
else{
recursive(rule->dependency[index])
execute build command here
index++;
}
}
char* pch = strtok(line, " ");
int h;
for (h = 0; pch != NULL; h++){
if (h == 0){
target = StringConstruct(pch);
pch = strtok(line, ":");
printf("Target %s\n", target);
}
else{
dep = realloc(dep, sizeof(char *) * (depIndex + 1));
dep[depIndex] = malloc(strlen(pch) + 1);
strcpy(dep[depIndex], pch);
printf("Length is %d\n", strlen(dep[depIndex]));
printf("Dependencies %s\n", dep[depIndex] );
depIndex++;
}
pch = strtok(NULL, " ");
}
static void processRule(rule_t* r, HashTable* hash)
{
if (r == NULL || r->depSize == 0){
return;
}
else{
int h;
rule_t* temp;
for (h = 0; h < r->depSize; h++){
temp = HashTableLookup(hash, r->dependency[h]);
processRule(temp, hash);
}
printf("BUILD TARGET IS %s WITH BUILD SIZE %d\n", r->target, r->bldSize);
}
int i;
for (i = 0; i< r->depSize; i++){
if (FileExists(r->dependency[i]) == 0){
if (HashTableContains(hash, r->dependency[i]) == 1){
printf("HAHAHA\n");
processRule(HashTableLookup(hash, r->dependency[i]), hash);
}
else{
printf("ERROR\n");
exit(4);
}
}
else{
if (FileExists(r->target) == 1){
if (TimeInfo(r->target, r->dependency[i]) == 0){
int h;
printf("FILE %s HAS BEEN EDITED, NEED TO REBUILD\n", r->dependency[i]);
for (h = 0; h< r->bldSize; h++){
system(r->buildcmd[h]);
}
else{
printf("FILE %s DOESN'T NEED TO BE BUILD AS NOTHING IS MODIFIED\n", r->dependency[i]);
}
} else{
printf("%s DOES NOT EXIST, SO BUILD ONE\n", r->target);
int m;
for (m = 0; m< r->bldSize; m++){
system(r->buildcmd[m]);
}
}
}
}
}
}
static boolean FileExists(const char* filename)
{
struct stat buf;
struct stat* bp = &buf;
int e = stat(filename, bp);
if (e == 0){
if (S_ISREG(buf.st_mode)){
return true;
}else{
return false;
}
}else{
return false;
}
}
static boolean TimeInfo(char* file1, char* file2)
{
if (FileModTime(file1) > FileModTime(file2)){
return true;
}
else{
return false;
}
}
static time_t FileModTime(const char* filename)
{
if (FileExists(filename)){
struct stat buf;
int e = stat(filename, &buf);
if (e ==1)
return 0;
printf("Time for %s is %d\n", filename, buf.st_mtime);
return buf.st_mtime;
}
printf("FILE NOT FOUND");
return 0;
}
if ((FileExists(r->target) == 0 || (FileExists(r->target) == 1 && TimeInfo(r->target, r->dependency[i]) == 0)){
printf("FILE %s HAS BEEN EDITED, NEED TO REBUILD OR FILE %s DOES NOT EXIST\n", r->dependency[i], r->target);
int h;
for (h = 0; h< r->bldSize; h++){
system(r->buildcmd[h]);
}
}
else{
printf("FILE %s DOESN'T NEED TO BE BUILD AS NOTHING IS MODIFIED\n", r->dependency[i]);
}
man popen
man exec
Regards
Friedrich