/* returns char ponter with string in it */ char* get_file_name () { printf("Enter file_name size\n"); int file_name_size = xscanf (); char *name_format = calloc ( sizeof(char) , 10 ); if ( name_format == NULL ) { printf("malloc failed\n"); return 0; } char *file_name = calloc ( sizeof(char) , file_name_size); if ( file_name == NULL ) { printf("malloc failed\n"); return 0; } printf("Enter file_name : only the chars upto specifies length will be taken\n"); sprintf( name_format , "%c%ds" , '%',file_name_size + 1); scanf( name_format , file_name ); while ( getchar() != '\n' ); printf("you entered :\t%s\n",file_name ); return file_name ; }
int main(int argc, char **argv) { /* Variables required by the algorithm. */ unsigned int old_n, n; unsigned short int **A; unsigned short int **B; unsigned short int **C; /* Variables required to calculate run time. */ clock_t cs, ce; /* Check presence of parameters. */ if (argc > 3) { if (strcmp(argv[1], "-r") == 0) { /* Get seed from mix function for filling the matrices. */ unsigned long int seed = mix(clock(), time(NULL), getpid()); old_n = atoi(argv[2]); n = normalize_power_of_two(old_n); A = init_matr(&n); B = init_matr(&n); C = init_matr(&n); srand(seed); /* Fill A and B randomly with 0s and 1s. */ randomize(A, &n); randomize(B, &n); } else { usage(); } } else if (argc == 2) { /* If only the flag is passed and not the number, exit with an error. */ if (strcmp(argv[1], "-r") == 0) usage(); xscanf("%u", &old_n); /* Normalize n to the closest power of two. */ n = normalize_power_of_two(old_n); A = init_matr(&n); B = init_matr(&n); C = init_matr(&n); fill_matr(A, &n, &old_n); fill_matr(B, &n, &old_n); } else { usage(); } cs = clock(); C = strassen(A, B, n); ce = clock(); printf("%u %.20fs\n", old_n, (float) (ce - cs) / CLOCKS_PER_SEC); if (strcmp(argv[3], "--scale-test") != 0) print_matr(C, &old_n); clear_matr(A, &n); clear_matr(B, &n); clear_matr(C, &n); return EXIT_SUCCESS; }
/* Calls n squared scanfs in order to fill the matrix with data. */ void fill_matr(unsigned short int **matr, unsigned int *n, unsigned int *old_n) { for (unsigned int i = 0; i < *n; i++) for (unsigned int j = 0; j < *n; j++) { /* Validates the case where the current row/column was added * due to normalization of the size. */ if (j > ((*old_n) - 1) || i > ((*old_n) - 1)) matr[i][j] = 0; else xscanf("%hu", &matr[i][j]); } }
//---------------------------------------------------------------------// int main(int argc, char *argv[]) { char strChaine[MAX_PATH] = ""; //msg pour entrer le chemin de fichier a analysé printf("Chemin du fichier a analyser : "); //appeller la fonction xscanf pour recuperer le chemin de ficher xscanf(strChaine, sizeof(strChaine)); //appeller la fonction PEfichier pour tester le ficher PEfichier(strChaine); getchar(); return 0; }
static fint iniprinter( void ) /* * iniprinter reads the file lpdevices in the gip_loc directory and * searches for the different printers for the current host. This * information is stored in the print_struct. */ { char nam[MAXNAMLEN+1]; /* maximum length of printer name */ char cmd[MAXCMDLEN+1]; /* maximum length of command string */ char com[MAXCOMLEN+1]; /* maximum length of printer comment */ char fnameb[MAXFILNAMLEN+1]; /* buffer for file name */ char hnameb[MAXHOSTNAMLEN+1]; /* buffer for hostname */ char host[MAXHOSTS*MAXHOSTNAMLEN+1]; /* buffer for hostnames */ fchar fname; /* points to fnameb */ fchar hname; /* points to hnameb */ FILE *f; /* C file descriptor */ fint r; /* return value */ int col, row; /* columns and rows */ int hlen; /* length of hostname */ if (nprntrs) return( nprntrs ); /* already done */ hname.a = hnameb; hname.l = MAXHOSTNAMLEN; /* initialize f character */ r = hostname_c( hname ); /* get hostname */ if (r) return( -1 ); /* cannot obtain hostname */ hlen = nelc_c( hname ); /* length of hostname */ hnameb[hlen] = 0; /* add zero byte */ fname.a = fnameb; fname.l = MAXFILNAMLEN; /* initialize f character */ r = fname_c( tofchar( "gip_loc:lpdevices" ), fname ); if (r) return( -2 ); /* fnameb too small */ fnameb[nelc_c( fname )] = 0; /* add zero byte */ f = fopen( fnameb, "r" ); /* open file */ if (f == NULL) return( -3 ); /* cannot open file */ while (xscanf( f, "%s %s %s %s %d %d", host, nam, com, cmd, &col, &row ) == 6) { char *hptr; /* pointer */ int found = 0; /* reset */ if (!strcmp( host, "*" )) { /* wild card */ found = 1; /* bingo */ } else if ( (hptr = strstr( host, hnameb )) != NULL ) { found = 1; /* bingo */ if (hptr != host) { /* not first in list */ if (hptr[-1] != ',') found = 0; /* no match */ } if (found && hptr[hlen] != 0 && hptr[hlen] != '.' && hptr[hlen] != ',') { found = 0; /* no match */ } } if (found) { /* we found one */ int nprn = 0; /* reset */ while (nprn < nprntrs && strcmp( nam, prntrs[nprn].nam ) ) nprn++; if ( nprn == nprntrs ) { /* not in list */ prntrs = realloc( prntrs, sizeof( print_struct ) * (++nprntrs) ); if (prntrs == NULL) { /* error */ r = -4; /* cannot allocate space */ break; /* quit searching */ } } strcpy( prntrs[nprn].nam, nam ); /* name of printer */ strcpy( prntrs[nprn].com, com ); /* comments */ strcpy( prntrs[nprn].cmd, cmd ); /* print command */ prntrs[nprn].col = col; /* number of columns */ prntrs[nprn].row = row; /* number of rows */ } } fclose( f ); if (!r) r = nprntrs; else nprntrs = r; /* save status */ return( r ); /* return to caller */ }
int main (void) { float f; double d; char c[8]; int result = 0; if (xsscanf (" 0.25s x", "%e%3c", &f, c) != 2) FAIL (); else if (f != 0.25 || memcmp (c, "s x", 3) != 0) FAIL (); if (xsscanf (" 1.25s x", "%as%2c", &f, c) != 2) FAIL (); else if (f != 1.25 || memcmp (c, " x", 2) != 0) FAIL (); if (xsscanf (" 2.25s x", "%las%2c", &d, c) != 2) FAIL (); else if (d != 2.25 || memcmp (c, " x", 2) != 0) FAIL (); if (xsscanf (" 3.25S x", "%4aS%2c", &f, c) != 2) FAIL (); else if (f != 3.25 || memcmp (c, " x", 2) != 0) FAIL (); if (xsscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2) FAIL (); else if (f != 4.25 || memcmp (c, " x", 2) != 0) FAIL (); if (xsscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2) FAIL (); else if (d != 5.25 || memcmp (c, " x", 2) != 0) FAIL (); const char *tmpdir = getenv ("TMPDIR"); if (tmpdir == NULL || tmpdir[0] == '\0') tmpdir = "/tmp"; char fname[strlen (tmpdir) + sizeof "/tst-scanf17.XXXXXX"]; sprintf (fname, "%s/tst-scanf17.XXXXXX", tmpdir); if (fname == NULL) FAIL (); /* Create a temporary file. */ int fd = mkstemp (fname); if (fd == -1) FAIL (); FILE *fp = fdopen (fd, "w+"); if (fp == NULL) FAIL (); else { if (fputs (" 1.25s x", fp) == EOF) FAIL (); if (fseek (fp, 0, SEEK_SET) != 0) FAIL (); if (xfscanf (fp, "%as%2c", &f, c) != 2) FAIL (); else if (f != 1.25 || memcmp (c, " x", 2) != 0) FAIL (); if (freopen (fname, "r", stdin) == NULL) FAIL (); else { if (xscanf ("%as%2c", &f, c) != 2) FAIL (); else if (f != 1.25 || memcmp (c, " x", 2) != 0) FAIL (); } fclose (fp); } remove (fname); return result; }
/* * This routine reads the specified file into a database and returns a * pointer to that database. */ ELT * DBRead(register FILE *file) { register int i; register int done; /* flag for input exhausted */ register double nx; /* x holder so x is not set before orienting */ int type; /* element type */ ELT *elist; /* pointer to the file's elements */ POINT *plist; /* pointer for reading in points */ char string[MAXSTRING], *txt; double x, y; /* x and y are read in point coords */ int len, brush, size; int lastpoint; SUNFILE = FALSE; elist = DBInit(); (void) fscanf(file, "%" MAXSTRING_S "s%*[^\n]\n", string); if (strcmp(string, "gremlinfile")) { if (strcmp(string, "sungremlinfile")) { error("`%1' is not a gremlin file", gremlinfile); return (elist); } SUNFILE = TRUE; } (void) fscanf(file, "%d%lf%lf\n", &size, &x, &y); /* ignore orientation and file positioning point */ done = FALSE; while (!done) { /* if (fscanf(file,"%" MAXSTRING_S "s\n", string) == EOF) */ /* I changed the scanf format because the element */ /* can have two words (e.g. CURVE SPLINE) */ if (fscanf(file, "\n%" MAXSTRING_S "[^\n]%*[^\n]\n", string) == EOF) { error("`%1', error in file format", gremlinfile); return (elist); } type = DBGetType(string); /* interpret element type */ if (type < 0) { /* no more data */ done = TRUE; } else { #ifdef UW_FASTSCAN (void) xscanf(file, &x, &y); /* always one point */ #else (void) fscanf(file, "%lf%lf\n", &x, &y); /* always one point */ #endif /* UW_FASTSCAN */ plist = PTInit(); /* NULL point list */ /* * Files created on the SUN have point lists terminated by a line * containing only an asterik ('*'). Files created on the AED have * point lists terminated by the coordinate pair (-1.00 -1.00). */ if (TEXT(type)) { /* read only first point for TEXT elements */ nx = xorn(x, y); y = yorn(x, y); (void) PTMakePoint(nx, y, &plist); savebounds(nx, y); #ifdef UW_FASTSCAN while (xscanf(file, &x, &y)); #else lastpoint = FALSE; do { fgets(string, MAXSTRING, file); if (string[0] == '*') { /* SUN gremlin file */ lastpoint = TRUE; } else { (void) sscanf(string, "%lf%lf", &x, &y); if ((x == -1.00 && y == -1.00) && (!SUNFILE)) lastpoint = TRUE; else { if (compatibility_flag) savebounds(xorn(x, y), yorn(x, y)); } } } while (!lastpoint); #endif /* UW_FASTSCAN */ } else { /* not TEXT element */ #ifdef UW_FASTSCAN do { nx = xorn(x, y); y = yorn(x, y); (void) PTMakePoint(nx, y, &plist); savebounds(nx, y); } while (xscanf(file, &x, &y)); #else lastpoint = FALSE; while (!lastpoint) { nx = xorn(x, y); y = yorn(x, y); (void) PTMakePoint(nx, y, &plist); savebounds(nx, y); fgets(string, MAXSTRING, file); if (string[0] == '*') { /* SUN gremlin file */ lastpoint = TRUE; } else { (void) sscanf(string, "%lf%lf", &x, &y); if ((x == -1.00 && y == -1.00) && (!SUNFILE)) lastpoint = TRUE; } } #endif /* UW_FASTSCAN */ } (void) fscanf(file, "%d%d\n", &brush, &size); (void) fscanf(file, "%d", &len); /* text length */ (void) getc(file); /* eat blank */ txt = (char *) malloc((unsigned) len + 1); for (i = 0; i < len; ++i) { /* read text */ int c = getc(file); if (c == EOF) break; txt[i] = c; } txt[len] = '\0'; (void) DBCreateElt(type, plist, brush, size, txt, &elist); } /* end else */ } /* end while not done */ ; return (elist); } /* end DBRead */
Suf_read_ASL(EdRead *R, int readall) #endif { int *d, isreal, i, k, n, nx, nx1; real *r, t; SufDesc *D; char *fmt; ASL *asl = R->asl; char sufname[128]; if (xscanf(R, "%d %d %127s", &k, &n, sufname) != 3) badline(R); if (k < 0 || k > 7 || n <= 0) badline(R); isreal = k & ASL_Sufkind_real; k &= ASL_Sufkind_mask; nx = (&asl->i.n_var_)[k]; if (k == 1) nx += n_lcon; if (n > nx) badline(R); if (readall & 1) { new_D: D = (SufDesc*)M1zapalloc(sizeof(SufDesc) + strlen(sufname) + 1); D->next = asl->i.suffixes[k]; asl->i.suffixes[k] = D; asl->i.nsuff[k]++; asl->i.nsuffixes++; strcpy(D->sufname = (char*)(D+1), sufname); D->kind = k; if (isreal) D->kind |= ASL_Sufkind_real; } else for(D = asl->i.suffixes[k]; ; D = D->next) { if (!D) { if (readall) goto new_D; skip: /* Skip this suffix table */ fmt = (char*)(isreal ? "%d %lf" : "%d %d"); do if (xscanf(R,fmt,&k,&t) != 2) badline(R); while(--n); return; } if (k == (D->kind & ASL_Sufkind_mask) && !strcmp(sufname,D->sufname)) break; } if ((D->kind & ASL_Sufkind_outonly) == ASL_Sufkind_outonly) goto skip; nx1 = nx + D->nextra; if (D->kind & ASL_Sufkind_real) { D->u.i = 0; if (!(r = D->u.r)) D->u.r = r = (real*)mem(nx1*sizeof(real)); if (n < nx) memset(r,0,nx*sizeof(real)); if (nx < nx1) memset(r+nx, 0, (nx1-nx)*sizeof(real)); if (isreal) do { if (xscanf(R,"%d %lf",&i,&t) != 2 || i < 0 || i >= nx) badline(R); r[i] = t; } while(--n); else do { if (xscanf(R,"%d %d",&i,&k) != 2 || i < 0 || i >= nx) badline(R); r[i] = k; } while(--n); } else { D->u.r = 0; if (!(d = D->u.i)) D->u.i = d = (int*)mem(nx1*sizeof(int)); if (n < nx) memset(d,0,nx*sizeof(int)); if (nx < nx1) memset(d+nx, 0, (nx1-nx)*sizeof(int)); if (isreal) do { if (xscanf(R,"%d %lf",&i,&t) != 2 || i < 0 || i >= nx) badline(R); d[i] = (int)(t + 0.5); } while(--n); else do { if (xscanf(R,"%d %d",&i,&k) != 2 || i < 0 || i >= nx) badline(R); d[i] = k; } while(--n); } D->kind |= ASL_Sufkind_input; }
static int split( char *path, char *dirc, char *name ) { char *s; /* pointer */ int l1, l2; /* length counters */ l1 = strlen( path ); /* length of path */ if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH ); s = strrchr( path, '/' ); /* find last / */ if ( s == NULL ) { /* no directory, so use current */ #if defined(__bsd__) /* get current working directory */ extern char *getwd( ); if ( getwd( dirc ) == NULL ) { #else extern char *getcwd( ); if ( getcwd( dirc, FILENAME_MAX ) == NULL ) { #endif return( GLOCK_ERROR_GETCWD ); } strcpy( name, path ); /* we've got it */ } else { /* strip direcotry from path */ s++; /* after this, the filename */ l2 = strlen( s ); /* length of filename */ if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH ); strcpy( name, s ); /* save file name */ strncpy( dirc, path, l1 - l2 ); /* now the directory */ dirc[l1-l2] = 0; /* add zero */ } l1 = strlen( dirc ); /* length of directory */ if ( dirc[l1-1] != '/' ) { dirc[l1] = '/'; dirc[l1+1] = 0; } return( 0 ); /* we're done */ } /* * makeconnection( ) reads the .glock file and make the connection with * lckserver. */ static int makeconnection( FILE *file ) { char hostname[STRINGLEN]; /* name of host */ int close( ); /* close */ int connect( ); /* connect */ int inet_addr( ); /* get inet address */ int port; /* port number */ int r; /* return value */ int socket( ); /* socket */ int obuf[2]; /* data from lckserver */ short portnumber; /* port number */ struct hostent *gethostbyname( ); /* gethostbyname */ struct hostent *hp; /* host */ struct sockaddr_in server; /* sock struct */ if ( xscanf( file, "%s %d", hostname, &port ) != 2 ) { fclose( file ); return( GLOCK_ERROR_FILEREAD ); } fclose( file ); /* close .glock file */ portnumber = port; /* the port */ r = socket( AF_INET, SOCK_STREAM, 0 ); /* create socket */ if ( r == -1 ) return( GLOCK_ERROR_SOCKET ); /* error */ hp = gethostbyname( hostname ); /* get host by name */ if ( hp == NULL ) { /* error */ server.sin_addr.s_addr = inet_addr( hostname ); if (server.sin_addr.s_addr == -1) { close( r ); return( GLOCK_ERROR_GETHOSTBYNAME ); } } else { server.sin_addr.s_addr = INADDR_ANY; /* any address */ memmove( (void *) &server.sin_addr, (void *) hp->h_addr, hp->h_length ); } server.sin_family = AF_INET; /* inet */ server.sin_port = htons( portnumber ); /* the port */ if ( connect( r, (void *) &server, sizeof( server ) ) == -1 ) { close( r ); return( GLOCK_ERROR_CONNECT ); } else { /* connection okay */ if ( get( r, obuf, sizeof( obuf ) ) ) { close( r ); return( GLOCK_ERROR_CONNECT1 ); /* error */ } if ( obuf[0] != 1 ) swapint( obuf, 2 ); /* swap bytes */ if ( obuf[0] != 1 ) { /* error */ close( r ); return( GLOCK_ERROR_CONNECT2 ); } else if ( obuf[1] != GLOCK_CONNECT ) { /* error */ close( r ); if ( obuf[1] == GLOCK_NOSOCK ) { return( GLOCK_ERROR_NOSOCKETS ); } else { return( GLOCK_ERROR_SERVERBAD ); } } } return( r ); /* we're done */ }
int ka_read_ASL(ASL *asl, EdRead *R, int mode, int **kap, size_t **kapZ) { int flags, *kai; size_t i, k, *ka, t; int j; unsigned Long u; k = asl->i.n_var0; if (!xscanf(R,"%d",&j) || j != k - 1) return 1; if ((i = k) < n_var) i = n_var; flags = asl->i.rflags; if (flags & ASL_use_Z) { *kap = kai = A_colstarts = 0; if (!(ka = A_colstartsZ)) A_colstartsZ = ka = (size_t*)M1alloc((i+1)*Sizeof(size_t)); *kapZ = ka + 1; } else { *kapZ = ka = A_colstartsZ = 0; if (!(kai = A_colstarts)) A_colstarts = kai = (int*)M1alloc((i+1)*Sizeof(int)); *kap = kai + 1; } if (sizeof(int) == sizeof(size_t)) { if (!ka) ka = (size_t*)kai; *ka++ = 0; *ka++ = 0; /* sic */ if (mode == 'K') { t = 0; while(--k > 0) { if (!xscanf(R, "%d", &u)) return 1; *ka++ = t += u; } } else { while(--k > 0) { if (!xscanf(R, "%d", &u)) return 1; *ka++ = u; } } } else if (flags & ASL_use_Z) { *ka++ = 0; *ka++ = 0; /* sic */ if (mode == 'K') { t = 0; while(--k > 0) { if (!xscanf(R, "%d", &u)) return 1; *ka++ = t += u; } } else { while(--k > 0) { if (!xscanf(R, "%d", &u)) return 1; *ka++ = u; } } } else { *kai++ = 0; *kai++ = 0; /* sic */ if (mode == 'K') { t = 0; while(--k > 0) { if (!xscanf(R, "%d", &u)) return 1; *kai++ = (int)(t += u); } } else { while(--k > 0) { if (!xscanf(R, "%d", &u)) return 1; *kai++ = (int)u; } } } return 0; }