int main (int argc, const char * argv[]) { char dest[1024]; // destination file name char destDC[1024]; // "double convert" destination char *input = NULL; char *srcEnc = NULL; char *destEnc = NULL; FILE *in = NULL; FILE *out = NULL; FILE *outDC = NULL; unsigned char *inStr = NULL; unsigned char *outStr = NULL; unsigned char *outDCStr = NULL; unsigned long inLen = 0; unsigned long outLen = 0; unsigned long outDCLen = 0; if (argc < 2) { input = malloc(1024 + 1); if (!input) { fprintf(stderr, "%s: %s\n", argv[0], "Error! No file input."); exit(0); } printf("Please enter a file to test: "); fscanf(stdin, "%s", input); } else { input = strdup(argv[1]); } if (argc < 3) { srcEnc = malloc(1024 + 1); if (!srcEnc) { fprintf(stderr, "%s: %s\n", argv[0], "Error! No encoding."); exit(0); } printf("Please enter the input encoding: "); fscanf(stdin, "%s", srcEnc); } else { srcEnc = strdup(argv[2]); } if (argc < 4) { destEnc = malloc(1024 + 1); if (!destEnc) { fprintf(stderr, "%s: %s\n", argv[0], "Error! No encoding."); exit(0); } printf("Please enter the output encoding: "); fscanf(stdin, "%s", destEnc); } else { destEnc = strdup(argv[3]); } WriteProgress(1); memset(dest, 0, 1024); strcpy(dest, input); strcpy(dest + strlen(input), "_out"); printf(dest); printf("\n"); memset(destDC, 0, 1024); strcpy(destDC, input); strcpy(destDC + strlen(input), "_doubled"); printf(destDC); printf("\n"); WriteProgress(2); inLen = fsize(input); WriteProgress(3); in = fopen(input, "r"); if (!in) goto cleanup; WriteProgress(4); out = fopen(dest, "w"); if (!out) goto cleanup; WriteProgress(5); outDC = fopen(destDC, "w"); if (!outDC) goto cleanup; WriteProgress(6); inStr = malloc(inLen); if (!inStr) goto cleanup; fread(inStr, inLen, 1, in); WriteProgress(7); // Convert from source encoding to dest outLen = intlConvertStrings(inStr, srcEnc, inLen, &outStr, destEnc); if (outLen) { WriteProgress(8); fwrite(outStr, outLen, 1, out); WriteProgress(9); } else { WriteProgress(10); goto cleanup; } WriteProgress(11); // Convert from dest back to source outDCLen = intlConvertStrings(outStr, destEnc, outLen, &outDCStr, srcEnc); if (outLen) { WriteProgress(12); fwrite(outDCStr, outDCLen, 1, outDC); WriteProgress(13); } else { WriteProgress(14); goto cleanup; } WriteProgress(15); cleanup: WriteProgress(16); free(input); free(srcEnc); free(destEnc); WriteProgress(17); if (inStr) free(inStr); if (outStr) free(outStr); if (outDCStr) free(outDCStr); WriteProgress(18); if (in) fclose(in); if (out) fclose(out); if (outDC) fclose(outDC); WriteProgress(19); return 0; }
static int SynoddOne(char *szDev, BOOL blRead) { int err = -1; int fd = -1; int counts = 0; ssize_t lProgressBytes = 0; unsigned long long ullProgTotalBytes = 0; unsigned long long ullTotalBytes = 0; FILE *fp = NULL; if (!szDev) { return -1; } if (0 > (fd = OpenDevice(szDev, blRead, 0))){ err = errno; goto END; } if (0 > CheckDevice(fd, &ullTotalBytes)){ err = errno; syslog(LOG_ERR, "%s(%d) failed to check [%s].", __FILE__, __LINE__, szDev); goto END; } //Open Progress File if (NULL == (fp = OpenProgress(szDev))) { goto END; } err = 0; while(1) { if (COUNTS_TO_CHECK == counts) { counts = 0; if (fd >= 0) { close(fd); } if (0 > (fd = OpenDevice(szDev, blRead, ullProgTotalBytes))){ err = errno; goto END; } } if (blRead) { lProgressBytes = read(fd, gszBuf, sizeof(gszBuf)); } else { lProgressBytes = write(fd, gszBuf, sizeof(gszBuf)); } if (0 > lProgressBytes) { if (ENOSPC != errno) { syslog(LOG_ERR, "(%s/%d) %s [%s] failed, errno=%m" ,__FILE__ ,__LINE__ , blRead?"read":"write", szDev); err = errno; } goto END; } ullProgTotalBytes += lProgressBytes; WriteProgress(fp, ullProgTotalBytes, ullTotalBytes); if (ullTotalBytes <= ullProgTotalBytes) { break; } counts++; } END: if (0 <= fd) close(fd); if (NULL != fp) fclose(fp); return err; }