src/lib/common.c 1.132

1.132 2009/09/23 18:42:27 angie
Fixed compiler warnings from gcc 4.3.3, mostly about system calls whose return values weren't checked and non-literal format strings with no args.
Index: src/lib/common.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/common.c,v
retrieving revision 1.131
retrieving revision 1.132
diff -b -B -U 4 -r1.131 -r1.132
--- src/lib/common.c	26 Jun 2009 19:57:33 -0000	1.131
+++ src/lib/common.c	23 Sep 2009 18:42:27 -0000	1.132
@@ -1778,9 +1778,9 @@
 }
 
 
 void mustRead(FILE *file, void *buf, size_t size)
-/* Read from a file or squawk and die. */
+/* Read size bytes from a file or squawk and die. */
 {
 if (size != 0 && fread(buf, size, 1, file) != 1)
     {
     if (ferror(file))
@@ -1876,8 +1876,27 @@
     }
 return x;
 }
 
+void mustGetLine(FILE *file, char *buf, int charCount)
+/* Read at most charCount-1 bytes from file, but stop after newline if one is
+ * encountered.  The string in buf is '\0'-terminated.  (See man 3 fgets.)
+ * Die if there is an error. */
+{
+char *success = fgets(buf, charCount, file);
+if (success == NULL && charCount > 0)
+    buf[0] = '\0';
+if (ferror(file))
+    errAbort("mustGetLine: fgets failed: %s", strerror(ferror(file)));
+}
+
+void mustWriteFd(int fd, void *buf, size_t size)
+/* Write size bytes to file descriptor fd or die.  (See man 2 write.) */
+{
+ssize_t result = write(fd, buf, size);
+if (result < size)
+    errAbort("mustWriteFd: write failed: %s", strerror(errno));
+}
 
 char *addSuffix(char *head, char *suffix)
 /* Return a needMem'd string containing "headsuffix". Should be free'd
  when finished. */
@@ -1959,9 +1978,9 @@
 char *firstWordInFile(char *fileName, char *wordBuf, int wordBufSize)
 /* Read the first word in file into wordBuf. */
 {
 FILE *f = mustOpen(fileName, "r");
-fgets(wordBuf, wordBufSize, f);
+mustGetLine(f, wordBuf, wordBufSize);
 fclose(f);
 return trimSpaces(wordBuf);
 }
 
@@ -2002,8 +2021,19 @@
 slReverse(&newList);
 return newList;
 }
 
+void mustSystem(char *cmd)
+/* Execute cmd using "sh -c" or die.  (See man 3 system.) */
+{
+if (cmd == NULL) // don't allow (system() supports testing for shell this way)
+    errAbort("mustSystem: called with NULL command.");
+int status = system(cmd);
+if (status != 0)
+    errAbort("mustSystem: system(%s) failed (exit status %d): %s",
+	     cmd, WEXITSTATUS(status), strerror(errno));
+}
+
 int roundingScale(int a, int p, int q)
 /* returns rounded a*p/q */
 {
 if (a > 100000 || p > 100000)