/* * Return err-number+string for 'err'. Use only with GetLastError(). * Does not handle libc errno's. Remove trailing [\r\n.] */ char *win_strerror (unsigned long err) { static char buf[512+20]; char err_buf[512], *p; if (err == ERROR_SUCCESS) strcpy (err_buf, "No error"); else if (err == ERROR_BAD_EXE_FORMAT) strcpy (err_buf, "Bad EXE format"); else if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_buf, sizeof(err_buf)-1, NULL)) { if (!get_error_from_kernel32(err,err_buf, sizeof(err_buf)-1)) strcpy (err_buf, "Unknown error"); } snprintf (buf, sizeof(buf), "%lu %s", err, err_buf); strip_nl (buf); p = strrchr (buf, '.'); if (p && p[1] == '\0') *p = '\0'; return (buf); }
CFGSection *read_cfg_file(const char *config_path) { char line[MAXLINE]; FILE *fd; char *str; CFGItem *item; CFGSection *default_sect = find_cfg_section("*"); if (default_sect == NULL) { create_cfg_section("*"); } set_cfg_section("*"); if (!is_file(config_path)) { fprintf(stderr, "No machine file found: '%s'\n", config_path); return NULL; } fd = fopen(config_path, "r"); if (fd == NULL) { fprintf(stderr, "Can't open config file: '%s'\n", config_path); return NULL; } /* Read the config file */ str = fgets(line, MAXSTR, fd); while (str != NULL) { int len; strip_nl(str); rtrim(str); len = strlen(str); if ((str[0] == '[') && (str[len-1] == ']')) { str[len-1] = EOS; str++; create_cfg_section(str); } else { if (len) { item = parse_line(str); item->next = cur_sect->items; cur_sect->items = item; } } str = fgets(line, MAXLINE, fd); } fclose(fd); return cfg; }
int r_socket ( int s, char *b, size_t l ) { int rd; memset ( b, 0, l ); rd = read ( s, b, l ); if ( rd>0 ) strip_nl(b, l); else return -2; if ( !strncmp( b, "ERR", 3 ) ) return -1; return 0; }
int read_db(char *filename) { char line[LINEMAX]; char path[LINEMAX]; FILE *in = fopen(filename, "r"); if(in == NULL) { printf("could not open database.\n"); return 0; } num_artists = 0; while(fgets(line, LINEMAX, in) != NULL) { artist curr_artist; album curr_album; /* consume blank lines */ while(line[0] == '\n') fgets(line, LINEMAX, in); curr_artist = add_artist(strip_nl(line)); fgets(line, LINEMAX, in); curr_album = add_album(curr_artist, strip_nl(line)); while(fgets(line, LINEMAX, in) != NULL) { if(line[0] != '\n') { fgets(path, LINEMAX, in); add_song(curr_album, strip_nl(line), strip_nl(path)); } else break; } } fclose(in); return 1; }
void write_defined(char *sname, int value) { FILE *fp; if ( (fp=fopen("zcc_opt.def","a")) == NULL ) { fprintf(stderr,"%s:%d Cannot open zcc_opt.def file\n", filename, lineno); exit(1); } strip_nl(sname); fprintf(fp,"\nIF !DEFINED_%s\n",sname); fprintf(fp,"\tdefc\tDEFINED_%s = 1\n",sname); fprintf(fp,"\tdefc %s = %d\n",sname,value); fprintf(fp,"ENDIF\n\n"); fclose(fp); }
/* * A wrapper for popen(). * 'cmd': the program + args to run. * 'callback': function to call for each line from popen(). * This function should return number of matches. * The callback is allowed to modify the given 'buf'. * * Returns total number of matches from 'callback'. */ int popen_run (const char *cmd, popen_callback callback) { char buf[1000]; int i = 0; int j = 0; FILE *f; char *env = getenv ("COMSPEC"); char *cmd2; const char *comspec = ""; const char *setdos = ""; /* * OpenWatcom's popen() always uses cmd.exe regardles of %COMSPEC. * If we're using 4NT/TCC shell, set all variable expansion to off * by prepending "setdos /x-3" to 'cmd' buffer. */ if (env) { DEBUGF (1, "%%COMSPEC: %s.\n", env); strupr (env); #if !defined(__WATCOMC__) if (strstr(env,"4NT.EXE") || strstr(env,"TCC.EXE")) setdos = "setdos /x-3 &"; #endif } else comspec = "set COMSPEC=cmd.exe &"; cmd2 = alloca (strlen(setdos) + strlen(comspec) + strlen(cmd) + 1); strcpy (cmd2, setdos); strcat (cmd2, comspec); strcat (cmd2, cmd); DEBUGF (1, "Trying to run '%s'\n", cmd2); #ifdef __CYGWIN__ if (!system(NULL)) { WARN ("/bin/sh not found.\n"); return (0); } #endif f = _popen (cmd2, "r"); if (!f) { DEBUGF (1, "failed to call _popen(); errno=%d.\n", errno); return (0); } while (fgets(buf,sizeof(buf)-1,f)) { int rc; strip_nl (buf); DEBUGF (2, " _popen() buf: '%s'\n", buf); if (!buf[0]) continue; rc = (*callback) (buf, i++); if (rc < 0) break; j += rc; } _pclose (f); return (j); }