void revList(List L){ if(length(L) == 0){ return; } int tmp = front(L); deleteFront(L); revList(L); append(L,tmp); }
/* argv[1]=the pusher, argv[2]=file, argv[3]=the type of first receiver, argv[4]=1st receiver argv[5]=the type of first receiver, argv[6]=1st receiver */ int main(int argc, char *argv[]) { struct receivers *list = NULL; struct stat fileStat; time_t curTime; char *file; int timeLen; char *cmd;//the copy command if(argc < 5 || argc%2 != 1) { printf("use the correct args\n"); printf("like as: push user1 test.txt 0 user2 1 group1\n"); return -1; } if(lstat(argv[2], &fileStat) != 0)//the file is a regular file? { perror("lstat failed\n"); return -1; } if(!S_ISREG(fileStat.st_mode)) { printf("it's not a regular file\n"); return -1; } curTime = time(NULL);//change the file name in order to avoid the duplication of name timeLen = (int)log10f((float)curTime) + 1; file = (char *)malloc(sizeof(TMP)+ sizeof(basename(argv[2])) + timeLen); sprintf(file, "%s%s%d", TMP, basename(argv[2]), (int)curTime); //get the list of receives if(-1 == revList(argv, &list)) return -1; if(insert2db(argv[1], file, list, curTime, fileStat.st_size)) //insert the data to db return -1; //copy the file cmd = (char *)malloc(sizeof(argv[2])*2+timeLen+4); sprintf(cmd, "cp %s %s", argv[2], file); //printf("%s\n", cmd); system(cmd); //free the memory delMem(list); free(file); free(cmd); return 0; }
int main(int argc, char * argv[]){ int count = 0; FILE *in, *out; char line[MAX_LEN]; char *token; int graphOrder = 0; int edge[2]; char blank[] = " "; Graph G = NULL; // check command line for correct number of arguments if( argc != 3 ){ printf("Usage: %s <input file> <output file>\n", argv[0]); exit(1); } // open files for reading and writing in = fopen(argv[1], "r"); out = fopen(argv[2], "w"); if( in==NULL ){ printf("Unable to open file %s for reading\n", argv[1]); exit(1); } if( out==NULL ){ printf("Unable to open file %s for writing\n", argv[2]); exit(1); } /* read each line of input file, then count and print tokens */ if(fgets(line, MAX_LEN, in) == NULL){ printf("Unable to read first line of file"); exit(1); } graphOrder = atoi(line); G = newGraph(graphOrder); while( fgets(line, MAX_LEN, in) != NULL) { count++; token = strtok(line, blank); edge[0] = atoi(token); token = strtok(NULL, blank); edge[1] = atoi(token); if(edge[0] == 0 && edge[1] == 0){ break; } addArc(G,edge[0],edge[1]); } printGraph(out,G); fprintf(out, "\n"); List S = newList(); for(int i = 1; i <= getOrder(G); i++){ append(S, i); } DFS(G,S); Graph T = transpose(G); DFS(T,S); revList(S); printComponents(out,T,S); freeGraph(&G); freeGraph(&T); freeList(&S); fclose(in); fclose(out); return(0); }