diff options
| author | Anselm R. Garbe <garbeam@gmail.com> | 2007-11-03 20:43:12 +0100 |
|---|---|---|
| committer | Anselm R. Garbe <garbeam@gmail.com> | 2007-11-03 20:43:12 +0100 |
| commit | 0e98090d653457488d74103dc44a1e2aba071895 (patch) | |
| tree | 074eedd4f7ad7838f02c1af8e49f8a5228142a9e | |
| parent | simplified (diff) | |
| download | dwm-0e98090d653457488d74103dc44a1e2aba071895.tar.gz dwm-0e98090d653457488d74103dc44a1e2aba071895.zip | |
removed fgets usage, increment offset until a line is read, dwm will drop all lines read in one call, except the first!!! one (previously it preferred the last) - but the current approach is simplier and works better for general purpose in conjunction with the offset handling
| -rw-r--r-- | dwm.c | 34 |
1 files changed, 27 insertions, 7 deletions
| @@ -1145,6 +1145,7 @@ quit(const char *arg) { | |||
| 1145 | readin = running = False; | 1145 | readin = running = False; |
| 1146 | } | 1146 | } |
| 1147 | 1147 | ||
| 1148 | |||
| 1148 | void | 1149 | void |
| 1149 | resize(Client *c, int x, int y, int w, int h, Bool sizehints) { | 1150 | resize(Client *c, int x, int y, int w, int h, Bool sizehints) { |
| 1150 | XWindowChanges wc; | 1151 | XWindowChanges wc; |
| @@ -1280,15 +1281,19 @@ restack(void) { | |||
| 1280 | 1281 | ||
| 1281 | void | 1282 | void |
| 1282 | run(void) { | 1283 | run(void) { |
| 1284 | char *p; | ||
| 1283 | fd_set rd; | 1285 | fd_set rd; |
| 1284 | int xfd; | 1286 | int r, xfd; |
| 1287 | unsigned int len, offset; | ||
| 1285 | XEvent ev; | 1288 | XEvent ev; |
| 1286 | 1289 | ||
| 1287 | /* main event loop, also reads status text from stdin */ | 1290 | /* main event loop, also reads status text from stdin */ |
| 1288 | XSync(dpy, False); | 1291 | XSync(dpy, False); |
| 1289 | xfd = ConnectionNumber(dpy); | 1292 | xfd = ConnectionNumber(dpy); |
| 1290 | readin = True; | 1293 | readin = True; |
| 1291 | stext[sizeof stext - 1] = '\0'; /* 0-terminator is never touched */ | 1294 | offset = 0; |
| 1295 | len = sizeof stext - 1; | ||
| 1296 | stext[len] = '\0'; /* 0-terminator is never touched */ | ||
| 1292 | while(running) { | 1297 | while(running) { |
| 1293 | FD_ZERO(&rd); | 1298 | FD_ZERO(&rd); |
| 1294 | if(readin) | 1299 | if(readin) |
| @@ -1300,12 +1305,27 @@ run(void) { | |||
| 1300 | eprint("select failed\n"); | 1305 | eprint("select failed\n"); |
| 1301 | } | 1306 | } |
| 1302 | if(FD_ISSET(STDIN_FILENO, &rd)) { | 1307 | if(FD_ISSET(STDIN_FILENO, &rd)) { |
| 1303 | if((readin = (stext == fgets(stext, sizeof stext - 1, stdin)))) | 1308 | switch((r = read(STDIN_FILENO, stext + offset, len - offset))) { |
| 1304 | stext[strlen(stext) - 1] = '\0'; /* remove tailing '\n' */ | 1309 | case -1: |
| 1305 | else if(feof(stdin)) | 1310 | strncpy(stext, strerror(errno), len); |
| 1311 | readin = False; | ||
| 1312 | break; | ||
| 1313 | case 0: | ||
| 1306 | strncpy(stext, "EOF", 4); | 1314 | strncpy(stext, "EOF", 4); |
| 1307 | else /* error occured */ | 1315 | readin = False; |
| 1308 | strncpy(stext, strerror(errno), sizeof stext - 1); | 1316 | break; |
| 1317 | default: | ||
| 1318 | stext[offset + r] = '\0'; | ||
| 1319 | for(p = stext; *p && *p != '\n'; p++); | ||
| 1320 | if(*p == '\n') { | ||
| 1321 | *p = '\0'; | ||
| 1322 | offset = 0; | ||
| 1323 | } | ||
| 1324 | else if(offset + r < len - 1) | ||
| 1325 | offset += r; | ||
| 1326 | else | ||
| 1327 | offset = 0; | ||
| 1328 | } | ||
| 1309 | drawbar(); | 1329 | drawbar(); |
| 1310 | } | 1330 | } |
| 1311 | while(XPending(dpy)) { | 1331 | while(XPending(dpy)) { |
