Script started on Wed Oct 23 12:33:49 2024 mweeks@air:Desktop$ ls pope* popen_example.c mweeks@air:Desktop$ gcc popen_example.c mweeks@air:Desktop$ cat popen_example.c /* Example of the popen/pclose command (process I/O). Get a list of files using the popen command. -MCW, Sept 2024 */ #include #include // defines "system", "exit" #define BUFFER_SIZE 1024 int main (int argc, char *argv[]) { // myProcess is a stream that we will read from. // This is like reading input from the user, except that // the input is coming from a different "file". FILE *myProcess; // buffer is a place to store text. // Instead of reading from the stream 1 char at a time, we // read many of them, defined as BUFFER_SIZE above. char buffer[BUFFER_SIZE]; int count = 0; printf("Here is a list of all files that end with .c\n"); // List all files in the current dir that end with .c // Open the stream myProcess = popen("ls *.c", "r"); // "r" for read // Always check to see if the command worked. if (myProcess == NULL) { // It did not work, so quit. printf("Error: ls *.c command failed\n"); exit(1); } // Read up to BUFFER_SIZE-1 chars. fgets(buffer, BUFFER_SIZE, myProcess); // Check for end-of-file while (!feof(myProcess)) { count++; // Print it out // Notice that there is no \n since one is in the buffer printf("File %d is %s", count, buffer); // get the next line fgets(buffer, BUFFER_SIZE, myProcess); } // Close the stream pclose(myProcess); return 0; } mweeks@air:Desktop$ ./a.out Here is a list of all files that end with .c File 1 is popen_example.c mweeks@air:Desktop$ ./a.out Here is a list of all files that end with .c File 1 is fopen_example.c File 2 is popen_example.c mweeks@air:Desktop$ echo $? 0 mweeks@air:Desktop$ echo $? 0 mweeks@air:Desktop$ RV=$? mweeks@air:Desktop$ echo $RV 0 mweeks@air:Desktop$ cd csc3320/programs/ mweeks@air:programs$ cd C_ch9 mweeks@air:C_ch9$ cat myexit.c #include #include /* needed for exit */ int main() { printf("I am going to exit with status code 42\n"); exit(42); } mweeks@air:C_ch9$ gcc myexit.c -o myexit mweeks@air:C_ch9$ echo $? 0 mweeks@air:C_ch9$ ./myexit I am going to exit with status code 42 mweeks@air:C_ch9$ echo $? 42 mweeks@air:C_ch9$ vi call_myexit.bash mweeks@air:C_ch9$ ls -l call_myexit.bash -rw-r--r-- 1 mweeks staff 259 Oct 23 12:54 call_myexit.bash mweeks@air:C_ch9$ chmod 700 call_myexit.bash mweeks@air:C_ch9$ ls -l call_myexit.bash -rwx------ 1 mweeks staff 259 Oct 23 12:54 call_myexit.bash mweeks@air:C_ch9$ cat call_myexit.bash #!/bin/bash # # This calls a program, and based on the return code, # issues an error or not. # -MCW # ./myexit if [ $? != 0 ] then echo " Error -- the last command failed" exit 1 else echo " The command worked" fi mweeks@air:C_ch9$ ./call_myexit.bash I am going to exit with status code 42 Error -- the last command failed mweeks@air:C_ch9$ cd .. mweeks@air:programs$ cd C_ch13 mweeks@air:C_ch13$ ls a.out ch13_6.c string_example.c ch13_3.c ch13_7.c string_example2.c ch13_4.c ch13_8.c test_string1.c ch13_5.c ch13_9.c test_string2.c mweeks@air:C_ch13$ grep strcpy *.c ch13_4.c: strncpy(str1, strcpy(str2, "abcd"), 4); ch13_4.c: char *strcpy(char * dest, const char * src); ch13_8.c: strcpy(q,p); ch13_9.c: strcpy(s1,"computer"); ch13_9.c: strcpy(s2,"science"); string_example.c: // strcpy -> strncpy string_example.c: // strcpy string_example.c: //strcpy(date1, date2); string_example2.c: // strcpy -> strncpy string_example2.c: // strcpy string_example2.c: //strcpy(date1, date2); mweeks@air:C_ch13$ cat ch13_4.c #include int main(){ char str1[5]; char str2[] = "abcd"; strncpy(str2, "abcd", 4); strncpy(str1, str2, 4); strncpy(str1, strcpy(str2, "abcd"), 4); char *strcpy(char * dest, const char * src); } mweeks@air:C_ch13$ cp ch13_4.c ch13_4b.c mweeks@air:C_ch13$ vi ch13_4b.c mweeks@air:C_ch13$ cat ch13_4b.c #include //#include int main(){ char str1[5]; char str2[] = "abcd"; // strncpy(str2, "abcd", 4); strncpy(str1, str2, 4); // strncpy(str1, strcpy(str2, "abcd"), 4); // char *strcpy(char * dest, const char * src); printf("str1 is [%s]\n", str1); return 0; } mweeks@air:C_ch13$ gcc ch13_4b.c ch13_4b.c:7:3: warning: implicitly declaring library function 'strncpy' with type 'char *(char *, const char *, unsigned long)' [-Wimplicit-function-declaration] strncpy(str1, str2, 4); ^ ch13_4b.c:7:3: note: include the header or explicitly provide a declaration for 'strncpy' 1 warning generated. mweeks@air:C_ch13$ vi ch13_4b.c mweeks@air:C_ch13$ gcc ch13_4b.c mweeks@air:C_ch13$ cat ch13_4b.c #include #include int main(){ char str1[5]; char str2[] = "abcd"; // strncpy(str2, "abcd", 4); strncpy(str1, str2, 4); // strncpy(str1, strcpy(str2, "abcd"), 4); // char *strcpy(char * dest, const char * src); printf("str1 is [%s]\n", str1); return 0; } mweeks@air:C_ch13$ ./a.out str1 is [abcd] mweeks@air:C_ch13$ cp ch13_4b.c ch13_4c.c mweeks@air:C_ch13$ vi ch13_4c.c mweeks@air:C_ch13$ cat ch13_4c.c #include #include int main(){ char str1[5]; char str2[] = "abcd"; strncpy(str1, "abcd", 4); //strncpy(str1, str2, 4); printf("str1 is [%s]\n", str1); return 0; } mweeks@air:C_ch13$ gcc ch13_4c.c mweeks@air:C_ch13$ ./a.out str1 is [abc] mweeks@air:C_ch13$ vi ch13_4c.c mweeks@air:C_ch13$ gcc ch13_4c.c mweeks@air:C_ch13$ ./a.out str1 is [abcd] str[0] = 97 a str[1] = 98 b str[2] = 99 c str[3] = 100 d str[4] = 16 mweeks@air:C_ch13$ mv ch13_4c.c ch13_4c_bad.c mweeks@air:C_ch13$ cp ch13_4c_bad.c ch13_4c_good.c mweeks@air:C_ch13$ vi ch13_4c_good.c mweeks@air:C_ch13$ cpp ch13_4c_good.c # 1 "ch13_4c_good.c" # 1 "" 1 # 1 "" 3 # 360 "" 3 # 1 "" 1 # 1 "" 2 # 1 "ch13_4c_good.c" 2 # 1 "/usr/include/stdio.h" 1 3 4 # 22 "/usr/include/stdio.h" 3 4 [Thousands of lines deleted] __builtin___strncpy_chk (str1, "abcd", 4, __builtin_object_size (str1, 2 > 1 ? 1 : 0)); //__builtin___strncpy_chk (str1, str2, 4, __builtin_object_size (str1, 2 > 1 ? 1 : 0)); printf("str1 is [%s]\n", str1); for (int i=0; i #include int main(){ char str1[5] = {0}; ; char str2[] = "abcd"; strncpy(str1, "abcd", 4); //strncpy(str1, str2, 4); printf("str1 is [%s]\n", str1); for (int i=0; i #include int main(){ char str1[100] = {0}; //char str2[] = "abcd"; strncpy(str1, "abcd", 4); strncat(str1, "efg", 7); //strncpy(str1, str2, 4); printf("str1 is [%s]\n", str1); for (int i=0; i #include int main(){ char str1[100] = {0}; //char str2[] = "abcd"; strncpy(str1, "abcd", 4); strncat(str1, "efg", 7); //strncpy(str1, str2, 4); printf("str1 is [%s]\n", str1); for (int i=0; i #include int main(){ char str1[100] = {0}; //char str2[] = "abcd"; strncpy(str1, "abcd", 4); strncat(str1, "efg", 7); //strncpy(str1, str2, 4); printf("str1 is [%s]\n", str1); for (int i=0; i