3 // Copyright (C) 2002 by Brian Palmer <brianp@sginet.com>
10 #define ERROR_SUCCESS 0
11 #define ERROR_NOTENOUGHPARAMS 1
12 #define ERROR_DEPENDFILENOTFOUND 2
13 #define ERROR_OUTOFMEMORY 3
14 #define ERROR_READERROR 4
15 #define ERROR_WRITEERROR 5
17 int main(int argc
, char *argv
[])
22 char* NewDependFileData
;
25 int RuleDependencySplit
= 0;
27 // Make sure they passed enough command line parameters
30 printf("Usage: deptool srcfile.d\n");
31 return ERROR_NOTENOUGHPARAMS
;
34 // Try to open the dependency file
35 DependFile
= fopen(argv
[1], "r+t");
36 if (DependFile
== NULL
)
38 printf("deptool: No such dependency file: %s\n", argv
[1]);
39 return ERROR_DEPENDFILENOTFOUND
;
43 fseek(DependFile
, 0, SEEK_END
);
44 DependFileSize
= ftell(DependFile
);
48 DependFileData
= (char *)malloc(DependFileSize
);
49 NewDependFileData
= (char *)malloc(DependFileSize
* 3);
50 if (!DependFileData
|| !NewDependFileData
)
52 printf("deptool: Out of memory!\n");
53 if (DependFileData
!= NULL
)
55 if (NewDependFileData
!= NULL
)
56 free(NewDependFileData
);
58 return ERROR_OUTOFMEMORY
;
60 memset(DependFileData
, 0, DependFileSize
);
61 memset(NewDependFileData
, 0, DependFileSize
* 3);
64 fread(DependFileData
, 1, DependFileSize
, DependFile
);
65 if (ferror(DependFile
))
67 printf("deptool: Dependency file read error.\n");
69 free(NewDependFileData
);
71 return ERROR_READERROR
;
74 // Loop through the dependency file data and
75 // insert the rule for the dependency file itself
76 for (CurIdx
=0,CurIdx2
=0; DependFileData
[CurIdx
]; CurIdx
++,CurIdx2
++)
78 // Find the first colon ':' in the file and insert
79 // the rule right before it
80 if (DependFileData
[CurIdx
] == ':')
82 NewDependFileData
[CurIdx2
] = ' ';
84 strcat(&NewDependFileData
[CurIdx2
], argv
[1]);
85 CurIdx2
+= strlen(argv
[1]);
86 NewDependFileData
[CurIdx2
] = ' ';
88 strcat(NewDependFileData
, &DependFileData
[CurIdx
]);
90 RuleDependencySplit
= CurIdx
+ 2;
95 NewDependFileData
[CurIdx2
] = DependFileData
[CurIdx
];
99 // Now loop through all the rule dependencies and
100 // turn them into rules themselves
101 strcat(NewDependFileData
, "\n\n");
102 CurIdx
= RuleDependencySplit
;
103 CurIdx2
= strlen(NewDependFileData
);
104 for (; DependFileData
[CurIdx
]; CurIdx
++,CurIdx2
++)
106 // If it's a line continuation char '\' then skip over it
107 if (DependFileData
[CurIdx
] == '\\')
113 // If it's a new line char '\n' then insert a colon ':' to make it a rule
114 if (DependFileData
[CurIdx
] == '\n')
116 NewDependFileData
[CurIdx2
] = ':';
120 NewDependFileData
[CurIdx2
] = DependFileData
[CurIdx
];
123 // Write out file data
125 fwrite(NewDependFileData
, 1, strlen(NewDependFileData
), DependFile
);
126 if (ferror(DependFile
))
128 printf("deptool: Dependency file write error.\n");
130 free(DependFileData
);
131 free(NewDependFileData
);
132 return ERROR_WRITEERROR
;
136 free(DependFileData
);
137 free(NewDependFileData
);
138 return ERROR_SUCCESS
;