int main(int argc, char *argv[]) { FILE *infile; char line[LINE_LEN + 2]; /* Allow for '\n' & NUL */ int i, N = 0; if (2 > argc) give_up("Usage: HEAD file [number_of_lines]"); if (NULL == (infile = fopen(argv[1], "r"))) give_up("Unable to open input file"); if (2 < argc) N = atoi(argv[2]); if (!N) N = 4; for (i = 0; i < N; ++i) { if (NULL == fgets(line, LINE_LEN + 1, infile)) break; line[LINE_LEN + 1] = NUL; /* Allow too-long lines */ fputs(line, stdout); if (!strrchr(line, '\n')) i -= 1; /* More to read */ } fclose(infile); return EXIT_SUCCESS; }
bool document_is_open(CFStringRef docNameCF, ProcessSerialNumber *psn) { char docNameData[255]; char *docName = &docNameData[0]; OSStatus status; AppleEvent ae, reply; AEBuildError buildError; char returnValue; char *eventDescriptor; /* Apple Events only support MacRoman, I think */ if (!CFStringGetCString(docNameCF, docName, 255, kCFStringEncodingMacRoman)) give_up("Document name could not be encoded as MacRoman, or too long"); if (docName[0] == '/') eventDescriptor = "'----':obj{form:enum('test'),want:type('docu'),seld:cmpd{relo:=," "'obj1':obj{form:prop, want:type('prop'), seld:type('ppth'), from:exmn()}," "'obj2':TEXT(@)},from:null()}"; else eventDescriptor = "'----':obj{form:enum('name'),want:type('docu'),seld:TEXT(@),from:null()}"; status = AEBuildAppleEvent(kAECoreSuite, kAEDoObjectsExist, typeProcessSerialNumber, psn, sizeof(*psn), kAutoGenerateReturnID, kAnyTransactionID, &ae, &buildError, eventDescriptor, docName); if (status != noErr) { fprintf(stderr, "check_open: AEBuildAppleEvent failed: error %d at pos %lu\n", buildError.fError, buildError.fErrorPos); fprintf(stderr, "(See http://developer.apple.com/technotes/tn/tn2045.html#errstable)\n"); exit(255); } status = AESendMessage(&ae, &reply, kAEWaitReply, kAEDefaultTimeout); if (status != noErr) die("AESend failed"); status = AEGetParamPtr (&reply, keyDirectObject, typeBoolean, NULL, &returnValue, 1, NULL); if (status != noErr) /* Presumably this is because the reply is an error message */ give_up("Application appears not to understand request"); return (returnValue != 0); /* We don't need to bother disposing of things, because we're about to finish */ }