Annotation of OpenXM/doc/OpenXM-web/version.c, Revision 1.1
1.1 ! takayama 1: /* $OpenXM$ */
! 2: #include <stdio.h>
! 3: #include <sys/types.h>
! 4: #include <sys/stat.h>
! 5:
! 6: #define SIZE 1024
! 7: main(int argc, char *argv[]) {
! 8: int c,i,j;
! 9: int counter = 0;
! 10: int line = 0;
! 11: char s[SIZE];
! 12: struct stat sb;
! 13: FILE *fp;
! 14: FILE *fp2;
! 15: char fin[SIZE];
! 16: char oldversion[SIZE];
! 17: char newversion[SIZE];
! 18: char oldversion2[SIZE];
! 19: char newversion2[SIZE];
! 20: char comm[SIZE];
! 21: oldversion[0] = newversion[0] = fin[0] = 0;
! 22:
! 23: fp = fopen(".tmp.txt","w");
! 24: if (fp == NULL) exit(10);
! 25:
! 26: for (i=1; i<argc; i++) {
! 27: if (strcmp(argv[i],"--oldversion")==0) {
! 28: i++;
! 29: strcpy(oldversion,argv[i]);
! 30: strcpy(oldversion2,argv[i]);
! 31: for (j=0; j<strlen(oldversion2); j++)
! 32: if (oldversion2[j] == '.') oldversion2[j] = '_';
! 33: }else if (strcmp(argv[i],"--newversion")==0) {
! 34: i++;
! 35: strcpy(newversion,argv[i]);
! 36: strcpy(newversion2,argv[i]);
! 37: for (j=0; j<strlen(newversion2); j++)
! 38: if (newversion2[j] == '.') newversion2[j] = '_';
! 39: }else if (strcmp(argv[i],"--file") == 0) {
! 40: i++;
! 41: strcpy(fin,argv[i]);
! 42: }
! 43: }
! 44:
! 45: if (fin[0] == 0 || oldversion[0] == 0 || newversion[0] == 0) {
! 46: fprintf(stderr,"options are not valid.\n");
! 47: usage();
! 48: exit(10);
! 49: }
! 50:
! 51: fp2 = fopen(fin,"r");
! 52: if (fp2 == NULL) {
! 53: fprintf(stderr,"File is not found.\n");
! 54: usage();
! 55: exit(10);
! 56: }
! 57:
! 58: while (fgets(s,SIZE,fp2) != NULL) {
! 59: if (substr(s,oldversion) && !substr(s,"Current")) {
! 60: replace(s,oldversion,newversion,fp);
! 61: }else if (substr(s,oldversion2)) {
! 62: replace(s,oldversion2,newversion2,fp);
! 63: }else{
! 64: fputs(s,fp);
! 65: }
! 66: }
! 67: fclose(fp);
! 68: sprintf(comm,"rm -f %s",fin);
! 69: system(comm);
! 70: sprintf(comm,"mv .tmp.txt %s",fin);
! 71: system(comm);
! 72: }
! 73:
! 74: usage() {
! 75: fprintf(stderr,"version --oldversion x.y.z --newversion p.q.r --file finename\n");
! 76: }
! 77:
! 78: there(char s[],int c) {
! 79: int i;
! 80: int n;
! 81: n = strlen(s);
! 82: for (i=0; i<n; i++) {
! 83: if (s[i] == c) return i;
! 84: }
! 85: return -1;
! 86: }
! 87:
! 88: substr(char s[],char a[]) {
! 89: int n,m,i,j;
! 90: n = strlen(s);
! 91: m = strlen(a);
! 92: for (i=0; i<n-m; i++) {
! 93: for (j=0; j<m; j++) {
! 94: if (s[i+j] != a[j]) break;
! 95: if (j == m-1) return 1;
! 96: }
! 97: }
! 98: return 0;
! 99: }
! 100:
! 101:
! 102: replace(char s[],char a[],char newversion[],FILE *fp) {
! 103: int n,m,i,j,k;
! 104: n = strlen(s);
! 105: m = strlen(a);
! 106: for (i=0; i<n-m; i++) {
! 107: for (j=0; j<m; j++) {
! 108: if (s[i+j] != a[j]) break;
! 109: if (j == m-1) {
! 110: for (k=0; k<i; k++) {
! 111: fputc(s[k],fp);
! 112: }
! 113: fputs(newversion,fp);
! 114: for (k=i+m; k<n; k++) {
! 115: fputc(s[k],fp);
! 116: }
! 117: return 1;
! 118: }
! 119: }
! 120: }
! 121: return 0;
! 122: }
! 123:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>