2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
15 /*---------------------------------------------------------------------------*/
17 Fatal_(int ln, char *fn, char *kind)
20 fprintf(stderr,"Fatal error. (%s:%d)\n",fn,ln);
21 fprintf(stderr,"%s Fatal error: %s\n",FileName, kind);
23 #define Fatal(foo) Fatal_(__LINE__,__FILE__,foo)
24 #define Assert() Fatal_(__LINE__,__FILE__,"Assert failed.")
26 /*---------------------------------------------------------------------------*/
27 /* get next field, check that the terminating whitespace is what we expect */
29 Ffield(FILE *fd, MD5_CTX *ctx,u_char term)
31 static u_char buf[BUFSIZ];
35 if((i=getc(fd)) == EOF) {
36 Fatal("Truncated patch.");
43 Fatal("Corrupt patch.");
44 printf("Token is too long.\n");
50 if(buf[l-1] != term) {
51 Fatal("Corrupt patch.");
52 fprintf(stderr,"Expected \"%s\" but didn't find it {%02x}.\n",
53 term == '\n' ? "\\n" : " ",buf[l-1]);
55 fprintf(stderr,"{%s}\n",buf);
60 fprintf(stderr,"<%s>\n",buf);
65 Fbytecnt(FILE *fd, MD5_CTX *ctx, u_char term)
70 p = Ffield(fd,ctx,term);
74 Fatal("Bytecount contains non-digit.");
78 u_chars += (*q - '0');
84 Fdata(FILE *fd, int u_chars, MD5_CTX *ctx)
86 u_char *p = Malloc(u_chars+1);
88 if(u_chars+1 != fread(p,1,u_chars+1,fd)) {
89 Fatal("Truncated patch.");
92 MD5Update(ctx,p,u_chars+1);
93 if(p[u_chars] != '\n') {
95 printf("FileData wasn't followed by a newline.\n");
96 Fatal("Corrupt patch.");
103 /*---------------------------------------------------------------------------*/
104 /* get the filename in the next field, prepend BaseDir and give back the result
105 strings. The sustitute filename is return (the one with the suffix SUBSUFF)
106 if it exists and the qualifier contains CTM_Q_Name_Subst
107 NOTA: Buffer is already initialize with BaseDir, CatPtr is the insertion
108 point on this buffer + the length test in Ffield() is enough for Fname() */
111 Fname(FILE *fd, MD5_CTX *ctx,u_char term,int qual, int verbose)
116 if ((p = Ffield(fd,ctx,term)) == NULL) return(NULL);
120 if (!(qual & CTM_Q_Name_Subst)) return(Buffer);
122 p = Buffer + strlen(Buffer);
124 strcat(Buffer, SUBSUFF);
126 if ( -1 == stat(Buffer, &st) ) {
130 fprintf(stderr,"Using %s as substitute file\n", Buffer);