2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
17 /*---------------------------------------------------------------------------*/
19 Fatal_(int ln, char *fn, char *kind)
22 fprintf(stderr,"Fatal error. (%s:%d)\n",fn,ln);
23 fprintf(stderr,"%s Fatal error: %s\n",FileName, kind);
25 #define Fatal(foo) Fatal_(__LINE__,__FILE__,foo)
26 #define Assert() Fatal_(__LINE__,__FILE__,"Assert failed.")
28 /*---------------------------------------------------------------------------*/
29 /* get next field, check that the terminating whitespace is what we expect */
31 Ffield(FILE *fd, MD5_CTX *ctx,u_char term)
33 static u_char buf[BUFSIZ];
37 if((i=getc(fd)) == EOF) {
38 Fatal("Truncated patch.");
45 Fatal("Corrupt patch.");
46 printf("Token is too long.\n");
52 if(buf[l-1] != term) {
53 Fatal("Corrupt patch.");
54 fprintf(stderr,"Expected \"%s\" but didn't find it {%02x}.\n",
55 term == '\n' ? "\\n" : " ",buf[l-1]);
57 fprintf(stderr,"{%s}\n",buf);
62 fprintf(stderr,"<%s>\n",buf);
67 Fbytecnt(FILE *fd, MD5_CTX *ctx, u_char term)
72 p = Ffield(fd,ctx,term);
76 Fatal("Bytecount contains non-digit.");
80 u_chars += (*q - '0');
86 Fdata(FILE *fd, int u_chars, MD5_CTX *ctx)
88 u_char *p = Malloc(u_chars+1);
90 if(u_chars+1 != fread(p,1,u_chars+1,fd)) {
91 Fatal("Truncated patch.");
94 MD5Update(ctx,p,u_chars+1);
95 if(p[u_chars] != '\n') {
97 printf("FileData wasn't followed by a newline.\n");
98 Fatal("Corrupt patch.");
105 /*---------------------------------------------------------------------------*/
106 /* get the filename in the next field, prepend BaseDir and give back the result
107 strings. The sustitute filename is return (the one with the suffix SUBSUFF)
108 if it exists and the qualifier contains CTM_Q_Name_Subst
109 NOTA: Buffer is already initialize with BaseDir, CatPtr is the insertion
110 point on this buffer + the length test in Ffield() is enough for Fname() */
113 Fname(FILE *fd, MD5_CTX *ctx,u_char term,int qual, int verbose)
118 if ((p = Ffield(fd,ctx,term)) == NULL) return(NULL);
122 if (!(qual & CTM_Q_Name_Subst)) return(Buffer);
124 p = Buffer + strlen(Buffer);
126 strcat(Buffer, SUBSUFF);
128 if ( -1 == stat(Buffer, &st) ) {
132 fprintf(stderr,"Using %s as substitute file\n", Buffer);