- fnlen = strlen(home) + strlen(cpf) + 13;
- filename = malloc(fnlen);
- snprintf(filename, fnlen, "%s/%s.REC.XXXXXX", home, cpf);
- mask = umask(0177);
- fd = mkstemp(filename);
- if (fd < 0) {
- fprintf(stderr, "Could not create receipt file: %s\n",
- strerror(errno));
- goto out;
+ /* Now it's time to decide which filename to write. We use
+ the declaration's filename as a base layout, because the
+ proprietary version of the IRPF program only recognizes
+ receipts if they have the same name as the declaration
+ files (disconsidering the extensions). For example, if the
+ declaration file is named "123.DEC", the receipt should be
+ named "123.REC". Therefore, if the declaration file has
+ the ".DEC" extension, we strip it out and add the ".REC".
+ Otherwise, we use the default template, which is to save
+ the receipt with the name "$CPF.REC". */
+ tmp = strstr(args->input_file, ".DEC");
+ if (tmp != NULL && tmp[sizeof(".DEC") - 1] == '\0') {
+ const char *p;
+ /* We found the ".REC" extension. */
+ p = strdup(args->input_file);
+ /* Replacing the ".DEC" by ".REC". Fortunately, we
+ just have to change one letter. */
+ tmp = strstr(p, ".DEC");
+ tmp[1] = 'R';
+ fname_len = strlen(p) + strlen(path) + 2;
+ fname = alloca(fname_len);
+ snprintf(fname, fname_len, "%s/%s", path, p);
+ } else {
+ /* The declaration filename does not follow the
+ convention, so we will not use it as a template.
+ We just generate a filename using "$CPF.REC". */
+ fname_len = strlen(cpf) + strlen(path) + sizeof(".REC") + 2;
+ fname = alloca(fname_len);
+ snprintf(fname, fname_len, "%s/%s.REC", path, cpf);