fill_tables.c.ftp
来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· FTP 代码 · 共 1,224 行 · 第 1/3 页
FTP
1,224 行
}/** D.3.3.6 ENDSWITCH *//*** D.3.3.7 INCREMENT counter of entries*/ t3_cnt++; } /*If *//** D.3.3 ENDIF !extract ok*/ } /* WHile T3 *//*** D.3 ENDWHILE !not end of file */ DPRINT ("Level Defn table has %d entries\n", t3_cnt);/*** D.4 RETURN 0 !success*/ return(0);}/*** END OF FUNCTION *//****===================================================================* E. FUNCTION load_table2** INPUT: FILE *infile Table file reading from* table2 *parmtab ptr to array of structs type table2** RETURN CODE:* 0> succesful, lvltab has been filled with info;* 1> error;*===================================================================**/int load_table2 (FILE *infile, table2 *parmtab){int sub; /* which sub table, 0 means main table */int code; /* parm id */int Index;table2 *t2; /* points to beginning of Main table/Sub table */ DPRINT (".. load_table2\n"); /* Table2's Format: parmid "parm desc" "unit" -any blks that doesn't have a decimal digit in 1st column is treated as comment blks; -'#####' is treated as end of this section; *//** E.1 FOR (each Table 2 to load) * DO*/ for (sub=0; sub <= 5; sub++) {/** E.1.1 SWITCH (which table to load)* Table2: * SET up pointer to begining of Table 2* SET up table 2 heading* Table2-Sub A:* SET up pointer to begining of Table 2-A* SET up table 2-A heading* Table2-Sub B:* SET up pointer to begining of Table 2-B* SET up table 2-B heading* Table2-Sub C:* SET up pointer to begining of Table 2-C* SET up table 2-C heading* Table2-Sub D:* SET up pointer to begining of Table 2-D* SET up table 2-D heading* Table2-Sub E:* SET up pointer to begining of Table 2-E* SET up table 2-E heading* ENDSWITCH*/ switch (sub) { case 0: t2=parmtab; strcpy(heading,"Table 2"); break; case 1: t2=parmtab[250].sub_tab2; strcpy (heading, "Table 2 - Sub A"); break; case 2: t2=parmtab[251].sub_tab2; strcpy (heading, "Table 2 - Sub B"); break; case 3: t2=parmtab[252].sub_tab2; strcpy (heading, "Table 2 - Sub C"); break; case 4: t2=parmtab[253].sub_tab2; strcpy (heading, "Table 2 - Sub D"); break; case 5: t2=parmtab[254].sub_tab2; strcpy (heading, "Table 2 - Sub E"); break; } /* SWitch *//** E.1.2 IF (FUNCTION locate_sec_heading) failed* THEN* RETURN 1* ENDIF*/ if (locate_sect_heading(infile, heading, &line)!= 0) return(1);/** E.1.3 INIT entry counter*/ cnt=0; /* number of entries in this table *//** E.1.4 WHILE (not end of file yet) DO*/ while (!feof(infile)) {/** E.1.4.1 IF (FUNCTION read_file_entry failed)* THEN* BREAK* ENDIF*/ if (read_file_entry (infile, &line, blk)) break;/** E.1.4.2 IF (line doesn't start out with a number)* THEN* CONTINUE !skip line* ENDIF*/ if (!isdigit(blk[0])) continue; /* skip comments */ /** E.1.4.3 IF (fail to extract parm_id, dummy string)* THEN* CONTINUE !skip line* ENDIF*/ /* get Code, Dummy is 1st word of Param Field */ if (sscanf (blk, "%d%s", &code, dummy) != 2) { fprintf(stderr, "Line %d: fail to get code & param\n", line); return(1);} /** E.1.4.4 IF (parm_id is out of range)* THEN* CONTINUE !skip line* ENDIF*/ if (code<0 || code>255) continue; /* skip invalid code */ /* locate Param, move max of 75, then cap it where there are 3 consecutive spaces *//** E.1.4.5 IF (fail to track where string dummy starts)* THEN* PRINT error message* RETURN 1* ENDIF*/ if ((ptr= strstr (blk, dummy)) == NULL) { fprintf(stderr, "Line %d: Error reading Field_Param\n",line); return(1);}/** E.1.4.6 STORE parameter name*/ Index= ptr - blk; /* pos of 1st Parm word */ strncpy (t2[code].field_param, ptr, 75);/** E.1.4.7 IF (no two-spaces follow parm_name)* THEN* PRINT error* RETURN 1* ENDIF*/ if ((ptr= strstr (t2[code].field_param, " ")) == NULL) { fprintf(stderr, "Error on Line %d: must have 2 spaces between Field & Unit\n", line, code); return(1); }/** E.1.4.8 CAP off parm_name where it ends*/ t2[code].field_param[ptr-t2[code].field_param]='\0'; Index += strlen(t2[code].field_param); /* pos at end of Parm field */ /* skip over spaces bet/w Parm and Unit fields and then copy max of 25 chars into Unit; cap it where 2 spaces occur; *//** E.1.4.9 LOCATE where Unit field begins* E.1.4.10 STORE Unit field*/ Index= strspn (blk+Index, " "); strncpy (t2[code].unit, ptr+Index, 25); if ((ptr= strstr (t2[code].unit, " ")) != NULL) t2[code].unit[ptr-t2[code].unit]='\0'; if (t2[code].unit[strlen(t2[code].unit)-1] == ' ') t2[code].unit[strlen(t2[code].unit)-1] = '\0';/** E.1.4.11 INCREMENT entry counter*/ cnt++; } /* T2 *//** E.1.4 ENDWHILE !end of file*/ DPRINT ("Defn %s has %d entries\n", heading, cnt); } /* FOr each table *//** E.1 ENDFOR !each Table 2*/return(0);/*** E.2 RETURN 0 !success*/}/* * END OF FUNCTION*//****===================================================================* F. FUNCTION FTP_getfile ** PURPOSE: * builds and executes a Bourne script file to retreive * the file specified from remote site via Ftp call;* get name of remote host & dir from "$pathnm/table.cfg";* use curr userid as anonymous password;** INPUT: * FILE *infile Name of file to ftp* char *loc_pathnm full path where config file is* "tables.cfg" config file- has info of remote host* * RETURN CODE:* 0> sucessfully ftp-ed; * 1> error: create script/ftp err/missing table.cfg;*===================================================================*/int FTP_getfile (char *filenm, char *loc_pathnm){FILE *f1, *f2;char filename[200];char hostnm[100]; /* name of remote site */char usernm[100]; /* using anonymous */char passwd[100]; /* anonymous */char pathnm[100]; /* full path of remote file to get */int n; /* working var */ DPRINT ("Entering FTP_getfile (%s/%s)\n", loc_pathnm, filenm);/* Open external config file to get remote host info *//** F.1 SET up name of local config file * !local_path/tables.cfg*/ sprintf (filename, "%s/tables.cfg", loc_pathnm); DPRINT ("Read Remote host info from '%s'\n", filename);/** * F.2 IF (unable to open config file)* PRINT error* RETURN 1* ENDIF*/ if ((f1=fopen (filename, "r"))==NULL) { fprintf(stderr, "Warning: '%s' is missing, cannot try to FTP\n\n", filename); return(1); }/*** F.3 READ hostname and remote pathname from file* * F.4 CLOSE up config file*/ n = fscanf (f1, "%s%s", hostnm, pathnm);; fclose(f1); /* close Config File *//*** F.5 IF (read failed)* PRINT message* RETURN 1*/ if (n != 2) { fprintf(stderr, "Warning: Fail to read '%s'\n", filename); return(1); }/** * F.6 SET password to "gribsimp22"*/ strcpy (passwd, "gribsimp22"); /* Ready to build Bourne script: *//*** F.7 IF (create temp script file fails)* PRINT error* RETURN 1* ENDIF*/ if ((f1=fopen ("temp_ftp_script","w"))==NULL) { fprintf(stderr,"Warning: failed to build FTP script\n"); return(1); }/*** F.8 CREATE ftp script & store in temp file** F.9 CLOSE temp file*/ fprintf (f1, "#!/bin/sh\n" "exec 1>&-;exec 2>&-\n" "ftp -in %s << STOP\n" "user anonymous %s\n" "cd %s\n" "lcd %s\n" "get %s\n" "quit\n" "STOP\n" "exit\n", hostnm, passwd, pathnm, loc_pathnm, filenm); fclose(f1); DPRINT ("execute ftp script: \n" " #!/bin/sh\n exec 1>&-;exec 2>&-\n" " ftp -in %s << STOP\n user anonymous %s\n" " cd %s\n lcd %s\n get %s\n quit\n STOP\n exit\n", hostnm, passwd, pathnm, loc_pathnm, filenm);/*** F.10 EXECUTE temp script** F.11 REMOVE script*/ n= system ("chmod 755 temp_ftp_script;temp_ftp_script"); unlink ("temp_ftp_script"); /*** F.12 IF (execute script failed)* PRINT error* RETURN 1* ENDIF*/ if (n!=0) { /* ck Stat of Systm call */ fprintf(stderr,"Warning: system call to ftp failed\n"); return(1); }/*** F.13 CHECK if ftp-ed file is available & readable* IF (failed)* PRINT warning* RETURN 1* ENDIF*/ sprintf (filename, "%s/%s", loc_pathnm, filenm); if ((f2= fopen(filename, "rb+"))==NULL) { fprintf(stderr,"Warning: '%s' not avail on %s in %s\n\n", filenm, hostnm, pathnm); return(1); }/*** F.14 CLOSE up ftp-ed file*/ fclose(f2);/** F.15 RETURN 0 !success*/DPRINT ("Leaving FTP_getfile with no errors\n");return(0);/** END OF FUNCTION**/}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?